我厌倦了,为服务和ui编写了相同的代码。然后,我试图为简单的操作编写一个转换器。这个转换器,将服务结果转换成MVC结果,对我来说似乎是一个很好的解决方案,但无论如何,我认为这将与MVC模式相反。
所以,在这里,我需要帮助,你对算法的看法-这是好还是不好?
谢谢
ServiceResult -基地:
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);
}
}发布于 2013-06-09 16:31:22
在我工作的公司里,我们使用同样的算法。我们认为它是一个MVVM,而不是MVC。所以我认为这个算法是好的。
我只是在想,你是怎么处理重定向的?您有重定向服务结果类型,还是决定重定向或不重定向控制器?
https://stackoverflow.com/questions/2911389
复制相似问题