C# 领域模型 +存储库:放置加载实体的代码的位置

发布于 2024-11-04 19:11:43 字数 846 浏览 3 评论 0原文

我有一个模型类,它是从我的存储库类中的“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);

所以为了加载/饱和 MyEntity 对象,我需要从数据库加载,然后调用生成器来确定“woo 因子”(咳咳)。从架构的角度来看,这段代码应该放在哪里?目前的想法:

1)在存储库中:如果我将其添加到此处,我觉得我将太多的责任交给了存储库。

2)在“MyEntity”类中。在此处添加代码,以便在访问 WooFactor 时延迟加载它。这会给 MyEntity 添加很多依赖项。

3)单独的服务类别——似乎有点过分而且没有必要。

I have a model class which is loaded from a "GetById" method in my repository class. I now need to add additional properties to this entity, which aren't saved in the database, but are calculated by a service class. Something like:

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

So in order to load/saturate a MyEntity object, I need to load from the db, and then call the generator to determine the "woo factor" (ahem). Where should this code go from an architectural viewpoint? Current thoughts:

1) In the repository: I feel like I'm handing too much responsibility to the repo if I add it here.

2) In the "MyEntity" class. Add the code in here that perhaps lazy-loads the WooFactor when it is accessed. This would add a lot of dependencies to MyEntity.

3) A separate service class - seems overkill and un-necessary.

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

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

发布评论

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

评论(4

待天淡蓝洁白时 2024-11-11 19:11:43
  • 如果 WooFactor 纯粹依赖于 MyEntity 属性,那么它必须在 MyEntity 内部完成,
  • 如果它需要外部信息(例如配置、规则等)那么它需要是存储库之外的一个单独的服务。我将在此处使用此附加属性创建一个 WooEntity

无论如何,它都不应该位于存储库中。

  • If WooFactor is purely dependent on MyEntity properties then it must be done inside MyEntity
  • If it requires external info (such as configuration, rules, etc) then it needs to be a separate service outside Repository. I would create a WooEntity here with this additional property.

In any case, it should never be in the Repository.

思慕 2024-11-11 19:11:43

谷歌 CQRS。您需要做的是将读和写问题分开。如果除实体之外的其他东西需要计算,您的答案显而易见。

Google CQRS. What you need to do is to separate the read and write concerns. If the calculation is needed by something else other than the entity, you have your answer in plain sight.

污味仙女 2024-11-11 19:11:43

我最近遇到了类似的问题,我需要聚合不同的数据来生成我的实体。我最终决定创建一个服务来处理我的 Entity 的构造以及 Entity 发生的任何操作

使用您的示例它可能看起来像这样

public MyEntityService
{
  public MyEntity GetById(int id)
  {
    MyEntity myEntity = _repo.GetById(id);
    myEntity.WooFactor = _wooFactorGenerator.CalculateWooFactor(myEntity);
    return myEntity;
  }
}

:因为与实体的任何交互都是通过服务完成的,因此对项目来说是最好的。

I ran into similar concerns recently where I needed to aggregate different data to produce my entity. I eventually decided to create a service to handle the construction of my Entity and any actions that happened to the Entity

Using your example It might look like this:

public MyEntityService
{
  public MyEntity GetById(int id)
  {
    MyEntity myEntity = _repo.GetById(id);
    myEntity.WooFactor = _wooFactorGenerator.CalculateWooFactor(myEntity);
    return myEntity;
  }
}

In the end this worked out the best for the project as any interaction to the Entity was done via the service.

睡美人的小仙女 2024-11-11 19:11:43

您管理从存储库获取数据的方式本质上是正确的 - 因此最好的通用方法是对服务执行相同的操作。拥有一致的架构和方法有很多优势。

最大的警告是管理依赖关系;我的一般假设是像这样的场景,物理数据访问被抽象出来 - 因此模型在技术上不依赖于 SQL、文件系统等。使用这种方法将允许您公开来自不同存储库的不同数据一致的方式。

  • 您使用延迟加载的想法很好;如果您抽象出技术依赖性,您对该选项的主要关注就会消失。
  • 单独的服务类可能看起来有点大材小用,但如果它意味着它可以让您更好地控制依赖项,那么付出的代价很小。

How you manage getting data from the repository is essentially correct - so the best general approach would be to do the same for the service. There's a lot of advantage in having a consistent architecture and approach.

The big caveat with that is managing the dependencies; my general assumption is scenarios like this is that the physical data access is abstracted out - so that the model isn't technically tied to SQL, the file system, etc. using this approach will allow you to expose different data from different repositories in a consistent manner.

  • Your idea of using lazy Load is good; and if you abstract out the technical dependencies your main concern with that option goes away.
  • A separate service class might seem like overkill but it's a small price to pay if it means it gives you better control over dependencies.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文