Search This Blog

Saturday, November 30, 2013

Front Controller Vs Page Controller in .Net


 


Criteria

Page Controller

 
Front Controller
 
Where to use
ASP.NET Web forms are implemented based on the page controller patter
ASP.NET MVC uses front controller pattern
Page Controller architecture has separate entry points for each request
Front controller architecture has a single entry point for all requests
Complexity / Ease of Implementation
Low complexity. Suits simpler web applications. Most of the time the commercial web application frameworks have built-in support.
Complexity is high compared to page controller. The single controller itself can be complex. However many CMS frameworks have this built in. (Eg: DNN)
Code Duplication / Code quality
Duplication can be high as the application grows. Has to implement a BaseController from which all page controllers extend.
Low code duplication. All common tasks can be put inside the front controller
Testability
Can be low. Has to go for a 2 part controller where one is HTTP dependant and the other is independent and testable.
The front controller will only handle transfer of the request to independent commands which could be testable. Has to be careful
Adaptability/Flexibility
When the page of the web application is very different from each other, the code duplication can be higher. If you try to counter this with separate inheritance tree, it can grow complex soon.
Since the front controller is centralized it's highly configurable. That's why many CMS frameworks use front controller to increase flexibility.
Performance
No extra bottleneck if plain page controller is used. However if a deep inheritance hierarchy is used performance can be a bit lower than normal.
The single front controller can end up being a bottleneck since it answers to all requests. Should avoid doing I/O or DB calls in the front controller as much as possible.
Thread Safety
The same page controller instance might handle (Depending on the web application framework, eg: ASP.Net) requests for the same page and thread-safety has to be considered. Use of static instances might be problematic.
Front controller can instantiate new command objects for each request and ensure thread safety at controller level. However model code still has to be thread safe.
Work distribution and responsibility
Easier to distribute the work among developers since each area of work can be done completely separately
Each developer has to have a good understanding of the Front Controller behavior as everything depends on it.

 

Wednesday, November 27, 2013

How to add Header and Footer on every page by using HttpModule in asp.net

Open visual studio and create a new website.
Right click on website and add a new class named PageFooterandHeader and inherit this class with IHttpModule inteface.


using System;
using System.Web;


public class PageFooterandHeader : IHttpModule
{
    const string PageHeaderString = "Page Header with the help of HttpModule";
    const string PageFooterString = "Page Footer with the help of HttpModule";

    public void Init(HttpApplication httpapp)
    {
        httpapp.BeginRequest += new EventHandler(httpapp_BeginRequest);
        httpapp.EndRequest += new EventHandler(httpapp_EndRequest);
    }

    void httpapp_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;
      
        context.Response.Write(PageHeaderString);
    }

    void httpapp_EndRequest(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext context = app.Context;

        context.Response.Write(PageFooterString);
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}


·         Open the web.config and register the module as below




<?xml version="1.0"?>

<configuration>
  <system.web>
    <httpModules>
<add name="PageFooterandHeader" type="PageFooterandHeader,App_Code"/>
    </httpModules>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>


Final Output:

Wednesday, November 13, 2013

Difference between Object, Dynamic and Var in c#

Object
Dynamic
Var
Can able to store any kind of value, because object is the base class of all type in .net framework.
Can able to store any type of the variable, similar to old VB language variable.
Can able to store any type of value but it require to initialize at the time of declaration.
Compiler has little information about the type
Compiler doesn't have any information about the this type of variable.
It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
Object type can be passed as function argument and function also can return object type
Dynamic type can be passed as function argument and function also can return object type
Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.
Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Casting is not require but you need to know the property and methods related to stored type
No need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.
Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time
Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type.
Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.
Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

Popular Posts