简体   繁体   English

实体框架4 DB-First依赖注入?

[英]Entity Framework 4 DB-First Dependency Injection?

I prefer creating my own DB, setting up indexes, unique constraints etc. Generating the domain model from the database is a cinch with the edmx Entity Framework designer. 我更喜欢创建自己的数据库,设置索引,独特的约束等。从数据库生成域模型是与edmx实体框架设计器的结合。

Now I'm interested in setting up some repositories an using Dependency Injection. 现在我有兴趣使用依赖注入建立一些存储库。 I've looked at some articles and posts on StackOverflow and seem to focus on the code-first approach. 我查看了StackOverflow上的一些文章和帖子,似乎专注于代码优先方法。 It is pretty slick how you can create a generic repository to handle the CRUD and use Dependency Injection to choose the implementation details. 如何创建一个通用的存储库来处理CRUD并使用依赖注入来选择实现细节是非常光滑的。

I'd like to do the same thing, but it seems that the domain model generated by the edmx process inherits concrete classes instead of implementing interfaces (ObjectContext/ObjectSet as opposed to IObjectContext/IObjectSet). 我想做同样的事情,但似乎edmx进程生成的域模型继承了具体的类而不是实现接口(ObjectContext / ObjectSet而不是IObjectContext / IObjectSet)。

Does anyone have any resources they can point me to on how I might use Dependency Injection when utilizing the db-first/code generation technique? 有没有人能够指出我在使用db-first /代码生成技术时如何使用依赖注入?

Maybe I am misunderstanding your question, but the fact that the EDMX generates code that inherits from ObjectContext doesn't stop you from using dependency injection. 也许我误解了你的问题,但是EDMX生成的代码继承自ObjectContext并不会阻止你使用依赖注入。 It sounds like you are worried about not being able to inject your ObjectSet into your Repository, but that isn't quite the way it is designed to be used. 听起来您担心无法将您的ObjectSet注入您的存储库,但这并不是它的设计使用方式。

With a generic repository pattern such as the one found here , the IRepository interface is the thing that you inject into your ViewModels/Controllers/Whatever. 使用通用存储库模式(例如此处的存储库模式),IRepository接口就是您注入ViewModels / Controllers / Whatever的东西。

So, you don't inject an IObjectContext or IObjectSet into your Repository; 因此,您不要将IObjectContext或IObjectSet注入存储库; instead, you inject your IRepsoitory into your classes that need it, and you provide an implementation of the IRepository interface that uses your ObjectSet. 相反,您将IRepsoitory注入需要它的类中,并提供使用ObjectSet的IRepository接口的实现。 You can then mock your IRepository interface for testing, or switch to a completely different concrete repository implementation, without affecting any of your other code. 然后,您可以模拟您的IRepository接口进行测试,或切换到完全不同的具体存储库实现,而不会影响您的任何其他代码。

We are currently doing this exact same thing with EF4 DB-first and the repository pattern I linked above, and it is working quite nicely. 我们目前正在使用EF4 DB-first以及上面链接的存储库模式执行完全相同的操作,并且它运行得非常好。

I myself have been researching for an answer for this question and I got this solution: DBContext generator tutorial to generate a POCO model after creating the Entity model with database first. 我自己一直在研究这个问题的答案,我得到了这个解决方案: DBContext生成器教程 ,在首先用数据库创建实体模型后生成POCO模型。

After that the implementation is pretty straightforward as this resembles very much with CodeFirst Repository&DI patterns Repository & DI (IoC) patterns 之后,实现非常简单,因为这与CodeFirst存储库和DI模式存储库和DI(IoC)模式非常相似

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

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