使用 petapoco 的工作单元和存储库模式(带事务)的设计/实现是否错误或错误

发布于 2024-12-18 09:49:37 字数 2635 浏览 4 评论 0原文

我是这个伟大的 micro-orm 工具 (petapoco) 的新手,我想知道如何在 Web 项目中使用 petapoco 实现 UoW 和存储库模式。我读过一些文章,但对如何设计/实现没有好主意。有人可以提供一些生产示例或指导我实现这一目标吗?

这是我的思考和部分实现代码,如有错误,请指教或评论。

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();
    }
}

这是一个简单的测试用例,大概是服务层调用的用法。

    [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();
        }
    }

我计划使用 autoFac 作为我的 Ioc 解决方案,并将每个 http 请求注入 uow 实例到存储库对象。

如果此代码错误或不好,请给我一些评论或建议。非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2024-12-25 09:49:37

代码是对的。恕我直言,只是有点过度架构。一件事:为了更清晰起见,我会将 SaveChanges 的名称更改为 CommitChanges

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文