Sam*_*Sam 4 c# asp.net entity-framework 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.
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.
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).
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?
Mar*_*ill 10
也许我误解了你的问题,但是EDMX生成的代码继承自ObjectContext并不会阻止你使用依赖注入.听起来您担心无法将您的ObjectSet注入您的存储库,但这并不是它的设计使用方式.
使用通用存储库模式(例如此处的存储库模式),IRepository接口就是您注入ViewModels/Controllers/Whatever的东西.
因此,您不要将IObjectContext或IObjectSet注入存储库; 相反,您将IRepsoitory注入需要它的类中,并提供使用ObjectSet的IRepository接口的实现.然后,您可以模拟您的IRepository接口进行测试,或切换到完全不同的具体存储库实现,而不会影响您的任何其他代码.
我们目前正在使用EF4 DB-first以及上面链接的存储库模式执行完全相同的操作,并且它运行得非常好.