Wednesday, August 27, 2008

MVC Pattern

Model-View-Controller
The UIP Application Block is based on the model-view-controller (MVC) pattern. This is a fundamental design pattern for the separation of user interface logic from business logic. The pattern separates the modeling of the application domain, the presentation of the application, and the actions based on user input into three distinct objects:


Model — This object knows all about the data to be displayed and is responsible for managing the data and the actions of the application. It can be thought of as the processing part of an input-process-output system.

View — This object manages the information displayed to users. Multiple views can be used to display the same information in different ways. It can be thought of as the output part of an input-process-output system.

Controller — This object allows the user to interact with the application. It takes input from the user and passes instructions to the model. In the input-process-output system, this is the input part.

Goals of the UIP Application Block
The UIP Application Block is designed to help you:

  • Abstract all navigation and workflow code from the user interface
  • Enable the use of the same programming model for different types of application
  • Remove all state management from the user interface
  • Persist snapshot state across processes

Tuesday, August 26, 2008

HTTP handlers and HTTP modules

All requests to IIS are handled through Internet Server Application Programming Interface (ISAPI) extensions. ASP.NET has its own filter to ensure pages are processed appropriately. By default, the ASP.NET ISAPI filter (aspnet_isapi.dll) only handles ASPX, ASMX, and all other non-display file formats used by .NET and Visual Studio. However, this filter can be registered with other extensions in order to handle requests to those file types, too, but that will be covered later. Every request flows through a number of HTTP modules, which cover various areas of the application (i.e. authentication and session intofmation). After passing through each module, the request is assigned to a single HTTP handler, which determines how the system will respond to the request. Upon completion of the request handler, the response flows back through the HTTP modules to the user.
HTTP Module
HTTP modules are executed before and after the handler and provide a method for interacting with the request. Custom modules must implement the System.Web.IHttpModule interface. Modules are typically synchronized with events of the System.Web.IHttpModule class (implemented within the Global.asax.cs or .vb file). The following consists of a list of events that should be considered when implementing your module:

· BeginRequest
· AuthenticateRequest
· AuthorizeRequest
· ResolveRequestCache
· AcquireRequestState
· PreRequestHandlerExecute
· PostRequestHandlerExecute
· ReleaseRequestState
· UpdateRequestCache
· EndRequest
· PreSendRequestHeaders*
· PreSendRequestContent*

HTTP Handlers
HTTP handlers proces the request and are generally responsible for initiating necessary business logic tied to the request. Custom handlers must implement the System.Web.IHttpHandler interface. Additionally, a handler factory can be created which will analyze a request to determine what HTTP handler is appropriate. Custom handler factories implement the System.Web.IHttpHandlerFactory interface.

When to use Modules and Handlers
With all of the options available in ASP.NET, it is sometimes hard to determine what solution best fits your needs. Of course, it's always best to keep things simple; but, you still need to take evolutionary considerations and experience levels of current and future team members who have a potential of working on teh project into account. Both modules and handlers add a layer of indirection that can be daunting to beginners and/or programmers who are not used to implementing quality designs (read: design patterns).

First, consider what it is that you want to do within your module or handler. Some functions, such as authentication and intstrumentation can be added within modules. Modules should be considered only when there is a need for pass-through and/or intercepting interaction with the request. Alternatively, handlers should be put in place when there is a need to direct functional actions to one or more parts of an application. Probably the most noted use of HTTP handlers is to the FrontController pattern, which allows requests to be refactored and assigned to different components within your application without implementing such changes in every page.

Second, is it worth it? Most applications do not require this level of indirection, which can be hard to understand and/or implement when not documented properly. Some methods, such as the PageController pattern, allow for common functionality to be reused across multiple pages by including this logic in a base System.Web.UI.Page object, and inheriting from this for every web page. When reviewing the PageController implementation, you should know and understand the appropriate use of inheritence. Although certain things can be done this way (i.e. authorization and instrumentation), this is not always the correct means. You should fully understand the pros/cons of utilizing both modules and handlers before deciding on one implementation over the other. With each of these considerations, and more, the decision to implement modules and/or handlers can be a daunting one. Such decisions should be led by an experienced .NET architect. In the absense of a skilled architect, you will be looking at a lot of leg-work to determine the best solution.

Wednesday, August 20, 2008

Cross Browsers - keystroke input

Does anyone have trouble finding a cross-browser way to determine what keystroke is pressed?

To catch a keypress in a input box and determining on if its a TAB key or ENTER key and then run some code, here is the script. This works fine for IE and firefox.




/>

WCF

Windows Communication Foundation (WCF) provides a unified framework for rapidly building service-oriented applications that makes it easy to build and consume secure, reliable, and transacted Web services.


WCF’s single programming model unifies the capabilities in ASMX, WSE, Remoting, COM+, and MSMQ; therefore developers need to learn only one programming model.


In addition, WCF services now offer more design flexibility by supporting architecture such as Representational State Transfer (REST), JavaScript Object Notation (JSON), and Plain Old XML (POX) encoding.
By default, WCF services speak standard WS-* protocols when sending and receiving messages for maximum interoperability. These features include:
1.Interoperable security, reliable messaging, and transaction support are provided through WS-* implementations.
2.Reliable messaging guarantees “in order” and “exactly once” delivery.
3.MSMQ infrastructure provides queued messaging.
4.Transaction support enables reliable execution or rollback of multi-step operations.

Hosting model independence enables WCF code to run in IIS as well as in any managed code application (ASP.NET, EXEs, NT Services, WinForm, etc.).

Tuesday, August 19, 2008

Cross Tabs and Pivots

PIVOT is an option in SQL 2005 which is similar to CROSS TABs in SQL server 2000. Cross tabs are fall better that pivots except on smallest tables. The case is same when taking performance into point. So from migrating from SQL 2000 to SQL 2005 , we need not rewrite properly wrriten cross tabs to PIVOTS (rewriting which may cause negative impact on performace!)
Let me illustrate this with a small example :

--===== Sample data #1 (#SomeTable1)--
===== Create a test table and some data
CREATE TABLE #SomeTable1 ( Year SMALLINT, Quarter TINYINT, Amount DECIMAL(2,1) )
GO

INSERT INTO #SomeTable1
(Year, Quarter, Amount)
SELECT 2006, 1, 1.1 UNION ALL
SELECT 2006, 2, 1.2 UNION ALL
SELECT 2006, 3, 1.3 UNION ALL
SELECT 2006, 4, 1.4 UNION ALL
SELECT 2007, 1, 2.1 UNION ALL
SELECT 2007, 2, 2.2 UNION ALL
SELECT 2007, 3, 2.3 UNION ALL
SELECT 2007, 4, 2.4 UNION ALL
SELECT 2008, 1, 1.5 UNION ALL
SELECT 2008, 3, 2.3 UNION ALL
SELECT 2008, 4, 1.9
GO


/************ All we need is an output like this*********************/

Year 1st Qtr 2nd Qtr 3rd Qtr 4th Qtr Total
------ ------- ------- ------- ------- -----
2006 1.1 1.2 1.3 1.4 5.0
2007 2.1 2.2 2.3 2.4 9.0
2008 1.5 0.0 2.3 1.9 5.7

Here is the simple,easy cross tab query for this :

SELECT Year,
SUM(CASE WHEN Quarter = 1 THEN Amount ELSE 0 END) AS [1st Qtr],
SUM(CASE WHEN Quarter = 2 THEN Amount ELSE 0 END) AS [2nd Qtr],
SUM(CASE WHEN Quarter = 3 THEN Amount ELSE 0 END) AS [3rd Qtr],
SUM(CASE WHEN Quarter = 4 THEN Amount ELSE 0 END) AS [4th Qtr],
SUM(Amount) AS Total
FROM #SomeTable1 GROUP BY Year

Here is the pivot for this :

SELECT Year,
COALESCE([1],0) AS [1st Qtr],
COALESCE([2],0) AS [2nd Qtr],
COALESCE([3],0) AS [3rd Qtr],
COALESCE([4],0) AS [4th Qtr],
COALESCE([1],0) + COALESCE([2] ,0) + COALESCE([3],0) + COALESCE([4],0) AS
Total
FROM (SELECT Year, Quarter,Amount FROM #SomeTable1) AS src
PIVOT (SUM(Amount) FOR Quarter IN ([1],[2],[3],[4])) AS pvt ORDER BY Year



The "Pivot" line. It identifies the aggregate to be used, the column to pivot in the FOR clause, and the list of values that we want to pivot in the IN clause... in this case, the quarter number. Also notice that you must treat those as if they were column names. They must either be put in brackets or double quotes (if the quoted identifier setting is ON).

Multiple aggregation is the problem with pivot tables.

Monday, August 18, 2008

Features in .NET 3.0 and 3.5

Features in .Net 3.0
1. Windows Communication Foundation ( The Unified Framework For Rapidly BuildingService-Oriented Applications )

2. Windows Presentation Foundation
3. Windows Cardspace
4. Windows Workflow Foundation (Tools and engine for building workflow-enabled apps )

Features in .Net 3.5
1. Ajax enabled controls
2. LINQ, DLINQ, BLINQ, PLINQ, XLINQ
3. LINQ to SQL, Linq to XML, Linq to datasets
4. Extension methods
5. Lambda expressions
6. Query expressions
7. Expression trees
8. Anonymous types
9. collection and object initializers
10. Typed local variables and arrays
11. LinqSqlDataSource control added
12. Enables debugging of javascript and intellisense for javascript

Dictionary Object

Searching a dictionary object becomes very easy! You neednot put a forloop and look through rather use a foreach loop to check the keyvalue pair to find the exact match as shown below :

Dictionary Employee = new Dictionary();

if (Employee .TryGetValue("Jim", out value))

{

Console.WriteLine("For key = \"Employee\", value = {0}.", value);

else

Console.WriteLine("Key = \"Employee\" is not found.");

}


Contains can also be used to search through the dictionary obejct, but used mostly inserting a new value into the dictionary, it checks and then adds.

if (Employee .ContainsKey("Jim"))
{
flag = true; //search value found
}
else
flag = false;//search value not found