add

Thursday, May 28, 2009

Using Extension Method

Why Extension Method ?

Many time we need a requirement where we need to add a new method to a class. Addition of new method is very easy if we have the class code but what if we don't have the source code of that class? We just have the dll..

One option is to inherit the existing class and add the new method or modify any existing method. The problem with this option is that it may not be possible every time due to OO Principles.

Here comes the concept of Extension Method

How to Use Extension Method

Here is a step by step tutorial on how to use extension method.

Suppose the case is that, we need to have a method called 'Next' and 'Previous' that will give us the next and previous number of the given number.

We will do it by using Extension method.

Step 1: Create a new Console application:
Step 2: Add a new class Item and name it 'Ext.cs'.
Step 3: Renamen the namespace of that class to System.
Step 4: Add the followign code:

public static class Ext
{
public static int Next(this int A)
{
return A + 1;
}
public static int Previous(this int A)
{
return A - 1;
}

}


The class will look like this:



Few things to explain:

1. Namespace has been renamed to 'System' since almost all C# source files have a using System; declaration, this extension method is effectively going to be available globally

2. An extension method must be static and public, must be declared inside a static class, and must have the keyword this before the first parameter type, which is the type that the method extends. Extension methods are public because they can be (and normally are) called from outside the class where they are declared.

Step 5: How to use an Extension Method:

We have created a public , static method using namespace System. Now the question is how to use it.
In step 1 , we have created a console application, so in the Main method of Program.cs, write this:

int a=10;
Console.WriteLine("Number after {0} is {1}", a, a.Next());
Console.WriteLine("Number after {0} is {1}", a, a.Previous());
Console.ReadLine();

You will notice here, as soon you type 'a' and press ., static mehtods declared in the Ext.cs will appears in intellisence and with a new symbol that marks it as extension.



Step 6: Build the application and press F5:

The output window will show the desired result:



Summary

Although this is not a big revolution, one advantage could be Microsoft IntelliSense support, which could show all extension methods accessible to a given identifier. However, the result type of the extension method might be the extended type itself. In this case, we can extend a type with many methods, all working on the same data. LINQ very frequently uses extension methods in this way.

The most common use of extension methods is to define them in static classes in specific namespaces, importing them into the calling code by specifying one or more using directives in the module.

Wednesday, May 27, 2009

What is difference between dataset and datareader ?

Following are some major differences between dataset and datareader :-

√ DataReader provides forward-only and read-only access to data, while the
DataSet object can hold more than one table (in other words more than one
rowset) from the same data source as well as the relationships between them.

√ Dataset is a disconnected architecture while datareader is connected
architecture.

√ Dataset can persist contents while datareader can not persist contents, they
are forward only.

Overview of ADO.NET architecture

The most important section in ADO.NET architecture is “Data Provider”. Data Provider
provides access to datasource (SQL SERVER, ACCESS, ORACLE).In short it provides
object to achieve functionalities like opening and closing connection, retrieve data and update data. In the below figure you can see the four main sections of a data provider :-

Connection.
Command object (This is the responsible object to use stored procedures)
Data Adapter (This object acts as a bridge between datastore and dataset).
Data Adapter (This object acts as a bridge between datastore and dataset).

Dataset object represents disconnected and cached data. If you see the diagram it is not in direct connection with the data store (SQL SERVER, ORACLE etc) rather it talks
with Data adapter, who is responsible for filling the dataset. Dataset can have one or more Datatable and relations.

Introduction to ASP.NET MVC

In this session we’ll take a look at WebForms and how its request pipeline is structured, and then introduce the MVC pattern, and how it is implemented into the ASP.NET MVC framework.

We’ll then discuss how WebForms and MVC differ from each other.
Then we’ll look into the default conventions and project structure of an MVC application.

Web Forms



In WebForms today you start out with an ASPX page.
Within the page you can have any number of user controls, custom controls, and server controls.
In addition, a page can have a master page, which can in turn have any number of controls itself.
When you introduce model data, due to the level of abstraction needed by WebForms, any of the components used can access to data.
The page, the master page, and any of their individual controls contain logic that both retreives model data and contains it view representation.

This is model that has made WebForms so successful and useful for web developers.

WebForms uses a pattern called page controller.
When a request comes into ASP.NET, the request is served by a specific ASPX page. Hence, the page itself serves as the controller, or face of the object that handles the request.

What is MVC ?

MVC or Model-View-Controller is an architectural pattern that separates model access from presentation logic.
It adds specific responsibility to individual portions of the application.
The model represents your business objects, that both houses your data, and contains business logic.
The controller is responsible for interacting with the model, and then passing that data down to the view.
The view’s sole responsibility is acting as a representation of the model data it is given by the controller.



ASP.NET MVC
So now let’s see how a typical ASP.NET MVC application contrasts to the way a WebForms application looked.



Notice that we still have our ASPX page, which in turn can leverage user controls, custom controls, and server controls. Nothing difference there.
The page can be associated with a master page, which can in turn have its own set of user controls, custom controls, and server controls. So far this is still the same as WebForms.
We re-introduce the model data, which is where the contrast between WebForms and MVC gets clearer.
With WebForms, any of the controls, or page, or master page was directly interacting with the model. We introduce the controller into the picture, which will now be the orchestrator between the model and the views.

Here, instead of the pages and controls directly accessing the model, the controller is strictly doing that. Once it gets the model data it is responsible for passing the data to the page, or view.

The main tenets of ASP.NET MVC are…
Alternative
ASP.NET MVC builds on top of ASP.NET, it doesn’t replace it. It is simply meant to serve as an alternative approach to developing web applications using ASP.NET.

Testable
While WebForms can be tested, it isn’t the most trivial task. ASP.NET MVC was built with testing in minding, so practicing TDD, or just simply writing unit tests, is a simple task with MVC.

Extensible
MVC follows the convention over configuration principle. At the same time, it isn’t meant to constrain developers by its conventions. MVC is built on an extensible framework that virtually every component can be easily replaced with your own logic.

Routable
ASP.NET MVC uses ASP.NET Routing to associate URL patterns with controller actions. This means that your MVC applications will naturally be search-engine optimized, since every controller action that corresponds to an HTTP GET can get be easily discovered.

Now’s let’s briefly discuss the differences between WebForms and ASP.NET MVC.
While MVC is built on top of ASP.NET, it doesn’t have…
Postbacks
View state or control state
Server-side form
Page/control lifecycle
The page/control lifecycle is still present if you’re using WebForms as your view engine, but even then, you most likely won’t be using it.

Example of ASP.NET MVC

Here we see an example of the default project structure of an ASP.NET MVC application.




Because ASP.NET MVC comes packaged with a set of default conventions, it’s important to understand those conventions, and even better how you can override them if you want.
Here we see the controllers, placed in the Controllers folder. You actually don’t have to place your controllers in this folder, but it is created for you by default, and is considered a pretty standard place for them.
The ASP.NET MVC project templates places a folder called Models in your project that can be used to contain your model classes. This is only one option for model placement, and shouldn’t be seen as guidance that this is the “right way” to do it. Your model classes are in no way coupled to their placement, so you can put them in a different folder, or even in another project if you want.

Views in an ASP.NET MVC application are by default located in the Views folder. Within the Views folder, there are two options to place your views:
Either underneath the Shared folder, which are views that are meant to be used by multiple controllers. These are typically user controls or master pages.
Or underneath a folder named after the Controller they are associated to. These are views that are specific to controllers.

This is the default convention, but it can be changed if you want. We’ll go into customizing this in the next session.

The route definitions that map URL patterns to your controllers are declared within your application’s Global.asax file. This way they can be created upon Application Start.

LINQ Architecture and Components




LINQ is currently integrated directly into the native syntax for both C# 3.0 and VB 9.0 – which are included with Visual Studio 2008

Other languages may also support Language Integrated Query syntax in the future.
There are many flavors of LINQ which we describe by the type of data it operates over. As you can see here, LINQ may operate over Objects, SQL, Datasets, and XML.

These are the four flavors of LINQ that we are shipping with the .NET Framework 3.5

LINQ to Objects
SQL-like queries for any .NET collection (or anything that implements Ienumerable)
The LINQ to Objects API supports queries over any .NET collection, such as arrays and generic lists.

This API is defined in the System.Linq namespaces inside System.Core.dll
LINQ to objects is enabled by including the System.Linq namespace.
Manipulating collections of objects, which can be related to each other to form a hierary or a graph. From a certain point of view, LINQ to Objects is the default implementation used by a LINQ query.

LINQ to SQL
Query enabled data access framework

LINQ to XML
Query enabled, smaller, faster XML DOM

The important thing to remember is that the querying syntax used in LINQ is consistent regardless of the type of data you’re working with.

FAQ

Will there be support for Oracle with LINQ to SQL?

There is no provider model for DLINQ. In order for a third party provider to support DLINQ, they would need to produce an entire API that mirrors DLINQ that covers everything from query generation to examining attributes on classes to providing a designer to interact with the database schema. Because there's a lower bar for enhancing a provider to support the Entity Framework (primarily just query generation), Oracle is only planning to support the Entity Framework.
 

Introducing LINQ




Orcas significantly improves the way developers handle data.
Traditionally developers have manipulated data differently depending on where the data resides and how the user connects to it.

With the introduction of Language Integrated Query developers can now deal with data using a consistent programmatic approach and perform data access with new data design surfaces.

LINQ aimes to reduce the complexity for developers and help boost their productivity through a set of extensions to the C# and Visual Basic programming languages as well as the Microsoft .NET Framework, which provides integrated querying for objects, databases, and XML data.

Using LINQ, developers will be able to write queries natively in C# or Visual Basic without having to use specialized languages, such as Structured Query Language (SQL) and Xpath.

With Visual Studio “Orcas”, you can work with data in the way that you want. You can create entire collections of objects from a database backend if you like. You can interact with data as rows or columns – whatever makes sense to your application.
Language Integrated Query or “LINQ” will dramatically change the way we work with and program against data. By creating a common querying language in LINQ, we’ve freed you, the developer to focus on things that matter most to you. LINQ will provide you the ease of use you’ve come to expect with Visual Studio offering both IntelliSense and Autocompletion right in the IDE.

Language Integrated Query provides native querying syntax in C# and VB.Net. This frees the developer from having to master independent data programmability technologies (e.g. Xpath, Xquery, T/SQL) and instead offers the developer a consistent way to query data.

The best part is that the LINQ code you write is consistent whether your data store is a SQL Server, contained in a ADO.NET DataSet, an XML document, an EDM you create or even objects you create in memory.

With Orcas, we have taken a more general approach and are adding general purpose query facilities to the .NET Framework that apply to all sources of information, not just relational or XML data.

This facility is called .NET Language Integrated Query (LINQ).
With Orcas and ADO.NET 3.5, LINQ is an inherent part of the C# and VB.Net languages offering both IntelliSense and Autocompletion.

LINQ provides developers with a consistent query language which they in turn may use on various types of data. Be it Objects, XML, Datasets, SQL Server (or other databases with ADO.NET providers) and Entities. Any data that may be placed into a .NET collection of type IEnumerable can be queried by LINQ.

If you’ve written T/SQL in the past then LINQ will offer you familiar constructs for projections, restrictions, sorting and grouping such as Select, Where, GroupBy, and OrderBy.

You can also adapt the query operators to your liking by using extension methods which can override their default behavior.

.Net Framework and VS Roadmap



.NET Framework 3.0

First, if we travel back in time to November of 2006, Microsoft released the .NET Framework 3.0.

.NET Framework 3.0 was installed by default with Windows Vista, however, it was also available as a installable component for Windows XP SP2 and Windows Server 2003.

Visual Studio Extensions

When we released the .NET Framework 3.0, we also released a couple of extensions for Visual Studio.

The first extensions, called the Visual Studio 2005 Extensions for Windows Workflow Foundation, provided templates and design-time support for creating workflows within Visual Studio. It is worth noting that this was a complete and fully supported set of extensions.

The second extensions were the Visual Studio Extensions for the Windows Communication Foundation and Windows Presentation Foundation November CTP.

These extensions simply provided project and item templates for building WCF services and WPF applications.

ASP.NET Ajax

Shortly after the release of the .NET Framework 3.0, Microsoft also released the ASP.NET AJAX 1.0 extensions.
These extensions are designed to enable web developers to build dynamic applications with ASP.NET 2.0.

Visual Studio 2008 and .NET Framework 3.5

It’s important to understand these out-of-band releases as we look at Visual Studio 2008 and the .NET Framework 3.5. Visual Studio 2008 and the .NET Framework 3.5 include enhanced versions of the technologies that have been released out-of-band, such as AJAX and design time support for WF, WPF, and WCF applications.
At this point in time the beta 2 release of Visual Studio 2008 and the .NET Framework 3.5 are available.

It is also worth mentioning that there is a go-live license available with the .NET Framework 3.5. So you can deploy applications into production using this license. To be clear, there is a difference between licensed and supported. The .NET Framework 3.5 will not be supported until it is officially released.
Visual Studio 2008 and the .NET Framework 3.5 are scheduled to be released at the end of 2007 and they will launch with Windows Server 2008 and SQL Server 2008 on Februrary 27th.

Beyond the release of Visual Studio 2008 and the .NET Framework 3.5
After the release of these technologies, there will be an update to the .NET Framework that will be released with SQL Server 2008. This update will add support for the ADO.NET Entity Framework, which will enable flexible data access to a variety of data stores.

Finally, next year we plan to release a major update to Visual Studio Team System and Team Foundation Server codenamed “Rosario”.