In our daily project development, we use the Open source framework spring.net to create object instances in order to reduce the coupling between the various tiers of the project.
Note: spring.net is a container that is used to create objects,
Spring.net's core IOC (inversion of Control), DI (Dependency injection), AOP (aspect-oriented programming)
Role: IOC: Create an object instance (from the container itself to the new object), DI: When creating an object instance, the property value is injected for this object.
Code implementation:
1. Use standard configuration initial container (use configuration to create container)
1), creating a container using a configuration file
1<?xml version="1.0"encoding="Utf-8"?>2<configuration>3<configSections>4<sectiongroup name="Spring">5<section name="Context"Type="Spring.Context.Support.ContextHandler, Spring.core"/>---Corresponds to the following <context> section, which specifies the path to another XML configuration file6<section name="Objects"Type="Spring.Context.Support.DefaultSectionHandler, Spring.core"/>---Corresponds to the following <object> section, configuring the class to create the instance7</sectionGroup>8</configSections>9<spring>Ten<context> One<resource uri="config://spring/objects"/> A<resource uri="File://service.xml"/> -</context> -<objects xmlns="http://www.springframework.net"> the<!--<description>an example that demonstrates simple IoC features.</description> -<ObjectName="Userinfoservice"Type="Spring.net.demo.userinfoservice,spring.net.demo">---name= "instance name of the class to be created: type=" namespace to create the instance. Class name, assembly " -<property name="Name"Value="Zhang San"/>---di use, name= "attribute name in class", value= "value to assign to property" -<property name=" Person" ref=" Person"/>---userinfoservice has a property person of type person, assigns a value to the property person +</Object> -<ObjectName=" Person"Type="Spring.Net.Demo.Person, Spring.Net.Demo">---Add a <object> section to instantiate the person class, +<property name=" Age"Value=" -"/>--Assigning a value to the age property of the person class using dependency injection A</Object>--> at</objects> -</spring> -</configuration>
2, in the project to introduce the process of collective
Create a business Layer object at the UI layer, use Spring.net to create the decoupling, click button to create the Userinfoservices object, assign to his interface Iuserinfoservice
1 Private void button1_click (object sender, EventArgs e)2 {3 // create a container. 4 Iuserinfoservice lister = (iuserinfoservice) ctx. GetObject ("userinfoservice"); 5 MessageBox.Show (Lister. ShowMsg ()); 6 }
Iuserinfoservice interface
1 Public Interface Iuserinfoservice 2 {3 string showmsg (); 4 }
Userinfoservice class
Public classUserinfoservice:iuserinfoservice { Public stringName {Get;Set; } PublicPerson Person {Get;Set; } Public stringshowmsg () {return "Hello spring.net"+name+","+Person . Age; } }
Person class
1 Public class Person 2 {3public intgetset;} 4 }
Use of Spring.net