Pages

I've migrated my blog

Thanks for visiting my blog. I am no longer maintaining this and I've migrated the blog to my personal domain . If you would like to follow my blog kindly use my new RSS feed

Sunday, April 24, 2011

Dynamic Polymorphism - A Real time example

In this blog post we are going to see, how to achieve dynamic polymorphism in c# using interfaces through a small game simulator called "War".

 The simulator is simple and straight forward to play. The user just need to select a weapon and click on "Attack".
Let me explain some theoretical background about "Dynamic Polymorphism" before dive into implementing the Game simulator. To put in a simple terms, Dynamic Polymorphism means changing the behavior at run time. Let us see an example of what does it mean. If a dog smells a cat, it will bark and if it smells a food, it will salivate. Here, the smelling sense of the dog is same. But depending on what does it smell, it change its behavior. Hope now you are aware of what is mean by dynamic polymorphism. Just stop reading and think what is the dynamic polymorphic behavior here in the "War" simulator.
.
.
.
.
.
.
.
Yes, you are absolutely correct!! The Attacking behavior is the dynamic polymorphic behavior. The behavior of attack button will vary with respect to the weapon the user has chosen. 

Fine.. Lets dive into the code!!

Step1: Design the Main Form
  1. Create a new Windows Forms Application and name it as "War"
  2. Rename the "Form1.cs" file to "MainForm.cs" and Click "Yes" to let the Visual Studio to do the renaming for you.
  3. Add a groupbox control and change its "Text" Property to "Weapon"
  4. Add three radio button controls inside the groupbox and change their "Text" Property to Sword, Gun and bomb respectively.
  5. Add a button control and change its "Text" property to "Attack!!".
  6. Rename the "Name " property of all the controls added to some meaningful names like rboSword for Sword Radiobutton
  7. The Final Finished will look like the screenshot in the beginning of this blog.  
 Step 2: Create an Interface called IWeapon
  1. Right click on the Project name "War" in the solution explorer and add a new interface file called "IWeapon.cs"
  2. This interface IWeapon will have a public method called "Attack" which defines a contract so that all the Weapon we are going to create should implement this IWeapon interface and provide the implementation code for the Attack() method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace War
{
    interface IWeapon
    {
        void Attack();
    }
}

Step3: Create the concrete Weapon Classes 
  1. Add three class files to the "War" Project with the names "Sword.cs", ""Gun.cs", "Bomb.cs"
  2. These classes represents the actual weapons and that we are going use in our simulator. Each of these classes will implement the IWeapon interface and provide the implementation code for the "Attack()" method.
  3. Modify the class files as follows
Sword.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace War
{
    class Sword : IWeapon
    {

        public void Attack()
        {
            System.Windows.Forms.MessageBox.Show("Sword!!");
        }
    }
}

Gun.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace War
{
    class Gun : IWeapon
    {

        public void Attack()
        {
            System.Windows.Forms.MessageBox.Show("Gun!!");
        }
    }
}

Bomb.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace War
{
    class Bomb : IWeapon
    {

        public void Attack()
        {
            System.Windows.Forms.MessageBox.Show("Bomb!!");
        }
    }
}
The Attack() method will show a message box with a text saying the weapon name being used.

Step 4: Program the "War" Simulator Main Form
Now the stage is set for the real show. Let us wire the code of MainForm.cs and make the "War" simulator.
  1. Add a private instance variable of type IWeapon in the MainForm class and initialize with the sword class. This will hold the reference of current weapon being used Note: Sword is the default weapon.
    public partial class MainForm : Form
        {
            private IWeapon weapon = new Sword();
    
  2. Now we are going to add event handlers for the weapons radiobuttons. They will change the reference stored in the instance variable "weapon" to the respective Weapons class. Add the checked change event for all the radio button and their event handlers will look like as follows.
    private void rboSword_CheckedChanged(object sender, EventArgs e)
        {
            weapon = new Sword();
        }
    
        private void rboGun_CheckedChanged(object sender, EventArgs e)
        {
            weapon = new Gun();
        }
    
        private void rboBomb_CheckedChanged(object sender, EventArgs e)
        {
            weapon = new Bomb();
        }
    
  3. Add the Click event handler for the "Attack" button. This event handler will simply call the Attack() method of the instance variable "weapon"
    private void btnAttack_Click(object sender, EventArgs e)
     {
         weapon.Attack();
     }
    
Summary
In this blog we have seen a basic real time example on implementing dynamic polymorphism. You can download the source code of this sample from here.

Friday, April 1, 2011

Entity Framework 4.0 As Class Library - Part 2

As the continuation of my previous blog post, In this blog post we are going to explore on how to consume the data access class library created in the previous blog post.

Consuming the Data Access Library created using Entity Framework 4.0 involves the following two steps.
  1. Adding reference to the class library
  2. Adding the connection string in the config file (App.config or Web.config)

Let us see how can we do these steps using a console application. To keep things simple I have opt for a console application. It holds the same for an ASP.NET , Windows Form, WPF,WCF, etc.

Console Application Creation

Create a new console application called "HrdConsoleApp"



Add a reference to the Class library

Right click on references and refer the class library "HRD.DataAccess" that we have created in the last blog.


Add reference to System.Data.Entity Library



Add the App.Config file to the console application by right clicking on the project name in the solution explorer and select "Add->New Item"




Copy the connection string from the App.Config file created in the HrdDataAccess Class library Project created earlier and paste it in the App.config file created in the previous step.



Thats all now it is all set to access the database with only minimal amount of code.. Here we go!!

Implementation Code:
using System;
using System.Linq;
using HRD.DataAccess;

namespace HrdConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            HRDEntities hrdEntities = new HRDEntities();
            foreach (Employee employee in hrdEntities.Employees.ToList())
            {
                Console.WriteLine("Name: " + employee.Name);
                Console.WriteLine("Department: " + employee.Department.Name);
                Console.WriteLine("########################");
            }
        }
    }
}

Output




Summary:

In this blog series (Part 1 and Part 2) we have seen how to create the data access layer using Entity Framework 4.0 as class library and how to consume it in an application. With the introduction of Entity Framework developing the code for data access layer is no longer a tidy and time consuming job!!