更新时间:2023-02-25 18:27:45
使用工作模式的单位,你不把所有的数据访问方法,工作独立的单元。您可以使用工作单位周围需要做的,这是在大多数情况下,在Web应用程序中的WebRequest整部作品。这个想法是,一个请求可能失败,或成功。当一个请求过程中添加2项数据库中,既要增加,还是不行。不只是其中之一。在大多数情况下,最简单的方法在MVC开始工作单位(或其他网络)的应用是在Global.asax
With the unit of work pattern, you don't put every dataaccess method in a separate unit of work. You use the unit of work around the whole work that needs to be done, which is in most cases in a web application a webrequest. The idea is that a request can fail, or succeed. When you add 2 items to the database during one request, the should be both added, or not. Not just one of them. In most cases, the easiest way to start a unit of work in a mvc (or other web) application is in the begin and end request methods of the global.asax
class Global
{
BeginRequest()
{
servicelocater.get<unitofwork>().start();
}
EndRequest()
{
var unit = servicelocater.Get<Unitofwork>();
try
{
unit.commit();
}
catch
{
unit.rollback();
throw;
}
}
}
class Repository<T>
{
public Repository(INHibernateUnitofwork unitofwork)
{
this.unitofwork = unitofwork;
}
public void Add(T entity)
{
unitofwork.session.save(entity);
}
}