ActionResult - Service

Posted by cem on Stack Overflow See other posts from Stack Overflow or by cem
Published on 2010-05-26T08:48:18Z Indexed on 2010/05/26 8:51 UTC
Read the original article Hit count: 257

Filed under:
|
|

I bored, writing same code for service and ui. Then i tried to write a converter for simple actions. This converter, converting Service Results to MVC result, seems like good solution for me but anyway i think this gonna opposite MVC pattern.

So here, I need help, what you think about algorithm - is this good or not?

Thanks

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) { }
}

Service - eg. 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();
}

Controller:

public class BaseController
{
    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);
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET