Tuesday, September 7, 2010

.NET Community Session : ASP.NET 4.0 In - Depth

I am going to speak on a Community Session Organized by DotNetFunda on ASP.NET 4.0 with Abhijit Jana.

ASP.NET 4.0 comes with lots of new features that enhance the developers to deal with ASP.NET applications easily and also core functionality to deal with both server side and client side of an ASP.NET application. These changes makes ASP.NET 4.0 applications more effective than the existing ASP.NET applications.

The Agenda for the Session :
(Click to enlarge to Full Size)

  • What’s New in ASP.NET 4.0
  • Improvement in Visual Studio 2010 for web development
  • Empty Website project and Cleaner Web.Config
  • MetaKeyword and MetaDescription enhancements
  • Controlling ViewState mechanism 
  • Controlling ClientID generation for asp.net controls
  • CompressionEnabled Session
  • CSS Improvements and CSS Friendly Menu Control
  • ListView rendering mechanism, Enhancement in Listview control
  • Enhancement in RadioButtonList and CheckBoxList
  • SEO Friendly URL (Web Form Routing)
  • Permanent Redirection mechanism
  • Improvements in Code Expressions
  • Overview of Dynamic data
  • Caching mechanism (Output Caching, Object caching etc.)
  • ASP.NET Applications / Database deployment.
  • Q & A
  • Summary
Speakers: Abhishek Sur & Abhijit Jana

Scheduled Date: 19-Sep-2010 (Sunday )

Scheduled Time: 2.00 PM to 4.30 PM

Live meeting URL:  https://www.livemeeting.com/cc/mvp/

Meeting ID:  Will be sent to your email id before the session scheduled date and time.

Is Paid?: No, It’s FREE Session

Is Registration required ? Yes . Please register here http://bit.ly/d6aVYM .

Disclaimer : http://www.abhisheksur.com/p/disclaimer.html

Monday, September 6, 2010

Use of Expression Trees in .NET for Lambda Decomposition

Expressions are the building block of any Lambda Expression. In C# 3.0, .NET framework has introduced a new technique to make use of Anonymous methods a better way. The "Little Gem" LINQ, uses Lambda expression extensively to invoke filter statements to IEnumerable objects and hence making the life for a programmer very easy. Simple lambda expressions like

x => x<5 

may come very handy as we do not need to define a method extensively for such a small purpose. C# also introduced a couple of Generic delegates like Func<...>, Action<...> etc. in Base Class Library which lets you to pass any type of argument to a method body. The one with Func are for methods which require something to return while ones with Action dont need to return anything.

To learn more about LINQ please have a quick pick on my article on LINQ and Lambda Expressions.But if you have already read about the basics of LINQ, it is not all that is needed. Lambda expressions are cool and extensive use of Anonymous methods are truly awesome, but basically when I think of using Lambda Expression in my own application, I always think of extending the Lambda expressions a bit further so that I could use it dynamically during runtime and later on compile them to produce the actual anonymous delegates. In this article I will put forward the story of Linq and Lambda Expressions further and introduce you with Expression Trees and also show you to create Expression Trees dynamically during runtime.

What is an Expression Tree?

Simply speaking, an expression tree is nothing but the representation of a Lambda Expression in terms of .NET objects. The Expression is the main class that holds any expression in it and it lets us to divide the whole body of the Expression into smaller individual units. For instance : 

Func<int, bool> mydelegate = x => x < 5;
Expression<Func<int, bool>> myexpressiondelegate = x => x < 5;

In the above code the delegate Func can take the expression x=>x<5 which represents an anonymous method that takes an integer argument and returns if the value is less than 5.
Download Sample - 38 KB


Saturday, September 4, 2010

Notion of System.Nullable - A Look

I love C# as a language. It has lots of flexibilities and hooks to turn around things that was impossible for other languages. One of such thing is the introduction of Nullable Types. In this post,  I will discuss how you could use Nullable to make sure your code works well for null values in Value Types.

Why System.Nullable?

You must already know, System.Nullable is the only structure that supports the storage of Null as a valid range of values. Generally in case of structures it cannot store null values in it. Say for instance :

Int32 is a structure. When you specify
int x;

It will automatically assign 0 to x. This is same as
int x = default(int);

Generally structures always recursively call default to initialize the value of it. Say for instance :

struct MyCustomStructure
{
     public int m;
     public int n;
 }

I have defined a custom structure MyCustomStructure. If you declare an object of it like :
MyCustomStructure myobj;

It will assign myobj to its default. You cannot assign null to myobj. The only way out to this situation is Nullable.

MyCustomStructure myobj = null; //throws exception
Nullable<MyCustomStructure> myobj = null; //works fine

Hence you can see, you should always use Nullable for a structure if you want to store the value of null to a structure. For data centric applications, we generally need to keep a variable out of the scope of valid values so that the variable can have a value which might identify the value as nothing. Null is taken as nothing, but in case of normal structures it does not allow us to do that. If you store nothing to an integer, it will automatically initialize to 0. But for any application 0 might be a valid value. So you cannot exclude the 0 from the range of data. Hence you need Nullable to workaround this issue.

Sunday, August 29, 2010

Implementation of an Observer

Hi Folks,

As few people told me to implement an observer in my last post where I just showed how to use it, here is the post where I am going to clear out confusions for you. If you have read my other post, you might already know what is an Observer and why it is required. Lets recap this a bit more.

An observer is a container which observes each element individually and notifies you when the object state is modified. The observer should contain methods that enables you to subscribe or unsubscribe individually so that when you subscribe for a  notification, it will keep on creating notification until you explicitly unsubscribe the Observer.

In .NET base class library, there are two interfaces introduced viz, IObservable and IObserver. These interfaces gives you a standard to develop Observable pattern and also recommends you to use it rather than doing it of your own. Microsoft also builds forth its Reactive Framework (I will discuss about it later) based on Observer pattern and lets us use it when Observer is required. 

In this post, I will discuss how you could use IObserver and IObservable to implement you own notifiers.


Download Sample - 33KB

Wednesday, August 25, 2010

Working with CollectionView in WPF(Filter,Sort, Group, Navigate)

If you are working with WPF for long, you might already have come across with ICollectionView. It is the primary Data object for any WPF list controls (like ComboBox, ListBox, ListView etc) that allows flexibilities like Sorting, Filtering, Grouping, Current Record Management etc. Thus it ensures that all the related information like filtering, sorting etc is decoupled from the actual control.  It is been very popular to those working with data object because of inbuilt support for all WPF List controls. So I thought I would consider to describe it a bit so that people might easily plug in the same to their own solution.


What is a CollectionView ? 

It is a layer that runs over the Data Objects which allows you to define rules for Sorting, Filtering, Grouping etc and manipulate the display of data rather than modifying the actual data objects. Therefore in other words, a CollectionView is a class which takes care of the View totally and giving us the capability to handle certain features incorporated within it.

How to Get a CollectionView ?

Practically speaking, getting a CollectionView from an Enumerable the most easiest thing I ever seen in WPF. Just you need to pass the Enumerable to CollectionViewSource.GetDefaultView. Thus rather than defining

this.ListboxControl.ItemsSource = this.Source;

you need to write :

this.ListboxControl.ItemsSource = CollectionViewSource.GetDefaultView(this.Source);

The List will get the CollectionView it requires.



So the CollectionView actually separates the View object List Control with the actual DataSource and hence gives an interface to manipulate the data before reflecting to the View objects. Now let us look how to implement the basic features for the ICollectionView.

Download Sample - 74KB

Tuesday, August 24, 2010

IObserver and IObservable - A New addition to BCL

With the introduction of new interfaces, it is time to get it on with discussion. With the current release of VS2010, there were two interfaces that were introduced viz, IObservable and IObserver. Here in the post, I am going to discuss about these interfaces and its connection to Push based approach on Reactive Framework.

IObserver and IObservable as a Dual to Enumerables

First, it should be noted, IObserver and IObservable is actually the mathematical dual of IEnumerable and IEnumerator. Based on iterator pattern, IEnumerable is actually a repository of elements that made up the objects. The IEnumerable holds all the objects and it uses IEnumerator to get each individual objects from the repository. The few methods in IEnumerator which the IEnumerable uses is MoveNext and Current. So for each iteration, the Enumerator calls MoveNext and assigns it to Current which is later on sent back to the external environment.

So if you consider the interface IEnumerable and IEnumerator it looks like :

public interface IEnumerator<out T> : IDisposable
{
          T Current { get; }
          bool MoveNext();
          void Reset();
 }

public interface IEnumerable<out T> : IEnumerable
{
     IEnumerator<T> GetEnumerator();
}

So the IEnumerator has MoveNext which is called every when we need to yield next element from the store. The MoveNext sets the Current item and sends it back to the Environment. So IEnumerable might be considered as Pull based approach and it is used for sequential retrieval of objects.

IObservable and IObserver introduced to BCL recently as stated is mathematical dual of IEnumerable and IEnumerator. Lets see the interfaces a bit :

public interface IObserver<in T>
{
    void OnCompleted();
    void OnError(Exception error);
    void OnNext(T value);
}

and for IObservable it is :
public interface IObservable<out T>
{
      IDisposable Subscribe(IObserver<T> observer);
}


Hence, if you see the difference between the two Interfaces, IEnuerator has Current and MoveNext. These methods are used to Pull objects from the repository. IObserver has OnNext which is used to Push objects to the repository. Again, if you look into IEnumerable, it uses GetEnumerator to pull back the object of IEnumerable, while IObservable has a Subscribe method which is used to push an Observer to the Observable. Hence you can easily say, Observable interfaces in BCL is a dual to Enumerables where the Former uses Push based approach and the later uses pull based approach.

Friday, August 20, 2010

How to Render Bitmap or to Print a Visual in WPF

Working with WPF never become so easy if there was no inbuilt capability to take snaps or print a visual object. Microsoft made a very constructive class hierarchy to add functionality in each of the classes and hence building the solid foundation of WPF presentation model. Every element that we place in WPF is  inherited from a Visual. BCL added inbuilt functionality for a visual to render itself as Bitmapsource or to print the same Visual directly to the printer. In this post I will discuss how easily you could render a Visual as BitmapSource and later use it as source of an Image control.

Using Sample Application

RenderTargetBitmap is a class that is basically used to render a Visual to a bitmap object. Lets demonstrate this using an InkCanvas. You might already know, there is a special canvas element called InkCanvas which lets the user draw image on the screen. So before we proceed with the code lets discuss the sample application a bit.

In the above snap you can see that I have placed one InkCanvas which allows you to write dynamically on the screen. The buttons will Render the bitmap image from the Canvas and add the item on the ListBox on the Right. You can see after I click on Render as Bitmap button, it actually places the same visual as BitmapSource on the Right hand side ListBox.






Similar to what we worked on, I changed the content a bit and took another shot, and it exactly does the same.
Finally when I click on Render the Grid as Bitmap, it actually renders the whole Grid including the listbox and all as Grid. There are few buttons more, like Save Selection as JPEG / Print as Visual, each of them have its own functionality.

Download the Sample - 60KB


Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!