Labels

Wednesday, December 12, 2007

ASP.NET MVC Framework

ASP.NET MVC Framework:

http://weblogs.asp.net/scottgu/archive/2007/12/09/asp-net-3-5-extensions-ctp-preview-released.aspx

Over the years ASP.NET does not have built-in support for developing web applications using a model-view-controller (MVC) based architecture. Microsoft is working towards implementing such framework and will be releasing a public preview of this ASP.NET MVC Framework a little later this year. A fully supported ASP.NET feature in the first half of next year.

Before we proceed further, access the link to know what is MVC.

This is true that the as of now ASP.Net does not having the built-in support for MVC framework. However it is possible to achieve the MVC Framework as below.

· Model: - This section is represented by Data view, Dataset, Typed Dataset, Business components, business entity models etc.

· View: - ASPX, ASCX, or windows application UI like data grid etc. form the view part of it.

· Controller: - In ASP.NET the behind code is the controller. As the events are handled by that part. Controller communicates both with Model as well as view.

But the new built-in-support implements the MVC in slightly a new way and provides more flexibility and loose coupling b/w Model, View & Controller.

A few quick details to share in the meantime about the ASP.NET MVC framework:

  • Enables clean separation of concerns, testability, and TDD by default - All core contracts within the MVC framework are interface based and easily mockable (it includes interface based IHttpRequest/IHttpResponse intrinsics). You can unit test the application without having to run the Controllers within an ASP.NET process (making unit testing fast). You can use any unit testing framework you want to-do this testing (including NUnit, MBUnit, MS Test, etc).
  • Highly extensible and pluggable - Everything in the MVC framework is designed so that it can be easily replaced/customized (for example: you can optionally plug-in your own view engine, routing policy, parameter serialization, etc). It also supports using existing dependency injection and IOC container models (Windsor, Spring.Net, NHibernate, etc).
  • Powerful URL mapping - It includes a very powerful URL mapping component that enables you to build applications with clean URLs. URLs do not need to have extensions within them, and are designed to easily support SEO and REST-friendly naming patterns. For example, I could easily map the /products/edit/4 URL to the "Edit" action of the ProductsController class in my project above, or map the /Blogs/scottgu/10-10-2007/SomeTopic/ URL to a "DisplayPost" action of a BlogEngineController class.
  • Compatabilty with Existing:
    • The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as "view templates".
    • It does not, however, use the existing post-back model for interactions back to the server. Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).
    • Fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc.

Thanks & Regards,

Arun Manglick || Tech Lead

MVC Architecture

MVC Framework:


What is a Model View Controller (MVC) Framework?

MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers.

  • "Models" in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).
  • "Views" in a MVC based application are the components responsible for displaying the application's user interface. Typically this UI is created off of the model data (for example: we might create an Product "Edit" view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).
  • "Controllers" in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information - it is the controller that handles and responds to user input and interaction.

One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated.

The MVC pattern can also help enable red/green test driven development (TDD) - where you implement automated unit tests, which define and verify the requirements of new code, first before you actually write the code itself.


Thanks & Regards,

Arun Manglick || Tech Lead

Monday, December 10, 2007

B'Bye 2007

Do not have time to breath:

While 2007 saw the launch of several different technologies, two primary themes emerged: AJAX and the Microsoft .NET Framework 3.0.

AJAX:

· AJAX was inaugurated as a first-class Web application development platform with the ASP.NET AJAX Extensions.

· The extensions library introduced both Client And Server Libraries that can abstract much of the plumbing detail involved in connecting JavaScript code running in the browser with business logic running on an application server.

· Additionally, the library established a new mechanism for dynamically associating script behaviors with existing controls, provided localization functionality on both client and server, and much more.

The .NET Framework 3.0 introduced three technologies that are already causing a dramatic shift in how applications are architected.

· Windows Presentation Foundation (WPF) took the traditional message-pump, clipping-rectangle architecture of Win32 and flipped it on its head, leveraging advancements in graphic cards and offering Windows developers the declarative, container-based mode of authoring user interface elements long enjoyed by Web application developers.

· Windows Communication Foundation (WCF) pulled together all the various methods of creating a distributed application under one abstraction layer.

· Windows Workflow Foundation (Windows WF) added a declarative workflow modeling language and execution engine, functionality that was previously available only as an additional app, not as a platform service.

Silverlight has been poised to take the world by storm, enabling a richer browser-based user experience than has yet been possible in Web application development. Moreover, with the release of LINQ in Visual Studio 2008 and the ADO.NET Entity Framework (following soon after the Visual Studio release), how we think about data access is about to undergo a major paradigm shift.

When you consider the enormity of the .NET Framework 3.0 and AJAX is it now a best practice to abandon all of those OO practices and split class hierarchies into pairs of logic service classes and data token classes that can easily be passed between remote services.

So if you think you’ll get two seconds to take a breath, keep holding on. J

Thanks & Regards,

Arun Manglick || Tech Lead

Hidden field doubts

Here I need to confirm – Changes made at client side of a Button click, survives after the Post back completes.

Lets assume in a page below is the set of controls.

· lblHide – Expectation is to hide in JS.

· lblDeclarativeValue - Expectation is to change its value in JS

· HiddenField1 - Expectation is to change its value in JS

· TextBox1 - Expectation is to change its value in JS

· Button2 – Makes a postback to see the above chagnes made in JS at Server side

<div id="divHidden" runat="server" style="border:solid 1px red;" title="Hidden Button" >

<asp:Label ID="lblHide" runat="server" Text="Try to Make - Invisible at Cleint Side"></asp:Label><br />

<asp:Label ID="lblDeclarativeValue" runat="server"></asp:Label><br />

<asp:HiddenField ID="HiddenField1" runat="server" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Postback" OnClientClick="SetValue();" OnClick="Button2_Click" />

</div>

Below is the required JS.

function SetValue()

{

// Make Invisible at Client Side and check at serverside if the declarative value has overridden the cleint changess

document.getElementById('lblHide').style.display='none';

// Change the value at Cleint Side and check at serverside if the declarative value has overridden the cleint changes

document.getElementById('lblDeclarativeValue').innerHTML='Changed Declarative';

document.getElementById('HiddenField1').value= 'Change';

document.getElementById('TextBox1').value="Text1";

}

Here check the effect when there is no declarative values been assigned to the controls on the page.

PostBack Results – At server side.

· Render Results - After the postback completes

    • lblHide – Always visible
    • lblDeclarativeValue – Invisible [as no declarative value is assigned]
    • HiddenField1 - 'Change'
    • TextBox1 - Text1

Now see the results when the declarative values are assigned.

<div id="divHidden" runat="server" style="border:solid 1px red;" title="Hidden Button" >

<asp:Label ID="lblHide" runat="server" Text="Try to Make - Invisible at Cleint Side"></asp:Label><br />

<asp:Label ID="lblDeclarativeValue" runat="server" Text="Declarative Label"></asp:Label><br />

<asp:HiddenField ID="HiddenField1" runat="server" Value=" Declarative Hidden " />

<asp:TextBox ID="TextBox1" runat="server" Text="Declarative Text"></asp:TextBox>

<asp:Button ID="Button2" runat="server" Text="Postback" OnClientClick="SetValue();" OnClick="Button2_Click" />

</div>

PostBack Results – At server side.

· Render Results - After the postback completes

    • lblHide – Always visible
    • lblDeclarativeValue – Declarative Label
    • HiddenField1 - 'Change'
    • TextBox1 - Text1

Summary:

· HiddenField/Textbox will survive the changes made at client side.

· Whereas label does not.

Thanks & Regards,

Arun Manglick || Tech Lead

Swap Myth - Value/Refrence Type

In the interview if I ask a simple question – How swap of Value and Referecne type works. Outcome is always unsatisfactory.

Here is the myth.

Suppose you have two Value types and two Reference Types. Lets try to swap them.

Value Types –

string str1 = "Hello";

string str2 = "World";

MessageBox.Show("Before Change: " + str1 + "," + str2); // Hello World

SwapMe( str1, str2);

MessageBox.Show("After Change: " + str1 + "," + str2); // Hello World [Does not works]

· The values of the parameters is passed as ‘By Value’.

· Hence swapping did not worked.

· To success, need to pass the value types By Reference using the ‘ref’ keyword.

SwapMe(ref str1, ref str2);

Reference Types –

Employee obj1 = new Employee(5);

Employee obj2 = new Employee(6);

MessageBox.Show("Before Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6

Swap.SwapMe(obj1, obj2);

MessageBox.Show("After Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6 [Does not works]

· Before we proceed, just notice that the value of the parameter is the address of the object (Pointer to the object) and not the value of object.

· The address is passed as ‘By Value’ and not ‘By Reference’. (Might be feeling strange).

· Hence swapping the address would not affect the actual addresses.

· But if you need to do this, pass it using ‘ref’.

Swap.SwapMe(ref obj1, ref obj2);

But at the same time instead of swapping if you want to change the values as below, then it will work perfectly fine without the use of ‘ref’ keyword.

Employee obj1 = new Employee(5);

Employee obj2 = new Employee(6);

MessageBox.Show("Before Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 5, 6

ChangeMe s(obj1, obj2);

MessageBox.Show("After Swap: " + obj1.i.ToString() + "," + obj2.i.ToString()); // 6, 5 [Does works]

· As above described value of the parameter is the address of the object (Pointer to the object) and not the value of object.

· The address is passed as ‘By Value’ and not ‘By Reference’.

· But code is dereferencing the objects and then changing its value.

· Hence it will change to the actual object, without the need of using ‘ref’.

Supporting fucntions:

// public static void SwapMe(ref Employee a, ref Employee b)

public static void SwapMe(Employee a, Employee b)

{

Employee temp;

temp = a;

a = b;

b = temp;

}

//public static void SwapMe(ref string a, ref string b)

public static void SwapMe(string a, string b)

{

string temp;

temp = a;

a = b;

b = temp;

}

public static void ChangeMe(Employee a, Employee b)

{

a.i = 55;

b.i = 66;

}

Thanks & Regards,

Arun Manglick || Tech Lead

Wednesday, December 5, 2007

Display/Visible styling property in JS

Hi,

Here, is the fact that Display/Visible styling property in JS works perfectly in IE and Firefox.

Display

· Block

· None

Visiblilty

· Visible

· Hidden

function Hide1()

{

//debugger;

obj=document.getElementById('divHidden');

if(obj.style.display == "")

{

obj.style.display='none';

}

else

{

obj.style.display='';

}

}

function Hide2()

{

obj=document.getElementById('divHidden');

if(obj.style.visibility == "")

{

obj.style.visibility='hidden';

}

else

{

obj.style.visibility='';

}

}

Thanks & Regards,

Arun Manglick || Tech Lead