During this time there are many articles about IOC components in the garden, because they have also been learning the various components of IOC, as well as the IOC's ideas, a lot of common IOC components: AUTOFAC, Ninject, utity including. NET comes with MEF and so on. Because today in Saturday, the girlfriend went to work overtime (also a standard program, do Java development), idle to Nothing, I think according to reflection can write a simple IOC component. The IOC component is just plain to instantiate the corresponding interface based on reflection. Don't say much nonsense, start talking about my solutions.
1, the project structure diagram:
- Ioctest is a web MVC project.
- Common to instantiate the corresponding interface through a configuration file
- Interfaces defined by IBLL
- BLL Implementation Interface
2. References
- The Ioctest project references IBLL, common projects, and cannot reference the BLL project, so that the Ioctest project relies only on interfaces.
- BLL Project referencing IBLL and implementing interfaces
- Modify the BLL project DLL build path so that its DLLs are generated into the bin directory of the Ioctest project, as set
3, the following we see the specific implementation
(1) in the IBLL layer of the IHelloWord.cs class we define an interface, the code is as follows
Using system;using system.collections.generic;using system.linq;using system.text;namespace IBLL{public Interface Ihelloword { string SayHello (string Name);} }
(2) The BLL layer implements the interface
Using system;using system.collections.generic;using system.linq;using system.text;using IBLL;namespace BLL{ public class Helloword:ihelloword { #region Ihelloword member public string SayHello (string Name) { return "Hello_" +name; } #endregion } }
(3) Under the root directory of the Ioctest project, the following configuration is done (Helloword and Ihelloword are corresponding):
<appSettings> <add key= "IBLL. Ihelloword "Value=" BLL,BLL. Helloword "/> </appSettings>
Description
The key value is the full name of the interface (namespace + class name), the value is the class that implements the interface, two parts, the comma is preceded by the generated DLL name, and the comma is followed by the full class name (namespace + class name).
(4) The IOCReflecter.cs class of the Common project obtains the instantiated object of the corresponding interface according to the configuration file, and the code is implemented as follows
Using system;using system.collections.generic;using system.linq;using system.text;using System.Collections;using System.reflection;using system.configuration;namespace common{public class Iocreflecter {private static have Htable S_type=null; Static Iocreflecter () {s_type = new Hashtable (); } public static T createintance<t> () {type t=typeof (T);//key Type type = S_type [T] as Type;//value if (Type = = null) {string[] Assemblyinfos = ConfigurationManager . Appsettings[t.fullname]. Split (', '); Type = Assembly.Load (Assemblyinfos[0]). GetType (Assemblyinfos[1]); S_type. ADD (t, type); } return (T) CreateObject (type); }///<summary>//TypeName Get type Object///</summary>//<param name= "TypeName "></param>//<returns></returns> public statIC Type GetType (string typeName) {if (string. Isnullorwhitespace (TypeName)) {return null; } return Type.GetType (TypeName); }///<summary>//Get objects///</summary>//<param name= "T" ></param> <returns></returns> private Static Object CreateObject (Type t) {if (t = = n ull) {return null; }//Find constructors with no parameters//If you need to initialize the constructor with parameters T.getconstructors () gets all the constructors, it. GetParameters () gets all the parameters of the constructor, ConstructorInfo nonparameterconstructors= t.getconstructors (). Where (it=>it. GetParameters (). length==0). FirstOrDefault (); if (nonparameterconstructors = = null) {throw new Exception (t.fullname+ "must have a parameterless or default constructor"); }//Call number constructor Create object return T.invokemember (null, bindingflags.createinstance, NULL, NULL, NUll); } }}
(5) test
Add the HomeController.cs file in the Ioctest Project controllers code as follows
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using IBLL; Using Common;namespace ioctest.controllers{public class Homecontroller:controller {//// GET:/ home/public ActionResult Index () { //Get instantiated object Ihelloword Hello = iocreflecter.createintance< Ihelloword> (); Viewbag.message = Hello. SayHello ("Eric"); return View ();}} }
@{ viewbag.title = "Index";} The last one:
To this end, ready to pack up downstairs to eat, afternoon to the National Library reading, follow-up AUTOFAC, Ninject, utity summed up, feel Ninject more useful, interested students can study.
IOC functionality through reflection