Sony Arouje

a programmer's log

Generic Factory

leave a comment »

Here I am explaining how we can create a Factory class that is similar to ChannelFactory class. Who ever worked on Windows communication foundation will be aware of how to create a channel to a service using ChannelFactory. Creating a channel to the service is as follows.
ChannelFactory<> factory=new ChannelFactory<>
Unfortunately I couldn’t find any class that can create an object like ChannelFactory. So I created a Factory class that will create the object by just passing the interface.

I created a xml file to configure the interface and the class implements it. Below is the structure of my xml file.

<Configurations>
    <Config Interface=”IAddressBo” ActivatorClass=”AddressBo.CAddress” FullPath=”AddressBo.dll”></Config>
    <Config Interface=”IUserBo” ActivatorClass=”UserManager.UserBo” FullPath=”UserManager.dll”></Config>
</Configurations>

Below is the class reads the xml and create the instance of the requested class

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Xml.Linq;
using System.Threading;
namespace Factory
{
    public class ObjectFactory
    {
        public static I CreateInstance<I>() where I : class
        {
                if (_pooledObjects.ContainsKey(typeof(I)) == false)
                {
                    object classInstance = InstanceCreator<I>();
                    _pooledObjects.Add(typeof(I), classInstance);
                }
                return _pooledObjects[typeof(I)] as I;
        }

        private static object InstanceCreator<I>() where I:class
        {
            string className = null;
            string fullPath = null;

            XDocument configXML = XDocument.Load(“BoConfig.xml”);
            var configs = (from config in configXML.Descendants(“Config”)
                          where (config.Attribute(“Interface”).Value == typeof(I).Name.ToString())
                          select new AssemblyDetails
                          {
                              ActivatorClass = config.Attribute(“ActivatorClass”).Value,
                              FullPath = config.Attribute(“FullPath”).Value
                          }).Take(1);

            AssemblyDetails assemblyDetail = configs.First<AssemblyDetails>();
            className = assemblyDetail.ActivatorClass;
            fullPath = assemblyDetail.FullPath;

            if (className != null)
            {
                Assembly assembly;
                assembly = Assembly.LoadFrom(fullPath);
                Type type = assembly.GetType(className);
                return Activator.CreateInstance(type);
            }
            else
            {
                return null;
            }
        }
    }
}

AssemblyDetails  is a class created to store the details of the assembly. Below is the class

public class AssemblyDetails
{
    string _activatorClass;
    string _fullPath;
    public string ActivatorClass
    {
        get { return _activatorClass; }
        set { _activatorClass = value; }
    }
    public string FullPath
    {
        get { return _fullPath; }
        set { _fullPath = value; }
    }
}

Now it’s the time to see how can we create an instance of a class.

IUserBo user = ObjectFactory.CreateInstance<IUserBo>();

Technorati Tags: ,

Advertisements

Written by Sony Arouje

August 25, 2010 at 4:19 pm

Posted in .NET

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: