Spring.net QuickStart: Controlling rollover, Dependency injection, tangent-oriented programming
Spring.net main functions:
1.IoC: Control rollover (inversion of controls) understood as abstract factory
Rollover control: The right to create objects is controlled by the developer himself, and is transferred to the container.
2.DI: Dependency Injection (Dependency injection)
Dependency Injection: When an object is created through a container, the initialization of the object can inject default values (which can be complex types) to properties, parameters of the constructor method, and so on.
3.AOP: Aspect-oriented programming (similar: Pipelines, MVC filters, etc.)
First, the IOC sample demo:
1. New WinForm Project
2. Create a new folder in the solution Lib, used to store the DLLs and files used by spring.net
Spring.Core.dll, Common.Logging.dll (Spring.Core.dll internal use), Spring.Core.pdb, Spring.Core.xml
3. First add Spring.net core Dll:Spring.Core.dll and Common.Logging.dll references
4. Configure the app. Config file:
5. Code calls:
Using spring.context;using spring.context.support;using system;using system.windows.forms;namespace CZBK. heimaoa.springnetdemo{public Partial class Form1:form {public Form1 () { InitializeComponent ( ); } private void Button1_Click (object sender, EventArgs e) { Iapplicationcontext context = Contextregistry.getcontext ();//Create container iuserinfoservice Userinfoservice = (iuserinfoservice) context. GetObject ("Userinfoservice"); MessageBox.Show (Userinfoservice.showmsg ());}}}
Userinfoservice class:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace CZBK. heimaoa.springnetdemo{public class Userinfoservice:iuserinfoservice {public string showmsg () { return "Hello World";}}
Iuserinfoservice Interface:
Namespace CZBK. heimaoa.springnetdemo{public Interface Iuserinfoservice { string showmsg ();} }
6. Effect:
7.Demo Source Download: Click to download >>
Second, DI: Dependency Injection Sample Demo:
1. Add a complex Type:
Namespace CZBK. heimaoa.springnetdemo{public class person {public int age {get; set;}} }
2. Modify the previous code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace CZBK. heimaoa.springnetdemo{public class Userinfoservice:iuserinfoservice {public string Name {get; set;} Public person person {get; set;} public string showmsg () { return ' Hello World: ' + Name + ' age is: ' + Person.age;}} }
3. Modify the App. config configuration:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <sectiongroup name= " Spring "> <section name=" context "type=" Spring.Context.Support.ContextHandler, Spring.core "/> <sectio n Name= "Objects" type= "Spring.Context.Support.DefaultSectionHandler, Spring.core"/> </sectionGroup> </ configsections> <spring> <context> <resource uri= "config://spring/objects"/> </context > <objects xmlns= "http://www.springframework.net" > <object name= "userinfoservice" type= "CZBK". HeiMaOA.SpringNetDemo.UserInfoService, CZBK. Heimaoa.springnetdemo "> <property name=" name "value=" Zhang San "/> <property name=" person "ref=" person "/ > </object> <object name= "person" type= "CZBK. HeiMaOA.SpringNetDemo.Person, CZBK. Heimaoa.springnetdemo "> <property name=" Age "value="/> </object> </objects> </s Pring></conFiguration>
4. Operating effect:
5. SOURCE download: Click to download >>
Third, Improve:
Place the Objects node configuration in app. Config into a separate XML file
1. Cut the objects node into the Objects.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><objects xmlns= "Http://www.springframework.net" > <object Name= "Userinfoservice" type= "CZBK. HeiMaOA.SpringNetDemo.UserInfoService, CZBK. Heimaoa.springnetdemo "> <property name=" name "value=" Zhang San "/> <property name=" person "ref=" person "/></object><object name=" person "type=" CZBK. HeiMaOA.SpringNetDemo.Person, CZBK. Heimaoa.springnetdemo "> <property name=" age "value="/></object> </objects>
2. Modify the App.confgi file:
<?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 " /> </sectionGroup> </configSections> <spring> <context> < Resource uri= "Config://spring/objects"/> <resource uri= "File://objects.xml"/> <!--specify XML file location (Here the program will go to the Bin\Debug or release directory to find this file, need to modify the next XML file attributes, to always copy to the output directory)- </context>
<objects xmlns= "Http://www.springframework.net" > <!--This node needs to be reserved--
</objects>
</spring></configuration>
3. Modify the Objects.xml file properties:
4. Operating effect:
5. SOURCE download: Click to download >>
Iv. use of spring.net in MVC4
1.WEB Engineering Add DLL reference: Click to download the required DLL file >>
2. Create a new config folder and controller XML file under Web engineering:
3. Controller code:
namespace webapp.controllers{public class Userinfocontroller:controller {//// GET:/userinfo/ //iuserinfoservice userinfoservice = new Userinfoservice (); Modified to the following: CZBK. HeiMaOA.IBLL.IUserInfoService Userinfoservice {get; set;} This completes the decoupling of the web layer and the BLL layer public actionresult Index () { return View (); }}
4.controllers.xml File Configuration content:
<?xml version= "1.0" encoding= "Utf-8"? ><objects xmlns= "Http://www.springframework.net" > <object Type= "Webapp.controllers.userinfocontroller,webapp" singleton= "false" > <!--specify namespaces, assemblies, whether or not to single - <property name= "Userinfoservice" ref= "Userinfoservice"/> <!-- Configure the complex types used in Userinfocontroller userinfoservice--> </object>
<object type= "CZBK. HEIMAOA.BLL.USERINFOSERVICE,CZBK. Heimaoa.bll "singleton=" false ">
</object> </objects>
5. Modify the Web. config file in the website to add the following nodes:
<configSections> <!--spring.net configuration- <sectiongroup name= "Spring" > <section Name = "Context" type= "Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/> <!--designated as mvc4--> </sectionGroup> </configSections> <!--spring.net configuration-- <spring> < context> <resource uri= "File://~/Config/controllers.xml"/> <!--specifying controller XML Files--- </ context> </spring> <!--spring.net Configuration End--
6. SOURCE download: Click to download >>
V. Summary:
Spring.net essentially the bottom is to use reflection to read the configuration file, the need for changes in the future does not need to modify the code, directly modify the configuration file can be, convenient and flexible.
Vi. spring.net Chinese Help document (online version):
Click to download >>
Spring.net control Rollover, dependency injection, plane-oriented programming