因此,我尝试在系统的设计阶段实施最佳实践。我将使用带有 Entity Framework 4、ASP.NET MVC 3 C# 和存储库/工作单元模式的 DI 容器 (ninject)。
我将拥有每个数据库的表都映射到类的模型。我将在 web.config 中为每个数据库都有一个连接字符串。我将为每个连接字符串创建一个类(使用 EF 约定命名相同),该类继承自 DbContext,并为每个表提供一个 DbSet。我将为每个数据库实体提供一个抽象存储库接口,该接口按约定命名 ITRepository,其中 T 是数据库实体的名称(数据库实体 Car 的 ICarRepository)。每个存储库接口都将为实体保存一个 IQueryable,并定义应公开哪些方法。我将有一个具体的存储库 EFTRepository,它继承自 ITRepository 并定义公开方法的工作方式。我将定义一个 GenericRepository
,用于仅需要最少访问权限的实体,例如基本 CRUD。我将使用 ninject 中的 ControllerFactory 作为 DI 容器。我将在 Application_Start() 中附加 ninject 控制器工厂。我将绑定所有依赖项,例如工厂的 AddBindings() 方法中的 Bind().To()
。我将为每个数据库实现一个工作单元模式,并遵守命名约定 UnitOfWorkDB(数据库 Automobile 的 UnitOfWorkAutomobile)。工作单元将保存相关的数据库上下文和存储库。我这样做对吗?我以前没有使用过这组模式,并且想确保我将正确使用它们。欢迎批评指正!
So, I am trying to implement best practices during the design phase of a system. I am going to be using a DI container (ninject) with Entity Framework 4, ASP.NET MVC 3 C#, and the repository / unit of work pattern.
I am going to have models where each database's tables are mapped to classes.I am going to have a connection string for each database in web.config.I am going to have a class for each connection string (named the same using EF convention) which inherits from DbContext and has a DbSet for each table.I am going to have an abstract repository interface for each database entity named by convention ITRepository where T is the name of the DataBase entity (ICarRepository for database entity Car). Each repository interface will hold an IQueryable for the entity and also define what methods should be exposed.I am going to have a concrete repository EFTRepository which inherits from ITRepository and defines how the exposed methods work.I am going to have a GenericRepository<TEntity>
defined for use with entities that require only minimal access such as basic CRUD.I am going to use a ControllerFactory from ninject as a DI container.I am going to attach the ninject controller factory in Application_Start().I am going to bind all my dependencies, such as Bind<ITRepository>().To<EFTRepository>()
in the factory's AddBindings() method.I am going to implement a Unit Of Work pattern for each database and keep to naming conventions UnitOfWorkDB (UnitOfWorkAutomobile for database Automobile).The Unit Of Work will hold the relevant database context and repositories.Am I going about this right? I haven't used this set of patterns before and want to make sure I am going to be using them correctly. Criticisms welcome!
发布评论
评论(1)
从基础存储库继承可以提供一些简单的“通用”功能,但在其他情况下,您很快就会发现尝试拥有基于通用的存储库会变得很混乱(假设这就是继承的内容)。
你所描述的其他一切似乎都很好。
你没有说要注入什么,但我假设你将注入一个服务,该服务将采用一个工作单元(例如 IUnitOfWork,作为每个请求实例创建)和一个 IWhateverRepostory
Inheriting from a base repository can provide some simple 'generic' functions but in other situations you'll quickly find out it becomes messy trying to have a generic based repository (assuming that was what was inheriting).
Everything else you described seems pretty good.
You didn't say what was going to be injected where though, I'm assuming you will be injecting a service, the service will take a unit of work (IUnitOfWork for ex., created as a per request instance) and an IWhateverRepostory