gpt4 book ai didi

inversion-of-control - 如何以编程方式注册依赖于 CaSTLe Windsor 已注册组件列表的组件?

转载 作者:行者123 更新时间:2023-12-04 08:20:50 25 4
gpt4 key购买 nike

我正在以编程方式注册一组服务,这些服务都实现了相同的接口(interface) IRule。我有另一个看起来像这样的服务:

public class MyService {
private IEnumerable<IRule> _rules;
public MyService(IEnumerable<IRule> rules){
_rules = rules;
}
}

Hammett 发布了一些看起来像我想要的东西,http://hammett.castleproject.org/?p=257 .我将签名更改为 IRule[] 并在帖子中尝试了 ArrayResolver 技巧,但这对我不起作用(注意,它也没有破坏任何东西)。

有人知道如何像我上面发布的代码那样以编程方式注册组件吗?

最佳答案

如果您不想更改 MyService 的签名并继续使用 IEnumerable<IRule> ,您还可以创建自定义 ISubDependencyResolver。这就是我们所做的:

public class EnumerableResolver : ISubDependencyResolver
{
private readonly IKernel kernel;

public EnumerableResolver(IKernel kernel)
{
this.kernel = kernel;
}

public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
Type targetType = dependency.TargetType;
if (targetType == null)
{
throw new ArgumentException("TargetType property cannot be null", "dependency");
}

if (targetType.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
Type service = targetType.GetGenericArguments()[0];
return this.kernel.HasComponent(service);
}
return false;
}

public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
Type service = dependency.TargetType.GetGenericArguments()[0];
Array array = this.kernel.ResolveAll(service, (IDictionary)null);
return Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { service }), new object[] { array });
}
}

它需要像这样在容器中注册:

container.Kernel.Resolver.AddSubResolver(new EnumerableResolver(this.Kernel));

关于inversion-of-control - 如何以编程方式注册依赖于 CaSTLe Windsor 已注册组件列表的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068417/

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