Thursday 23 September 2010

Tip of the day – A good use of auto implemented properties.

A very practical use of auto implemented properties could be to create a immutable lightweight class. An auto implemented property is a ‘property’ in C# without fields. The C# JIT Compiler generates the IL with a field for the auto implemented properties. (hence the name). The IL also replaces the Get and Set accessor stubs in the Properties to return and set the automatically generated field.

Take a look at the below code ( class ‘Books’). There are Title, Author and PublshedYear auto implemented properties. Notice the set is private, This makes the ‘Books’ class immutable.

autoprop

The tip is to use this kind of construct to replace structs when ever a reference type is required. Also notice that the constructor is made ‘private’. Simply because i find the factory method ‘CreateBook’  semantically appealing to call the private constructor rather than having to use the auto implemented property directly in the constructor for initializing the Books object.

Take a look at the IL for Books.,  Notice the ‘k__BackingField’ feild generated by the C# Compiler.

image

//////////////get_Author: string() IL////////////////////

.method public hidebysig specialname instance string
        get_Author() cil managed
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       11 (0xb)
  .maxstack  1
  .locals init (string V_0)
  IL_0000:  ldarg.0
  IL_0001:  ldfld      string ConsoleApplication1.Books::'<Author>k__BackingField'
  IL_0006:  stloc.0
  IL_0007:  br.s       IL_0009
  IL_0009:  ldloc.0
  IL_000a:  ret
} // end of method Books::get_Author

 

//////////////////set_Author:void(string) IL////////////////

.method private hidebysig specialname instance void
        set_Author(string 'value') cil managed
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       8 (0x8)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  stfld      string ConsoleApplication1.Books::'<Author>k__BackingField'
  IL_0007:  ret
} // end of method Books::set_Author

Now, a demo of using the above immutable lightweight class

democode

Happy patterns discovery..

I’ve used the MSDN example here to illustrate.

Sunday 19 September 2010

Design Patterns – Types

Design patterns are proven practises and principles for building enterprise software. They generalize solutions to common problems.  By adapting design patterns in our software architecture, we leverage the proven solutions and concepts. This makes the code easier to maintain / understand / relate to.

Design patterns are broadly classified into the following categories.

1] Structural Patterns

- Focus on building flexibility,maintainability and security

2] Creational Patterns

- Focus on increasing flexibility,i.e what , who, how and when of object creation.

3] Behavioural Patterns

- Focus on algorithms and communication between them.

I’ll start with the Structural Patterns in my next post.

Saturday 18 September 2010

My love for patterns and discovery of them..

It all started in my undergraduate days back in 2003 when i used to work a lot on VB & C++. I had no idea that patterns existed then!. One fine day a friend of mine suggested using a ‘Singleton’. Since than I've had the hunger for discovering more and more of ‘patterns’ and their usage.

After i moved to the managed world (.Net) it started appealing more and more and today, i cannot think of an application without it. I’ve moved on to application architecture and business processes all the way realizing how vital the role of patterns in real world development (civil engineering  to software engineering). Apart from all the other stuff that still interests me., I’ve decided to take a rather focussed approach to patterns, software design and application architecture from now. I look forward to a sound discussion with you all on them.

Thursday 16 September 2010

IE 9

is cool.. and i like the changes..

Saturday 11 September 2010

Android

Found a Blogger app for my HTC Desire..so,here I am, doing a test post from my phone..
Published with Blogger-droid v1.5.8

Tuesday 7 September 2010

Entity Framework 4.0 and ASP.Net 4.0

This is a very good example of real world EF and ASP.Net .
Published with Blogger-droid v1.5.8