实体服务类是否应该调用另一个实体的服务或其存储库

发布于 2024-12-03 01:15:48 字数 250 浏览 0 评论 0原文

我正在开发一个 ASP.Net MVC 3 Web 应用程序 (EF 4.1),该应用程序分层:模型、存储库、服务、控制器、某些情况下的视图模型和视图。

现在我的问题是最佳实践之一。如果需要访问另一个实体的实体服务类使用其服务或其存储库。例如,假设实体 A 的服务方法需要在创建 A 时更新实体 B。 A 的服务类应该使用 B 的存储库还是服务层?两者都是可能的,但最佳实践是什么?就我个人而言,我更喜欢使用一项服务来访问另一项服务。这样,可以说它可以访问更进化的方法。

I am working on an ASP.Net MVC 3 web application (EF 4.1) separated in layers: Models, Repositories, Services, Controllers, ViewModels in some cases, and Views.

Now my question is one of best practice. Should an entity service class that needs access to another entity use its service or its repository. For example, let's say that a service method for entity A needs to update entity B when A is created. Should A's service class use B's repository or service layer? Both are possible, but what is the best practice? Personally, I would prefer a service to access another service. That way, it as access to more evolved methods so to say.

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

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

发布评论

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

评论(3

素年丶 2024-12-10 01:15:48

我更喜欢在服务类之间调用,因为您可能还需要来自另一个服务的一些业务逻辑。但要小心避免循环依赖。我建议您使用依赖注入,这将帮助您避免可能的循环依赖。还可以考虑为您的服务类创建接口,并从您的客户端类使用此接口(将具体实现传递给构造函数)。

然后您的 SericesA 将查找:

class ServiceA : IServiceA
{
    public ResultA Method1() { //some logic };
    public void Method2() { //some logic };
}

ServiceB,它依赖于 ServcieA。

class ServiceB: IServiceB
{
   private IServiceA _serviceA;
   public ServiceB(IServiceA serviceA)
   {
      _serviceA = serviceA;
   }

    public ResultB Method()
    {
         var result = _serviceA.Method1();
         // get result from service A and create and return result for service B
    }
}

I would prefer calling between service classes, because you may need also some business logic from another service. But be careful to avoid circular dependencies. I'm recommending you to use dependency injection which will help you to avoid possible circular dependencies. Also consider to create interfaces for your service classes and use this interface from your client classes (pass the concrete implementation to the constructor).

Then your SericesA will look:

class ServiceA : IServiceA
{
    public ResultA Method1() { //some logic };
    public void Method2() { //some logic };
}

ServiceB which depends on ServcieA.

class ServiceB: IServiceB
{
   private IServiceA _serviceA;
   public ServiceB(IServiceA serviceA)
   {
      _serviceA = serviceA;
   }

    public ResultB Method()
    {
         var result = _serviceA.Method1();
         // get result from service A and create and return result for service B
    }
}
蓦然回首 2024-12-10 01:15:48

我倾向于依靠常用的原则和实践来做出此类决策; DRY(不要重复自己)和 KISS(保持简单,愚蠢)在这里可能适用。

除非您需要由于绕过实体 B 的服务类而重复某些逻辑,否则我会直接从实体 A 的服务类调用实体 B 的存储库。

这是一个小细节,但它意味着少涉及一个类(ServiceClassA > RepositoryClassB 而不是 ServiceClassA > ServiceClassB > RepositoryClassB),这使得它在我看来是一个更简单的解决方案。

华泰

I tend to fall back on commonly used principles and practices for these sorts of decisions; DRY (Don't Repeat Yourself) and KISS (Keep It Simple, Stupid) may be applicable here.

Unless you needed to repeat some logic as a result of bypassing entity B's service class, I would call entity B's repository directly from entity A's service class.

It is a minor detail, but it means there is one less class involved (ServiceClassA > RepositoryClassB rather than ServiceClassA > ServiceClassB > RepositoryClassB) which makes it a simpler solution in my mind.

HTH

愁杀 2024-12-10 01:15:48

你不应该混合这些。你有一个实体/文档,你应该有专用的存储库、服务、控制器。如果您想在任何地方使用本文档特定的任何内容,它应该通过服务调用,而不是直接使用其他类的存储库。就设计而言,这不是一个好的做法。

You should never mix these. you got an enity/document you should have dedicated repository, service, controller for that. If you want to use anything specific to this document anywhere, it should go via service call, not directly using repository of other class. That is not a good practice in terms of design.

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