问题描述
我的工作从过去的几天里,我已完成了高达好的扩展,相信实施Repository模式和的UnitOfWork。我相信有很多方法来实现这一点,但我很有趣的发现对于最好的方法是什么。
我使用的Visual Studio采取非常简单的例子,codeD在ASP.NET MVC 5 2013年,我的问题的重点是实施的UnitOfWork的。它是建议使用多个UnitOfWorks每个企业关注的私有方法实现资源库的功能和赠送的控制器使用????公共职能
我在通过通用存储库中的控制器类函数表(SQL Server)的。我有IGenericRepository有IQueryable的一个功能,我有GenericRepository类我在哪里实现了这个接口。我这是从baseContext继承FunctionContext。我已经baseContext之所以这么所有dbcontexts可以使用一个路径击中数据库,但同时保持表的有限数量的业务需求。
现在在我的方法;
- 一个BaseContext:的DbContext
- 多的DbContext,捆绑所有需要的表个别企业的关注扩展BaseContext
- 通用库接口(CRUD)
- 通用仓库实现类
- 针对库类,以防扩大通用Reposirtory需要在CRUD操作的顶部多的功能。
- 个别的UnitOfWork - >仅适用于使用功能采取所需资料库/存储库中的私有方法和提供公共方法
- 控制器调用需要的UnitOfWork使用公共的方法。
在下面的程序中,所有我得到的功能标题列表,并传递给控制器打印
通用库接口
公共接口IGenericRepository< TEntity> :IDisposable接口
{
IQueryable的< TEntity>得到所有();}
通用仓库
公共类GenericRepository< TEntity> :IGenericRepository< TEntity>其中,TEntity:类
{
保护DbSet< TEntity> _DbSet;
私人只读的DbContext _dbContext; 公共GenericRepository()
{ } 公共GenericRepository(的DbContext的DbContext)
{
this._dbContext =的DbContext;
_DbSet = _dbContext.Set< TEntity>();
} 公众的IQueryable< TEntity>得到所有()
{
返回_DbSet;
}
公共无效的Dispose()
{ }
}
BaseContext
公共类BaseContext< TContext> :哪里的DbContext TContext:的DbContext
{
静态BaseContext()
{
Database.SetInitializer< TContext>(NULL);
} 保护BaseContext()
:基地(NAME = ApplicationDbConnection)
{}
}
FunctionContext
公共类FunctionsContext:BaseContext< FunctionsContext>
{
公共DbSet< App_Functions>功能{搞定;组; }
}
函数映射类
[表(功能)
公共类App_Functions
{
公共App_Functions()
{
} [键]
公众诠释Function_ID {搞定;组; } [StringLength(50)]
[需要]
公共字符串名称{搞定;组; } 公众诠释Hierarchy_level {搞定;组; }
}
功能域类
公共类功能
{
公共功能()
{ } 公众诠释Function_ID {搞定;组; }
公共字符串名称{搞定;组; }
公众诠释Hierarchy_level {搞定;组; }
}
Function_UnitOfWork
公共类Function_UnitOfWork
{
私人GenericRepository< App_Functions> _function_Repository =新GenericRepository&所述; App_Functions>(新FunctionsContext()); 公共Function_UnitOfWork()
{ } 私人列表<功能与GT; getAllFunctionsFromRepository()
{
清单<功能与GT;查询=新的List<功能及GT;(); 查询= _function_Repository.GetAll()选择(X =方式>的新功能
{
Function_ID = x.Function_ID,
标题= x.Title,
Hierarchy_level = x.Hierarchy_level
})了ToList()。 返回查询;
} 公开名单<功能与GT; GetAllFunctions()
{
返回getAllFunctionsFromRepository();
}}
控制器类
公共类HomeController的:控制器
{
公众的ActionResult指数()
{
Function_UnitOfWork UOF =新Function_UnitOfWork(); 变种A1 = UOF.GetAllFunctions(); 的foreach(在A1 VAR项)
{
VAR B1 = item.Title;
}
返回查看();
}
}
虽然它是基于认为,单位工作模式的只是名字建议限制对象的时间跨度。因此,在控制它的使用应该是这样
公众的ActionResult指数()
{
使用(VAR UOF =新Function_UnitOfWork()){ 变种A1 = UOF.GetAllFunctions(); 的foreach(在A1 VAR项)
{
VAR B1 = item.Title;
}
} 返回查看();
}
此外,我们使用的一种方法是(简称)
公共类数据对象{}公共类回购:IRepo< T>其中T:数据对象公共类RepoController< T> :控制器其中T:{数据对象
保护IRepo< T>回购;
}
所以,控制器将是通用的,将有特定的回购字段
您将使用一些依赖注入工具,就像ninject或MEF来约束控制器与幕后回购。
I am working on implementing Repository Pattern and UnitOfWork from last few days, which I have completed to upto good extend, I believe. I am sure there are plenty of ways to implement that but what I am interesting to find best approach for that.
I am taking very simple example coded in ASP.NET MVC 5 using visual studio 2013. my main focus of question is implementation of UnitOfWork. is it advisable to use multiple UnitOfWorks for each business concerns implementing repository functions in private method and giving away public functions for controller to use????
I have function table (SQL Server) in the controller class via generic repository. I have IGenericRepository which has IQueryable one function, I have GenericRepository class where i am implementing this interface. I got FunctionContext which is inherited from baseContext. The reason i have baseContext so all the dbcontexts can use one path to hit database but same time keep number of table limited to business need.
now in my approach;
- One BaseContext : DbContext
- multiple DbContext, bundling all required table to individual business concern that extend BaseContext
- Generic Repository Interface (CRUD)
- Generic Repository Implementation class
- specific Repository class, extending Generic Reposirtory in case require more function on top of CRUD operations.
- Individual UnitOfWork --> taking to required repository/ repositories in private method and provide public method only for using functions
- Controller call require UnitOfWork to use public methods.
in following program, all i am getting list of function title and passing to controller to print
Generic Repository Interface
public interface IGenericRepository<TEntity> : IDisposable
{
IQueryable<TEntity> GetAll();
}
Generic Repository
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
protected DbSet<TEntity> _DbSet;
private readonly DbContext _dbContext;
public GenericRepository()
{
}
public GenericRepository(DbContext dbContext)
{
this._dbContext = dbContext;
_DbSet = _dbContext.Set<TEntity>();
}
public IQueryable<TEntity> GetAll()
{
return _DbSet;
}
public void Dispose()
{
}
}
BaseContext
public class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext()
: base("name = ApplicationDbConnection")
{ }
}
FunctionContext
public class FunctionsContext : BaseContext<FunctionsContext>
{
public DbSet<App_Functions> Functions { get; set; }
}
Function Mapping class
[Table("Functions")]
public class App_Functions
{
public App_Functions()
{
}
[Key]
public int Function_ID { get; set; }
[StringLength(50)]
[Required]
public string Title { get; set; }
public int Hierarchy_level { get; set; }
}
Function Domain class
public class Functions
{
public Functions()
{
}
public int Function_ID { get; set; }
public string Title { get; set; }
public int Hierarchy_level { get; set; }
}
Function_UnitOfWork
public class Function_UnitOfWork
{
private GenericRepository<App_Functions> _function_Repository = new GenericRepository<App_Functions>(new FunctionsContext());
public Function_UnitOfWork()
{
}
private List<Functions> getAllFunctionsFromRepository()
{
List<Functions> query = new List<Functions>();
query = _function_Repository.GetAll().Select(x => new Functions
{
Function_ID = x.Function_ID,
Title = x.Title,
Hierarchy_level = x.Hierarchy_level
}).ToList();
return query;
}
public List<Functions>GetAllFunctions()
{
return getAllFunctionsFromRepository();
}
}
Controller class
public class HomeController : Controller
{
public ActionResult Index()
{
Function_UnitOfWork UOF = new Function_UnitOfWork();
var a1 = UOF.GetAllFunctions();
foreach(var item in a1)
{
var b1 = item.Title;
}
return View();
}
}
While it is opinion based, just the name of of Unit Of Work pattern suggest limited timespan of the object. So the use of it in controller should be something like
public ActionResult Index()
{
using(var UOF = new Function_UnitOfWork()) {
var a1 = UOF.GetAllFunctions();
foreach(var item in a1)
{
var b1 = item.Title;
}
}
return View();
}
Also one approach we use is (in short)
public class DataObject { }
public class Repo: IRepo<T> where T: DataObject
public class RepoController<T> : Controller where T: DataObject {
protected IRepo<T> Repo;
}
So controllers will be generic and will have field for particular repo
You will use some dependency injection tool, like ninject or mef to bound controllers with repos behind the scene.
这篇关于最好的做法在ASP.NET中实现MVC模式库和的UnitOfWork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!