MVC3设计——存储库模式和服务层

发布于 2024-10-31 19:11:01 字数 1431 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初懵 2024-11-07 19:11:01

在分层应用程序架构中,有一条基本规则,即您绝不能绕过某一层。如果您直接从控制器查询存储库,则将违反该规则。

那又怎样?你可能会说。 如果服务层没有增加任何价值怎么办?好吧,将来可能......

您可以选择打破规则,但是那么它就不再是分层应用程序了< /强>。这也可能没问题 - 还有其他好的(甚至更好)应用程序架构可用,但我认为首先您应该就整体架构做出决定,然后坚持该决定。否则,您最终会得到意大利面条式代码 - 当它是分层应用程序时,我们将其称为烤宽面条:)

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 :)

压抑⊿情绪 2024-11-07 19:11:01

这取决于。如果您计划将来有复杂的业务规则,我会添加服务层。如果您的站点仅在服务层中执行很少或没有逻辑的 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.

梦行七里 2024-11-07 19:11:01

控制器是否应该能够通过存储库模式获取实体,或者必须从服务层检索数据。

理想情况下,控制器应仅使用本身依赖于一个或多个存储库的服务层,以便将一个或多个简单的 CRUD 操作聚合为一项业务操作。但在某些情况下,在简单的应用程序中,您可能不需要服务层并让控制器直接使用存储库。

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.

庆幸我还是我 2024-11-07 19:11:01

什么更适合您以及您的风格是什么始终是一个问题。对于我来说,我更喜欢从控制器操作访问服务层。然后该服务将访问存储库模型。

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文