ActionResult - 服务

时间:2010-05-26 08:48:18

标签: c# asp.net-mvc

我很无聊,为服务和ui写了相同的代码。然后我试着为简单的动作编写一个转换器。这个转换器,将服务结果转换为 MVC结果,对我来说似乎是一个很好的解决方案,但无论如何我认为这将与MVC模式相反。

所以在这里,我需要帮助,你对算法的看法 - 这是好还是不好?

由于

ServiceResult - Base:

public abstract class ServiceResult
{
    public static NoPermissionResult Permission()
    {
        return new NoPermissionResult();
    }

    public static SuccessResult Success()
    {
        return new SuccessResult();
    }

    public static SuccessResult<T> Success<T>(T result)
    {
        return new SuccessResult<T>(result);
    }

    protected ServiceResult(ServiceResultType serviceResultType)
    {
        _resultType = serviceResultType;
    }

    private readonly ServiceResultType _resultType;
    public ServiceResultType ResultType
    {
        get { return _resultType; }
    }
}
public class SuccessResult<T> : ServiceResult
{
    public SuccessResult(T result)
        : base(ServiceResultType.Success)
    {
        _result = result;
    }

    private readonly T _result;
    public T Result
    {
        get { return _result; }
    }
}
public class SuccessResult : SuccessResult<object>
{
    public SuccessResult() : this(null) { }
    public SuccessResult(object o) : base(o) { }
}

服务 - 例如。 ForumService:

public ServiceResult Delete(IVUser user, int id)
{
    Forum forum = Repository.GetDelete(id);
    if (!Permission.CanDelete(user, forum))
    {
        return ServiceResult.Permission();
    }
    Repository.Delete(forum);
    return ServiceResult.Success();
}

控制器:

public class BaseController : Controller
{
    public ActionResult GetResult(ServiceResult result)
    {
        switch (result.ResultType)
        {
            case ServiceResultType.Success:
                var successResult = (SuccessResult)result;
                return View(successResult.Result);
                break;
            case ServiceResultType.NoPermission:
                return View("Error");
                break;
            default:
                return View();
                break;
        }
    }
}

[HandleError]
public class ForumsController : BaseController
{
    [ValidateAntiForgeryToken]
    [Transaction]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Delete(int id)
    {
        ServiceResult result = ForumService.Delete(WebUser.Current, id);

        /* Custom result */
        if (result.ResultType == ServiceResultType.Success)
        {
            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "The forum was successfully deleted.";
            return this.RedirectToAction(ec => Index());
        }
        /* Custom result */

        /* Execute Permission result etc. */
        TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "A problem was encountered preventing the forum from being deleted. " +
            "Another item likely depends on this forum.";


        return GetResult(result);
    }
}

1 个答案:

答案 0 :(得分:1)

在我工作的公司中,我们使用相同的算法。我们认为它是MVVM而不是MVC。所以我认为这个算法很好。

我只是想知道,你如何处理重定向?您是否有重定向服务结果类型,或者您决定在控制器中重定向还是不重定向?