The analysis of Spring.net in demo

Source: Internet
Author: User

1. Understanding Spring.net

Spring.net is an application framework designed to assist developers in creating enterprise-class. NET Applications. It provides many aspects of functionality, such as dependency injection, aspect-oriented programming (AOP), data access abstraction, and ASP. Based on the Java version of the Spring Framework, spring.net ported the core concepts and ideas of Spring.java to the. NET Platform.

Enterprise applications are typically composed of multiple physical layers, and each physical layer is often divided into functional layers. There is a need for collaboration between different levels, for example, the business service layer typically needs to use objects from the data access layer to implement a use case. Regardless of how the application is built, it ultimately manifests itself as a series of collaborative objects that together form the complete application. So we say that objects in the application have dependencies on each other.

. NET platform provides a rich set of capabilities for building applications, from very basic primitive types and base class libraries (and the methods of defining new classes) to well-functioning application servers and web frameworks. But the. NET platform itself does not provide any way to manage the underlying application modules and combine them into a collaborative whole, relying only on architects or developers to create (a series of) applications. Admittedly, there are many design patterns available for the design of business systems, and we can use these patterns to combine classes or objects into a complete application that works. Models such as factories, abstract factories, Builder, decorations and service Locator have been widely accepted and adopted in today's software development industry (which may be why these patterns were first trained as models). These patterns are very good, but they are just some of the best programming methods that have been named, and in the introduction of these patterns, you will generally explain what they are used for, the best places to apply them, what problems they can solve, and so on. We can find these patterns from a number of books and wikis, then read them carefully and then implement them in our own applications.

Spring.net's IOC container solves the problem of how to synthesize applications into classes, objects, and service groups in enterprise applications. IOC containers are very orthodox (by: formalized, the implication is that these methods are stereotyped and tested by the industry for many years) to combine disparate components into complete applications. The Spring.net framework is the best programming method that has been tested for years by countless applications in the industry and has been trained as a design pattern, and in fact these patterns have become the code of our architecture and development, and through spring.net we can integrate them directly into our own applications. It's really a good thing that many organizations and agencies have developed strong, well-maintained applications with the spring framework.

2.spring.net Component Function Description

Here's a blog address http://blog.csdn.net/lee576/article/details/8621212 (already written in detail)

(mainly applied to two DLLs)

Common.Logging.dll

Spring.Core.dll

3. Learn to start with the demo

Demo Environment is vs2013

Create a new solution--"Create a console application (whatever you create, you decide)--" Use NuGet to load spring.net packages

Project:

How to use the NuGet package (http://springframework.net/)

    • Right-click on your console project

Here, I choose is Spring.Web.MV4, directly click Install on OK. (After the installation, in the project file, more Packages folder, remember, the folder copy out, later, directly referenced inside the DLL file is OK, do not need to use NuGet)

Then we start to implement

Control reversal (Factory mode):

Code implementation:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection.Emit;usingSystem.Text;usingSystem.Threading.Tasks;usingSpring.context;usingSpring.Context.Support;namespaceioctest{Internal classProgram {Private Static voidMain (string[] args) {            //creating a Container objectIapplicationcontext CTX =Contextregistry.getcontext (); IPerson P= (IPerson) ctx. GetObject ("MyPerson");            P.sayhello ();        Console.readkey (); }    }     Public InterfaceIPerson {voidSayHello (); }     Public classPerson:iperson { Public stringName {Get;Set; }  Public intId {Get;Set; }  PublicCar MyCar {Get;Set; }  PublicPerson () {Console.WriteLine ("The non-parametric constructor method is called"); }         PublicPerson (stringname) {Console.WriteLine ("The string parameter constructor method is called"); Name=name; }         PublicPerson (intID) {Console.WriteLine ("The int parameter constructor method is called"); Id=ID; }         PublicPerson (intIdstringname) {Console.WriteLine ("The int+string parameter constructor method is called"); Name=name; Id=ID; }         Public voidSayHello () {Console.WriteLine (Name+": Say Hello"+Mycar.brand); }    }     Public classCar { Public stringBrand {Get;Set; } }}

App. Config:

<?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> <startup> <supportedruntime version="v4.0"sku=". netframework,version=v4.5"/> </startup> <spring> <context> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net"> <description>an example that demonstrates simple IoC features.</description> <!--name:objec The name of T, type:'about the fully qualified name of the type (namespace + class name), the assembly name of the class (namespace)'-<ObjectName="MyPerson"Type="ioctest.person,ioctest"> <!--<property name="Brand"Value="Audi"/>--> <!--constructor Define construction method index: Parameter index, value: parameter value call There is also a constructor for the parameter--<!--<constructor-a RG index="0"Value="Ten"/>--> <!--name: The parameter value of the constructor method is argument--<!--<constructor-arg Name="ID"Value="Ten"/>--> <!--<constructor-arg value="Yzhou"/>--> <!--Type: constructor parameter types--<!--<constructor-arg type="string"Value="Ten"/>--> <constructor-arg index="0"Value="Ten"/> <constructor-arg index="1"Value="Yzhou"/> <!--Property Dependency Injection--<property name="Name"Value="yzhou1111"/> <property name="MyCar" ref="Car1"/> </Object> <ObjectName="Car1"Type="ioctest.car,ioctest"> <property name="Brand"Value="Audi"/> </Object> </objects> </spring></configuration>

Temporarily do not look at the spring node in the configuration file

Steps:

Add configsections First

<configsections>    <sectiongroupname= "Spring">      < Sectionname= "Context"type= "Spring.Context.Support.ContextHandler, Spring.core"/>      < Sectionname= "Objects"type= "Spring.Context.Support.DefaultSectionHandler, Spring.core" />    </sectiongroup>  </configsections>

Note: The configsections node must be the first child node of the configuration node

Otherwise it will error "configuration system failed to initialize"

Add the Spring node again

<Spring>    <Context>      <ResourceURI= "Config://spring/objects"/>    </Context>    <Objectsxmlns= "Http://www.springframework.net">      <Description>An example that demonstrates the simple IoC features.</Description>         </Objects>  </Spring>

Simply put, a class in the spring node corresponds to an object,

Form:

< Objects >    <  name= "names are random, but this represents the need to instantiate the class's name"  type  namespace + class name), (class's Assembly names Typically refers to a namespace, because the name of the assembly can be modified) "></object></objects  >

Dependency Injection: (Construction method + property),

    • Construction method: (keyword Constructor-arg) Note the number of Constructor-arg in each object, representing the constructors that have several parameters

Multiple forms:

<constructor-arg index= "0" value= "ten"/>: A constructor that represents a parameter, sorted up and down the constructors in the class. If there are two constructors with 1 parameters, then which one is used;

<constructor-arg index= "0" value= "ten"/>
<constructor-arg index= "1" value= "Zhangsan"/>: A constructor representing two parameters. ;

<constructor-arg name= "id" value= "ten"/>: The value of name represents the character of the parameter in the constructor;

<constructor-arg type= "string" value= "ten"/>: type represents the types of formal parameters in a constructor to differentiate

    • Properties: (keyword property)

<property name= "name" value= "yzhou1111"/> name indicates that the property field in the class

<property name= "MyCar" ref= "Car1"/> Note the use of ref, in the person class, defines the car class as an attribute field, so use ref

The analysis of Spring.net in demo

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.