Sunday, February 15, 2009

Implementing Strategy Design Pattern...

Implementing the strategy pattern helps in decoupling the application.The main idea behind you can interchange the different implementation of your algorithm without breaking the code.

What I mean by that is .. I have an interface, IGetData which defines a single method which returns a Customer object.

public interface IGetData
{
Customer GetData();
}

This interface is implemented by two different classes...SqlDataSource which gets the customer object from the SqlServer Database, while XmlDataSource gets the Customer object from the XmlDatasource.
The data class encapsulates the interface[IGetData]... in the Constructor of the Data class. you can pass in different implementations of IGetData. i.e you can pass any class which implements the IGetData interface.
By doing this .. you can have some factory method which will return the appropriate class which implements the IGetData interface.

PS: Click on the image to enlarge.

You can decouple more by using either factory or abstract factory pattern.
SourceCode download ---> http://dotnetdevblog.googlepages.com/StrategyPattern.zip

Happy Programming!!!
Yash

Accessing Embedded Resources in an assembly....

Recently I was working on a project where..I had to embed some Config.Xml files in an assembly. That assembly was later on added to the main project.
Accessing the resource:
Step1: Add the resource and make the "BUILD Action" as embedded resource in Visual Studio.

PS: Click on the image, to View the Code..
Step2: Use Reflector to get the resource Name.
Step3: Load the assembly .. and use the method "GetManifestResouceStream" to access the resource..

Assembly assembly = Assembly.Load("MyLib");
Stream st = assembly.GetManifestResourceStream("MyLib.Config.Config.xml");
Its a nice way to access embedded resources from the build..
Happy Programming!!!
Yash