初入Unity--IOC_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 初入Unity--IOC

初入Unity--IOC

 2014/4/5 17:14:23  老树昏鸦  博客园  我要评论(0)
  • 摘要:1.先定义一个数据接口和两个数据实现类1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceIDAL7{8publicinterfaceIStudent9{10//读11stringReading();1213//写14stringWriting();1516}17}ViewCode1usingIDAL;2usingSystem;3usingSystem
  • 标签:

1.先定义一个数据接口和两个数据实现类

class="code_img_closed" src="/Upload/Images/2014040517/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('5c4ec143-5f03-43f4-8578-7d0056b11c61',event)" src="/Upload/Images/2014040517/2B1B950FA3DF188F.gif" alt="" />
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace IDAL
 7 {
 8    public interface IStudent
 9     {
10        //
11        string Reading();
12 
13        //
14        string Writing();
15 
16     }
17 }
View Code
 1 using IDAL;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace MSDAL
 8 {
 9     public class Student : IStudent
10     {
11 
12         public string Reading()
13         {
14             return "Reading , this is MSDAL";
15         }
16 
17         public string Writing()
18         {
19             return "Writing , this is MSDAL";
20         }
21     }
22 }
View Code
 1 using IDAL;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace MySqlDAL
 8 {
 9     public class Student : IStudent
10     {
11         public string Reading()
12         {
13             return "Reading , this is MySqlDAL";
14         }
15 
16         public string Writing()
17         {
18             return "Writing , this is MySqlDAL";
19         }
20     }
21 }
View Code

2.实现客户端调用---直接调用

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //1.紧耦合 要切换数据库改动很多
 6              MSDAL.Student studentMS = new MSDAL.Student();
 7              Console.WriteLine("1" + studentMS.Writing() + studentMS.Reading());
 8              //MySqlDAL.Student studentMySql = new MySqlDAL.Student();
 9              //Console.Write(studentMySql.Writing() + studentMySql.Reading());
10      }  
    }

3.实现客户端调用---接口编程 依赖抽象

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5           //2.接口编程 依赖抽象    要切换数据库改动较多
 6              IStudent student = new MSDAL.Student();//Student类对象还是出现在前台, 紧耦合
 7             // IStudent student = new MySqlDAL.Student();
 8              Console.WriteLine("2" + student.Writing() + student.Reading());
 9         }
10     }     

4.实现客户端调用---反射解耦

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5              //3.反射解耦            
 6             string assemblyName = "MSDAL";//程序集名称
 7             string className = assemblyName + ".Student";//类的全名称
 8             //IOC(控制反转):对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它
 9             IStudent studentReflection = (IStudent)Assembly.Load(assemblyName).CreateInstance(className);
10             Console.WriteLine("3" + studentReflection.Writing() + studentReflection.Reading());   
11       }
12     }     

5.实现客户端调用---反射+配置文件

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5 
 6             //4.反射+配置文件
 7             string assemblyNameConfiguration = ConfigurationManager.AppSettings["DAL"]; //程序集名称 可在配置文件中灵活切换数据层
 8             string classNameConfiguration = assemblyNameConfiguration + ".Student";//类的全名称
 9 
10             //IOC(控制反转):对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它
11             IStudent studentConfiguration = (IStudent)Assembly.Load(assemblyNameConfiguration).CreateInstance(classNameConfiguration);
12             Console.WriteLine("4" + studentConfiguration.Writing() + studentConfiguration.Reading());
13             }
14     }            

6.实现客户端调用---Spring.Net创建对象解耦

 需要添加Spring.Core.dll程序集引用

 1   class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5          //5.Spring.Net创建对象
 6             IApplicationContext SpringContext = Spring.Context.Support.ContextRegistry.GetContext();//spring容器
 7             IStudent studentSpring = (IStudent)SpringContext.GetObject("DAL");//创建实例对象
 8             Console.WriteLine("5" + studentConfiguration.Writing() + studentConfiguration.Reading());
 9         }
10     }

 配置文件  

 1 <?xml version="1.0"?>
 2 <configuration>
 3   <configSections>
 4     <sectionGroup name="spring">
 5         <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
 6         <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
 7       </sectionGroup>
 8   </configSections>
 9 <spring>
10     <context>
11       <!--<resource uri="~/Objects.xml"/>-->
12       <resource uri="config://spring/objects"/>
13     </context>    
14     <objects>    
15       <object id="DAL" type="MSDAL.Student,MSDAL"></object>
16     </objects>
17   
18   </spring>
19 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
View Code

7.实现客户端调用---Unity 创建对象解耦

 需要添加Microsoft.Practices.Unity.dll和Microsoft.Practices.Unity.Configuration.dll程序集引用

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5              //6.Unity 创建对象
 6             IUnityContainer mycontainer = new UnityContainer();//Unity容器
 7             UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");//配置文件信息
 8             section.Configure(mycontainer);//将配置文件信息给Unity容器        
 9             IStudent studentUnity = mycontainer.Resolve<IStudent>();//创建实例对象
10             Console.WriteLine("6" + studentUnity.Writing() + studentUnity.Reading());  
11         }
12     }    

配置文件

 1 <?xml version="1.0"?>
 2 <configuration>
 3   <configSections>
 4 
 5     <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
 6  </configSections>
 7 <unity>
 8     <container>     
 9       <register type="IDAL.IStudent,IDAL" mapTo="MSDAL.Student,MSDAL">
10         <lifetime type="singleton"/>
11       </register>
12     </container>
13   </unity>
14 
15 <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
16 </configuration>
View Code

 

 

  • 相关文章
发表评论
用户名: 匿名