views:

166

answers:

2

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?

+6  A: 

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
    }
}

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.

Jamie Ide