存储库模式问题

发布于 2024-10-30 05:56:37 字数 555 浏览 1 评论 0原文

我首先为每个实体创建了一个新的存储库接口,例如 IClientRepository。然后我为每个实体创建了一个类,例如 SqlClientRepository。我对很多实体都这样做了,然后意识到它们都有相同的方法:添加、更新、删除、GetAll。

所以我意识到我应该只制作一个界面,而不是为每个界面制作一个单独的界面。

问题是,现在我的依赖项注入不起作用,因为我只能将接口映射到一个存储库:

Bind<IClientRepository>().To<SqlClientsRepository>().WithConstructorArgument("connectionString", WebConfigurationManager.ConnectionStrings["MyDb"].ConnectionString);

我能看到的唯一解决方法是组合所有存储库或返回到我的第一次尝试。使用第一次尝试还允许我更改某些实体的返回类型,例如 IClientRepository.Add() 可以返回新客户端的 id,而其他一些实体可能不需要它。

欣赏任何想法。

I first made a new repository interface for each of my entities e.g. IClientRepository. I then made a class for each entity e.g. SqlClientRepository. I did this for a lot of my entities and then realized that they all had the same methods: Add, Update, Delete, GetAll.

So I realized that I should probably just make the one interface instead of making a separate one for each.

The problem is that now my dependency injection won't work since I can only map the interface to one repository:

Bind<IClientRepository>().To<SqlClientsRepository>().WithConstructorArgument("connectionString", WebConfigurationManager.ConnectionStrings["MyDb"].ConnectionString);

The only work-around I can see is to combine all of the repositories or go back to my first attempt. Using the first attempt would also allow me to change the return types for certain entities e.g. IClientRepository.Add() could return the new client's id, and some other entities might not need that.

Appreciate any thoughts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还不是爱你 2024-11-06 05:56:37

您可以让 ClientRepository 实现 BaseRepository 和特定的 IClientRepository。这样,您的 Base 可以具有通常的添加/删除等功能,并且您的 IClientRepository 可以具有专门的方法(或者在某些情况下可能为空)。您的 IoC 可以使用 IClientRepository 进行解析。

我就是这样做的:

public class CustomerRepository : BaseRepository<Customer>, ICustomerRepository {...}
and
unityContainer.RegisterType<ICustomerRepository, CustomerRepository>();

祝你好运:)

You could have your ClientRepository implement both your BaseRepository and a specific IClientRepository. That way your Base can have the usual Add/Remove etc and your IClientRepository could have specialized methods (or be empty in some cases probably). Your IoC could resolve using your IClientRepository.

This is how i do it:

public class CustomerRepository : BaseRepository<Customer>, ICustomerRepository {...}
and
unityContainer.RegisterType<ICustomerRepository, CustomerRepository>();

Good luck :)

人心善变 2024-11-06 05:56:37

存储库的常见方法是使用通用 CRUD 方法或所有存储库都可以使用的方法创建一个像“IRepository”这样的基本接口。然后,为每个特定存储库创建一个新接口,该接口派生自基本接口,但包含该存储库的特定于域的方法。对于基本存储库实现和更具体的实现类可以执行相同的操作。这样,您就可以拥有通用功能的通用位置,但仍然可以为每个具体的特定于域的存储库实现提供特定的接口。

A common approach for repositories is to create a base interface like "IRepository" with the common CRUD methods or methods that all repos can use. Then for each specific repository create a new interface that derives from the base interface but includes the domain-specific methods for that repo. The same thing can be done for a base Repository implementation and the more specific implementation classes. This way you have a common place for common functionality, but you still have specific interfaces for each concrete domain-specific repository implementation.

給妳壹絲溫柔 2024-11-06 05:56:37

如果不知道你想要实现什么目标,就很难确定。在我的最新项目中,每个类都有一个存储库,IRepository。这可能会让你摆脱 IoC 绑定的麻烦。

It's hard to say for sure without knowing what you are trying to accomplish. In my latest project, there is one repository per class, IRepository<T>. That might get you out of your IoC binding trouble.

小嗲 2024-11-06 05:56:37

一种解决方案是使接口通用。这样,它就足够通用了,同时仍然很灵活。

one solution is to make the Interface generic. this way, it's general enough, while still being flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文