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

C#对轻量级(IoCContainer)依赖注入Unity的使用

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

概述

Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入。Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活,更易于维护。这样的程序也更容易在开发期间进行测试。你可以模拟对象,具有较强的具体依赖关系的垫片(轻量级模拟实现),如数据库连接,网络连接,ERP连接,和丰富的用户界面组件。例如,处理客户信息的对象可能依赖于其他对象访问的数据存储,验证信息,并检查该用户是否被授权执行更新。依赖注入技术,可确保客户类正确实例化和填充所有这些对象,尤其是在依赖可能是抽象的 。

 

Unity 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <container>
      <!--register type="full class name,namespace"-->
      <register type="UnityTest.ISqlHelper,UnityTest" mapTo="UnityTest.MysqlHelper,UnityTest">
        <lifetime type="singleton"/>
      </register>
    </container>
  </unity>
</configuration>

需要注意的是type和mapTo的值,用逗号隔开两部分,一是类的全部,包括命名空间,二是命名空间。

那么,也有其他的方法,先设置好命名空间,那就直接写类名即可,这个就不说了。

这里是简单的配置,详细的的配置自行搜索。

 

下载与引用

到官方下载:http://unity.codeplex.com/

项目里引用dll

Microsoft.Practices.Unity.dll

Microsoft.Practices.Unity.Configuration.dll

 

程序

假设对数据库操作类进行更换,那先建立一个操作类的接口,具体实现留着派生的类。

操作类接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public interface ISqlHelper
    {
         string SqlConnection();
    }

    public interface IOtherHelper
    {
        string GetSqlConnection();
    }
}

 

派生类一:Ms SQL Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MssqlHelper : ISqlHelper
    {
        public string SqlConnection()
        {
            return "this mssql.";
        }
    }
}

派生类二:MySQL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MysqlHelper : ISqlHelper
    {
        public string SqlConnection()
        {
            return "this mysql.";
        }
    }
}

其他类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UnityTest
{
    public class MyOtherHelper : IOtherHelper
    {
        ISqlHelper sql;
        public MyOtherHelper(ISqlHelper sql)
        {
            this.sql = sql;
        }
        public string GetSqlConnection()
        {
            return this.sql.SqlConnection();
        }


    }
}

 

主程序调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;


namespace UnityTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer mycontainer = new UnityContainer();


            //已有对象实例的配置容器注册
            // MysqlHelper d = new MysqlHelper();
            //mycontainer.RegisterInstance<ISqlHelper>(d);

            //类型的配置容器注册
            //mycontainer.RegisterType<ISqlHelper, MysqlHelper>();

            //配置文件注册
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(mycontainer);
            //mycontainer.LoadConfiguration(); 

            //调用依赖
            ISqlHelper mysql = mycontainer.Resolve<ISqlHelper>();
            Console.WriteLine(mysql.SqlConnection());

            //构造函数注入
            mycontainer.RegisterType<IOtherHelper, MyOtherHelper>();
            IOtherHelper other = mycontainer.Resolve<IOtherHelper>();
            Console.WriteLine(other.GetSqlConnection());

            Console.ReadKey();

        }
    }
}

 

到这里,算结束了。

自己去复制代码运行一次,相信你一定能更深刻地理解。

 

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C#网页自动登录和提交POST信息的多种方法发布时间:2022-07-13
下一篇:
C#方法名前的方括号发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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