1

对于能够使用 ASP.NET MVC 和 C# 支持多个数据库层的存储库,骨架设计会是什么样子?如果我同时支持 LINQ to SQL 和 NHibernate,我想看看设计会是什么样子。我将如何创建我的数据库对象,并在我的 BLL 层中调用它的方法?

4

1 回答 1

8

存储库模式可能是最好的解决方案。您将为每个存储库定义一个接口,然后为 Linq2Sql 和 NHibernate 实现创建具体的存储库,例如

public interface ICustomerRepository
{
    Customer GetCustomer(int id);
}

public class NhibCustomerRepository : ICustomerRepository
{
    public Customer GetCustomer(int id)
    {
        // NHibernate implementation
    }
}

public class LtsCustomerRepository : ICustomerRepository
{
    public Customer GetCustomer(int id)
    {
        // Linq2Sql implementation
    }
}

依赖注入框架,例如Ninject,可以很容易地在实现之间动态切换。您可能需要使用 NHibernate 进行一些额外的依赖注入,以将当前 ISession 传递到您的存储库中,以便它们参与相同的工作单元。

于 2009-07-03T13:49:12.627 回答