Spring.net (ii)----Preliminary discussion on IOC container

Source: Internet
Author: User

I was in the last article about Spring.net "Spring.net Framework Introduction and Module description" in very detailed introduction, this article on the old words from the mention. Let's go straight to the subject.

1. First understand the two interfaces.
Iobjectfactory interface and Iapplicationcontext interface: He two is called "container" or "IOC container".

The core principle of the spring.net framework is non-invasive.
The Iobjectfactory interface is the actual container that initializes, configures, and manages objects.
The Iobjectfactory fully qualified named Spring.objects.factory.iobjectfactory,iobjectfactory interface has multiple implementations, the most
Commonly used is the Spring.Objects.Factory.XML.XmlObjectFactory.

2. Object-defined XML file
Add in app. Config or Web. config
<spring>
<context type= "Spring.Context.Support.XmlAppliationContext", Spring.core "/>
<resource uri=file://objects.xml//>
</spring>
Note: The type attribute of the context can be omitted.

The order of the 3.spring and context is limited, and the "Spring/context" is defined as a string constant in the spring.net framework.
The name of the contact that represents the context since the Abstractapplicationcontext class. Available by:
Iapplicationcontext Context=contextregistry.getcontext (); To get the contact value, here's a little bit of attention. In the use of his
You have to make the <spring> points in the configuration file work, so you have to. NET configuration files <configSections> nodes under the Register class
Spring.Context.Support.ContextHandler, which allows the Contextregistry class to initialize the application context, which is implemented in the FCL
The IConfigurationSectionHandler interface

3.XML object definition can be registered at <objects> node
<section name= "Objects" type= "Spring.context.support.defultsectionhandler,spring.core" >

A simple example of a 4.spring.net IOC container----implemented with a simple three layer
It's useless to say so much, but let's take a closer look at this simple example:
First build the project and then add a reference to the Spring.core assembly.
Two. Refer to system.configuration----need to use the ConfigurationManager class method.
Three. Configure the. NET profile----specific configuration as follows:
<?xml version= "1.0" encoding= "Utf-8"?
  < Configuration>
    <configsections>
      < Sectiongroup name= "Spring",
        <section name= "context" &NBSP;
                 type= " Spring.Context.Support.ContextHandler, Spring.core "/>
        < Section name= "Objects" &NBSP;
                  type= "Spring.Context.Support.DefaultSectionHandler, Spring.core"/>
      </sectiongroup>
    </configsections>
     <spring>
<context>
<resource uri= "Config://spring/objects"/>
</context>
<objects>
<!--This configuration is based on the actual program, Userscompontents is a class under assembly Spring.Demo.Compontext--
<object name= "Users"
Type= "Spring.demo.compontext.userscompontents,spring.demo.compontent" >
</object>
</objects>
</spring>
</configuration>
Four. Establish the entity classes and interfaces for the project and make excuses.
Here is the schematic of the Instance program: Model (Entity layer), Service (interface), compontent (business logic to implement the interface), SimpleTest (test)

Five. First look at the definition of the entity layer, I believe that everyone can understand the code is not much to say, directly posted up.
using System;
Using System.Collections.Generic;
Using System.Text;

   namespace Spring.Demo.Model
   {
    //<summary>
   //user class
   //</summary>
     public class Users
    {
       //< Summary>
       ///number
       // /</summary>
        private int _oid;
        public int Oid
        {
            get {return _oid;}
            set {_oid = value;}
       }

       //<summary>
       //Name
       //</summary
        private string _name;
        public string Name
        {
            get {return _name;}
            set {_name = value;}
       }

       //<summary>
       //Gender
       //</summary
        private string _sex;
        public string Sex
        {
            get {return _sex;}
            set {_sex = value;}
       }

<summary>
Age
</summary>
private int _age;
public int Age
{
get {return _age;}
set {_age = value;}
}
}
}


Six. Look at the definition of the interface
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace Spring.Demo.Service
{
public interface Iusers
{
<summary>
Ways to return the user's details
</summary>
<returns></returns>
String GetUserInfo ();
}
}
Seven. Logic to implement Interface methods
using System;
Using System.Collections.Generic;
Using System.Text;
Using Spring.Demo.Service;
Using Spring.Demo.Model;

Namespace Spring.Demo.Compontext
{
public class Userscompontents:iusers
{
Public userscompontents ()
{ }

           # Region get user Information
           public string GetUserInfo ()
           {
               users user = new Users ();
              user. Oid = 1;
              user. Name = "Beniao";
              user. Sex = "Boy";
              user. Age = +;

return string. Format ("number: {0}---> Name: {1}---> Gender: {2}---> Age: {3}",
User. Oid
User. Name,
User. Sex,
User. Age);
}
#endregion
}
}
Eight.IOC container through the spring.net iapplicationcontextGet assembly information from config file and invoke method to implement control inversion
//Get assembly information from config file
Iapplicationcontext context = configurationmanager.getsection ("Spring/context")
As Iapplicationcontext;
Calling methods
Users as configuration sections in the config file
<object name= "Users"
Type= "Spring.demo.compontext.userscompontents,spring.demo.compontent" >
</object>
Iusers user = context. GetObject ("Users") as Iusers;
Console.WriteLine (user. GetUserInfo ());
Console.read ();
The results of the operation are as follows:


About the spring.net of the IOC container is briefly introduced here, I also just learned spring.net, the article will inevitably appear wrong, the present
Spring.net's popularity is too low, I hope that interested in a lot of friends to discuss, but also hope that the experts can teach more. As found in the article
Error I hope you leave a message, we learn together, improve.

This sample program downloads:spring.demo. rar

Spring.net (ii)----Preliminary discussion on IOC container

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.