• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#Ioc容器Unity,简单实用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

开头先吐槽一下博客园超级不好用,添加图片后就写不动字了,难道是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
)这个类去实现。



                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       
                                                       

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++实现精确计时发布时间:2022-07-14
下一篇:
解析C语言的sizeof发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap