Spring.net Study Note II (Build an IOC container yourself in a factory way)

Source: Internet
Author: User

The first article can roughly run up to a spring.net program, and now it's about how to build an IOC container to better understand and master the IOC.

The first step: simulate the IOC container with a dictionary.

1 Public class Iocfactory2     {3Private IDictionary<string, Object>Iocdict = new Dictionary<string, Object>();4 Public iocfactory (string fileName)5         {6 XElement root = Xelement.load (fileName);7 8 XNamespace space = "Http://www.springframework.net";9 var objects = from obj in root. Elements (Space + "Object") select obj;Ten Iocdict = objects. ToDictionary ( One k = K.attribute ("name"). Value, A v = -                     { - string typeName = V.attribute ("type"). Value; the Type type = Type.GetType (typeName); - return Activator.CreateInstance (type); -                     } -                 ); +         } -///<Summary> + /// Get Object A///</Summary> at///<paramname= "id"></param> -///<returns></returns> - Public Object GetObject (string name) -         { - object result = null; - if (Iocdict.containskey (name)) in             { - result = Iocdict[name]; to             } + return result; -         } the}

Step Two: Calling code

1  Static voidMain (string[] args)2         {3             varPath =@"E:\code\IOCDemo\MovieApp\Objects.xml";4Iocfactory factory =Newiocfactory (path);5Ipersondao DAO = (Ipersondao) factory. GetObject ("Persondao");6             if(DAO! =NULL)7             {8 DAO. Save ();9Console.WriteLine ("I'm the IOC method .");Ten Console.readkey (); One             } A console.readline (); -}

Step Three: Configuration files

1<objects xmlns="http://www.springframework.net">2<!--here to put all the nodes inside the container--3<description>an example that demonstrates the simple IoC features.</description>4<!--name must be unique, the full name of the Type= class, where the assembly is located--5<ObjectName="Persondao"Type="Dao.persondao1,dao">6</Object>7</objects>8   

Operation Result:

In this test, when the LINQ to XML was inadvertently encountered when the root.elements ("Object") return value is NULL, it is later known because of the namespace, the accepted argument is that Xname,xname and string have an implicit conversion, However, XML with command space must have a namespace, otherwise it will not get the desired value, through the Anti-compilation tool, public static XName operator + (XNamespace ns, string localname); As long as the XNamespace plus local name (string), is a xname, so directly connected.

Spring.net Study Note II (Build an IOC container yourself in a factory way)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.