根据构造函数参数属性使用 autofac 解决依赖关系

发布于 2024-11-25 20:17:29 字数 655 浏览 2 评论 0原文

假设我有两个服务(A 和 B)都注册为 ISomeService。还假设我将来需要上课,这将需要其中一项服务。但我还不知道班级名称。我所知道的是他们正在实现给定的接口或抽象类(实现给定接口的所有类都需要相同的 ISomeService 实例)。我怎样才能确保这个决议得以实现?

这是一个相关问题,其中名称使用解析服务的类是已知的。 但在我的例子中,类名 CustomerRepository 的名称事先是未知的。仅接口(如 IUserRepository 或 IFunctionRepository)或抽象类的名称是已知的。换句话说,我只知道 SomeRepository:IUserRepository 和 IUserRepository 类型需要 A。我必须允许用户编写更多实现 IUserRepository 的类。

这是提出问题的更好方法。 我需要所有 IFunctionRepository 构造函数来获取 A(其中 A:ISomeService) 和所有 IUserRepository 构造函数来获取 B(其中 B:ISomeService)

Suppose I have two services(A and B) both registered as ISomeService. Also suppose I will be needing classes in the future which will need one of these services. But I don't know the class names yet. All I know is they are implementing a given interface or abstract class(All classes implementing a given interface need the same instance of ISomeService). How can I make sure this resolution happens?

Here is a related question where the name of the class which uses the resolved service is known.
But in my case the name of the class name CustomerRepository is not known ahead. Only the name of interface(like IUserRepository or IFunctionRepository) or abstract class is known. In other words I just know that
SomeRepository: IUserRepository and that IUserRepository types need A. I have to allow user to write more classes implementing IUserRepository.

Here is a better way to put the question.
I need all IFunctionRepository constructors to get A(where A:ISomeService) and all IUserRepository constructors to get B(where B:ISomeService)

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

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

发布评论

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

评论(1

じее 2024-12-02 20:17:29

您应该问自己的问题是:容器如何知道何时使用哪个实现?如果没有附加信息,容器就无法知道这一点。

而且,通过 ISomeService 的多个实现,您是否应该要求客户端始终使用多个实例?

通常我会认为像 IUserRepository 这样的服务在任何时候都只有一种实现。当然,您可以删除一种实现并使用另一种实现,但客户端类只需要一个实例,而不是多个实例。

如果您的容器确实包含同一接口的多个实现,那么客户端类接受多个实例应该是有意义的,或者至少有一些元数据来知道要使用哪个实现。

对于客户端来说,使用 Autofac 接受多个实例就像依赖 IEnumerable 一样简单,容器将移交所有实例。如果客户端类应该对使用哪个实例做出一些明智的决定,那么您应该使用元数据来扩充注册。您涉及的问题使用类本身形式的某种元数据。更好的方法也许是使用更加解耦的元数据形式。有关如何在 Autofac 中实现此功能的讨论,请参阅此处

The question you should be asking yourself is: how can the container know when to use which implementation? Without additional information, the container cannot know this.

And also, with several implementations of ISomeService, should you require that clients always work with multiple instances?

Usually I would think that services like IUserRepository only have one implementation at any one time. Sure, you could rip out one implementation and use another, but client classes will only require one instance, not several.

If it is really the case that your container will contain several implementations of the same interface, it should make sense for client classes to accept multiple instances, or at least have some metadata to know which implementation to use.

For clients to accept multiple instances with Autofac is as simple as taking a dependency on IEnumerable<ISomeService> and the container will hand over all instances. If client classes should make some intelligent decision on which instance to use, you should augment the registrations with metadata. The question you relate to uses some sort of metadata in the form of the classes themselves. A better way is perhaps to use a more decoupled form of metadata. A discussion on how this is implemented in Autofac can be found here.

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