Spring.net and asp.net mvc combination example

Source: Internet
Author: User

One, gossip

Because the project is going to use the IOC framework, there are several excellent IOC frameworks for the IOC framework selection, such as: Sping.net,castle,unity ... Of course, there are more than three, there are other IOC framework, castle and unity use of the similar, spring.net in front of both the use of a larger difference. Information, Documentation and blog spring.net>castle>unity. These three have big, strong team support, and I feel spring.net doing the best I can, regardless of the strength of the update, the documentation and the examples.

In view of the Spring.net function, information documents, Bowen, etc. more perfect and rich, coupled with my spring.net relatively familiar with some, personnel training, etc. are easier, so selected Spring.net as the project's IOC framework.

Two. An introductory example of the combination of spring.net and asp.net mvc

1) Dependent components

The following components need to be used here: spring.net

Common.Logging.dll

Spring.Core.dll

Spring.Web.Mvc.dll

2) Environment Construction

Getting Started sample habits start with Hello world, this time is no exception

First, create a asp.net MVC project:

We create a new Ihello interface and a Hello implementation class in the Model folder

Ihello interface

public interface Ihello

{

String SayHelloWorld ();

}

Hello implementation class

public class Hello:ihello

{

public string SayHelloWorld ()

{

Return "Hello world!";

}

}

Return a string "Hello world!" in the SayHelloWorld method.

Let's test it, in HomeController we write the following code:

HomeController class

public class Homecontroller:controller

{

Public Ihello hello = new Hello ();



Public ActionResult Index ()

{

Viewbag.message = Hello. SayHelloWorld ();



return View ();

}



Public ActionResult About ()

{

return View ();

}

}

We instantiate a Hello object, hard-coded way, first run a look at the effect:

All right, okay, let's introduce spring.net.

3) Introduction of Spring components

The Spring.net components mentioned above are introduced into the project to:

4) Modify Web.config add Spring configuration:

Web.config

<configSections>

<sectiongroup name= "Spring" >

<section name= "Context" type= "Spring.Context.Support.MvcContextHandler, SPRING.WEB.MVC"/>

</sectionGroup>

</configSections>



<spring>

<context>

<resource uri= "~/configs/spring.xml"/>

</context>

</spring>

We refer to another XML file Spring.xml, which is used to configure objects and to set dependent dependencies.

5) Remove the specific implementation

Before configuring the object, we first change the HomeController code to remove the specific implementation:

  public class Homecontroller:controller

{

Public Ihello Hello {get; set;}



Public ActionResult Index ()

{

Viewbag.message = this. Hello.sayhelloworld ();



return View ();

}



Public ActionResult About ()

{

return View ();

}

}

6 Configuring objects and setting dependent dependencies

Spring.xml

<?xml version= "1.0" encoding= "Utf-8"?>

<objects xmlns= "Http://www.springframework.net" >

<object id= "Hello" type= "SpringMvcApp.Models.Hello, Springmvcapp"/>

<object type= "SpringMvcApp.Controllers.HomeController, Springmvcapp" singleton= "false" >

<property name= "Hello" ref= "Hello"/>

</object>

</objects>

The point to note here is that the object default configuration is a single example, controller is not a single case, so here the singleton is set to false.

If this is already configured, let's test it.

Unfortunately, we didn't get the object instance we wanted.

7) Inherit spring.

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.