Bla*_*man 1 c# nhibernate design-patterns repository-pattern linq-to-sql
对于能够使用ASP.NET MVC和C#支持多个数据库层的存储库,骨架设计会是什么样子?我希望看到如果我支持LINQ to SQL和NHibernate,设计会是什么样子.我如何创建我的数据库对象,并在我的BLL层中调用它的方法?
该库模式可能是我们的最佳解决方案.您将为每个存储库定义一个接口,然后为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
}
}
Run Code Online (Sandbox Code Playgroud)
依赖注入框架(如Ninject)使得在实现之间动态切换变得容易.您可能必须使用NHibernate进行一些额外的依赖注入,以将当前的ISession传递到您的存储库,以便它们参与相同的工作单元.
| 归档时间: |
|
| 查看次数: |
696 次 |
| 最近记录: |