リポジトリにトランザクションを開始する機能を与え、TransactionCommand を関係するすべてのリポジトリに渡します。最終リポジトリの更新が完了したら、コミットします。このようにして、アドホック/カスタム クエリを記述しなくても、既存のリポジトリ (基本的にモデル/エンティティごとに 1 つ) を引き続き使用できます。
モデル/エンティティごとに個別のリポジトリを定義するのではなく、モデル/エンティティ自体にリポジトリ インターフェイスを実装してもらいたいと思います。前者は多態的なリポジトリ操作を許可しますが、後者は許可しません。生きて燃える。
これはトランザクション リポジトリの使用法です。このメソッドは Entity1Service クラスから取得されました (エンティティごとに 1 つのクラスである私のサービス レイヤーには、目的を達成するために 1 つ以上のリポジトリを使用する静的メソッドが含まれています)。
public class Entity1Service
{
public static bool WriteEntity1(Entity1 entity1)
{
Entity1RepositorySQLite sqlRepo = new Entity1RepositorySQLite();
sqlRepo.BeginTransaction();
int entity1Id;
if (!LocalIdService.ReadAndIncrementEntity1Id(out entity1Id, sqlRepo.TransactionCommand))
{
sqlRepo.RollbackTransaction();
return false;
}
int entity1IdOld = entity1.Id;
try
{
IdMapService.CreateMapEntity1Id(entity1Id, entity1Id, sqlRepo.TransactionCommand);
entity1.Id = entity1Id;
sqlRepo.Write(attachment);
}
catch (Exception)
{
sqlRepo.RollbackTransaction();
attachment.Entity1Id = attachmentIdOld;
throw;
}
sqlRepo.EndTransaction();
return true;
}
}
リポジトリ定義
public interface ITransaction
{
SqliteCommand TransactionCommand { get; set; }
bool InTransaction { get; }
void BeginTransaction();
void RollbackTransaction();
void CloseTransaction();
void EndTransaction();
}
public class RepositorySQLiteTransactional<T> : RepositorySQLite<T>, ITransaction where T : new()
{
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper) : base(dataMapper) { }
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper, SqliteCommand transactionCommand) : this(dataMapper)
{
_dbLayer.DblTransactionCommand = transactionCommand;
}
public SqliteCommand TransactionCommand
{
get { return (SqliteCommand)_dbLayer.DblTransactionCommand; }
set
{
_dbLayer.DblTransactionCommand = value;
}
}
public bool InTransaction
{
get { return _dbLayer.DblInTransaction; }
}
public void BeginTransaction()
{
try
{
_dbLayer.DblBeginTransaction();
}
catch( Exception )
{
_dbLayer.DblCloseTransaction();
throw;
}
}
public void RollbackTransaction()
{
_dbLayer.DblRollbackTransaction();
}
public void CloseTransaction()
{
_dbLayer.DblCloseTransaction();
}
public void EndTransaction()
{
_dbLayer.DblEndTransaction();
}
}
public interface IRepository<T>
{
List<T> Read(IConditions conditions);
T FindOne(IQuery query);
List<T> FindAll(IQuery query);
void WriteOne(T obj);
void WriteOne(T obj, out int newId);
void WriteOne(IQuery query);
void WriteAll(List<T> objs);
void UpdateOne(T obj);
void UpdateAll(List<T> objs);
void UpdateOne(IQuery query);
void ReplaceAll(List<T> objs);
void DeleteAll();
void DeleteAll(List<T> objs);
//void Add(T entity);
//void Delete(T entity);
//void Edit(T entity);
//void Save();
}
public class RepositorySQLite<T> : IRepository<T> where T : new()
{
protected AndroidDB _dbLayer;
protected IDataMapperSQLite<T, string[]> _dataMapper;
private RepositorySQLite() // force data mapper init
{
}
public RepositorySQLite(IDataMapperSQLite<T, string[]> dataMapper)
{
}
public List<T> Read(IConditions conditions) { throw new NotImplementedException(); }
public void WriteOne(T obj, out int newId) { throw new NotImplementedException(); }
public void WriteOne(IQuery query) { throw new NotImplementedException(); }
private void ClearMapState()
{
}
public void ReplaceAll(List<T> objs)
{
}
public void WriteAll(List<T> objList)
{
}
public void WriteOne(T obj)
{
}
public void UpdateOne(T obj)
{
}
public void UpdateAll(List<T> objs)
{
}
public void UpdateOne(IQuery query)
{
}
public T FindOne(IQuery query)
{
}
public List<T> FindAll(IQuery query)
{
}
public void DeleteAll(List<T> objs)
{
}
public void DeleteAll()
{
}
public void DeleteAll( IQuery query )
{
}
}