Unity -- IOC

Source: Internet
Author: User

1. Define a data interface and two data Implementation classes first}View Code}View Code}View Code2. client call --- direct call

              Main(                            MSDAL.Student studentMS =               Console.WriteLine( + studentMS.Writing() +                                  }  
    }
3. implement client call --- Interface Programming dependency Abstraction
              Main(                          IStudent student =  MSDAL.Student();                           Console.WriteLine( + student.Writing() +      }     
4. implement client call --- reflection decoupling
              Main(                             assemblyName = ;              className = assemblyName + ;                          IStudent studentReflection =             Console.WriteLine( + studentReflection.Writing() +      }     
5. implement client call --- reflection + configuration file
             Main(                             assemblyNameConfiguration = ConfigurationManager.AppSettings[];               classNameConfiguration = assemblyNameConfiguration + ;                           IStudent studentConfiguration =             Console.WriteLine( + studentConfiguration.Writing() +      }            
6. implement client call --- Spring. Net object creation decoupling

You need to add Spring. Core. dll Assembly reference

               Main(                        IApplicationContext SpringContext = Spring.Context.Support.ContextRegistry.GetContext();             IStudent studentSpring = (IStudent)SpringContext.GetObject();             Console.WriteLine( + studentConfiguration.Writing() +      }

Configuration File

View Code7. implement client call --- Unity creates object decoupling

You need to add Microsoft. Practices. Unity. dll and Microsoft. Practices. Unity. Configuration. dll Assembly references.

              Main(                            IUnityContainer mycontainer =  UnityContainer();             UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection();             section.Configure(mycontainer);             IStudent studentUnity = mycontainer.Resolve<IStudent>();             Console.WriteLine( + studentUnity.Writing() +      }    

Configuration File

View Code

 

 

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.