麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

IOC

2019-11-14 16:44:56
字體:
來源:轉載
供稿:網友

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 : IStudent10     {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 : IStudent10     {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

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产一区二区亚洲 | 91久久线看在观草草青青 | 国产精品久久久在线观看 | 黄网站免费观看视频 | 成人在线视频免费 | 精品国产乱码久久久久久丨区2区 | 91成人在线免费观看 | www.7777在线| 国产成人aⅴ | 369看片你懂的小视频在线观看 | 国产一区二区二 | 黑人操穴| 日韩电影av在线 | 黄色免费高清网站 | 精品在线观看一区二区三区 | 斗破苍穹在线免费 | 国产精品视频一区二区三区四区国 | 精品一区二区久久久 | 成人午夜免费看 | 日韩欧美中文字幕视频 | 毛片免费在线观看视频 | 手机av在线电影 | 亚洲精品午夜在线 | 成人精品一区二区三区中文字幕 | 欧美日韩一 | 369看片你懂的小视频在线观看 | 久久久久久久久久久国产精品 | 亚洲日本高清 | av手机在线免费播放 | 国产亚洲精品久久 | 欧美一级特黄特色大片免费 | 国产精品成人久久 | 日韩黄色三级视频 | 天天色综合6 | 欧美中文字幕一区二区三区亚洲 | 久久99网| 中国免费黄色 | 91麻豆精品国产91久久久更新资源速度超快 | 1区2区3区国产 | 黄色网址在线免费播放 | 中国精品久久 |