Analysis of the most confusing Design of ASP. NET Routing

Source: Internet
Author: User

 
 
  1. public abstract class RouteBase  
  2. {  
  3.     protected RouteBase() { }  
  4.     public abstract RouteData GetRouteData(HttpContextBase httpContext);  
  5.     public abstract VirtualPathData GetVirtualPath(  
  6.         RequestContext requestContext,  
  7.         RouteValueDictionary values);  

Why is it an abstract class without any implementation, rather than an interface as follows )?

 
 
  1. public interface IRoute  
  2. {  
  3.     RouteData GetRouteData(HttpContextBase httpContext);  
  4.     VirtualPathData GetVirtualPath(  
  5.         RequestContext requestContext,  
  6.         RouteValueDictionary values);  

Isn't it more beautiful to do this? In this way, the IRoute type can be used in the code to avoid the unpleasant naming of RouteBase and people who do not know whether or not they agree ). To put it another step, the "beauty" in naming is a trivial matter ...... However, the abstract class has a very serious limitation in the. NET platform: A class cannot inherit multiple base classes. Therefore, the. NET platform is always more inclined to use interfaces than abstract classes.

However, there cannot be any implementation in the interface, so where is the reusable function suitable? Framework Design Guildlines tells us that in a class library, it is best to define a default implementation for the interface, which is also a "Reference" for developers to "scale ". That is to say, if there is anything that needs to be reused, we can do this:

 
 
  1. public abstract class RouteBase : IRoute  
  2. {   
  3.     // reusable implementations  
  4. }  
  5.  
  6. public class Route : RouteBase  
  7. {  
  8.     // concrete implementations  

In fact, many class libraries on the. NET platform also follow this practice. A typical practice is the Extender model of the ASP. net ajax framework:

 
 
  1. public interface IExtenderControl { }  
  2. public abstract class ExtenderControl : Control, IExtenderControl { } 

Even in the ASP. net ajax Control Tookit project, there are further extensions:

 
 
  1. public abstract class ExtenderControlBase : ExtenderControl { }  
  2. public class AnimationExtenderControlBase : ExtenderControlBase { }  
  3. public class AutoCompleteExtender : AnimationExtenderControlBase { } 

It seems that Microsoft's promotion of Framework Design Guidelines within the project team is not thorough enough.

On the. NET platform, a pure abstract class is not implemented. I suspect that the person who wrote this code has just switched from C ++ to C # -- but ASP. NET Routing also has an interface such as IRouteConstraint). Why didn't the author realize it and no one gave a different opinion? The Microsoft development team should have a strict Code Review process. How can such Code be officially released? You must know that once an interface is published, it cannot be deleted. That is to say, it is difficult for Microsoft to make up for this error.

If the method name is not good or the responsibilities are unclear, you can add ObsoleteAttribute to the old method so that the compiler will prompt that the method has expired ), and delegate the call of the old method to the new implementation. For example:

 
 
  1. public abstract class CodeDomProvider : Component  
  2. {  
  3.     [Obsolete(  
  4.         "Callers should not use the ICodeCompiler interface and should  
  5.          instead use the methods directly on the CodeDomProvider class.  
  6.          Those inheriting from CodeDomProvider must still implement this 
  7.          interface, and should exclude this warning or also obsolete this 
  8.          method.")]  
  9.     public abstract ICodeCompiler CreateCompiler();  
  10.  
  11.     [Obsolete(  
  12.         "Callers should not use the ICodeParser interface and should  
  13.          instead use the methods directly on the CodeDomProvider class.  
  14.          Those inheriting from CodeDomProvider must still implement this 
  15.          interface, and should exclude this warning or also obsolete this 
  16.          method.")]  
  17.     public virtual ICodeParser CreateParser();  
  18.  
  19.     ...  

However, the current problem is a "class", and this class is everywhere. For example, in RouteData, there is an attribute Route, which is the RouteBase type-if you change it to the IRoute interface, at least the project must be re-compiled before it can be "upgraded ". As a public class library, especially a mature framework such as. NET, it should be "Painless.

This article is from Zhao's blog garden Article ASP. NET Routing, the most confusing design.

  1. ASP. NET Control Learning Summary
  2. Some basic knowledge about ASP. net mvc Framework
  3. ASP. NET cache mechanism: balance between development efficiency and Optimization
  4. How to Avoid ASP. NET cache occupying system resources
  5. Comments on ASP. net web controls

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.