陕西省城乡住房建设厅网站,外贸建站软件,网站开发目的和意义,电商平台网站运营方案概述 Unity是一个轻量级的可扩展的依赖注入容器#xff0c;支持构造函数#xff0c;属性和方法调用注入。Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活#xff0c;更易于… 概述 Unity是一个轻量级的可扩展的依赖注入容器支持构造函数属性和方法调用注入。Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题。构建一个成功应用程序的关键是实现非常松散的耦合设计。松散耦合的应用程序更灵活更易于维护。这样的程序也更容易在开发期间进行测试。你可以模拟对象具有较强的具体依赖关系的垫片轻量级模拟实现如数据库连接网络连接ERP连接和丰富的用户界面组件。例如处理客户信息的对象可能依赖于其他对象访问的数据存储验证信息并检查该用户是否被授权执行更新。依赖注入技术可确保客户类正确实例化和填充所有这些对象尤其是在依赖可能是抽象的 。 Unity 配置文件 ?xml version1.0 encodingutf-8 ?
configuration configSections section nameunity typeMicrosoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration/ /configSections unity xmlnshttp://schemas.microsoft.com/practices/2010/unity container !--register typefull class name,namespace-- register typeUnityTest.ISqlHelper,UnityTest mapToUnityTest.MysqlHelper,UnityTest lifetime typesingleton/ /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.RegisterInstanceISqlHelper(d); //类型的配置容器注册 //mycontainer.RegisterTypeISqlHelper, MysqlHelper(); //配置文件注册 UnityConfigurationSection section (UnityConfigurationSection)ConfigurationManager.GetSection(unity); section.Configure(mycontainer); //mycontainer.LoadConfiguration(); //调用依赖 ISqlHelper mysql mycontainer.ResolveISqlHelper(); Console.WriteLine(mysql.SqlConnection()); //构造函数注入 mycontainer.RegisterTypeIOtherHelper, MyOtherHelper(); IOtherHelper other mycontainer.ResolveIOtherHelper(); Console.WriteLine(other.GetSqlConnection()); Console.ReadKey(); } } } 到这里算结束了。 自己去复制代码运行一次相信你一定能更深刻地理解。 转载于:https://www.cnblogs.com/maijin/p/7918804.html