假设有两个存储库接口:
interface IFooRepository
{
void Delete(int id);
}
interface IBarRepository
{
void Delete(int id);
}
Run Code Online (Sandbox Code Playgroud)
和IUnitOfWork接口类似:
interface IUnitOfWork : IDisposable
{
void Commit();
void Rollback();
}
Run Code Online (Sandbox Code Playgroud)
使用ServiceStack.ORMLite实现这些接口的最佳实践是什么,以便用户可以像使用它们一样使用它们
MyFooRepository.Delete(4);
// if an Exception throws here, Bar won't be deleted
MyBarRepository.Delete(7);
Run Code Online (Sandbox Code Playgroud)
要么
using (var uow = CreateUnitOfWork())
{
MyFooRepository.Delete(4);
MyBarRepository.Delete(7);
uow.Commit(); //now they are in an transaction
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ServiceStack.ORMLite实现Repository Pattern,如下所示:
public class MyRepository : IMyRepository
{
private IDbConnectionFactory DbConnectionFactory = null;
public MyRepository(IDbConnectionFactory dbConnectionFactory)
{
DbConnectionFactory = dbConnectionFactory;
}
public void MyMethod()
{
using (var connection = DbConnectionFactory.OpenDbConnection())
using (var cmd = connection.CreateCommand())
{
//Do something here
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我需要在DbTransaction中扭曲一些数据库操作时,我不知道如何处理DbTransaction.它看起来像是TransactionScope一个解决方案,但我不知道这是否太重了.