C # Use Spring. NET to implement IOC control inversion and DI dependency injection (Code tutorial ),

Source: Internet
Author: User

C # Use Spring. NET to implement IOC control inversion and DI dependency injection (Code tutorial ),

Import Common. Logging, Spring. Core

Create user information business interface class:

IUserInfoService. cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SpringNet{   public  interface IUserInfoService    {              string ShowMsg();    }}

Implementation interface:

UserInfoService. cs

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace SpringNet {class UserInfoService: IUserInfoService {public string UserName {get; set;} public Person person {get; set;} public string ShowMsg () {return "Hello World! "+ UserName +" Age: "+ person. Age ;}}}

Person. cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SpringNet{    public  class Person    {        private int age;        private string name;        public int Age { get => age; set => age = value; }        public string Name { get => name; set => name = value; }    }}

Configure App. config

 
   
      
   
    
             
       
        
         
          
           
         
        
        
        
        
          An example that demonstrates simple IoC features.
         
         
        
      
     
    
   
  
 

Services. xml

 
   
  
   
An example that demonstrates simple IoC features.
  
      
       
      
    
   
      
     
   
 

You need to set the xml file attribute to copy to the output directory.

Key code:

IApplicationContext ctx = ContextRegistry.GetContext();            IUserInfoService lister = (IUserInfoService)ctx.GetObject("UserInfoService");            MessageBox.Show(lister.ShowMsg());

Project Structure:

Running result:

Related Article

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.