Spring.NET dependency injection (II) - implanted object

1. Abstract injection Interface

public  interface IDeviceWriter
    {
         void saveToDevice();
    }

 

2. The specific implementation of the interface

  Achieve 1

public   class  FloppyWriter: IDeviceWriter
    {
         public   void  saveToDevice () 
        {
            Console.WriteLine ( " Save to floppy ... " );
        }
    }

 

  Achieve 2

public   class  UsbDiskWriter: IDeviceWriter
    {
         public   void  saveToDevice () 
        {
            Console.WriteLine ( " saved to the mobile hard drive ... " );
        }
    }

 

3. The need to inject business objects

public  class MemoryDevice
    {
         public IDeviceWriter DeviceWriter {  getset; }

         public  void Save()
        {
             if (DeviceWriter ==  null)
            {
                 throw  new Exception( " 需要设备... ");
            }

            DeviceWriter.saveToDevice();
        }
    }

 

4. Business calling component

public   class  DeviceComponent
    {
         public   static   void  SaveDevice ()
        {
             the try
            {
                 // taken from the config file assembly information
                the IApplicationContext ConfigurationManager.GetSection context = ( " Spring / context " AS  the IApplicationContext;

                 // call the method
                MemoryDevice device = context.GetObject ( " algDevice " AS  MemoryDevice;
                device.Save ();
            }
             the catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

 

The client calls

class SpringDeviceTest : ITestCase
    {
         public  void Run()
        {
            DeviceComponent.SaveDevice();

            Console.Read();
        }
    }

 

6. Profiles

< 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" />
     </ sectionGroup >
   </ configSections >

   < spring >
     < context >
       < resource  uri ="config://spring/objects" />
     </ context >
    
     < objects >
     ...
      < object  id ="algFloppy"  type ="CsharpTrainer.Strategy.Device.FloppyWriter, CsharpTrainer.Strategy"   />
       < object  id ="algUsb"  type ="CsharpTrainer.Strategy.Device.UsbDiskWriter, CsharpTrainer.Strategy"   />
       < object  id ="algDevice"  type ="CsharpTrainer.Strategy.Device.MemoryDevice, CsharpTrainer.Strategy" >
         < property  name ="DeviceWriter"  ref ="algFloppy"   />
         <!-- <property name="DeviceWriter" ref="algUsb" /> -->
       </ object >
     </ objects >
   </ spring >
  ...
</ configuration >

 

7. Run results

  Save to floppy disk ...

 

Reproduced in: https: //www.cnblogs.com/davidgu/archive/2012/05/31/2528024.html

Guess you like

Origin blog.csdn.net/weixin_34417814/article/details/93802797