31 July, 2012

Factory Design Pattern


Factory design pattern implements the concept of real world factories. Factory pattern is a creational design pattern. It deals with creating object without specifying exact class. In general, actors of factory patterns are a client, a factory and a product. Client is an object that requires another object for some purposes. Rather than creating the product instance directly, the client delegates this responsibility to the factory. The factory then creates a new instance of the product, passing it back to the client. Figure shows the whole process.

 
Application
For an example, a banking application works with accounts. In this application there are different types of account like saving account and checking account. All accounts are derived from an abstract account name IAccount. The IAccount defines the withdraw, deposit and interest rate which must be implemented by the concrete accounts (saving account, checking account). If clients want to know the interest rate of the Saving Account. It just invoke the factory to create an instance of Saving account.  Being invoked factory, it creates an instance of the saving account and then client just get the interest rate by invoking interest method. The client uses the object as casted to the abstract class without being aware of the concrete object type. Over all implementation of this scenario is given below.

The advantage is in here that new account can be added without changing a single line of code in the client code. Here an object is created without exposing instantiation logic to the client. The object generation is centralized here.

Benefits:
  • Factory Pattern is the mostly used pattern.
  •  Decoupled the classes i.e eliminates the need to bind application specific class
  • The code only deals with the Interface.
  •  Factory Pattern provides the way to create multiple instances of classes.
  •  This provides a hook so that we can derive a sub-class to create different controls to display the data.
  •   Factory method connects the class hierarchies with minimum coupling.
  •  Product implementation may change over time but client remains unchanged.

Implementation
Step 1: Create an Interface - IAccount
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    public interface IAccount
    {
        string Withdraw(int amount);
        string Deposit(int amount);

        double InterestRate();
    }
} 

Step 2: Create concrete classes

Saving account
 

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

namespace FactoryPattern
{
    /// 
    /// Concrete class SavingsAccount
    /// 
    public class SavingsAccount : IAccount
    {
        #region IAccount Members

        public string Withdraw(int amount)
        {
            throw new NotImplementedException();
        }

        public string Deposit(int amount)
        {
            throw new NotImplementedException();
        }

        public double InterestRate()
        {
            return 12.5;
        }

        #endregion
    }
}


Checking Account


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

namespace FactoryPattern
{

    /// 
    /// Concrete class CheckingAccount
    /// 
    public class CheckingAccount : IAccount
    {
        #region IAccount Members

        public string Withdraw(int amount)
        {
            throw new NotImplementedException();
        }

        public string Deposit(int amount)
        {
            throw new NotImplementedException();
        }

        public double InterestRate()
        {
            return 10.24;
        }

        #endregion
    }
} 


Step 3: Create a Factory Object Enum
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    /// 
    /// FactoryObject Enum to configure object
    /// 
    public enum FactoryObject
    {
        SavingAccount,
        CheckingAccount
    }
}

Step 4: Create a factory class

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

namespace FactoryPattern
{
    /// 
    /// Factory class to create object
    /// 
    public static class Factory
    {
        public static IAccount CreateObject(FactoryObject factoryObject)
        {
            IAccount objIAccount = null;

            switch (factoryObject)
            {
                case FactoryObject.SavingAccount:
                    objIAccount = new SavingsAccount();
                    break;

                case FactoryObject.CheckingAccount:
                    objIAccount = new CheckingAccount();
                    break;

                default:
                    break;
            }

            return objIAccount;
        }
    }
}


Step 5: Access from client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryPattern
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Create object by factory pattern
            IAccount objSavingAccount = Factory.CreateObject(FactoryObject.SavingAccount);
            IAccount objCheckingAccount = Factory.CreateObject(FactoryObject.CheckingAccount);

            //Access object
            Console.WriteLine("Saving Account Interest Rate: " + objSavingAccount.InterestRate());
            Console.WriteLine("Checking Account Interest Rate: " + objCheckingAccount.InterestRate());


            Console.ReadLine();
            
        }
    }
}

28 July, 2012

Singleton Design Pattern

Design Pattern
 Design pattern is a solution of known problems. These are strategies of solving commonly occurring problems. A design pattern is not a finish design. It is like a template to solve a problem.  

Singleton Design Pattern
Singleton is a software design pattern. It is restrict to create object more than once. This is actually needed when one object can perform its action in whole system. We frequently use a database connection object to connect with database. We don’t need multiple objects to create connection with database and close it. We can use singleton in this scenario.


Implementation of Singleton by C#

Step 1: Create EmployeeInfo class
This class is to hold employee information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    public class EmployeeInfo
    {
        public string EmpName { get; set; }
        public string EmpDesignation { get; set; }
        public int MonthlySalary { get; set; }
    }
}


Step 2: Create a singleton class
 Here EmployeeService is a singleton class. Constructor of this class is private so that nobody can create its instance from outside.  Instance() is a static method which creates instance of the singleton class. It actually forces that only one instance of the object will be created. Lock() is used to create instance of the singleton pattern in thread safe manner in multi threaded environment. The other two methods are used to add employee information in list and get employee salary. These two are as usual method. Don’t mix up Singleton class and Static class. Keep in mind; in static class everything must be static like Method, constructor, properties. But in singleton class it is not required. Hope, you will be clear after the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    /// 
    /// EmployeeService is a singleton class
    /// 
    public class EmployeeService
    {

        //Static object of singleton class
        private static EmployeeService instance;

        private List lstEmployeeInfo = null;

        /// 
        /// Restrict to create object of Singleton class
        /// 
        private EmployeeService()
        {
            if (lstEmployeeInfo == null)
            {
                lstEmployeeInfo = new List();
            }
        }


        /// 
        /// The static method to provide global access to the singleton object.
        /// 
        /// Singleton object of EmployeeService class
        public static EmployeeService Instance()
        {
            
            if (instance == null)
            {
                //Thread safe singleton

                lock (typeof(EmployeeService))
                {
                    instance = new EmployeeService();
                }
            }

            return instance;
        }


        /// 
        /// Add employee information to the Employee information list
        /// 
        /// 

        public void AddEmployeeInfo(EmployeeInfo objEmployeeInfo)
        {
            lstEmployeeInfo.Add(objEmployeeInfo);
        }


        /// 
        /// Get Salary by Name
        /// 
        /// 
        /// Salary of Employee
        
        public int GetEmployeeSalaryByName(string name)
        {
            int monthlySalary = 0;
            foreach (EmployeeInfo objEmployeeInfo in lstEmployeeInfo)
            {
                if (objEmployeeInfo.EmpName.Contains(name))
                    monthlySalary = objEmployeeInfo.MonthlySalary;
            }
            return monthlySalary;
        }

    }
}




Step 3: Access singleton class
This class creates an instance of singleton class by EmployeeService objEmployeeService = EmployeeService.Instance(); and access singleton class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            EmployeeInfo objEmpInfo1 = new EmployeeInfo() { EmpName = "Mahedee", EmpDesignation = "Senior Software Engineer", MonthlySalary = 00000 };
            EmployeeInfo objEmpInfo2 = new EmployeeInfo() { EmpName = "Kamal", EmpDesignation = "Software Engineer", MonthlySalary = 30000 };


            //Create a singleton object
            EmployeeService objEmployeeService = EmployeeService.Instance();
            

            objEmployeeService.AddEmployeeInfo(objEmpInfo1);
            objEmployeeService.AddEmployeeInfo(objEmpInfo2);

            Console.WriteLine(objEmpInfo2.EmpName + " : " + objEmployeeService.GetEmployeeSalaryByName("Kamal"));

            Console.ReadLine();

        }
    }
}

11 June, 2012

ASP.NET Membership Step by Step


ASP.NET provides build in control to validate and store user credential. So, membership helps to manage user authentication and authorization in website. The membership provider is the glue between Login controls and membership database.

Step 1: Implementing the Membership and Role Provider
Suppose we are going to create a website by Visual Studio 2010. If you create a website by visual studio 2010, you will get login and Registration pages by default. But we want to create it manually so that we can understand its functionality.

So, what should we do? Don’t worry. Let’s create an empty website. To create an empty website, go to Add → New Website → ASP.NET Empty Website.
In the empty website, we will get only an web.config file.

To store user information, we need to create required tables, sp and other information into the database in the SQL Server. To create membership tables and sps follow the following steps.

  1. Go to directory C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
  2. Click aspnet_regsql.exe. You will see the following screen.

Click next to continue


Select Configure SQL Server for application services and Press Next


Type your server name. Here my server name is SOFT. Select SQL Server Authentication, type user name and password of server. Then select Database Name. Click Next to continue. You will see the following screen.


Click Next to continue.


Click Finish. Now all tables and SPs are imported to desired database.

Step 2: Configuring Connection String: To retrieve membership information, create a connection string. This connection string can be used for retrieving other information from database for the website.

  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=SOFT;Initial Catalog=TourismDB; User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
  connectionStrings>


Step 3: Configuring Authentication: Authentication is the process by which system can identify who are you? ASP.NET framework supports 3 types of Authentication.

  • Windows Authentication
  • .NET Passport Authentication
  • Form Authentication.

We will configure form authentication here. When form authentication is enabled, users are typically identified by the cookies. There are several options for configuring form authentication. One of the options is cookiless which enables you to use form authentication even when the browser does not support cookies.

In this case, Membership handles all details’ of retrieving and storing user and role information. Add the following portion in web.config file to enable form authentication.

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" cookieless="AutoDetect">
      forms>
    authentication>
  system.web>


Step 4: Membership Provider Settings:
To enable membership, add the following code snippet to web.config file. There are many elements in membership provider. We will use only the important elements here. Basic elements are – name, type( name space of the provider), connectionStringName (name of connection string which is used to connect with membership database). You can use PasswordFormat either of the three – Hash, encrypted and clear. Default value of PasswordFormat attribute is Hashed. You can use maxInvalidPasswordAttempts – number of maximum attempts to enter invalid password,  minRequiredPasswordLength – minimum password length.

<system.web>

   <membership>
      <providers>
        <clear/>
       
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      providers>
    membership>
system.web>

Step 5: Add a Login Form: Add a login form name Login.aspx in the website. Now add a Login control in Login page. You can change the style of the login control according to your requirements. Elegant format of login control is used here. For the first time, there is no user available. So we must create some users. We used here CreateUserText="Register" CreateUserUrl="~/Register.aspx". This two properties are used here so that we can create a new user by going to Register.aspx page.

<asp:Login ID="loginUser" runat="server" CreateUserText="Register"
            CreateUserUrl="~/Register.aspx"
            onauthenticate="loginUser_Authenticate" BackColor="#F7F7DE"
            BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px"
            Font-Names="Verdana" Font-Size="10pt" Height="135px" Width="286px">
            <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
        asp:Login>


Add the following code snippet for handling corresponding onauthenticate event
 
protected void loginUser_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool isLogin = Membership.ValidateUser(loginUser.UserName, loginUser.Password);
        if (isLogin)
        {
            loginUser.Visible = true;
            Session["user"] = User.Identity.Name;
            FormsAuthentication.RedirectFromLoginPage(loginUser.UserName, true);
            Response.Redirect("Default.aspx");
        }
    }


After log in successfully. Page will be redirected to Default.aspx page. So we need to add a Default.aspx page. We will add it later.

Step 6: Add Register Form: In step 5 I have added property CreateUserUrl="~/Register.aspx" .But this page is not exists. So create a page Register.aspx. In this form we will use a CreateUserWizard which enable to display a user registration form. Like login control you can also change style of CreateUserWizard control. I used here simple format.

<asp:CreateUserWizard ID="RegisterUser" runat="server"
            OnCreatedUser="RegisterUser_CreatedUser" BackColor="#E3EAEB"
            BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px"
            Font-Names="Verdana" Font-Size="0.8em">


            <ContinueButtonStyle BackColor="White" BorderColor="#C5BBAF"
                BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                ForeColor="#1C5E55" />
            <CreateUserButtonStyle BackColor="White" BorderColor="#C5BBAF"
                BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                ForeColor="#1C5E55" />
            <TitleTextStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                asp:CreateUserWizardStep>
                <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                asp:CompleteWizardStep>
            WizardSteps>
            <HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid"
                BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White"
                HorizontalAlign="Center" />
            <NavigationButtonStyle BackColor="White" BorderColor="#C5BBAF"
                BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
                ForeColor="#1C5E55" />
            <SideBarButtonStyle ForeColor="White" />
            <SideBarStyle BackColor="#1C5E55" Font-Size="0.9em" VerticalAlign="Top" />
            <StepStyle BorderWidth="0px" />

        asp:CreateUserWizard>


Add the following code snippet for handling corresponding OnCreatedUser event.



     
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

        string continueUrl = RegisterUser.ContinueDestinationPageUrl;
        if (String.IsNullOrEmpty(continueUrl))
        {
            continueUrl = "Login.aspx";
        }
        Response.Redirect(continueUrl);
    }


Step 7: Add location tag for Register page: Add the following code snippet in web.config file. This means register page is allowed to all. Because, for the first time you have to create a user without login to the application.

  <location path="Register.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      authorization>
    system.web>
  location>


Step 8: Add Default Page: Add Default.aspx page. You can add here some message here because page will be redirected to the default page after successfully logged in.

Now run the application. You will see the following login screen.

 But you don’t have any user to login. So, click Register in login page and you will be redirected to Register page.


Now create a user and try to login by that user from login page. Yes! you have logged in and redirected to Default.aspx.





Step 9: Add Login Status: We always see a message “Welcome Mahedee!” after login and Login before login to the site. We see this login status in all pages. If user want to login from any pages, he just need to click in login link and go to login page.  Here he can login and enter the site as authentic user. Here we have created a master page and login status control to do exactly the same thing. Here I used LoginStatus and LoginView Control. It should be mentioned here what are the functions of LoginStatus and LoginView Control? LoginStatus control enables you to display a login or logout link depending on user authentication. It actually displays whether user is currently logged in or not. LoginView enables you to display different content depending on user Authentication status or role.

Add the following code snippet in MasterPage.master.

    <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
        <AnonymousTemplate>
            [ <a href="Login.aspx" id="HeadLoginStatus" runat="server">Log Ina> ]
        AnonymousTemplate>
        <LoggedInTemplate>
            Welcome <span class="bold">
                <asp:LoginName ID="HeadLoginName" runat="server" />
            span>! [
            <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out"
                LogoutPageUrl="~/" />
            ]
        LoggedInTemplate>
    asp:LoginView>


Now if you add MasterPage.master to any of your pages. You will get login status after login to the application. Here I used MasterPage in Default.aspx.

Step 10: Add Role Manager: To add role manager add the following code snippet to web.config file.

<roleManager enabled="true">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
      providers>
    roleManager>


Step 11: Create Role Manager Page: Create a page like below for managing roles.



Step 12: Add Role Provider in web.config: Add the following code snippet for web.config for enable Role Provider.

<roleManager enabled="true">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
      providers>
    roleManager>

Step 13: Manage Role: In step 11 we saw Role manager page. Now we will create the corresponding code for managing (Create, delete roles & assign users to role) roles. Here I also add a grid to display user role mapping.

  1. Create Roles: Add the following code for corresponding event to create role. I have added the code for corresponding Create button event.
 
protected void btnCreateRole_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.RoleExists(this.txtRole.Text))
            {
                Roles.CreateRole(txtRole.Text);
                BindRoles();
                BindUsers();
                this.lblMsg.Text = "Role created successfully";
            }
            else
            {
                lblMsg.Text = "Role already exists";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }


b.Bind Roles: I have bind a dropdown list for Roles.
 
    private void BindRoles()
    {
        SqlConnection con = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["ApplicationServices"]));

        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select UserId, UserName from aspnet_users", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.ddlUsers.DataSource = ds;
        ddlUsers.DataTextField = "UserName";
        ddlUsers.DataValueField = "UserName";
        ddlUsers.DataBind();

        con.Close();
    }

c.Bind Users: A dropdown list is bound for roles in this occasion also.
    private void BindUsers()
    {
        SqlConnection con = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["ApplicationServices"]));

        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("SELECT RoleId, RoleName FROM aspnet_Roles", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.ddlRoles.DataSource = ds;
        ddlRoles.DataTextField = "RoleName";
        ddlRoles.DataValueField = "RoleName";
        ddlRoles.DataBind();

        con.Close();
    }

d.Assign Roles to User: Use the following code snippet to assign roles to users.
protected void btnAssign_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Roles.IsUserInRole(this.ddlRoles.SelectedValue))
            {
                Roles.AddUserToRole(this.ddlUsers.SelectedValue,this.ddlRoles.SelectedValue);
                BindUsers();
                BindRoles();
                BindUserRole();
                this.lblMsg.Text = "User assigned to role successfully";
            }
            else
            {
                this.lblMsg.Text = "Role already exits for the user.";
            }
        }
        catch (Exception ex)
        {
            this.lblMsg.Text = ex.Message;
        }
    }

e.Remove role from the user: I have added a gridview in rolemanger page. Here is the code snippet of delete button event handler. Roles.RemoveUserFromRole(string username, string rolename) Method is responsible to remove role form the user.
protected void btnDelete_Click(object sender, EventArgs e)
    {
        LinkButton btnDelete = sender as LinkButton;

        //Identify the clicked row
        int rowIndex = Convert.ToInt32(btnDelete.Attributes["RowIndex"]);

        GridViewRow gvRow = this.gvRoleToUsers.Rows[rowIndex];

        try
        {
            Roles.RemoveUserFromRole(gvRow.Cells[0].Text, gvRow.Cells[1].Text); //username, role name
            BindUsers(); 
            BindRoles();
            BindUserRole();
            this.lblMsg.Text = "User is removed from the role";
        }
        catch (Exception ex)
        {
            this.lblMsg.Text = ex.Message;
        }


    }

f.Delete roles: The following code snippet is responsible to delete a roles.
    protected void btnDeleteRole_Click(object sender, EventArgs e)
    {
        try
        {
            Roles.DeleteRole(this.ddlRoles.SelectedValue); //Responsible to delete role
            this.BindRoles();
            this.BindUserRole();
            this.lblMsg.Text = "Role is deleted!";
        }
        catch (Exception exp)
        {
            this.lblMsg.Text = exp.Message;
        }
    }

Step 14: Show and Hide Menu depending on Roles: We have already created roles and assigned roles to users. Now if we want to show and Hide the menus depending on roles, we have to follow the following steps.

  1. Add sitemap: Add web.sitemap in the project. We have added this site map with a treeview. Have a look in the web.sitemap content.

xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="ASP.NET" title=""  description="" roles="Administrator">
    <siteMapNode url="Defult.aspx" title="Home"  description=""/>
    <siteMapNode url="Register.aspx" title="Register"  description="" />
    <siteMapNode url="RoleManager.aspx" title="Role Manager" description="" roles="Administrator"/>
    <siteMapNode url="ManageUsers.aspx" title="User Manager" description="" roles="Administrator,IT officer"/>
  siteMapNode>
siteMap>

You may understand above code snippet that only Administrator can view RoleManager.aspx page and Administrator and IT Officer can view ManageUsers.aspx page. There is no restriction in other pages.


  1. Add Location element in web.config: To hide and show menu as described above add the following code snippet in web.config file.


  <location path="RoleManager.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrator"/>
        <deny users="*"/>
      authorization>
    system.web>
  location>

  <location path="ManageUsers.aspx">
    <system.web>
      <authorization>
        <allow roles="Administrator,IT officer"/>
        <deny users="*"/>
      authorization>
    system.web>
  location>


Ok now run your application. Login to the application and you will see menus according to your roles.