StructureMap:在基类中注入原始属性

发布于 2024-11-02 20:43:34 字数 1772 浏览 1 评论 0原文

与此问题相关,但是这个问题是关于StructureMap的。

我有以下接口定义:

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

我有多个实现 ICommandHandler 的实现,需要解析。我想在实现 ICommandHandler 的实例中自动注入这两个属性。

我找到了一种通过让所有实现继承基类型和 [SetterProperty] 属性来注入 ILogger 的方法:

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

    public bool SendAsync { get; set; }
}

但是,这并不能帮助我注入所有实现中的原始 bool 类型。

命令处理程序实现了一个从基本接口继承的通用接口:

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

这是我当前的配置:

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 委托)。

有哪些方法可以使用 StructureMap 更有效地做到这一点?

Related to this question, but this question is about StructureMap.

I have got the following interface definition:

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

I have multiple implementations that implement ICommandHandler and need to be resolved. I want to automatically inject these two properties in instances that implement ICommandHandler.

I found a way to do inject the ILogger by letting all implementations inherit from a base type and the [SetterProperty] attribute:

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

    public bool SendAsync { get; set; }
}

This however, doesn't help me with injecting the primitive bool type in all implementations.

The command handlers implement a generic 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.

As you can see, I set the property for each and every configuration, which is quite cumbersome (however, it is pretty nice that the SetProperty accepts a Action<T> delegate).

What ways are there to do this more efficiently with StructureMap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-11-09 20:43:34

也许你可以用这个?

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

Maybe you could use this?

container.Configure(r => r.For<ICommandHandler>()
    .OnCreationForAll(handler =>
    {
        handler.Logger = container.Resolve<ILogger>();
        handler.SendAsync = true;
    }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文