简体   繁体   English

将实体框架实体公开给其他客户端

[英]Expose Entity Framework entities to other clients

I have a .NET 4 class library that contains an Entity Framework data model and a collection of classes that provide common functionality using those entities. 我有一个.NET 4类库,其中包含实体框架数据模型和使用这些实体提供通用功能的类的集合。 These classes are used across different types of applications. 这些类可用于不同类型的应用程序。

So, my question is would it be considered good practice to expose entities contained within the class library to other applications? 因此,我的问题是,将类库中包含的实体公开给其他应用程序是否被视为一种好习惯?

If your entities are advanced enough to meet your persistence needs and your domain needs (or external application needs) and there is not much or a low-likelihood of "cross-layer pollution", then I say yes it's a good practice. 如果您的实体足够先进,可以满足您的持久性需求域需求(或外部应用程序需求),并且“跨层污染”的可能性不大或很低,那么我说是不错的做法。 It can also be a good practice in the agile development sense: good-enough-for-now. 在敏捷开发的意义上,这也是一种好的做法:“足够好了,现在就做”。

Given a longer period of time and if being a purist is more your inclination, then it starts to become a bad practice because you're increasing the coupling, for example, if you start adding attributes to deal with persistence, validation, and serialization. 给定更长的时间,如果您更倾向于纯粹主义者,那么这将成为一种不良习惯,因为您正在增加耦合,例如,如果开始添加属性来处理持久性,验证和序列化。

Some ways to avoid that are to use something like AutoMapper , generated code, or hand-coded facades, service layers, and/or adapters to minimize the effects. 避免这种情况的一些方法是使用诸如AutoMapper ,生成的代码或手动编码的外观,服务层和/或适配器之类的东西来最小化影响。

Because entity framework changes whenever the database changes, I don't believe this would provide an adequate API. 由于实体框架随数据库的变化而变化,因此我认为这不会提供足够的API。 Instead, I would recommend creating a Data Transfer Object to act as an intermediary between external code and each entity that needs to be accessed. 相反,我建议创建一个数据传输对象,以充当外部代码和每个需要访问的实体之间的中介。 Furthermore, consider creating a Facade class (Service layer) that will mediate between the internals of your library and the external "client." 此外,考虑创建一个Facade类(服务层),该类将在库的内部和外部“客户端”之间进行中介。 Good article on DTOs: http://msdn.microsoft.com/en-us/magazine/ee236638.aspx 有关DTO的好文章: http : //msdn.microsoft.com/en-us/magazine/ee236638.aspx

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

相关问题 更新具有与其他实体的多个关系的实体框架实体 - Update entity framework entity with multiple relationships to other entities 实体框架5添加具有其他外键关系的实体和子实体 - Entity Framework 5 add entity and child entities with other foreign key relationships 在包含其他实体列表的实体框架中更新实体 - Updating an entity in Entity Framework which contains lists of other entities 实体框架保存更改-在其他客户端中看不到更改 - Entity Framework savechanges - can't see changes in other clients 实体框架:当其他选项出现问题时,将依赖关系注入实体 - Entity Framework: Injecting dependencies into entities when other options are problematic 如何在C#WebCore实体框架中获取其他实体 - How to obtain other Entities in C# WebCore Entity Framework 实体框架的包含和选择是其他一些实体 - Entity Framework Include and Select are gettind some other entities 在Entity Framework 4中执行本机sql并返回除已知实体以外的其他实体 - Executing native sql in Entity Framework 4 and returning other than known entities 防止在创建具有实体框架关系的其他实体时复制现有对象 - Prevent duplication of existing object on creating other entities with entity framework relationship 实体框架相同的实体 - Entity Framework same entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM