在结构图3中为DecorateAllWith()方法定义过滤器 [英] Define filter for DecorateAllWith() method in structure-map 3

查看:100
本文介绍了在结构图3中为DecorateAllWith()方法定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下语句用 Decorator1<> 装饰我的所有 ICommandHandlers<>

I used following statement to decorate all my ICommandHandlers<> with Decorator1<>:

ObjectFactory.Configure(x =>
{
   x.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});

但是,由于 Decorator1<> 实现了 ICommandHandlers<> Decorator1<> 类也进行自我装饰。

But because Decorator1<> implements ICommandHandlers<>, the Decorator1<> class decorates itself too.

因此,问题在于,当我注册所有 ICommandHandler<> Decorator1 会无意注册。 >的。
如何过滤 DecorateWithAll()来装饰所有 ICommandHandler<> ,除了 Decorator1<>

So, the problem is that the Decorator1 registers inadvertently, when I register all the ICommandHandler<>'s. How can I filter DecorateWithAll()to decorate all ICommandHandler<>, except Decorator1<>?

推荐答案

ObjectFactory 已过时。

您可以将 Decorator1<> 排除为已注册为普通 ICommandHandler<> 像这样的代码:

You can exclude Decorator1<> from being registered as a vanilla ICommandHandler<> with code like this:

var container = new Container(config =>
{
    config.Scan(scanner =>
    {
        scanner.AssemblyContainingType(typeof(ICommandHandler<>));
        scanner.Exclude(t => t == typeof(Decorator1<>));
        scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
    });
    config.For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
});






更新

对于多个模块,您可以使用注册表DSL 使用StructureMap构成应用程序的一部分

For multiple modules you can use The Registry DSL to compose a portion of your application

using StructureMap;
using StructureMap.Configuration.DSL;

public class CommandHandlerRegistry : Registry
{
    public CommandHandlerRegistry()
    {
        Scan(scanner =>
        {
            scanner.AssemblyContainingType(typeof(ICommandHandler<>));
            scanner.Exclude(t => t == typeof(Decorator1<>));
            scanner.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
        });
        For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(Decorator1<>));
    }
}

只有合成根需要了解所有注册表实现。

Only the Composition Root needs to be aware of all the Registry implementations.

 var container = new Container(config =>
{
    config.AddRegistry<CommandHandlerRegistry>();
});

您甚至可以选择查找所有注册中心运行时实例(您仍然需要确保所有必需的程序集都已加载)

You even have the option of finding all Registry instances at runtime (you still need to ensure all the required assemblies are loaded)

var container = new Container(config =>
{
    var registries = (
        from assembly in AppDomain.CurrentDomain.GetAssemblies()
        from type in assembly.DefinedTypes
        where typeof(Registry).IsAssignableFrom(type)
        where !type.IsAbstract
        where !type.Namespace.StartsWith("StructureMap")
        select Activator.CreateInstance(type))
        .Cast<Registry>();

    foreach (var registry in registries)
    {
        config.AddRegistry(registry);
    }
});

这篇关于在结构图3中为DecorateAllWith()方法定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆