Reflection + configuration file implementation IOC container

Source: Internet
Author: User

IOC implementations:


IOC container We just stay in the know is not good, we have to make the impression to be more profound, then I show you a code to see the implementation of the IOC container in the code.


Code implementation:


To create a class library:


The class library of the solution is established:




Create an entity class: User:


<span style= "FONT-SIZE:18PX;"    >using system;using system.collections.generic;using system.text;namespace spring.demo.model{//<summary> User class///</summary> public class Users {///<summary>///</summ        ary> private int _oid;            public int Oid {get {return _oid;}        set {_oid = value;}        }///<summary>//Name///</summary> private string _name;            public string Name {get {return _name;}        set {_name = value;}        }//<summary>//gender///</summary> private string _sex;            public string Sex {get {return _sex;}        set {_sex = value;}        }//<summary>//Age///</summary> private int _age;            public int Age {get {return _age;} set { _age = value; }}}}</span>

To create an Iusers interface:


<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.text;namespace spring.demo.service{public    Interface Iusers {///<summary>///        How to return the user's details//</summary>//        <returns ></returns>        string GetUserInfo ();}    } </span>

Create an implementation class that implements the Iusers interface:


<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.text;using spring.demo.service;using Spring.demo.model;namespace spring.demo.compontext{public    class Userscompontents:iusers    {        public Userscompontents ()        {}        #region get user Information public        string GetUserInfo ()        {            Users user = new users ();            User. Oid = 1;            User. Name = "Beniao";            User. Sex = "Boy";            User. Age =;            return string. Format ("number: {0}---> Name: {1}---> Gender: {2}---> Age: {3}",                user. Oid,                user. Name,                user. Sex,                user. age);        }        #endregion    }}</span>

To create a test class:


<span style= "FONT-SIZE:18PX;" >using Itoo. Library.core.aop;using spring.context;using spring.demo.service;using system;using System.Collections.Generic; Using system.configuration;using system.linq;using system.text;namespace sping.demo.simpletest{    class Program    {        static void Main (string[] args)        {            Iusers studentchangebll = springhelper.getobject<iusers> ( "Users");            Console.WriteLine (Studentchangebll.getuserinfo ());            Console.read ();}}}    </span>

Create a configuration file in the console program:


<span style= "font-size:18px;" ><?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <sectiongroup name=      "Spring" > <section name= "context" type= "Spring.Context.Support.ContextHandler, Spring.core"/> <section name= "Objects" type= "Spring.Context.Support.DefaultSectionHandler, Spring.core"/> &L t;/sectiongroup> </configSections> <spring> <context> <resource uri= "Config://spring/obj ECTs "/> </context> <objects xmlns=" Http://www.springframework.net "> <!--This configuration is based on the actual program, Usersco Mpontents is a class under assembly Spring.Demo.Compontext--<object name= "Users" type= "Spring.Demo.Compontext.User Scompontents,spring.demo.compontext "singleton=" false "> </object> </objects> </spring>< /configuration></span> 

After running, found springhelper but small reference. We generally write in code that we write in this way:


<span style= "FONT-SIZE:18PX;" >//get assembly information from config file iapplicationcontext context = configurationmanager.getsection ("Spring/context") as                               The iapplicationcontext;//call method//users the configuration section in the config file//<object name= "Users"       //        type= " Spring.demo.compontext.userscompontents,spring.demo.compontent ">//</object>iusers user = context. GetObject ("Users") as Iusers;</span>

So that we can get the object out of the configuration file, but we don't want to have extra code in the code, and we don't have to write it again every time we do the new object: iapplicationcontext context = Configurationmanager.getsection ("Spring/context") as Iapplicationcontext, which increases the cost of our maintenance of the code, so we wrap this phrase up, The encapsulated code is this:


Create a class: Springhelper:


<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using Spring.Context;using Spring.context.support;namespace Itoo. library.core.aop{    public class springhelper    {        //<summary>       //Spring Container context         //</summary>        private static Iapplicationcontext springcontext        {             get            {                 return Contextregistry.getcontext ();           }        }        <summary>       ///Get the configuration file configuration object         //</summary>       /<typeparam name= "T" ></typeparam >       //<param name= "objname" ></param>        //<returns></returns>        public Static T getobject<t> (string objname) where t:class        {             return (T) springcontext.getobject (objname);        }   }}</span>

The above code we can each read the configuration file in the sentence removed, we can directly write such a sentence can be: iusers STUDENTCHANGEBLL = springhelper.getobject<iusers> (" Users ");


This shows the importance of encapsulation, when I was doing AOP, when my master saw something like this, he talked to me about the problem, I was mengmengdongdong, I didn't do the next step, and now I think, the problem is that I didn't do it at all, or that knowledge didn't go down to that level, The aspect of knowing this knowledge is not so deep. All the questions have to be done.


Summary:


From the above practice to analysis, we found that we seem to be new things, in fact, we have learned, like the IOC container, we have learned the reflection and configuration files, we found that the IOC container is not reflected and configuration file to achieve it, Reflection and configuration files are something that we have learned in the big talk design paradigm, and this is not something new. A seemingly complex thing, there are simple things to assemble, we know this, there is no fear of new things.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Reflection + configuration file implementation IOC container

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.