zoukankan      html  css  js  c++  java
  • StructureMap 使用的实例构造函数是程序运行时确定的配置方法

      .net下IOC框架StructureMap的使用介绍比较多,这里不作介绍。未入门的请移步StructureMap介绍

    在项目里面我们一般实现的接口类的构造函数具有参数。例如:

    using System;
    using System.Text;

    namespace Dynamic.BLL.Interface
    {

     public interface IModuleFactory
        {
    
       string Do();
        }
    }

    namespace Dynamic.BLL.Libs
    {

    public class ModuleCreatorTest : IModuleFactory
        {
            public int OrgRuleId { get; set; }
    
    
            public ModuleCreatorTest(int orgRuleId)
            {
                OrgRuleId = orgRuleId;
    
            }
    
         pubic   string Do()
       {
         return OrgRuleId ;
        }
      }
    }

    这里我使用APPSetting配置文档进行配置:

    <configuration>
      <configSections>
        <section name="StructureMap" type="StructureMap.Configuration.StructureMapConfigurationSection,StructureMap"/>
      </configSections>
      <StructureMap MementoStyle="Attribute">
    
        <!--ModuleCreatorTest-->
      <DefaultInstance PluginType="Dynamic.BLL.Interface.IModuleFactory,Dynamic.BLL.Interface" PluggedType="Dynamic.BLL.Libs.ModuleCreatorTest,Dynamic.BLL.Libs" Scope="PerRequest" orgRuleId="1"/>
     </StructureMap>
    </configuration>

    在具体使用的时候我们需要创建一个IDictionary<string,object>对象传入构造函数的参数。

       var cons = new Dictionary<string, object> {{"orgRuleId", 2}};
              IModuleFactory moduleFactory =
                  StructureMap.ObjectFactory.GetInstance<IModuleFactory>(
                      new ExplicitArguments(cons));
  • 相关阅读:
    IOI2000 回文字串
    洛谷 P2758 编辑距离
    NOIP2012普及第三题 摆花
    [SCOI2005]骑士精神
    [USACO1.5]八皇后 Checker Challenge
    Codeforces Round #637 (Div. 2) 题解
    核电站---两种DP解法
    POJ1077 八数码问题
    洛谷 P1162 填涂颜色
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/xmax130/p/5527068.html
Copyright © 2011-2022 走看看