Friday, April 25, 2008

Prabath Siriwardena on Open Id

When I joined in has as a software engineer, one of my first mentors was Prabath Siriwardena. Although it was just 5-6 months in his team, I learnt a lot from him, ranging from writing good code to, team work. He recently acquired his MSc in computer Science from the University of Moratuwa. and today works at WSO2 in the open id integration team. You can listen to his pod cast on Open id here, in this talk he mentions about what open id tries to solve and it's advantages.

Tuesday, April 22, 2008

Initializer Running in The Opposite Order as Constructors

What would be the output of this C# program be?

class Program
{
static void Main(string[] args)
{
new Derived();
}
}

public class Base
{
A ob = new A("Initialized base A");
public Base() { Console.WriteLine("In base constructor");}
}

public class Derived : Base
{
A ob = new A("Initialized derived A");
public Derived() { Console.WriteLine("In derived constructor");}
}

public class A
{
public A(string str) { Console.WriteLine(str);}
}

if your answer was :
Initialized base A
In base constructor
Initialized derived A
In derived constructor
Then you are wrong !!!, actually the answer is

Initialized derived A
Initialized base A
In base constructor
In derived constructor
Weird right?, the order is reversed, the initializers are called first in the order derived then base, then
the constructors get called from base to derived.

Imagine that one day you thought of putting a abstract method in the class Base and which you implement in the Derived class , but the
implimentaion of the abstract method in Derived accesses a instance variable in the Derived class.
In this instance if the initializers ran in the order of the constructors , you will end up with a null point exception.

although this looks reasonable, it also means the you can call a method in the Derived class from the Base class even without calling
the Derived constructor...how weired is that.

Friday, April 18, 2008

Unity Explained

I had some time in the night to make use of the MS Unity applcation block, and I thought of writting a post on an example written uing Unity.
To use the dependency injection in your code via Unity is just 2 steps.
1) Create an instance of the Unity container, like this.
//Import UnityContainer container = new UnityContainer();
UnityContainer container = new UnityContainer();


2) Register your type with the Unity container. This can be done through a configuration file or thorugh code, for our example we are using the latter approach.
container.RegisterType <IService, ServiceImpl>();

3) Get the required instance from the container, using the Resolve()
DisplayClass display = container.Resolve<DisplayClass>();

The complete code example is as follows :

using System;

using Microsoft.Practices.Unity;
using Microsoft.Practices.ObjectBuilder2;
namespace unity1

{
class Program
{
static void Main(string[] args)
{
//Create the container
UnityContainer container = new UnityContainer();

//Register the our type
container.RegisterType <IService, ServiceImpl>();

//Get an instance of our type NOTE : an instance of ServiceImpl
//will in be constructor injected into the DisplayClass when it is created

DisplayClass display = container.Resolve<DisplayClass>();

display.Show();
}
}

public interface IService
{
void Show();
}

public class ServiceImpl : IService
{
public void Show()
{
Console.WriteLine("In ServieImpl Show()");
}
}

public class DisplayClass
{

private IService srv;
public DisplayClass(IService srv)
{
this.srv = srv;
}

public void Show()
{
srv.Show();
}
}
}

In this example you can see that the ServiceImple class impliments the IService interface and we register this type with the container in our
main method, also you can see that the Display class's constructor takes an instance of IService.
In our main method we create an instance out of the DisplayClass using the Resolve method of the container and whats nice is that an instance of IService is created by the container and used to instansiate the DisplayClass.

Sunday, April 13, 2008

Dependency injection and MS Unity Application Block

Building flexible and maintainable application means to achieve decoupled or very loosely coupled design.
Loosely coupled application also mean that you can test them easily using mocks.
Two ways of achieving loosely coupled nature in an application is via techniques Dependency injection and service locator.
When we talk about dependency injection we are talking about plug-in based development and it's all about how to get a concrete implementation into your application without re-compiling your code..
You can read about dependency injection here, but this post is not about what dependency injection is but about frameworks that lets us do this in a productive manner.
Frameworks like Spring uses dependency injection techniques and there are other containers available.
What I am interested about is the container that Microsoft released last week, the Unity Application block.
This is a container that allows you use dependency injection in a productive manner in the .Net applications:


Unity tries to achieve the following vie dependency injection:
1) Provides simplified object creation.


2) Support of requirement abstraction, this allows you to specify dependencies at runtime or in a configuration file.

3) It increases flexibility by deferring component configuration to the container.
Unity is the new member to the MS pattern and practice, you can learn more about unity in it's official community site here...

Thursday, April 10, 2008

Exception Handling Best Practices

Thought of writing some thing on exception and how it should be handled. These are some of the practices to be followed when managing exceptions in code to give you some more performance.

1) Do not use exceptions to control application logic. Throwing an exception is expensive (building the stack trace, etc are expensive). For an example when searching for a student and when it does not exist, do not throw a StudentNotFoundException, as this is a common scenario, return a boolean to indicate this or else another way is to return an enumerated type to denote error.


2) Use validation code to avoid exceptions, examples like checking for null etc..can avoid unnecessarily generating exceptions.

3) Use a “finally “ block to ensure resources are released (everyone knows that by default.)

4) Re-throwing exceptions with the “throw” keyword is expensive this is same as throwing a new exception.

5) Don’t catch exception that you can’t handle. The best way is to let it propagate up the call stack so a most suitable user can (part of the code that is...) handle it.

6) Do not catch general exception, this leads hard to debug when it’s time to do so !.


Wednesday, April 9, 2008

Same Old Story...

Ok, here we go again..it's a new project, a new cubical and a code base that I need to start familiarizing...well that adds up to, I don’t get time to blog or at least read something new, ye the same old story..
After much consideration I applied for an ADSL line at home, my first thought was not too, as the weekends are allocated for family, friends and long hours of sleep, but then again I could at least do something...so I went for it.
The funniest thing was that I applied through SLT last month and still my ADSL line was not activated. Finally I called the branch I applied the line through, to find out that my application never reached the ADSL division, great!!!.
I had something to say to SLT that day over the phone...and the operator called me back after 10 minutes to apologize and to say that she will submit it to the ADSL division right away. So hopefully I will be back to blogging in a couple of days.

Friday, April 4, 2008

The Whole New Environment...

I should have learnt from my lessons! Every time I move into a new project, it seems that everything goes wrong, why?

My development environment tends to work counter to my expectation.

I have noticed this in almost all the projects I have worked in, the reason being, the tools and other stuff specific to the previous project play almost against the environment needed for the current project. How to overcome this issue? Simple, take a backup of all your souvenirs in your machine and format. This seems to do the stuff as always and I think this should be the case, starting with a whole new environment for your new system (just my opinion).

Tuesday, April 1, 2008

IE 8 Beta

Here is the link where you can download Internet Explorer 8 beta., There are some new features that has been added, you can read more about it here.The only reason I installed IE 7 was due to it's "Tab" feature, but after some use I understood it was not like IE 6 but lot of hangs, crashes and was not much of a stable product. No offence but, it's my opinion.IE 8 beta was released for download, I think 2 weeks back, but I did not have much intrest but then again it does have some new features, maybe it will prove much better to me then IE7.