我将如何设计一个存储库来处理多个数据访问策略? [英] How would I design a repository to handle multiple data access strategies?

查看:109
本文介绍了我将如何设计一个存储库来处理多个数据访问策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What would a skeleton design look like for a repository able to support multiple database layers using ASP.NET MVC and C#? I want to see what a design would look like if I support both LINQ to SQL and NHibernate. How would I create my database object, and call a method on it in my BLL layer?

推荐答案

repository模式可能是这种情况的最佳解决方案。你会定义一个接口,每一个仓库,然后创建LINQ2SQL和NHibernate实现,如:

The repository pattern is probably the best solution for this. You would define an interface for each repository, then create concrete repositories for Linq2Sql and NHibernate implementations, e.g.

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传递到你的资料库,让他们参与到同一个单位的工作。

A dependency injection framework, such as Ninject, makes it easy to dynamically switch between implementations. You may have to do some extra dependency injection with NHibernate to pass the current ISession into your repositories so that they participate in the same unit-of-work.

这篇关于我将如何设计一个存储库来处理多个数据访问策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆