Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How ASP.NET MVC 3 makes dependency injection easier to implement

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how ASP.NET MVC 3 makes dependency injection easier to implement, concise and easy to understand, and definitely brightens your eyes. I hope you can learn from the details of this article.

I didn't find a complete example, only some code snippets, so I sorted through them and had a little personal experience. I took it out and shared it with you.

If you encounter a high person, please do not hesitate to comment, the following is a code snippet.

1. The code that implements the dependency injection interface IDependencyResolver and MyDependencyResolver.cs provided in MVC3 Beta:

Using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace Demo {public class MyDependencyResolver: IDependencyResolver {# region IDependencyResolver member / private UnityContainer _ unityContainer / construct / dependency injection container public MyDependencyResolver (UnityContainer aUnityContainer) {_ unityContainer = aUnityContainer;} public object GetService (Type aServiceType) {try {return _ unityContainer.Resolve (aServiceType) } catch {/ as required by Microsoft, this method must return null without parsing any object, and must do so! Return null;}} public IEnumerable GetServices (Type aServiceType) {try {return _ unityContainer.ResolveAll (aServiceType) } catch {/ as required by Microsoft, this method must return an empty collection without parsing any objects, and must do so! Return new List ();}} # endregion}}

2. Set the dependency injection parser DependencyResolver in Global.asax.cs (this is a global static class, which is also added by MVC3 Beta):

Using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Microsoft.Practices.Unity Namespace Demo {/ / Note: For instructions on enabling IIS6 or IIS7 classic mode, / / visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication: System.Web.HttpApplication {public static void RegisterGlobalFilters (GlobalFilterCollection filters) {filters.Add (new HandleErrorAttribute ()) } public static void RegisterRoutes (RouteCollection routes) {routes.IgnoreRoute ("{resource} .axd / {* pathInfo}") Routes.MapRoute ("Default", / / Route name "{controller} / {action} / {id}", / / URL with parameters new {controller = "Home", action = "Index", id = UrlParameter.Optional});} protected void Application_Start () {AreaRegistration.RegisterAllAreas () RegisterGlobalFilters (GlobalFilters.Filters); RegisterRoutes (RouteTable.Routes); / / set dependency injection RegisterDependency ();} private static UnityContainer _ Container Public static UnityContainer Container {get {if (_ Container = = null) {_ Container = new UnityContainer ();} return _ Container } protected void RegisterDependency () {Container.RegisterType (); DependencyResolver.SetResolver (new MyDependencyResolver (Container));}

3. The code of Controller, HomeController.cs:

Using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; namespace Demo.Controllers {public class HomeController: Controller {[Dependency] public ITest Test {get; set;} public ActionResult Index () {ViewModel.Message = Test.GetString (); return View () } public ActionResult About () {return View ();}

4. ITest.cs code:

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {public interface ITest {string GetString ();}}

5. Test.cs code:

Using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Demo {public class Test:ITest {# region ITest member public string GetString () {return "Run demo!";} # endregion}}

* Note: this article is only applicable to the ASP.NET MVC3 Beta version. If the official version comes out in the future, it may not be implemented in this way. After all, for dependency injection,

It changes all the time from MVC1-> MVC3 Preview1-> MVC3 Beta.

The above is how ASP.NET MVC 3 makes dependency injection easier to implement. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report