Asp.net MVC source code analysis --Asp.net MVC and Ninject. MVC Integrated Analysis

Source: Internet
Author: User

 

In the previous article, we analyzed DependencyResolver and Service location. dependencyResolver can be used to register our own IResolver implementation to change the framework behavior. in Niject. MVC uses this mechanism to implement a Service location mechanism to create a Controller.

Niject. MVC source code: https://github.com/ninject/ninject.web.mvc

Bytes ------------------------------------------------------------------------------------------

First, find the NinjectMVC3.cs file. This class is required for all asp.net MVC projects that introduce Niject. MVC dll.

 

1 # if NUGET

2 [assembly: WebActivator. PreApplicationStartMethod (typeof (SampleApplication. App_Start.NinjectMVC3), "Start")]

3 [assembly: WebActivator. ApplicationShutdownMethodAttribute (typeof (SampleApplication. App_Start.NinjectMVC3), "Stop")]

4

5 namespace SampleApplication. App_Start

6 {

7 using System. Reflection;

8 using Microsoft. Web. Infrastructure. DynamicModuleHelper;

9 using Ninject;

10 using Ninject. Web. Mvc;

11

12 public static class NinjectMVC3

13 {

14 private static readonly Bootstrapper bootstrapper = new Bootstrapper ();

15

16 /// <summary>

17 // Starts the application

18 /// </summary>

19 public static void Start ()

20 {

21 DynamicModuleUtility. RegisterModule (typeof (OnePerRequestModule ));

22 DynamicModuleUtility. RegisterModule (typeof (HttpApplicationInitializationModule ));

23 bootstrapper. Initialize (CreateKernel );

24}

25

26 /// <summary>

27 // Stops the application.

28 /// </summary>

29 public static void Stop ()

30 {

31 bootstrapper. ShutDown ();

32}

33

34 /// <summary>

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

36 /// </summary>

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

38 private static IKernel CreateKernel ()

39 {

40 var kernel = new StandardKernel ();

41 RegisterServices (kernel );

42 return kernel;

43}

44

45 /// <summary>

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

47 /// </summary>

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

49 private static void RegisterServices (IKernel kernel)

50 {

51 kernel. Load (Assembly. GetExecutingAssembly ());

52}

53}

54}

55 # endif

After Assembly is loaded, the StartMethod method of WebActivator is called to start the NinjectMVC3.Start method. The bootstrapper. Initialize method is called in this method.

Www.2cto.com

In this Initialize method, many interface implementations are injected to the kernel container, and the IDependencyResolver Implementation of Ninject is registered to DependencyResolver.

DependencyResolver. SetResolver (CreateDependencyResolver ());

Finally, we can inject all the implementations of the DependencyResolver interface in the MVC Framework to Ninject.

When our Controller constructor has interface parameters and is registered with Ninject, you can also smoothly create interfaces in the Controller parameters (because all controllers are created through Ninject ), in this way, we can extract some business behaviors in the Controller to create independent classes (business layer), so as to divide the class hierarchy and design more clearly.

For example:

 

In this case, you only need to register the IAccountManager implementation AccountController with the Iinject container in Application_Strat.

 

From the rain of November

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.