Is this bad or wrong design/implementation of Unit of Work and repository pattern (with transaction) using petapoco

StackOverflow https://stackoverflow.com/questions/8305455

Pergunta

I'm new to this great micro-orm tool (petapoco) and I wonder how to implements UoW and repository pattern using petapoco in web project. I've been read some articles but have no good ideas how to design/implements. Could some one provide some production example or direct me to achieve this?

Here is my thinking and pattial implementation code, please advice or comment if I'm wrong.

public interface IUnitOfWork
{
    void StartNew();
    void Commit();
    void Rollback();
}

public class PetaPocoUnitOfWork : IUnitOfWork
{
    private PetaPoco.Database _db = null;

    public PetaPocoUnitOfWork(PetaPoco.Database db)
    {
        _db = db;
    }
    public void StartNew()
    {
        _db.BeginTransaction();
    }

    public void Commit()
    {
        _db.CompleteTransaction();
    }

    public void Rollback()
    {
        _db.AbortTransaction();
    }
}

public class UnitOfWorkFactory
{
    public static IUnitOfWork GetInstance()
    {
        return new PetaPocoUnitOfWork(Project.Core.Domain.ProjectDb.GetInstance());
    }
}

interface IRepository<T>
{
    void Insert(T entity);
    void Update(T entity);
    void Delete(T entity);
    List<T> FetchAll();
    List<T> FetchAll(int startIndex, int endIndex, int count);
    T Fetch(int uid);
    void SaveChanges();
}

public class TireRepository : IRepository<Tire>
{
    private IUnitOfWork _uow = null;
    public TireRepository(IUnitOfWork uow)
    {
        _uow = uow;
    }
    public void Insert(Tire entity)
    {
        var db = ProjectDb.GetInstance();
        db.Save(entity);
    }

    public void Update(Tire entity)
    {
        throw new NotImplementedException();
    }

    public void Delete(Tire entity)
    {
        var db = ProjectDb.GetInstance();
    }

    public List<Tire> FetchAll()
    {
        throw new NotImplementedException();
    }

    public List<Tire> FetchAll(int startIndex, int endIndex, int count)
    {
        throw new NotImplementedException();
    }

    public Tire Fetch(int id)
    {
        throw new NotImplementedException();
    }

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

Here is the simple test case, and it's probably usage of service layer call.

    [TestMethod()]
    public void InsertTest()
    {
        IUnitOfWork uow = Xyz.Core.UnitOfWorkFactory.GetInstance();
        TireRepository target = new TireRepository(uow);
        Tire entity = new Tire();
        entity.Description = "ABCD";
        entity.Manufacturer = 1;
        entity.Spec = "18R/V";
        try
        {
            target.Insert(entity);
            target.SaveChanges();
        }
        catch
        {
            uow.Rollback();
        }
    }

I plan to use autoFac to be my Ioc solution and will inject uow instance per http request to repository object.

Please give me some comment or advice if this code wrong or bad. Many thanks.

Foi útil?

Solução

The code is right. Just a little over-architected IMHO. One thing: I would change the name of SaveChanges to CommitChanges for better clarity.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top