mercredi 21 octobre 2015

Unit of Work by using Windsor Castle

I want to implement Unit of Work patter with the usage of Windsor Castle in order to controll the trasanctions and sessions. What I did till now is: My business class methods:

[UnitOfWork]
public class TAManager : ITAManager
{
    private readonly IRepository _Repository;
    public Manager(IRepository takRepository)
    {
        _takRepository = takRepository;
    }

    public void Add(TA entity)
    {
        _Repository.Add(entity);
        _Repository.Save();
    }

    public void Update(TA entity)
    {
        _takRepository.Update(entity);
        _takRepository.Save();
    }
}

Unit of Work class:

 public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _dbContext;

    public UnitOfWork(DbContext context)
    {
        _dbContext = context;
    }

    public void Commit()
    {
        _dbContext.SaveChanges();
    }

    public void BeginTransaction()
    {
        throw new NotImplementedException();
    }

    public void Rollback()
    {
        throw new NotImplementedException();
    }
}
public class UnitOfWorkAttribute : Attribute { }    

UoW interface:

 public interface IUnitOfWork
{
    void  BeginTransaction();   
    void Commit(); 
    void Rollback();
}
Interceptor class:
public class UnitOfWorkInterceptor : IInterceptor

{

    private readonly IUnitOfWork _unitOfWork;

    public UnitOfWorkInterceptor()
    {
    }

    public UnitOfWorkInterceptor(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public void Intercept(IInvocation invocation)
    {

    //try
    //{

        //    _unitOfWork.BeginTransaction();
        //    invocation.Proceed();

        //}
        //catch (Exception ex)
        //{
        //    _unitOfWork.Rollback();
        //}
        //finally
        //{
        //   _unitOfWork.Rollback();
        //}
    }

}

This is how I have registered business class and interceptor

 container.AddFacility<WcfFacility>()
            .Register(
                Types.FromAssemblyNamed("WWW.Application")
                    .Where(t => t.GetInterfaces().Any(i => i.Namespace == "http://ift.tt/1ZWQEE5"))
                    .Configure(delegate (ComponentRegistration c)
                    {
                        //configuring delegate to obtain object of registered component and add an interceptor to it thus all classes for ITAManager have interceptor
                        var x = c.Interceptors(InterceptorReference.
                            ForType<UnitOfWorkInterceptor>()).Anywhere;
                    })
                    );

Am I going in right direction? Where to implement the logic of making rollbacks? How should I integrate interceptor (or using of work class) with the business class and methods in order to try and catch when an exeption occures? For now I only like to trigger rollback when an exception occures.

Aucun commentaire:

Enregistrer un commentaire