Monday, February 28, 2011

15 Tips from Daily .NET Tips

Hi Friends,

Daily .NET Tips is aiming to sharing useful coding tips and tricks for .NET Developers. This site completely design for sharing Tips and Tricks, useful Code Snippet which anyone use in daily development work and targeted anything related with .NET. Lets put a few frequently interesting tips which we have already published over there.
  1. How to count number of active session for State Server Session Mode ?
  2. How to use Runtime Objects in Watch Window during debugging in Visual Studio ?
  3. How to calculate Session data size for SQL Server session mode?
  4. Know when your application pools recycling in IIS 7.5 – Log an entry in Event Viewer while recycling
  5. Workaround For Non Serializable Types
  6. How EnableViewStateMAC Makes ViewState Secure ?
  7. Different approaches to Casting
  8. How to Display “Yes” or “No” Instead of Checkbox while binding Boolean value with GridView ?
  9. Changing Variables Display Format in Watch Window
  10. Getting Added And Deleted Items In A List Using LINQ
  11. How to setup multiple startup projects in Visual Studio ?
  12. Displaying Custom Messages / Images with NULL Value in ASP.NET GridView
  13. ASP.NET Validation Control with Images Notification
  14. How to expand specific Tree View Node programmatically in ASP.NET ?
  15. Display custom messages or images when there is no records in GridView Data Source
For more tips please visit http://dailydotnettips.com and follow @DailyDotNetTips at Twitter

You can subscribe to feeds on the site, and for your information, its absolutely free.


Thanks !

Sunday, February 27, 2011

Dependency Injection & IOC

Well, after I introduced Inversion of Control with few simple examples in my previous post, I  thought it would be nice to take this discussion further with more implementation of Dependency Injection Principles. Later I would also take a look at some of the existing frameworks available with MS Patterns and Practices which follow these well known principles. If you remember my previous post on Inversion of control with interfaces, I have clearly stated that we could invert the control to some other module to make strong decoupling on one class with the other. In this post I will first cover the basics of the design principle on Dependency Injection and later talk about DI containers.

Dependency Injection

If you know about traditional Design Patterns, you should already know few of the most used pattern called Factory. Factory pattern allows you to put your object creation into a special module called Factory. Thus your objects will be created just inside your factory classes. But does it worth to put all the object creation inside your factory classes ? That means each of those classes will hold reference of all the objects that needed to be created. Lets see the code to show up what I am talking about :


public enum ProcessorType
{
    x86,
    x64
}
public class Computer
{
    public IntelProcessor GetProcessor(double speed, ProcessorType type, string version)
    {
        return new IntelProcessor { Speed = speed, Type = type, Version = version };
    }
}

public class IntelProcessor
{
    public string Version { get; set; }
    public ProcessorType Type { get; set; }

    public double Speed { get; set; }

    public string GetProcessorInfo()
    {
        return string.Format("{0} Ghz Processor for {1}, v{2}", this.Speed, this.Type, this.Version);
    }

    public override string ToString()
    {
        return this.GetProcessorInfo();
    }
}

Sunday, February 20, 2011

Inversion of Control : Practical usage of Interface, Delegate or Events

Inversion of control is an architectural design principle which modifies the general flow of control of a program. You can say a program is a sequence of instructions that are running in a predefined sequence. Hence the sequence of execution of the program representing a sub-routine is defined when the class is actually implemented. Therefore, when the object is produced, it would have a sequence of instruction through which the control moves when it is called to execute. Inversion of control is a special circumstance where you will invert the predefined control flow to some arbitrary call defined way down the stream by the user who actually using the component. In this post, I will show you how you can implement inversion of control in your code and also approaches which help you while implementing your own class.

You can generally implement inversion of control in C# using 3 approaches :

  1. Interface
  2. Delegate
  3. Event

Here I will implement each of them with sample code.

Download Sample Code : 39KB

Sunday, February 13, 2011

Code Contracts in .NET 4.0 & its Internals

After I have introduced you the Internals of Exception handling, I have mentioned that I will cover some of the features that is coming as an improvement to it. Code Contract in .NET 4.0 is one among them. In this post, I will cover how you could use code contract in your application and also take the way through to its internals later on.


One can argue with me, that code contract is a part of unit testing and might be included as a part of.NET test projects, but according to me, it is not restricted only for testing frameworks, you could use it more likely for your own libraries so that you could have better code handling and also some way of separation between the validation logic for which the library is built in and how you would react with the outside world. Now let us discuss, how to use it in your program, and later on I will build an application to use it.

Sunday, February 6, 2011

Internals of Anonymous Types

.NET comes with lots of new features as it introduces new features. Many of them are quite useful while others might be very specific to an issue. One of the features that came up recently with the introduction of .NET 3.5 in C# is anonymous method. Anonymous means an object with no name. Before .Net 3.5 we had concept of anonymous delegates in C#. If you read about internals of delegate, you must already know how anonymous delegates are declared to CLR. In fact, there is no concept of anonymous in MSIL. Hence everything that you see as anonymous is actually a level of abstraction to us, and lets us to avoid the complexity to maintain unnecessary types to our program. In this post, I will discuss about anonymous type in terms of MSIL.


The Basics

Anonymous types are those types which are not declared before they are used. Say while you are doing your program, you want a temporary storage of your data, what you need to do, you need either declare a concrete class for the storage of the same, or you can use any collection like ArrayList, HashTable etc to store the key value collection. C# 3.5 and onwards allows you to dynamically create a type and use it directly in your program. You can use ‘var’ or implicit type declaration to ensure you could use the properties just like normal types.

var myruntimeObject = new { FirstProperty = 10, SecondProperty = new DateTime(), ThirdProperty = "string type" };

Console.WriteLine("Type of myruntimeObject is {0}", myruntimeObject.GetType().Name);
Console.WriteLine("Type of FirstProperty is {0}",       myruntimeObject.FirstProperty.GetType().Name);
Console.WriteLine("Type of SecondProperty is {0}", myruntimeObject.SecondProperty.GetType().Name);
Console.WriteLine("Type of ThirdProperty is {0}",        myruntimeObject.ThirdProperty.GetType().Name);

Console.Read();

In the above example I have created one anonymous type using var. The var will give you a chance to get intellesense in your code. You can see the variable holds an object of an anonymous type generated runtime. The power of var in code can also be tested. Just hover over the var keyword and you will see that the type is defined as anonymous.

Saturday, February 5, 2011

Get Reflected use TypeDescriptor

Hi there. Reflection is one of the major library that runs over the CLR which lets you get information about a CLR type or an object during runtime. A large number of application which is built today is taking advantage of Reflection to make their 3rd party codes plugin to their application dynamically. The Reflection lets you explain objects and its behavior dynamically and without any static binding available to any of those objects.

 But if you just need to get information about the objects at runtime, Reflection APIs needs a hands experience and a lot of heck to do a job. A number of classes that is built over the Reflection APIs can be used to make your life easier while you code. One of the few libraries that are available with you that I must address is the classes within ComponentModel namespace. In this post I will give you a sample demonstration of how you could use Descriptor types to get information about Properties, Attributes, Events etc without invoking a single line of Reflection calls. I hope you could use the code later while building your library.


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 !!!