根据构造函数参数属性使用 autofac 解决依赖关系
我正在使用 Autofac。我想根据应用于构造函数参数的属性注入依赖项的不同实现。例如:
class CustomerRepository
{
public CustomerRepository([CustomerDB] IObjectContainer db) { ... }
}
class FooRepository
{
public FooRepository([FooDB] IObjectContainer db) { ... }
}
builder.Register(c => /* return different instance based on attribute on the parameter */)
.As<IObjectContainer>();
属性将提供数据,例如连接字符串,我可以使用它来实例化正确的对象。
我该怎么做?
I'm using Autofac. I want to inject a different implementation of a dependency based on an attribute I apply to the constructor parameter. For example:
class CustomerRepository
{
public CustomerRepository([CustomerDB] IObjectContainer db) { ... }
}
class FooRepository
{
public FooRepository([FooDB] IObjectContainer db) { ... }
}
builder.Register(c => /* return different instance based on attribute on the parameter */)
.As<IObjectContainer>();
The attributes will be providing data, such as a connection string, which I can use to instance the correct object.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想为
CustomerRepository
和FooRepository
提供IObjectContainer
的不同实现。如果是这种情况,属性可能是薄金属尺。相反,我将向您展示如何使用 Autofac 实现多个实现。(为简洁起见,已省略
.ContainerScoped()
等调用。)首先,通过命名注册为每个连接字符串注册一个
IObjectContainer
版本:然后,解析存储库注册中的特定实例:
这使得存储库没有配置信息:
It sounds like you want to provide different implementations of
IObjectContainer
toCustomerRepository
andFooRepository
. If that is the case, attributes might be a thin metal ruler. Instead, I'll show you how I would implement multiple implementations with Autofac.(Calls such as
.ContainerScoped()
have been left out for brevity.)First, register a version of
IObjectContainer
for each connection string by naming the registrations:Then, resolve the specific instances in the repository registrations:
This leaves the repositories free of configuration information:
当您有多个存储库并且它们的构造函数参数很少时,布莱恩的答案就足够好了。但是当你有很多根时,很难建立你的根。您可以通过扫描解析接口时的类元数据来实现此目的。当您获得有关其参数的信息时,您可以解决它的实际实现。请参阅我的回答此处。
Bryan's answer is good enough while you have several repositories and they have few constructor parameters. But it is difficult to set up your root when you have many of them. You can achieve this by scanning your class metadata on resolving an interface. When you get info about its parameters you can resolve actual implementation of it. See my answer here.