tnblog
首页

unity

233人阅读 2018/11/26 17:24 评论:0 手机浏览 收藏
分类: IOC


             1、引用unity.dll 和 unity.Interception.dll

            

              2、文件配置--将配置文件属性改为始终复制

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>

    <!--<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />-->

  </configSections>

  <unity>

    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Unity.Interception.Configuration"/>

    <assembly name="Service"/>

    <containers>

      <container name="defaultContainer">
                   
        <register type="Service.IService.IUserinfoService, Service" mapTo="Service.ServiceImp.UserinfoService, Service"/>

      </container>

    </containers>

  </unity>

</configuration>

         

        3、调用

     private static IUnityContainer _container;

        private static readonly unityhelper dbinstance = new unityhelper("defaultContainer");

        private unityhelper(string containerName)

        {

            //UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            //_container = new UnityContainer();

            //section.Configure(_container, containerName);

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

            fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "bin\\UnityIocHelper\\unity.config");//找配置文件的路径

            Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);

            _container = new UnityContainer();

            section.Configure(_container, containerName);

        }

         4、调用的类与实现的类呈现继承关系

评价
每天学一学总会有用武之地
文章
6
评论
8
分类
16
关注
16
{{item.ArticleTitle}}
{{item.BlogName}} : {{item.Content}}