MVC uses Spring.net to apply IOC (dependency inversion) Learning notes 3_ practical tips

Source: Internet
Author: User

By now, we have basically built the framework of the project, but there is a problem in the project, although the layer and layer using the interface for isolation, but the instantiation of the interface, but also introduced the interface implementation class dependencies, such as the following code:

Private Iuserservice _userservice;
Private Iuserservice userservice
{get
 {return _userservice?? (_userservice = new UserService ());
 set {_userservice = value;}
}

Interface-oriented programming, controller should only rely on the site business layer interface, and can not rely on the implementation of the specific, otherwise, it is against the layer between the original intention of setting the interface.

In addition, if the upper layer relies on only the underlying interfaces, when you do unit testing, you can use mock tools such as moq,fakes to simulate the implementation of the interface according to the actual needs, you can flexibly control the return value of the interface to test all kinds of situations, if rely on specific implementation, the project testability will be greatly reduced , which is not conducive to automated unit testing.

Instead of relying on a specific implementation, you cannot get an instance of a class using the usual t-t = new T (), which requires a unified management of the object lifecycle, dependencies, etc. through the IOC container, where we will use the spring.net to apply the IOC.

The use of Spring.net in console programs

We'll show you how to use the Spring.net with a simple console example

To create a test class:

Namespace Springnetdemo
{public
 interface IClass
 {
  string Name {get; set;}
  Student Monitor {get; set;}
  String getmsg ();
 }
 public class Class:iclass
 {public
  string Name {get; set;}
  Public Student Monitor {get; set;}

  public string getmsg ()
  {return
   "class name:" + name + ", monitor: + monitor.name;
  }
 }

 public class Student
 {public
  string Name {get; set;}
 }
}

Two classes, an interface, a string type attribute in the student class, and a monitor property of the student type in addition to the string Name property in the Name,class class. Method Getmsg can return a brief description of the current class object, including the class name and monitor name two contents. Class classes implement the IClass interface.

Let's do a simple test:

IClass C6=new Class ()
{
 Monitor = new Student ()
 {
  name = "Mrs. Li Furong"
 },
 name = "Six Class"
};
Console.WriteLine (C6. Getmsg ());
Console.readkey ();

The output is:

Next, we change the spring.net container to declare the object

1. Referencing DLL files first

Requires the logging components used by the core libraries Spring.Core.dll and Spring.net Common.Logging.dll

2. Then we need to know the current assembly name and namespace

3. Create a new XML file in the project named Services.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <objects xmlns=
"Http://www.springframework.net" >
 < Description>an example that demonstrates simple IoC features.</description>
 <object name= "Class" type= "Springnetdemo.class,springnetdemo" >
 <property name= "name" value= "top class"/> <property
 " Monitor "ref=" Student "/>
 </object> <object name=" Student "type=" Springnetdemo.student
 , Springnetdemo ">
 <property name=" name "value=" Chen Two Egg "/>
 </object>
</objects>

Create a new objects root node in XML that joins the object child node that requires the container to be generated, the type attribute of the object child needs to indicate the full name (with assembly) of the class, and the current namespace, if you need to assign a default value to the properties of the current class. You can add a property node to the object node, configure its Value property to assign an initial value to the class's properties, and, if the property of the class is still another class object, you can create a new object node of that type and give its Name property. The ref attribute is then pointed to the name property of the new object node in the property node of the current attribute.

Note: to set the XML file to copy if newer or always copy, the build will not be automatically copied to the program directory

4. Then configure the Spring.net information in the application configuration 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= "File://services.xml"/>
 </context>
 </spring>
</configuration>

Run the program to get the output result:

Successful implementation of the IOC

The use of spring.net in asp.net mvc

method is similar to the one in the console program

1. Similarly, you first import DLL files

The number of DLL files that need to be referenced in the MVC project is a little more than five, and three web-related DLLs are required in addition to the two valuable.

2. For ease of management, we create a new config folder in the MVC project directory to save the configuration file and create a new two XML file in it

Controllers.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <objects xmlns=
"Http://www.springframework.net" >,
 < Object Type= "PMS. WebApp.Controllers.UserController, PMS. WebApp "singleton=" false ">
 <property name=" UserService "ref=" UserService "/>
 </object>
</objects>

Services.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <objects xmlns=
"Http://www.springframework.net" >
 < Object Name= "UserService" type= "PMS". Bll. UserService, PMS. BLL "singleton=" false ">
 </object>
</objects>

Also for convenience of management, we save the controller and business classes in two files, and the rules for the nodes in the file are exactly the same as in the console example.

3. Modify Web.config configuration file

In the configsections node of the configuration file, add the Sectiongrup node in the diagram, add the spring node to the configuration node, and use the resource node to set the path of the configuration file in the context node in the spring node.

4. Modify the Global file

Modify the Global.asax file of the root directory to change the parent class of the Mvcapplication class from HttpApplication to Springmvcapplication.

Copy Code code as follows:
public class Mvcapplication:springmvcapplication//httpapplication

5. Finally, the code in the original controller was modified to successfully use Spring.net in the MVC project to implement the IOC

Private Iuserservice _userservice;
Private Iuserservice userservice
//{
//get {return _userservice?? (_userservice = new UserService ()); }
//set {_userservice = value;}
Private Iuserservice UserService {get; set;}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.