asp.net unity配置文件实现ioc di

nuget 包下载

Nuget:
unity.configuration ,
unity ,unity.abstractions ,
unity.interception ,
unity.interception.configuration(这个一定要下载,不然没办法读取配置文件)

web

web层(可以做成单例,全局只有一个unity容器)

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
  
            fileMap.ExeConfigFilename = "UNITY.Config";//找配置文件的路径
            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
            IUnityContainer container = new UnityContainer();
            section.Configure(container, "kso");
            IUSERBLL ss = container.Resolve<IBLL.IUSERBLL>();
            ss.Show();
            Console.ReadKey();

bll 层

web 引用 业务层以及接口层,还有dal层要不然实例化不了

		 [Dependency] //属性注入 必须是public 否则会报错
        public Istudentdal Istudentd { get; set; }

        [InjectionConstructor]//构造函数注入 ,推荐使用,自动构造下一层
        public USERBLL(IUSERDAL uSERDAL) {
            string usedal = uSERDAL.stu();
            uSERDAL.show(); Console.WriteLine("bll实例化的cUSERBLL");
        }
        public void create()
        {
            Console.WriteLine("bll实例化的create");
        }

        public void Show()
        {
            Istudentd.show();
            Istudentd.stushows(); Console.WriteLine("bll实例化的USERBLL  Show");
        }

dal 层

跟上面差不多, 接口与实现
。。。。。省略

web 配置文件


<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
    <containers>

      <container name="kso">
        <register type="IBLL.IUSERBLL,IBLL" mapTo="BLL.USERBLL,BLL"/>
        <register type="IDAL.Istudentdal,IDAL" mapTo="DAL.studentdal,DAL"  />
        <register type="IDAL.IUSERDAL,IDAL" mapTo="DAL.USERDAL,DAL" />
      </container>
    </containers>
  </unity>
</configuration>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值