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));
  • 相关阅读:
    为没有源码的DLL文件添加强名称
    部署.Net Core APi+Vue 到 linux centos 服务器(一)
    安装nginx
    Linux常用命令大全
    Linq 根据list属性去重复
    jQuery Validate验证框架详解
    mysql+C#
    微信支付配置参数
    自定义截取数,截取字符串,返回字符串数组。
    Git GUI基本操作
  • 原文地址:https://www.cnblogs.com/xmax130/p/5527068.html
Copyright © 2011-2022 走看看