假设有两个仓库接口实现工作和存储库模式的单位的做法:建议使用ServiceStack.ORMLite
interface IFooRepository
{
void Delete(int id);
}
interface IBarRepository
{
void Delete(int id);
}
而像一个IUnitOfWork接口:
interface IUnitOfWork : IDisposable
{
void Commit();
void Rollback();
}
什么是最好的做法使用ServiceStack.ORMLite实现这些接口,以便用户可以使用它们,如
MyFooRepository.Delete(4);
// if an Exception throws here, Bar won't be deleted
MyBarRepository.Delete(7);
或者
using (var uow = CreateUnitOfWork())
{
MyFooRepository.Delete(4);
MyBarRepository.Delete(7);
uow.Commit(); //now they are in an transaction
}
我会建议避免使用的UOW尽可能多可能。通过这样的开放式交易通常是一个非常糟糕的设计。 (在以前的版本中,我自己是犯这个东西的) – 2014-09-08 15:27:00