实体框架4 DB-首先依赖注入? [英] Entity Framework 4 DB-First Dependency Injection?

查看:126
本文介绍了实体框架4 DB-首先依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我preFER创造我自己的数据库,建立索引,唯一约束等,从数据库生成域模型与EDMX实体框架设计不在话下。

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.

现在我感兴趣的是建立一些仓库的使用依赖注入。我看着在计算器上的一些文章和帖子,似乎把重点放在code-第一种方法。这是pretty光滑如何创建一个通用的存储库来处理CRUD和使用依赖注入来选择的实施细节。

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. It is pretty slick how you can create a generic repository to handle the CRUD and use Dependency Injection to choose the implementation details.

我想要做同样的事情,但似乎由E​​DMX过程中产生的域模型继承,而不是实现接口的具体类(ObjectContext的/对象集,而不是IObjectContext的/ IObjectSet)。

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).

有没有人有他们可以点我就利用DB-第一/ code代技术时,我会如何使用依赖注入任何资源?

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?

推荐答案

也许我误解你的问题,但该EDMX产生code,从ObjectContext的继承不使用依赖注入阻止你的事实。这听起来像你担心不能够注入的对象集到库,但是这是不太它被设计成可以使用的方式。

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. 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.

通过一个通用的存储库模式,如发现之一<一个href=\"http://huyrua.word$p$pss.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/\">here,在IRepository界面是你注入你的ViewModels /控制器的东西/不管。

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.

所以,你不注射的IObjectContext的或IObjectSet到你的仓库;相反,你注入你IRepsoitory到您的类需要它,你提供使用您的ObjectSet的IRepository接口的实现。然后,您可以嘲笑你的IRepository接口进行测试,或者切换到一个完全不同的具体仓库实现,而不会影响您的任何其他code的。

So, you don't inject an IObjectContext or IObjectSet into your Repository; instead, you inject your IRepsoitory into your classes that need it, and you provide an implementation of the IRepository interface that uses your ObjectSet. 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.

我们现在正在做与EF4 DB-第一,我上面链接库这个模式完全一样的东西,它是工作得相当好。

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.

这篇关于实体框架4 DB-首先依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆