gpt4 book ai didi

c# - EF 通用存储库到任何数据提供者

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:51 25 4
gpt4 key购买 nike

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

现在我有(要点链接):

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

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

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

最佳答案

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

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

更新:

您应该根据 IRepository 构建您的 BLL,而无需对实现一无所知,然后您可以使用构造函数注入(inject)或依赖解析器将合适的实现传递给 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 中,您依赖于 IProductRepository ont on ProductRepository:

public class ProductService
{
private readonly IProductRepository repository
public ProductService(IProductRepository repository){
this.repository = repository
}
// ........ the code of your BLL
}

关于c# - EF 通用存储库到任何数据提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com