Saturday, October 2, 2010

Deploying a Single EXE for the Application

Many applications consist of an EXE file that depends on many DLL files. When we deploy the application, all the files must be deployed on the client machine. If you want to deploy just a single EXE file instead of all depended DLL files, there is a way to achieve this. All the dependent DLLs can be embedded into the EXE file and then whole application can be deployed with a single EXE file.

For this, first you need to identify all the dependent DLLs which are not shipped with .Net framework. Add all the identified DLLs into your project and for each DLL file you add, open the properties page and change the its “Build Action” to “Embedded Resource”. This will instruct compiler to embed the DLLs into your EXE file, and you can deploy this one EXE file.

Saturday, April 17, 2010

Structural Patterns - Adapter Pattern

Design Patterns help to make a system independent of how its objects are created, composed, and represented. These patterns become important when emphasis shifts away from
Hard-Coding a fixed set of behaviours toward defining a
smaller set of fundamental behaviours that can be composed in to any number of more complex ones. [GOF]

Pattern which defines the composition of classes and objects in the system to build the desired structure is known as Structural Pattern. Structural patterns define a way to combine two or more independent developed libraries. Developed libraries may differ in their implementation but can work for each other to produce the desired result without knowing the internal details.

For example, a particular Car model can make use of the cooling system to provide A/C feature though the cooling system is developed independent of the Car model.

One example of the structural pattern is Adapter pattern. Adapter pattern promotes the development of one interface to a particular implementation in order to provide uniform abstraction of different interfaces. Adapter pattern is particularly useful when you have two different classes with incompatible interfaces.

There are 3 entities to participate in the Adapter pattern. If a class wants to make use of an implementation, then these classes can be named as Target and Adaptee, respectively. The wrapper which provides the requested operations by the target using the implementation of the Adaptee is called Adapter. Below is the interaction diagram between Target, Adapter and Adaptee.


Implementation

Let’s take the example of Stack implementation.

Suppose Stack interface is exposed to the client to support stack data structure and operations. Now, there is a need to implement the Generic stack data structure. One possible approach is to use generic dictionary to implement generic stack. Generic dictionary is the class implemented in library to support generic and retrieval and storage of the items.

We can classify stack interface exposed to the client as Target and Dictionary class as an Adaptee. Create a wrapper class (GenericStack) which takes the responsibility to implement the stack operations using dictionary class. One of the possible implementation will follow the structure shown in the below image.


Code:

Stack Interface



internal


interface


IStack


where
T :

IComparable



{

///

/// To Push Item in the Stack

///

void Push(T item);

///

/// Pop up the given item from the Stack

///

T Pop();


/// To Pop up the item from the Stack

T[] Pop(T item);

}

}



Generic Stack


/// This class defines Generic Stack.

class

GenericStack
:

IStack

where T :

IComparable

{

#region Fields


private

Dictionary
<int, T> dicStackItemCollection;



private


Dictionary
<int,

int
> dicItemKeyCollection;



int
iItemcount;



#endregion
Fields



#region
Properties



#endregion
Properties



#region
Methods



public
GenericStack()



{



iItemcount = 0;



}



///



///
To Push Item in the Stack



///



///



public


void
Push(T item)



{



// Initialize Stack Item Collection - Deferred initialization



if
(dicStackItemCollection ==

null
)



{



dicStackItemCollection =

new


Dictionary
<int, T>();



dicItemKeyCollection =

new


Dictionary
<int,

int
>();



}



// Do Not Add Null items



if
(item !=

null
)



{



dicStackItemCollection[iItemcount++] = item;



dicItemKeyCollection[item.GetHashCode()] = iItemcount;

}

}

///



///
To Pop up the item from the Stack

public T Pop()



{



// Set Item to Its default value



T item =

default
(T);



// If Item exists, pop



if
(iItemcount > 0)



{



item = dicStackItemCollection[iItemcount - 1];



dicStackItemCollection.Remove(iItemcount - 1);



iItemcount--;



}



return
item;



}



///



///
Pop up the given item from the Stack



///
Returns all removed items from the stack.



///



///



///



public
T[] Pop(T item)



{



T[] items =

null
;



int
itemKey;



// If Item exists, pop



if
(iItemcount > 0 && IsItemExists(item,

out
itemKey))



{



items =

new
T[itemKey - 1];



while
(itemKey > 0)



{



// Get the Item from the Collection before removing it



items[itemKey - 1] = dicStackItemCollection[itemKey];



// Remove item from the Collection



dicStackItemCollection.Remove(itemKey--);



}



iItemcount = dicStackItemCollection.Count + 1;



}



return
items;



}



///



///
To Get the Item from the collection



///



private


bool
IsItemExists(T item,

out


int
itemKey)



{



itemKey = -1;



if
(dicItemKeyCollection.ContainsKey(item.GetHashCode()))



{



itemKey = dicItemKeyCollection[item.GetHashCode()];



}



return
itemKey != -1;



}

#endregion Methods

}



Client:



class


Client

{

#region Fields

private

IStack
<int> stack;

#endregion Fields

#region Properties

#endregion Properties

#region Methods

public Client(IStack<int> stack)

{

this.stack = stack;

}

///

/// To Simulate Operations on Stack

///

internal

void
DoSomeOperartions()

{

int item;

Console.WriteLine("Push: 5");

stack.Push(5);

Console.WriteLine("Push: 4");

stack.Push(4);

item = stack.Pop();

Console.WriteLine("Pop: " + item);

Console.WriteLine("Push: 3");

stack.Push(3);

item = stack.Pop();

Console.WriteLine("Pop: " + item);

item = stack.Pop();

Console.WriteLine("Pop: " + item);

item = stack.Pop();

Console.WriteLine("Pop: " + item);

}

#endregion Methods

}



Main Method:


class

Program

{

static

void
Main(string[] args)

{

IStack<int> stack =


new


GenericStack
<int>();


// Set Stack to the Client



Client
<int> client =

new


Client
<int>(stack);



// Ask client to do some operations



client.DoSomeOperartions();



Console
.ReadLine();

}

}


** Please note that above implementation relies on the Hash code of the object in the stack. Results may be unpredictable if GetHashCode() method has been overridden in object’s type.

Monday, April 5, 2010

Understanding Pointers with inner wears :)

It was the summer of 2007 when I was waiting for the Joining dates of Infosys Technologies (I waited till winterJ). During those days, I used to visit some C language communities on Orkut. One day I saw a very interesting discussion about the pointers in C.

One guy asked a question in the C community:

Why can't an integer pointer point to a floating number????

Immediately after this, there was another question commeting to the above question:

Why can't boys wear bra and panties????

Some supported this comment and some gave nice replies to the actual question. Here is what I replied to the post….

-------------------------------------------------------------------------------------------------------------------

“Any pointer type always points to a single byte.... and that byte is always the starting byte of the variable...
if a variable is a pointer, it will store the address of the first byte of the variable only.

So, you can store the address of the any type of the variable to the any type of the pointer.......
now the question arises... THEN WY DIFFERENT TYPES Of POINTERS......

By making a type of pointer ... it tells the compiler that how many bytes a processor will process wen it will encounter the pointer.
In dos environment...
If integer pointer..... will process two bytes simultaneously.
If float pointer..... Will process 4 bytes.

And it also tells to the compiler that on increment..... Which byte will be the next address.
i.e. if float pointer is ptrf, then ptrf++ will jump on the 5th next byte.

----------------------------------------------
so...... store anyone, anywhere... But don’t put your expectations.......
in a nutshell......
"boys can wear panties and bra..... But don’t expect them to behave like a girl"

-------------------------------------------------------------------------------------------------------------------

Though the discussion was really good but what I remember even today is the last line of my replyJ. And so the subject line of the blog.