ASP. NET MVC3 + Ninject. Mvc3 dependency injection can be so simple

Source: Internet
Author: User

I haven't written an article for a long time, so I'm lazy.

Today, I received an email calledHMQMy colleagues raised questions about ASP. NET MVC3 dependency injection,

At that time, when I wrote this article, the official MVC3 version was not available yet. At that time, I used the MVC3 Beta version, and now MVC4 is coming soon,

Therefore, this Demo was successfully tested in the current environment and there was no problem, but there may be problems in the current environment (I have not tested it ),

If there is time in the day, I will take this Demo and make appropriate modifications to make it suitable for the current MVC3 version. However, I don't think it is necessary,

Because it is very simple to use dependency injection in ASP. NET MVC3, it can be implemented with almost no lines of code. I recommend using Ninject,

The procedure is as follows:

 

Step 1: Create an ASP. NET MVC3 project.

Step 2: directly enter the command on the NuGet console:Install-package Ninject. Mvc3

After installing the source code package, all the dependency injection frameworks have been set up, so you do not need to change any code,

You will find that a "App_Start" folder is added to the project, and a code file named "NinjectMVC3.cs" is generated in this folder.

Step 3: open \ App_Start \ NinjectMVC3.cs, find the RegisterServices method, and write your dependency injection ing Code directly.

 

The code list is as follows:

Automatically Generated \ App_Start \ NinjectMVC3.cs code:

[Assembly: WebActivator. PreApplicationStartMethod (typeof (MvcApplication3.App _ Start. NinjectMVC3), "Start")]

[Assembly: WebActivator. ApplicationShutdownMethodAttribute (typeof (MvcApplication3.App _ Start. NinjectMVC3), "Stop")]

 

Namespace MvcApplication3.App _ Start

{

Using System. Reflection;

Using Microsoft. Web. Infrastructure. DynamicModuleHelper;

Using Ninject;

Using Ninject. Web. Mvc;

Using MvcApplication3.Services;

{

 

Public static class NinjectMVC3

Private static readonly Bootstrapper bootstrapper = new Bootstrapper ();

 

/// <Summary>

/// Starts the application

/// </Summary>

Public static void Start ()

{

DynamicModuleUtility. RegisterModule (typeof (OnePerRequestModule ));

DynamicModuleUtility. RegisterModule (typeof (HttpApplicationInitializationModule ));

Bootstrapper. Initialize (CreateKernel );

}

/// <Summary>

/// Stops the application.

/// </Summary>

Public static void Stop ()

{

Bootstrapper. ShutDown ();

}

/// <Summary>

/// Creates the kernel that will manage your application.

/// </Summary>

/// <Returns> The created kernel. </returns>

Private static IKernel CreateKernel ()

{

Var kernel = new StandardKernel ();

RegisterServices (kernel );

Return kernel;

}

 

/// <Summary>

/// Load your modules or register your services here!

/// </Summary>

/// <Param name = "kernel"> The kernel. </param>

Private static void RegisterServices (IKernel kernel)

{

// Write the dependency injection relationships you need to bind. Other Code except this method is automatically generated, so we don't have to worry about it.

Kernel. Bind <ITestService> (). To <TestService> ();

}

}

}

 

The test code is as follows:

\ Services \ ITestService. cs

Namespace MvcApplication3.Services

{

Public interface ITestService

{

String GetMessage (string aString );

}

}

 

\ Services \ TestService. cs

Using System;

 

Namespace MvcApplication3.Services

{

Public class TestService: ITestService

{

String ITestService. GetMessage (string aString)

{

Return "--------------" + (String. IsNullOrEmpty (aString )? String. Empty: aString) + "--------------";

}

}

}

 

\ Controllers \ HomeController. cs

Using System. Web. Mvc;

Using Ninject;

Using MvcApplication3.Services;

 

Namespace MvcApplication3.Controllers

{

Public class HomeController: Controller

{

 

[Inject]

Public ITestService Service {get; set ;}

 

Public ActionResult Index ()

{

ViewBag. Message = Service. GetMessage ("Welcome to ASP. net mvc! ");

Return View ();

}

 

Public ActionResult About ()

{

Return View ();

}

 

}

}

 

On this basis, we can perform some optimizations on our own to make it easier to use dependency injection and be more suitable for team development,

It is really good to say a word about NuGet. We should make good use of it. We originally wanted to upload the source code. Unfortunately, the network speed is very slow and it cannot be uploaded at all,

If you need the Demo source code, leave your email. If you see it, it will be sent to you, but I believe you do not need it at all.

 

Author: lauma

Related Article

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.