简体   繁体   English

DTO和实体框架

[英]DTOs and Entity Framework

My solution uses Entity Framework, and I translate the EF models to DTO objects to pass up and down from the UI layer. 我的解决方案使用Entity Framework,我将EF模型转换为DTO对象以从UI层上下传递。

But I have a design question: I have a Person table, and a PersonUnavailibility table. 但我有一个设计问题:我有一个Person表和一个PersonUnavailibility表。 A person can have period where they are unavailable. 一个人可以有他们不在的时期。

My PersonDTO object has all the properties of the PersonEF model, as well as a List<PersonUnavailibilityDTO> objects. 我的PersonDTO对象具有PersonEF模型的所有属性,以及List<PersonUnavailibilityDTO>对象。 So, when I get my person, I also get the persons periods of unavailibility. 所以,当我找到我的人时,我也会得到不可用的人。

However, should my PersonUnavailibilityDTO have a PersonDTO object? 但是,我的PersonUnavailibilityDTO是否应该有PersonDTO对象? So if I get a PersonUnavailibilityDTO object, I can see which person it's related to? 所以如果我得到一个PersonUnavailibilityDTO对象,我可以看到它与哪个人有关系?

If so, I get a circular references. 如果是这样,我会得到一个循环引用。 My PersonUnavailibilityDTO's Person property, has a list of all his PersonUnavailibility rows... and each of those, has a PersonDTO , and each of those has a list of.... etc etc. 我的PersonUnavailibilityDTO's Person属性,列出了他所有的PersonUnavailibility行...以及每个行的PersonDTO ,每个行都有一个......等等。

What is the best design for this sort of thing? 这种东西最好的设计是什么? Only include child objects related to the parent object? 只包含与父对象相关的子对象?

That is, only the PersonDTO has a list of PersonUnavailibilityDTOs , but the PersonUnavailibilityDTO doesn't have a PersonDTO ? 也就是说,只有PersonDTO具有列表PersonUnavailibilityDTOs ,但PersonUnavailibilityDTO没有PersonDTO

It depends. 这取决于。

In most cases it would not be required since you will be viewing the dto's from the persons view and the unavailabilities are always accessed through the person. 在大多数情况下,由于您将从人员视图中查看dto,因此不需要它,并且始终通过此人访问不可用性。

But when you will access your object the other way around it is nescessary to add the person to the unavailability. 但是,当您访问对象时,相反的方法是将此人添加到不可用状态。

Try to keep your Dto's as small and minimalistic as possible. 尽量让你的Dto尽可能小巧简约。

One of possible approaches would be to have a generic composite dto model to carry all possible combinations of entities. 一种可能的方法是使用通用复合dto模型来承载所有可能的实体组合。

http://netpl.blogspot.com/2010/12/generic-dto-model-and-other-silverlight.html http://netpl.blogspot.com/2010/12/generic-dto-model-and-other-silverlight.html

At the price of losing the strict structure, you get a reusable one. 以失去严格结构为代价,您将获得可重复使用的结构。

As a general recommendation, it's better to avoid bidirectional relationships between objects. 作为一般建议,最好避免对象之间的双向关系。

The risk is to have objects that are out of sync, ie object A points to B (or a collection of B) but B doesn't point to A - typically, it will have a null reference instead. 风险是让对象不同步,即对象A指向B(或B的集合)但B不指向A - 通常,它将具有空引用。 To prevent that from happening, you always have to keep both ends of the relationship synchronized, which comes at a cost. 为了防止这种情况发生,您始终必须保持关系的两端同步,这需要付出代价。

However, this point is mitigated when the objects are immutable (which may be the case with your DTO's) since they are composed at creation and never modified afterwards, nullifying the risk of discrepancy between them. 但是,当对象是不可变的时(这可能是您的DTO的情况),这一点得到了缓解,因为它们是在创建时组合的,之后从未修改过,从而消除了它们之间出现差异的风险。

As Jeroen pointed out, I'd still recommend going for the simplest possible solution : PersonDTO -> PersonUnavailibilityDTO until definitely proven that you need the reverse relationship. 正如Jeroen指出的那样,我仍然建议寻找最简单的解决方案: PersonDTO -> PersonUnavailibilityDTO直到明确证明你需要反向关系。

I prefer removing collections from DTOs and adding service methods to request the collections instead: 我更喜欢从DTO中删除集合并添加服务方法来请求集合:

function GetUnavailable(PersonID, Options) as IEnumerable(of PersonUnavailabilityDTO)

This way 这条路

  1. No circular references 没有循环引用

  2. DTO tends to be light weight DTO往往重量轻

  3. The actual collection can be neatly specified with additional filters, ordering or paging applied directly in database 可以使用额外的过滤器,直接在数据库中应用的排序或分页来整齐地指定实际的集合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM