这是正确的域模型吗?

发布于 2024-11-24 12:03:17 字数 1103 浏览 5 评论 0原文

我在书本和互联网上搜索了我的问题的答案,但没有找到。也许你可以帮助我。

假设我有这样构建的域层(我认为这是基本结构):

Product
IProductRepository
IProductService
ProductCalculator


class ProductService : IProductService
//..
    public List<Product> GetSimilarProducts(productId) {
        // body of this method is just an example, it doesn't have sense
        var product = repo.FindById(productId)
        repo.FindBySimilarName(product.Name);

        // some more logic
        // and more

        return similarsProducts;
    }
//..

class ProductCalculator : ICalculate
//..
    private Product _p;
    public ProductCalculator(Product p) {
        _p = p;
    }

    public double Calculate() {
            // logic..
            // calling repo
            // calling services
            // calling own class methods

            return result;
    }

//..

那么,问题是:ProductCalculator 是否可以同时使用 ProductRepository 和 ProductService? 或者也许我会走得更远:这是正确设计的域吗?

服务可以用自己的方法来解决任务吗?我正准备将 ProductCalculator 更改为 Service,但我不知道这是否正确。我只是问,在我看来,不。

最好的问候

ps 您是否有值得查看其域模型的开源项目(例如在 github 上)的链接?

I have searched in my books and in the Internet answer to my question but I didn't finded. Maybe you could help me.

Let's suppose that I have domain layer counstructed in this way (I think it is basical stucture):

Product
IProductRepository
IProductService
ProductCalculator


class ProductService : IProductService
//..
    public List<Product> GetSimilarProducts(productId) {
        // body of this method is just an example, it doesn't have sense
        var product = repo.FindById(productId)
        repo.FindBySimilarName(product.Name);

        // some more logic
        // and more

        return similarsProducts;
    }
//..

class ProductCalculator : ICalculate
//..
    private Product _p;
    public ProductCalculator(Product p) {
        _p = p;
    }

    public double Calculate() {
            // logic..
            // calling repo
            // calling services
            // calling own class methods

            return result;
    }

//..

So, the question is: Does ProductCalculator can use both ProductRepository and ProductService?
Or maybe I go even farther: Is this correct designed domain?

Can service use his own methods to solve the task? Im heading to change ProductCalculator into Service but I dont know is this correct. Im just asking, in my opinion, no.

Best regards

ps Have you got links to an open source projects (eg. on github) that are worth to look at theirs domain model?

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

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

发布评论

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

评论(1

桃扇骨 2024-12-01 12:03:18

当模型的概念会扭曲任何实体或值对象时,服务是合适的。

根据 Evans 的 DDD,良好的服务具有以下特征:

  • 操作与不是自然部分的域概念相关实体或值对象的
  • 接口是根据域模型中的其他元素定义的
  • 操作是无状态的

Los Techies 有一些关于何时创建服务的精彩文章。

When concepts of the model would distort any Entity or Value Object, a Service is appropriate.

From Evans’ DDD, a good Service has these characteristics:

  • The operation relates to a domain concept that is not a natural part of an Entity or Value Object
  • The interface is defined in terms of other elements in the domain model
  • The operation is stateless

Los Techies has some great articles about when to create a service.

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