Define a filter for the DecorateAllWith () method in map structure 3

I used the following statement to decorate all of mine ICommandHandlers<>with Decorator1<>:

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

But since it Decorator1<>implements ICommandHandlers<>, the class Decorator1<>also adorns itself.

So, the problem is that it Decorator1logs inadvertently when I log everything ICommandHandler<>. How to filter DecorateWithAll()to decorate everything ICommandHandler<>except Decorator1<>?

+4
source share
1 answer

ObjectFactory outdated.

You can exclude Decorator1<>from registration as vanilla ICommandHandler<>with the following code:

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<>));
});

UPDATE

DSL-

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<>));
    }
}

Registry.

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

Registry ( )

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);
    }
});
+1

All Articles