本節目錄
介紹
UOW(全稱UnitOfWork)是指工作單元.
在Abp中,工作單元對於倉儲和應用服務方法默認開啟。並在一次請求中,共享同一個工作單元.
同時在Abp中,不僅支持同一個數據庫連接,還支持事務處理.
分析Abp源碼
1.UnitOfWorkRegistrar

2.ComponentRegistered

3.IsConventionalUowClass

4.Intercept

5.PerformSyncUow

實現UOW
定義IUnitOfWork
public interface IUnitOfWork
{
//1.開啟事務
//2.設置Filter(本例中不做演示)
void Begin(UnitOfWorkOptions options);
void Complete();
}
public class UnitOfWorkOptions
{
public bool? IsTransactional { get; set; }
}
實現uow,在uow中會提供db的創建,這樣才能管理到每個db.
public class EfUnitOfWork : UnitOfWorkBase
{
public static DbContext DbContext { get; set; }
public static DbContext GetDbContext()
{
if (DbContext == null)
{
DbContext = new DemoDb();
}
return DbContext;
}
public override void Begin(UnitOfWorkOptions options)
{
if (options.IsTransactional == true)
{
CurrentTransaction = new TransactionScope();
}
}
public TransactionScope CurrentTransaction { get; set; }
public override void Complete()
{
SaveChanges();
if (CurrentTransaction != null)
{
CurrentTransaction.Complete();
}
}
private void SaveChanges()
{
DbContext.SaveChanges();
}
}
public abstract class UnitOfWorkBase : IUnitOfWork
{
public virtual void Begin(UnitOfWorkOptions options)
{
}
public virtual void Complete()
{
}
}
定義與實現倉儲層,這里不再做DbProvider.
public interface IRepository
{
}
public interface ITaskRepository : IRepository
{
void Add(Task task);
}
public class TaskRepository : ITaskRepository
{
public void Add(Task task)
{
var db = (DemoDb)EfUnitOfWork.GetDbContext();
db.Tasks.Add(task);
}
}
定義與實現應用層
public interface IApplicationService
{
}
public interface ITaskAppService : IApplicationService
{
void CreateTask(CreateTaskInput input);
}
public class TaskAppService : ITaskAppService
{
private ITaskRepository _repository;
public TaskAppService(ITaskRepository repository)
{
_repository = repository;
}
public void CreateTask(CreateTaskInput input)
{
_repository.Add(new Task(input.Name));
}
}
public class CreateTaskInput
{
public string Name { get; set; }
}
定義與實現uow攔截器
internal class UnitOfWorkInterceptor : IInterceptor
{
private IUnitOfWork _unitOfWork;
public UnitOfWorkInterceptor(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void Intercept(IInvocation invocation)
{
_unitOfWork.Begin(new UnitOfWorkOptions());
invocation.Proceed();
_unitOfWork.Complete();
}
}
定義在IApplicationService與IRepository接口下攔截
static void Kernel_ComponentRegistered(string key, Castle.MicroKernel.IHandler handler)
{
var type = handler.ComponentModel.Implementation;
if (typeof(IApplicationService).IsAssignableFrom(type) || typeof(IRepository).IsAssignableFrom(type))
{
handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(UnitOfWorkInterceptor)));
}
}
執行
static void Main(string[] args)
{
using (var container = new WindsorContainer())
{
container.Register(Component.For<IInterceptor, UnitOfWorkInterceptor>());//先注入攔截器
container.Register(Component.For<IUnitOfWork, EfUnitOfWork>());
container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
container.Register(Component.For<ITaskAppService, TaskAppService>());
container.Register(Component.For<ITaskRepository, TaskRepository>());
var person = container.Resolve<ITaskAppService>();
person.CreateTask(new CreateTaskInput() { Name = "3333" });
}
Console.ReadKey();
}
會自動在application method的結尾調用Complete.
另外也可以在uow上定義option為啟用事務.在本例中稍作擴展即可實現.
本文地址:http://neverc.cnblogs.com/p/5263558.html
