实现域服务作为存储库的扩展方法

发布于 2024-10-10 02:35:40 字数 136 浏览 0 评论 0 原文

我对领域服务的理解是,它们执行相关存储库边界之外的任务(CRUD 相关任务)。

既然 .Net 允许扩展方法,为什么不将域服务实现为存储库的扩展方法,从而减少在需要时实例化存储库和服务的需要呢?

如果有任何意见,我将不胜感激。

My understanding of Domain Services is that they perform tasks that are outside the boundaries of the related repository (CRUD related tasks).

Since .Net allows for Extension Methods, why not implement Domain Services as Extension Methods for Repositories and thus reduce the need to instantiate both a repository and a service when required?

I'd appreciate any comments.

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

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

发布评论

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

评论(4

我是男神闪亮亮 2024-10-17 02:35:40

“我对领域服务的理解是,它们执行相关存储库边界之外的任务(CRUD 相关任务)。”

你的理解和我的不一样,是:

域服务用于封装落在单个聚合/实体/值对象边界之外的逻辑。

存储库关注的是存储,而不是领域逻辑,因此两者是非常不同的东西。

老实说,我在这里看到的有关服务的问题越多,我就越觉得人们对服务的实际含义普遍感到困惑。我认为这可能是由于以下内容之间的模糊性造成的:

  1. 应用程序服务 - (UI 调用的内容和事务开始的位置)
  2. 基础设施服务 - (例如 IEmailSenderService、ICreditCardPaymentGateway 之类的东西)
  3. 域服务 - (提供纯域逻辑,就像聚合/实体)

Jimmy Bogard 编写了 关于他们独特角色的好文章

所以回答你的问题:这是一个坏主意,因为你会混淆两个非常不同的概念,并且会违反 建议零售价

"My understanding of Domain Services is that they perform tasks that are outside the boundaries of the related repository (CRUD related tasks)."

Your understanding is different to mine, which is:

Domain Services are used to encapsulate domain logic that falls outside the boundary of a single aggregate/entity/value object.

Repositories are concerned with storage, not domain logic, and therefore the two are VERY different things.

To be honest, the more I see questions on here surrounding domain services, the more I feel there is general confusion surrounding what domain services actually are. I think this may due to the ambiguity between:

  1. Application Services - (what the UI calls & where transactions begin)
  2. Infrastructure Services - (things like an IEmailSenderService, ICreditCardPaymentGateway)
  3. Domain Services - (provides pure domain logic, just like an aggregate/entity)

Jimmy Bogard has written a good article about their distinctive roles.

So to answer your question: It's a bad idea because you'd be mixing up two very distinct concepts and would be violating the SRP

剪不断理还乱 2024-10-17 02:35:40

第一个问题是您的对象将充满基础设施方法。

其次,您必须为这些包含扩展方法的静态类添加另一层,恕我直言,这将分散您的服务层和域模型的代码。

第三,您必须通过标记接口对对象进行分类,以过滤它们能够调用的方法。

另一个问题是执行的上下文,当您在对象上调用 CRUD 方法时,它将在客户端或服务器端执行?

此外,还违反了持久性无知域模型,

最后一点是,在 ORM 中,保存的不是单个对象,而是保存的会话,回想一下 工作单元 模式。

first issue is that your object will be full of infrastructure methods.

and second you must add another layer for these static classes containing extension methods, that will scatter the code of your service layer and domain model IMHO.

third is that you must classify your objects by e.g. marker interface to filter the methods that they are able to call.

another issue is the context of execution, when you call a crud method on an object , it's would be executed client side or server side?

in addition is violating the persistence ignorant domain model,

and last is that in ORM's it's not a single object that will be saved but a session will be saved , recall Unit of Work pattern.

并安 2024-10-17 02:35:40

域服务很可能使用多个存储库。例如同时存在客户和订单存储库。

所以我认为这是一个坏主意。

A domain service may very well use multiple repositories. e.g. Customer and Order repositories at the same time.

So I think it is a bad idea.

别理我 2024-10-17 02:35:40

正如 DDD 中所指定的,服务是无状态方法(或者,在 OO 世界中,是一类相关方法),因此最终,扩展方法确实符合要求。

在存储库上使用扩展方法的最终结果将是从存储库公开服务接口,同时在存储库外部定义实现(这很好地减少了存储库的传出耦合)。通过使用委托模型定义服务方法,可以以更“OO 友好”的方式实现类似的结果(在 .Net 3.5 及更高版本中,您可以利用 Func<>Action< >),其中委托实现在其他地方定义(并且可以通过 基于委托的工厂)。

public class MyRepository
{
    //Repository Specific Methods
    public DomainObject FindById(...)
    ...

    //"Service" Methods as Delegates
    public Func<DomainObject, SomeResult> ProcessDomainObjectAndGetBackSomeResult
    {
        get
        {
            return ServiceMethodFactory.ProcessDomainObjectAndGetBackSomeResult;
        }
    }
}

Services, as specified in DDD, are stateless methods (or, in the OO world, a class of related methods) so ultimately, extension methods do fit the bill.

The end result of using extension methods on your Repositories will be to expose the service interface from the Repository, while defining the implementation outside of the repository (which nicely reduces the efferent coupling of your repository). A similar result can be achieved in a more "OO-Friendly" manner by defining the service methods using delegate model (in .Net 3.5 and above you can leverage Func<> and Action<>), where the delegate implementations are defined elsewhere (and can be accessed via a delegate based factory).

public class MyRepository
{
    //Repository Specific Methods
    public DomainObject FindById(...)
    ...

    //"Service" Methods as Delegates
    public Func<DomainObject, SomeResult> ProcessDomainObjectAndGetBackSomeResult
    {
        get
        {
            return ServiceMethodFactory.ProcessDomainObjectAndGetBackSomeResult;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文