简体   繁体   English

为什么EF实体正在依赖POCO

[英]Why EF Entities are making a dependency and the need of POCO

I'm pretty new to IoC, Dependency Injection and Unit Testing. 我是IoC,依赖注入和单元测试的新手。 I'm starting a new pet project and I'm trying to do it right. 我正在开始一个新的宠物项目,我正在努力做到这一点。

I was planning to use the Repository pattern to mediate with the data. 我打算使用Repository模式来调解数据。 The objects that I was going to return from the repositories were going to be objects collected from a Linq to entities data context (EF4). 我将从存储库返回的对象将是从Linq收集到实体数据上下文 (EF4)的对象。

I'm reading in "Dependency Injection" from Mark Seeman that doing it, makes an important dependency and will definitely complicate the testing (that's what he's using POCO objects in a Library Project). 我正在阅读Mark Seeman的“Dependency Injection”,认为这样做会产生一个重要的依赖性,并且肯定会使测试变得复杂(这就是他在图书馆计划中使用POCO对象的原因)。

I'm not understanding why. 我不明白为什么。 Although the objects are created by a linq to entities context, I can create them simply calling the constructor as they were normal objects. 虽然对象是由linq到实体上下文创建的,但我可以创建它们只是调用构造函数,因为它们是普通对象。 So I assume that is possible to create fake repositories that deviler these objects to the caller. 所以我假设有可能创建伪造的存储库,将这些对象转发给调用者。

I'm also concerned about the automatic generation of POCO classes, which is not very easy. 我也担心自动生成POCO类,这不是很容易。

Can somebody bring some light? 有人可以带点光吗? Are POCO objects trully necessary for a decoupled and testable project? POCO对象是解耦和可测试项目的必要条件吗?

* *EDIT: Thanks to Yuck I understand that it's better to avoid autogeneration with templates, which brings me to a design question. * *编辑:感谢Yuck我明白最好避免使用模板自动生成,这让我想到了一个设计问题。 If I come from a big legacy database wich his tables are assuming a variety of responsabilities (doesn't fit well with the concept of a class with a single responsability), what's the best way to deal with that? 如果我来自一个庞大的遗留数据库,他的表格承担了各种责任(不适合具有单一责任的类的概念),那么处理这个问题的最佳方法是什么?

Delete the database is not an option ;-) 删除数据库不是一个选项;-)

No, they're not necessary it just makes things easier, cleaner. 不,它们没有必要它只是让事情变得更容易,更清洁。

The POCO library won't have any knowledge that it's being used by Entity Framework. POCO库不会知道实体框架正在使用它。 This allows it to be used in other ways - in place of a view model, for instance. 这允许以其他方式使用它 - 例如代替视图模型。 It also allows you to use the same project on both sides of a WCF service which eliminates the need to create data transfer objects (DTO). 它还允许您在WCF服务的两侧使用相同的项目,从而无需创建数据传输对象(DTO)。

Just two examples from personal experience but there are surely more. 仅举两个个人经验的例子,但肯定会有更多。 In general the less a particular object or piece of code knows about who is using it or how it's being used will make it more adaptable and generic for other situations. 一般来说, 一个特定的对象或一段代码知道谁在使用它或者它如何被使用将使其更具适应性和通用的其他情形。

You also mention automatic generation of POCO classes . 您还提到了POCO类的自动生成 I don't recommend doing this. 我不建议这样做。 Were you planning to generate the class definitions from your database structure? 您是否计划从数据库结构生成类定义?

I was planning to use the Repository pattern to mediate with the data. 我打算使用Repository模式来调解数据。 The objects that I was going to return from the repositories were going to be objects collected from a Linq to entities data context (EF4). 我将从存储库返回的对象将是从Linq收集到实体数据上下文(EF4)的对象。

The default classes (not the POCOs) EF generates contain proxies for lazy loading and are tied at the hip to Entity Framework. EF生成的默认类(不是POCO)包含延迟加载的代理,并且与实体框架紧密相关。 That means any other class that wants to use those classes will have to reference the required EF assemblies. 这意味着任何其他想要使用这些类的类都必须引用所需的EF程序集。

This is the dependency Mark Seeman is talking about. 这是Mark Seeman所说的依赖。 Since you are now dependent on these non-abstract types, which in turn are dependent on EF, you cannot simply change the implementation of your repository to something different (ie just using your own persistence store) without addressing this change in the class that depend on these types. 由于您现在依赖于这些非抽象类型,而这些类型又依赖于EF,因此您无法简单地将存储库的实现更改为不同的(即仅使用您自己的持久性存储),而无需在依赖于此类的类中解决此更改在这些类型上。

If you are truly only interested in the public properties of the EF generated types then you can have the partial classes generated by EF implement a base interface. 如果您真的只对EF生成的类型的公共属性感兴趣,那么您可以让EF生成的部分类实现基本接口。 Put all the properties you need in that base interface and pass the dependency in as the base interface - now you only depend on the base interface and not EF anymore. 将所需的所有属性放在该基本接口中并将依赖项作为基接口传递 - 现在您只依赖于基本接口而不再依赖于EF。

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

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