C# 领域模型 +存储库:放置加载实体的代码的位置
我有一个模型类,它是从我的存储库类中的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WooFactor
纯粹依赖于MyEntity
属性,那么它必须在MyEntity
内部完成,WooEntity
。无论如何,它都不应该位于
存储库
中。WooFactor
is purely dependent onMyEntity
properties then it must be done insideMyEntity
WooEntity
here with this additional property.In any case, it should never be in the
Repository
.谷歌 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.
我最近遇到了类似的问题,我需要聚合不同的数据来生成我的实体。我最终决定创建一个服务来处理我的
Entity
的构造以及Entity
发生的任何操作使用您的示例它可能看起来像这样
:因为与
实体
的任何交互都是通过服务完成的,因此对项目来说是最好的。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 theEntity
Using your example It might look like this:
In the end this worked out the best for the project as any interaction to the
Entity
was done via the service.您管理从存储库获取数据的方式本质上是正确的 - 因此最好的通用方法是对服务执行相同的操作。拥有一致的架构和方法有很多优势。
最大的警告是管理依赖关系;我的一般假设是像这样的场景,物理数据访问被抽象出来 - 因此模型在技术上不依赖于 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.