[英]Service layer classes in ASP.NET MVC application best practices
Can you provide a code sample with basic architecture guidlines for creating service layer classes (which supposed to be consumed by web front-ends, web api etc.)? 您能否为代码示例提供用于创建服务层类(应该由Web前端,Web api等使用)的基本体系结构指南?
Do you think this is a good tutorial? 您认为这是一个很好的教程吗? http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs
I personally don't like how that article describes passing errors from a service layer back to the controller (with IValidationDictionary), I would make it work more like this instead: 我个人不喜欢这篇文章描述如何将错误从服务层传递回控制器(使用IValidationDictionary),我希望它像这样工作:
[Authorize]
public class AccountController : Controller
{
private readonly IMembershipService membershipService;
// service initialization is handled by IoC container
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
// .. some other stuff ..
[AllowAnonymous, HttpPost]
public ActionResult Register(RegisterModel model)
{
if (this.ModelSteate.IsValid)
{
var result = this.membershipService.CreateUser(
model.UserName, model.Password, model.Email, isApproved: true
);
if (result.Success)
{
FormsAuthentication.SetAuthCookie(
model.UserName, createPersistentCookie: false
);
return this.RedirectToAction("Index", "Home");
}
result.Errors.CopyTo(this.ModelState);
}
return this.View();
}
}
Or.. as mikalai mentioned, make the service throw validation exceptions, catch them in a global filter and insert into model state. 或者..如mikalai所述,使服务抛出验证异常,将其捕获到全局过滤器中并插入模型状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.