Thinking in Asp.Net MVC – (From MVCHandler to controller)

Thinking in Asp Net MVC – (From MVCHandler to controller)

  • Controller hierarchy

interface IController{
void Execute (RequestContext requestContext);
}

interface AsyncController: IController{
IAsyncResult BegineExecute (RequestContext requestContext, AsyncCallback callback, object state);
void EndExecute (IAsyncResult asyncResult);
}

class ControllerBase :IController {
virtual void Execute(RequestContext requestContext);
abstract void ExecuteCore();
void IController.Execute(RequestContext requestContext);
ControllerContext ControllerContext {ger;set;}
TempDataDictionary TempData {get;set;}
object ViewBag { [return :Dynamic] get;}
ViewDataDictionary ViewData {get;set;}
}

class ControllerContext{
ControllerContext(RequestContext requestContext, ContollerBase controller);
ControllerContext(HttpContextBase httpContext, RouteData routeData, ControllerBase controller);
virtual ControllerBase Controller { get;set; }
RequestContext RequestContext { get;set; }
virtual HTTPContextBase HttpContext { get;set; }
virtual RouteData RouteData { get;set; }
}

abstract class controller :
ControllerBase,
IController,
IAsyncController,
IActionFilter,
IAuthorizationFilter,
IExceptionFilter,
IResultFilter,
IDisposable
{
...
virtual void Execute (...)
virtual void ExecuteCore()
virtual IAsyncResult BeginExecute(..)
virtual void EndExecute(...)
virtual IAsyncResult BeginExecuteCore(...)
virtual void EndExecuteCore(...)
}

Design points

  • interface to saperate the sync and async
  • provide base class and include the common implementations for ‘sync’ behavior
  • inside the ControllerBase class , very clearly defined what must be overrided(abstract) and what can be overrided(virtual)
  • provide a ‘common place’ to transfer/store data (XXX_Dictionary, object) for child classes
  • a object link to HTTP Request(ControllerContext)
  • the ControllerContext object is the ‘glue’ of Controller world, HTTPRequest and route data.from this object itself , provided all information about each HTTP Request ‘life cycle’.
  • the abstract ‘Controller’ class is a facade encapsolation where should inherit from , provided default implementations of async behavior and different filters . also ‘virtual’ the Execution logic so that children can change as they want .

This is from the static structure point of view .now stand from the creation point of view ,see what happens during the creation of a controller.

interface IControllerFactory{
IController CreateController(RequestContext requestContext,
string controllerName);

SessionStateBehavior GetControllerSessionBehavior(
RequestContext requestContext, string controllerName);

void ReleaseController(IController controller);
}

Enum SessionStateBehavior {
Default,Required,ReadOnly,Disabled
}
class ControllerBuilder{
IcontrollerFactory GetControllerFactory();
void SetControllerFactory(Type controllerFactoryType);
void SetControllerFactory(IControllerFactory controllerFactory);
HashSet DefaultNamespaces {get;}
static ControllerBuilder Current {get;}
}

class MvcRouteHandler : IRouteHandler{
IControllerFactory controllerFactory;

MvcRouteHandler (IControllerFactory controllerFactory) {
...
return new MvcHandler(requestContext);
}

Controller Creation

URL Routing->
HTTPRouteHandler ->
Create Controller Builder ->
Create Controller Factory ->
Create Controller

Author: lanliang

Programmer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s