2

我正在尝试使我的EF通用存储库更加“宽泛”,以便与任何数据提供程序(ORM)(如 EF、NHibernate、Amazon Services 等)一起使用,而无需与EF上下文进行任何绑定。

现在我有(要点链接):

我已经重写了IRepositoryRepository的一部分。

但不知道如何处理EF以及如何最终摆脱它。

你能给我一些文章或代码示例,以便在我的工作中找到正确的方向。

4

1 回答 1

1

IRepository id 的想法是为Repository 中的每个 ORM 进行不同的实现。这样您就不必根据 ORM 本身来构建您的解决方案。

然后我认为当你试图让你的存储库来处理许多 ORM 时你做错了

更新:

您应该根据 IRepository 构建您的 BLL,而不需要对实现一无所知,然后您可以使用构造函数注入或依赖解析器将合适的实现传递给 BLL。

例如

public interface IProductRepository{
   void AddProduct(Product model);
   void UpdateProduct(Product model);
}

EF实施:

public class ProductRpository : IProductRepository
{
   void AddProduct(Product model)
   {
      //Add any code you want maybe EF code.
   }
   void UpdateProduct(Product model)
   {
      //........
   }
}

然后在 BLL 中,您依赖于 ProductRepository 上的 IProductRepository :

public class ProductService
{
   private readonly IProductRepository repository
   public ProductService(IProductRepository repository){
      this.repository = repository
   }
// ........ the code of your BLL
}
于 2013-03-13T09:20:06.933 回答