using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Transactions;
|
|
namespace CY_DocumentSynchroWCFService
|
{
|
/// <summary>
|
/// 集合单元事务处理具体实现
|
/// </summary>
|
public class UnitOfWorkForSql : IUnitOfWork
|
{
|
private Dictionary<IAggregateRoot, ICommonDAL> addedEntities;
|
private Dictionary<IAggregateRoot, ICommonDAL> changedEntities;
|
private Dictionary<IAggregateRoot, ICommonDAL> deletedEntities;
|
|
public UnitOfWorkForSql()
|
{
|
addedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
|
changedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
|
deletedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
|
}
|
|
/// <summary>
|
/// 修改
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <param name="unitofWorkRepository"></param>
|
public void RegisterAmended(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
|
{
|
if (!changedEntities.ContainsKey(entity))
|
{
|
changedEntities.Add(entity, unitofWorkRepository);
|
}
|
}
|
/// <summary>
|
/// 新增
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <param name="unitofWorkRepository"></param>
|
public void RegisterNew(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
|
{
|
if (!addedEntities.ContainsKey(entity))
|
{
|
addedEntities.Add(entity, unitofWorkRepository);
|
};
|
}
|
/// <summary>
|
/// 删除
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <param name="unitofWorkRepository"></param>
|
public void RegisterRemoved(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
|
{
|
if (!deletedEntities.ContainsKey(entity))
|
{
|
deletedEntities.Add(entity, unitofWorkRepository);
|
}
|
}
|
/// <summary>
|
/// 事务提交
|
/// </summary>
|
public bool Commit()
|
{
|
bool isSuccess = true;
|
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
|
{
|
foreach (IAggregateRoot entity in this.addedEntities.Keys)
|
{
|
isSuccess = this.addedEntities[entity].InserModel(entity);
|
if (!isSuccess)
|
break;
|
}
|
if (isSuccess)
|
{
|
foreach (IAggregateRoot entity in this.changedEntities.Keys)
|
{
|
isSuccess = this.changedEntities[entity].UpdateModel(entity);
|
if (!isSuccess)
|
break;
|
}
|
}
|
if (isSuccess)
|
{
|
foreach (IAggregateRoot entity in this.deletedEntities.Keys)
|
{
|
isSuccess = this.deletedEntities[entity].DeleteModel(entity);
|
if (!isSuccess)
|
break;
|
}
|
}
|
if (isSuccess)
|
scope.Complete();
|
}
|
return isSuccess;
|
}
|
}
|
}
|