StructureMap.配置自动设置

本文关键字:设置 配置 StructureMap | 更新日期: 2023-09-27 17:52:38

我有2个插件在我的应用程序(ASP。. NET MVC 4).它们都使用相同的接口。

例如

MyApp.Plugins.Abstract.IMainPlugin

使用所需的Execute方法

现在,我有一个名为MyApp.Plugins.ClockPluginMyApp.Plugins.TimePlugin的插件。ClockPlugin类内部有Code属性(public int Code {get; set;}),它必须由StructureMap使用StructureMap注入。配置文件

到目前为止,我在StructureMap.config中有:

<StructureMap MementoStyle="Attribute">
  <DefaultInstance
    PluginType="MyApp.Plugins.Abstract.IMainPlugin, MyApp.Domain"
    PluggedType="MyApp.Plugins.TimePlugin, MyApp.Plugins" />
</StructureMap>

如何在此默认实例中添加ClockPlugin并自动设置Code ?

StructureMap.配置自动设置

根据文档:

基本构造函数或setter参数只是在Xml节点

中定义为属性
例如:

<StructureMap MementoStyle="Attribute">
  <DefaultInstance
    PluginType="StructureMap.Testing.Widget.IWidget,StructureMap.Testing.Widget"
    PluggedType="StructureMap.Testing.Widget.ColorWidget,StructureMap.Testing.Widget"
    Scope="Singleton"
    Color="Red" />
</StructureMap>

应该设置Color属性为Red