Autofac:解决对命名实例的特定依赖关系 [英] Autofac: Resolve a specific dependency to a named instance
问题描述
使用Autofac,我想注册一个组件并指定要解析为命名实例的特定依赖项.
Using Autofac, I would like to register a component and specify for a specific dependency to be resolved to a named instance.
我使用构造函数注入找到了以下示例,这几乎是我想要的.
I found samples like the following using constructor injection which is almost what I want.
builder.Register(c => new ObjectContainer(ConnectionStrings.CustomerDB))
.As<IObjectContainer>()
.Named("CustomerObjectContainer");
builder.Register(c => new ObjectContainer(ConnectionStrings.FooDB))
.As<IObjectContainer>()
.Named("FooObjectContainer");
builder.Register(c => new CustomerRepository(
c.Resolve<IObjectContainer>("CustomerObjectContainer"));
builder.Register(c => new FooRepository(
c.Resolve<IObjectContainer>("FooObjectContainer"));
但是,我需要在属性注入中使用它,并且我不想指定所有依赖项.
However, I need this with property injection and I don't want to specify all dependencies.
类似的东西:
builder.Register<CustomerRepository>().With<IObjectContainer>("CustomerObjectContainer");
builder.Register<FooRepository>().With<IObjectContainer>("FooObjectContainer");
所有未指定的依赖关系的建立应在未命名的实例中发生.
The build up of all unspecified dependnecies should happen with unnamed instances.
谢谢, 亚历克斯
[丹尼尔(Danielg)对答案的补充]
[ADDITION TO THE ANSWER FROM Danielg]
根据类型解析该类型的任何属性的重载.
An overload to resolve by type for any property of that type.
public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> WithDependency<TLimit, TReflectionActivatorData, TStyle, TProperty>(
this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration,
Func<IComponentContext, TProperty> valueProvider)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithProperty(new ResolvedParameter((p, c) =>
{
PropertyInfo prop;
return p.TryGetDeclaringProperty(out prop) &&
prop.PropertyType == typeof(TProperty);
},
(p, c) => valueProvider(c)));
}
推荐答案
我认为autofac尚不具备实现此目的的简便方法,但可以稍作努力.
I don't think that autofac has a shorthand way of doing this yet, but it is possible with a little effort.
我已经写了一个扩展方法来做到这一点.将其扔到静态扩展类中,就可以了.该扩展程序还显示了如何长期执行此操作.
I've written an extension method that does this. Throw it in a static extension class and you should be fine. The extension also shows how to do this the long way.
public static IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> WithResolvedProperty<TLimit, TReflectionActivatorData, TStyle, TProperty>(
this IRegistrationBuilder<TLimit, TReflectionActivatorData, TStyle> registration,
string propertyName, Func<IComponentContext, TProperty> valueProvider)
where TReflectionActivatorData : ReflectionActivatorData
{
return registration.WithProperty(new ResolvedParameter((p, c) =>
{
PropertyInfo prop;
return p.TryGetDeclaringProperty(out prop) &&
prop.Name == propertyName;
},
(p, c) => valueProvider(c)));
}
不要介意超长方法签名,autofac注册非常冗长.
Don't mind the super long method signature, autofac registrations are very verbose.
您可以像这样使用扩展名.
You can use the extension like this.
builder.RegisterType<Foo>()
.WithResolvedProperty("Bar", c => c.Resolve<IBar>());
这篇关于Autofac:解决对命名实例的特定依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!