编程语言
首页 > 编程语言> > C# Ioc容器Unity,简单实用

C# Ioc容器Unity,简单实用

作者:互联网

原文:C# Ioc容器Unity,简单实用

开头先吐槽一下博客园超级不好用,添加图片后就写不动字了,难道是bug

好进入正题,先来说下依赖注入,简单来说就是定义好接口,上层代码调用接口,具体实现通过配置文件方式去指定具体实现类。

首先我们需要通过nuget来安装unity

安装好后就可以写一个方法类去实现依赖注入

public class UnityIocHelper : IServiceProvider
    {
        private readonly IUnityContainer _container;
        private static readonly UnityIocHelper dbinstance = new UnityIocHelper("DBcontainer");
        private UnityIocHelper(string containerName)
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            _container = new UnityContainer();
            section.Configure(_container, containerName);
        }

        public static UnityIocHelper DBInstance
        {
            get { return dbinstance; }
        }
        public object GetService(Type serviceType)
        {
            return _container.Resolve(serviceType);
        }
        public T GetService<T>()
        {
            return _container.Resolve<T>();
        }
        public T GetService<T>(params ParameterOverride[] obj)
        {
            return _container.Resolve<T>(obj);
        }
        public T GetService<T>(string name, params ParameterOverride[] obj)
        {
            return _container.Resolve<T>(name, obj);
        }
    }

具体调用方法

public static IDatabase Base(string connString)
        {
            return UnityIocHelper.DBInstance.GetService<IDatabase>(“MySql”, new ParameterOverride(
              "connString", connString));
        }

配置信息

<unity>
  <typeAliases>
    <typeAlias alias="IDatabase" type="LRMC.Data.IDatabase,LRMC.Data" />
    <typeAlias alias="SqlServer" type="LRMC.Data.SqlDapper.Database,LRMC.Data.SqlDapper" />
    <typeAlias alias="MySql"     type="LRMC.Data.MySqlDapper.Database,LRMC.Data.MySqlDapper" />
  </typeAliases>
  <containers>
    <container name="DBcontainer">
      <!-- 默认数据库软件类型:SqlServer,MySql,Oracle,Access,SQLite-->
      <type type="IDatabase" mapTo="SqlServer" name="SqlServer"></type >
      <type type="IDatabase" mapTo="MySql" name="MySql"></type >
    </container>
  </containers>
</unity>
typeAlias 指定一个类的别名,
<type type="IDatabase" mapTo="MySql" name="MySql"></type >就是
IDatabase这个接口通过
MySql(
LRMC.Data.MySqlDapper.Database
)这个类去实现。



                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       

 

标签:Resolve,container,C#,GetService,UnityIocHelper,Unity,return,Ioc,public
来源: https://www.cnblogs.com/lonelyxmas/p/11600675.html