In layered application architecture there's a fundamental rule that says that you must never bypass a layer. If you query your repository straight from your controller, you would be violating that rule.
So what? you may say. What if the service layer adds no value? Well, it might in the future...
You can choose to break the rule, but then it wouldn't be a layered application any more. That may be okay too - there are other good (even better) application architectures available, but I think that first you should make a decision regarding the overall architecture, and then you stick with that decision. Otherwise you'll end up with spaghetti code - we call it lasagna when it's a layered application :)
It depends. If you plan to have complex bussiness rules in the future I would add the service layer. If your site only does CRUD operations with little or no logic in the service layer you could directly call you repository layer.
Should a controller be able to get entities through the repository pattern, or must it retrieve data from the services layer.
Ideally the controller should use only the service layer which itself depends on one or more repositories in order to aggregate one or more simple CRUD operations into a business operation. There are cases though in simple applications where you might not need a service layer and have the controller directly use a repository.
public class UserController : MyServiceController<UserServices>
{
public ActionResult GetUser(int id)
{
var user = Service.GetUser(id);
return View(user);
}
}
public class UserServices : MyServices<User>
{
public User GetUser(int userId)
{
return Repository.Single(a=>a.Id == userId);
}
}
It's always a question what suits your better and what your style is. As for me, I prefer accessing service layer from controller action. The service will then access the repository model.
public class UserController : MyServiceController<UserServices>
{
public ActionResult GetUser(int id)
{
var user = Service.GetUser(id);
return View(user);
}
}
public class UserServices : MyServices<User>
{
public User GetUser(int userId)
{
return Repository.Single(a=>a.Id == userId);
}
}
发布评论
评论(4)
在分层应用程序架构中,有一条基本规则,即您绝不能绕过某一层。如果您直接从控制器查询存储库,则将违反该规则。
那又怎样?你可能会说。 如果服务层没有增加任何价值怎么办?好吧,将来可能......
您可以选择打破规则,但是那么它就不再是分层应用程序了< /强>。这也可能没问题 - 还有其他好的(甚至更好)应用程序架构可用,但我认为首先您应该就整体架构做出决定,然后坚持该决定。否则,您最终会得到意大利面条式代码 - 当它是分层应用程序时,我们将其称为烤宽面条:)
In layered application architecture there's a fundamental rule that says that you must never bypass a layer. If you query your repository straight from your controller, you would be violating that rule.
So what? you may say. What if the service layer adds no value? Well, it might in the future...
You can choose to break the rule, but then it wouldn't be a layered application any more. That may be okay too - there are other good (even better) application architectures available, but I think that first you should make a decision regarding the overall architecture, and then you stick with that decision. Otherwise you'll end up with spaghetti code - we call it lasagna when it's a layered application :)
这取决于。如果您计划将来有复杂的业务规则,我会添加服务层。如果您的站点仅在服务层中执行很少或没有逻辑的 CRUD 操作,您可以直接调用存储库层。
It depends. If you plan to have complex bussiness rules in the future I would add the service layer. If your site only does CRUD operations with little or no logic in the service layer you could directly call you repository layer.
理想情况下,控制器应仅使用本身依赖于一个或多个存储库的服务层,以便将一个或多个简单的 CRUD 操作聚合为一项业务操作。但在某些情况下,在简单的应用程序中,您可能不需要服务层并让控制器直接使用存储库。
Ideally the controller should use only the service layer which itself depends on one or more repositories in order to aggregate one or more simple CRUD operations into a business operation. There are cases though in simple applications where you might not need a service layer and have the controller directly use a repository.
什么更适合您以及您的风格是什么始终是一个问题。对于我来说,我更喜欢从控制器操作访问服务层。然后该服务将访问存储库模型。
It's always a question what suits your better and what your style is. As for me, I prefer accessing service layer from controller action. The service will then access the repository model.