使用 Entity Framework 4.1 的依赖注入以及“工作单元”和“存储库”图案
我已经看到使用工作单元和存储库模式的实体框架的各种实现。理想情况下,我想使用工作单元、数据库上下文、存储库(对于通用存储库和特定存储库)的接口,并在应用程序启动时在 BootStrapper 代码中实例化它们。这实际上可能吗?
检查的示例:
http://microsoftnlayerapp.codeplex.com (没有任何单独的工作单元类,数据库上下文本身实现了 IUnitOfwork)
http://efmvc.codeplex.com (使用数据库工厂实例化数据库上下文,无需利用DI 和数据库上下文未实现接口)
http://code.msdn.microsoft. com/ASPNET-MVC-Application-b01a9fe8(实例化数据库上下文和工作单元内的存储库,无需任何 DI)
某些实现不使用数据库上下文的任何接口,某些实现则实例化数据库上下文UnitOfWork 类内的存储库在构造函数中传递具体上下文类,有些在存储库的构造函数中传递工作单元的接口,这完全令人困惑。使用 Entity Framework 4.1 时是否有一种允许 DI 和 TDD 的最佳实践方法?有一个示例可以证明这一点吗?
I have seen various implementations for Entity Framework using unit of work and repository patterns. ideally I want to use interfaces for unit of work, database context, repository (for both generic and specific repositories) and instantiate them in the BootStrapper code on application startup. Is this actually possible?
samples checked:
http://microsoftnlayerapp.codeplex.com (doesn't have any separate unit of work class, the database context itself implements IUnitOfwork)
http://efmvc.codeplex.com (uses a database factory to instantiate database context without utilising DI and database context does not implement interface)
http://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8 (instantiates database context and the repositories inside unit of work without any DI)
some implementations don't use any interface for database context and some instantiate database context and the repositories inside the UnitOfWork class and pass concrete context class in the constructor and some pass the interface for unit of work in the constructor for repository and it is totally confusing. Is there one best practice approach that allows DI and TDD when using Entity Framework 4.1? Is there a sample that demonstrates this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止我有下一个:
声明:
实现:
IoC 实用程序:
Unity.config:
I have next so far:
Declaration:
Implementation:
IoC utility:
Unity.config: