关于使用 NHibernate 和/或 Web 服务的 UnitOfWork 的建议

发布于 2024-08-13 10:05:52 字数 1043 浏览 8 评论 0原文

我正在寻找以下建议。

我需要创建一个可在数据库和 Web 服务之间互换的 DAL。 我正在使用 C# 和 WPF 进行开发。

有没有人看到 IUnitOfWork 的任何好的实现,并允许使用某种 DI 切换不同的 DAL?

编辑

所以在阅读了一些内容之后。我决定使用存储库模式。代表模式采用 IUnitOfWork。上面的读法正好相反。这里还有一个例子来说明我所说的切换输入或输出的含义。

NHibernateProductRepository : IRepository<Product>  
{
    private IUnitOfWork _unitOfWork = null;

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

WebServiceProductRepository : IRepository<Product>  
{
    private IUnitOfWork _unitOfWork = null;

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

....

public static class Bootstrapper 
{
     public static void Load() 
     {
         // Some sort of IOC.. Which will also be used for DI. 
     }
}

所以我想我需要 IUnitOfWork 方面的帮助。 Web 服务存储库将会很棘手。那么我如何创建某种事务,如何排队等等。

最后一件事,在 WPF 环境中,多个线程可以将数据保存到数据库中,因此对于每个线程,它应该创建自己的 UnitOfWork 吗?我担心一个线程可能会在另一个线程中提交,等等。

I'm looking for some advice for the following.

I need to create a DAL that is interchangeable between a database and a webservice.
I'm developing in C# and WPF.

has anyone see any good implementations of an IUnitOfWork and allows different DAL to be switch in or out using some sort of DI?

EDIT

So after doing some reading. I've decided to use a Repository Pattern. The Rep Pattern takes in a IUnitOfWork. Above reads the opposite way around. Also here is an example for what i mean with the switch in or out.

NHibernateProductRepository : IRepository<Product>  
{
    private IUnitOfWork _unitOfWork = null;

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

WebServiceProductRepository : IRepository<Product>  
{
    private IUnitOfWork _unitOfWork = null;

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

....

public static class Bootstrapper 
{
     public static void Load() 
     {
         // Some sort of IOC.. Which will also be used for DI. 
     }
}

So I guess I need help with the IUnitOfWork stuff. The Web Service Repositories are going to be tricky. So how do I create some sort of transaction, and how do I queue etc etc.

One last thing, in a WPF environment multiple threads could be saving things to the database, so for each thread should it create it's own UnitOfWork? I'm scared a thread could commit half way through another etc.

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

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

发布评论

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

评论(1

仙女山的月亮 2024-08-20 10:05:52

如果您使用 NHibernate,您应该查看 SharpArchitecture。 contrib 项目涵盖了您的场景。

You should check out SharpArchitecture if you go with NHibernate. The contrib project has your scenario covered.

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