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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.
回答2:
I allready responded that twice xD
one here
Reposity pattern using linq
another here
ControllerFactory having controllers separated in a different project