ASP. net mvc best practices (3)

Source: Internet
Author: User

Address: http://space.itpub.net/740297/viewspace-588913

 

3. Use Bootstrap in global. asaxProgram(Bootstrapper) If you are in global. the asax application_start method executes too many tasks (such as configuring IOC/di, registering routing, model binding, view engine, and starting background services related to applications ). For example, in the latest oxite Source Code The following code snippet is available: protected virtual void onstart () {setupcontiner (); setupsite (); registerroutes (); registeractionfilters (); registermodelbinders (); registerviewengines (); launchbackgroundservices ();} Do you think it has performed too many tasks? Is there any way to expand it? How can I add a new route, model binding, and view engine to it without modifying it? Does it violate the open-closed principle? Bootstrapper is used to solve these problems. The process for creating bootstrapper is as follows:. create a task interface public interface ibootstrappertask {void execute ();} B. create a class for each type of task and implement the previously defined interface, such as: public class registercontrollerfactory: ibootstrappertask {private readonly icontrollerfactory _ controllerfactory; Public registercontrollerfactory (icontrollerfactory controllerfactory) {_ controllerfactory = controllerfactory;} public void execute () {controllerbuilder. current. setcontrollerfactory (_ controllerfactory);} public class registerroutes: ibootstrappertask {private readonly routecollection _ routes; Public registerroutes (): This (routetable. routes) {} public registerroutes (routecollection routes) {_ routes = routes;} public void execute () {_ routes. clear (); _ routes. ignoreroute ("{resource }. axd/{* pathinfo} "); _ routes. maproute ("default", "{controller}/{action}/{ID}", new {controller = "home", Action = "Index ", id = ""}) ;}} C. create a static bootstrapper class to execute these tasks: public static class bootstrapper {static bootstrapper () {configurecontainer ();} public static void run () {var tasks = servicelocator. current. getallinstances <ibootstrappertask> (); foreach (VAR task in tasks) {task. execute () ;}} Private Static void configurecontainer () {iunitycontainer Container = new unitycontainer (); unityconfigurationsection configuration = (unityconfigurationsection) configurationmanager. getsection ("Unity"); configuration. containers. default. configure (container); servicelocator. setlocatorprovider () => New unityservicelocator (container);} D. modify global. asaxprotected void application_start () {bootstrapper. run ();} e. modify Web . Config to concatenate tasks like this: <unity> <typealiases> <typealias alias = "icontrollerfactory" type = "system. web. MVC. icontrollerfactory, system. web. MVC "/> <typealias alias =" controllerfactory "type =" unitycommonservicelocatormvc. commonservicelocatorcontrollerfactory, unitycommonservicelocatormvc "/> <typealias alias =" ibootstrappertask "type =" unitycommonservicelocatormvc. ibootstrappertask, unitycommonservicelocatormvc "/> <Typealias alias =" registerroutes "type =" unitycommonservicelocatormvc. registerroutes, unitycommonservicelocatormvc "/> <typealias alias =" registercontrollerfactory "type =" unitycommonservicelocatormvc. registercontrollerfactory, unitycommonservicelocatormvc "/> </typealiases> <containers> <container> <types> <type name =" registerroutes "type =" ibootstrappertask "mapto =" registerroutes "/> <type = "IC Ontrollerfactory "mapto =" controllerfactory "/> <type name =" registercontrollerfactory "type =" ibootstrappertask "mapto =" registercontrollerfactory "> <typeconfig extensiontype =" Microsoft. practices. unity. configuration. typeinjectionelement, Microsoft. practices. unity. configuration "> <constructor> <Param name =" controllerfactory "parametertype =" icontrollerfactory "> <dependency/> </param> </constructor> </ty Peconfig> </type> </types> </container> </containers> </Unity> therefore, applications can be expanded by using the bootstrapper, we can create a new task and execute it in the application startup method without modifying anything.

Related reading:

    • ASP. net mvc unleashed (6)(Geez, 2009-3-17)
    • ASP. net mvc 1.0 officially released(Geez, 2009-3-18)
    • ASP. net mvc unleashed (6) (continued)(Geez, 2009-3-21)
    • ASP. net mvc technology topics(Zhu Xianzhong, 2009-3-27)
    • Action filter for ASP. net mvc notes (idotnetspace)
    • ASP. net mvc: analyze the tempdata mechanism (idotnetspace)
    • ASP. net mvc Futures: MVC control Overview (geez, 2009-4-09)
    • ASP. net mvc Futures: local view(Geez, 2009-4-10)
    • ASP. net mvc best practices (1)(Geez, 2009-4-10)
    • ASP. net mvc Best Practices (2)(Geez, 2009-4-13)

 

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.