C#Domain Model + repository:放置加载实体的代码的位置

Mat*_*rts 7 c# architecture domain-driven-design repository-pattern cqrs

我有一个模型类,它从我的存储库类中的"GetById"方法加载.我现在需要向此实体添加其他属性,这些属性不保存在数据库中,而是由服务类计算.就像是:

public class MyEntity
{
    public int ThingId { get; set; };
    public string ThingName { get; set; }

    // Set from the service
    public int WooFactor { get; set; }
}

public class WooFactorGenerator
{
    public int CalculateWooFactor(MyEntity thing); // Hits other services and repo's to eventually determine the woo factor.
}

// Code to get a "MyEntity":

var myEntity = repo.GetById(1);
var gen = new WooFactorGenerator();
myEntity.WooFactor = gen.CalculateWooFactor(myEntity);
Run Code Online (Sandbox Code Playgroud)

因此,为了加载/饱和MyEntity对象,我需要从db加载,然后调用生成器来确定"woo factor"(ahem).这段代码应该从架构的角度来看?目前的想法:

1)在存储库中:如果我在这里添加它,我觉得我对repo负有太多责任.

2)在"MyEntity"课程中.在这里添加代码,可能在访问WooFactor时延迟加载WooFactor.这会给MyEntity增加很多依赖.

3)一个单独的服务类 - 似乎有点过分和不必要.

Ali*_*tad 2

  • 如果WooFactor纯粹依赖于MyEntity属性,那么它必须在内部完成MyEntity
  • 如果它需要外部信息(例如配置、规则等),那么它需要是Repository 之外的单独服务。我会WooEntity用这个附加属性创建一个here。

无论如何,它永远不应该在Repository.