Current software industry has adapted MVC pattern hugely,
not only in Microsoft’s stack but also in Other like PHP ,ruby on Rails etc.
Let’s stick on MVC on top of Microsoft’s platform as I am
.NET developer and love Microsoft’s technology to develop application. We all
know, ASP.NET MVC has many advantages which made this booming technology in
market. As per my view the first and most strongest point of MVC is it’s
architecture, yes the decouple architecture.
The Idea behind the decouple architecture is to make it
flexible in extreme level. If we see the complete flow of request in MVC pipeline,
we will see that the request is going through various modules and the modules
are independent.
I mean we can easily detach/attach any module in pipeline.
Now, question is what is my profit by attaching or detaching module?
- If we detach any module, the execution flow may speed up because the path will decrease.
- We can attach our own module to implement our custom behavior which will override the normal flow of execution.
The last point is very important, In MVC we can easily
inject our own logic in middle of execution and the concept of extensibility comes
here.
Being a software developer, we believe and follow one
guideline that is “Module is open for extension but close for modification”. It
means we can extend the functionality of a module but should not modify
functionality of module. So that we will find various extension point of MVC
and implement out custom logic there.
Now, let us try to analyze MVC pipeline to detect various extensibility
point and we will relate some real time scenario to understand the need of extensibility.
At a
very high level, the lifecycle of a request in ASP.NET MVC is:
- The browser sends the GET or POST HTTP request to application;
- The request is inspected and routed to the right controller and the right action;
- The action collects or creates the data that needs to be returned to the browser very often from DB or other sources;
- This data is passed to the view that renders the response, which is sent back to the browser.
In time of Routing
This is one good extension point where we can implement our
own custom logic. There is once default routing pattern, which is given by
Microsoft, but in need we can implement our own custom routing mechanism.
Once the Route has been selected, the corresponding IRouteHandler is called. The default one calls the MvcHandler, which starts the real processing inside ASP.NET MVC.
Obviously, if you replace the default handler with something completely
different, you also take over the processing of the request, and so all the
steps that follow might not apply anymore.
Real time Example: Say
for example we want to set route constraint in url path , then we can implement
our custom logic to get it done. Another example is to implement dynamic
routing which will get generate from Database or some other data source.
Delegating handler
If you are experienced with HttpHandler and HttpModule then
probably you know how to inject your code in this section. This is MVC pipeline
which executes after route initialization/selection. This is one good place to implement
security in application.
Real time scenario: If
you want to implement our own piece of code to implement authentication or
authorization then this is the best place to do. You can sniff header of HTTP
request and based on that you can make decision whether you will allow the
request to next level or not?
Controller selection
Rather I would say before selection of controller. Based on
route pattern and parameter IControllerFactory creates an instance of a
controller and in this point, we can inject our IoC container to solve the
dependency of controller
Real time scenario : For
example ,we want to inject the dependency of controller in our application and
for that we have adapted constructor injection pattern. For that we can create
one dependency repository using any IoC container and can inject this in time
of controller instantiation. The IoC container module is smart enough to handle
this automatically.
In time of Action
execution
Once the controller has been instantiated, the specified
action has to be executed, and this is handled by the IActionInvoker. As with the IRouteHandler, in theory you could completely
replace this component with your own implementation, but then you will be
developing
against your own conventions. So, there is scope to inject our own
logic before and after of action execution in technical tern which is called
Action Filter.
There are 5 types of Action filters and those are
- · Authentication (added in MVC 5)
- · Authorization
- · Action Filter
- · Result Filter
- · Exception Filter
In need can extend
each and every filter and implement our custom action filter
Real time scenario: For example, you want
to implement global error handling mechanism in your application. For same you
can create one action filter and can decorate your controller with the filter.
Implement your Own
view Engine
There are many action results: one simply returns a text
string, another returns a binary file or a Json formatted result. Yet the most
important Action Result is the ViewResult, which renders and returns an HTML page to the browser.
We know that the HTML processing happens in view engine and
if you want you can create your own View engine to render HTML page
Real time scenario:
There are many third party view engine in market other than ASPX and Razor view
engine. They has their own mechanism to render view, In need you too can create
your own view engine with your own HTML tag set.
So, those are few very important and popular extensibility
point of ASP.NET MVC where you can get your magic done!
No comments:
Post a Comment