StructureMap: entering a primitive property in a base class

There is a question in this regard , but this question relates to StructureMap.

I have the following interface definition:

public interface ICommandHandler
{
    ILogger Logger { get; set; }
    bool SendAsync { get; set; }
}

I have several implementations that implement ICommandHandlerand should be allowed. I want to automatically enter these two properties in instances that implement ICommandHandler.

I found a way to do the injection ILogger, allowing all implementations to inherit from the base type and attribute [SetterProperty]:

public abstract class BaseCommandHandler : ICommandHandler
{
    [SetterProperty]
    public ILogger Logger { get; set; }

    public bool SendAsync { get; set; }
}

This, however, does not help me introduce a primitive type boolin all implementations.

Command handlers implement a common interface that inherits from the base interface:

public interface ICommandHandler<TCommand> : ICommandHandler
    where TCommand : Command
{
     void Handle(TCommand command);
}

Here is my current configuration:

var container = new Container();

container.Configure(r => r
    .For<ICommandHandler<CustomerMovedCommand>>()
    .Use<CustomerMovedHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

container.Configure(r => r
    .For<ICommandHandler<ProcessOrderCommand>>()
    .Use<ProcessOrderHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

container.Configure(r => r
    .For<ICommandHandler<CustomerLeftCommand>>()
    .Use<CustomerLeftHandler>()
    .SetProperty(handler =>
    { 
        handler.SendAsync = true;
    }));

// etc. etc. etc.

, , ( , SetProperty Action<T>).

StructureMap?

+5
1

, ?

container.Configure(r => r.For<ICommandHandler>()
    .OnCreationForAll(handler =>
    {
        handler.Logger = container.Resolve<ILogger>();
        handler.SendAsync = true;
    }));
+2

All Articles