• IOC


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

     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
  • 相关阅读:
    字节流、字符流
    ArrayList、LinkedList、Vector 的区别。
    Java 为每个原始类型提供了哪些包装类型:
    int 和 Integer 有什么区别
    怎样自动把报表插入到 word 文档中
    web 报表中电子图章 / 水印的轻松实现
    玩转报表排名
    报表设计技巧之隔行异色
    单据类报表的制作
    轻松 get 报表模糊查询技能
  • 原文地址:https://www.cnblogs.com/yangjingqi/p/3647286.html
Copyright © 2020-2023  润新知