Saturday, May 12, 2012

C# Abstract Factory Pattern combined with Dependancy Injection andInversion of Control

Saturday, May 12, 2012 Posted by Andre Broers , , , , , , 15 comments
In this example I will show the usage of the abstract factory pattern. After this I will show how you can inject the concrete factory dependancy in the sollution. In the final example I will use an Inversion of Control container to get the concrete factory. What I want to demo is that although we will use Inversion of Control we will only use it for the factory. The factory stays responsible for the instanciation and configuration of the IPerson concrete object (an Employee or a Customer). Let's continue.

Abstract Factory Pattern

First the UML Class Diagram of the Abstract Factory:
Code example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySample
{
    public interface IPerson
    {
        string Name { get; set; }
        string Type { get; set; }
    }

    public class Employee : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Employee"; } set { } }
    }

    public class Customer : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Customer"; } set { } }
    }

    public interface IPersonAbstractFactory
    {
        IPerson GetPerson();
        IPerson GetPerson(string name);
    }

    public class EmployeeFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Employee() { Name = "UnkownEmployee" };
        }

        public IPerson GetPerson(string name)
        {
            return new Employee() { Name = name };
        }
    }

    public class CustomerFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Customer() { Name = "UnkownCustomer" };
        }

        public IPerson GetPerson(string name)
        {
            return new Customer() { Name = name };
        }
    }

    public class MySample
    {
        public IPerson Person1;
        public IPerson Person2;

        public MySample()
        {
            IPersonAbstractFactory EmployeeFactory = new EmployeeFactory();
            Person1 = EmployeeFactory.GetPerson();
            Person2 = EmployeeFactory.GetPerson("Employee2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MySample m = new MySample();
            Console.WriteLine(m.Person1.Name + " of " + m.Person1.Type);
            Console.WriteLine(m.Person2.Name + " of " + m.Person2.Type);
            Console.ReadLine();
        }
    }
}

When we run this code the result is:
UnkownEmployee of Employee
Employee2 of Employee

When we run this code we have no way to change what objects the factory will create. When we add dependancy injection we can specify which object types we want the abstract factory to create and configure. Especially the configure part is important. By having a factory we can do all sorts of configuration done by the different factory implementations which will be impossible without the factory pattern.
Now it is time to add dependancy injection.

Dependancy Injection


In the following example dependancy injection is added:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySample
{
    public interface IPerson
    {
        string Name { get; set; }
        string Type { get; set; }
    }

    public class Employee : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Employee"; } set { } }
    }

    public class Customer : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Customer"; } set { } }
    }

    public interface IPersonAbstractFactory
    {
        IPerson GetPerson();
        IPerson GetPerson(string name);
    }

    public class EmployeeFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Employee() { Name = "UnkownEmployee" };
        }

        public IPerson GetPerson(string name)
        {
            return new Employee() { Name = name };
        }
    }

    public class CustomerFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Customer() { Name = "UnkownCustomer" };
        }

        public IPerson GetPerson(string name)
        {
            return new Customer() { Name = name };
        }
    }

    public class MySample
    {
        private IPersonAbstractFactory _PersonFactory;
        public IPerson Person1;
        public IPerson Person2;

        public MySample(IPersonAbstractFactory personfactory)
        {
            _PersonFactory = personfactory;
            Person1 = _PersonFactory.GetPerson();
            Person2 = _PersonFactory.GetPerson("Person2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IPersonAbstractFactory CustomerFactory = new CustomerFactory();
            MySample m = new MySample(CustomerFactory);
            Console.WriteLine(m.Person1.Name + " of " + m.Person1.Type);
            Console.WriteLine(m.Person2.Name + " of " + m.Person2.Type);
            Console.ReadLine();
        }
    }
}

Here we can choose what specific factory we want to use. We inject the dependancy into the MySample class.
The output is:
UnkownCustomer of Customer
Person2 of Customer

Now it is time to use an Inversion of Control container to administer the dependancies in one container. I will use TinyIOC, but there are many IOC libraries in nuget. Use the following to install TinyIOC in your project:
PM> install-package tinyioc
Successfully installed 'TinyIoC 1.1.1'.
Successfully added 'TinyIoC 1.1.1' to MySample.

PM>

Now the example code with Inversion of Control implemented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyIoC;

namespace MySample
{
    public interface IPerson
    {
        string Name { get; set; }
        string Type { get; set; }
    }

    public class Employee : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Employee"; } set { } }
    }

    public class Customer : IPerson
    {
        private string _Name;
        public string Name { get { return _Name; } set { _Name = value; } }
        public string Type { get { return "Customer"; } set { } }
    }

    public interface IPersonAbstractFactory
    {
        IPerson GetPerson();
        IPerson GetPerson(string name);
    }

    public class EmployeeFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Employee() { Name = "UnkownEmployee" };
        }

        public IPerson GetPerson(string name)
        {
            return new Employee() { Name = name };
        }
    }

    public class CustomerFactory : IPersonAbstractFactory
    {
        public IPerson GetPerson()
        {
            return new Customer() { Name = "UnkownCustomer" };
        }

        public IPerson GetPerson(string name)
        {
            return new Customer() { Name = name };
        }
    }

    public class MySample
    {
        private IPersonAbstractFactory _PersonFactory;
        public IPerson Person1;
        public IPerson Person2;

        public MySample(IPersonAbstractFactory personfactory)
        {
            _PersonFactory = personfactory;
            Person1 = _PersonFactory.GetPerson();
            Person2 = _PersonFactory.GetPerson("Person2");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var container = TinyIoCContainer.Current;
            container.Register<IPersonAbstractFactory>(new CustomerFactory());

            MySample m = container.Resolve<MySample>();
            Console.WriteLine(m.Person1.Name + " of " + m.Person1.Type);
            Console.WriteLine(m.Person2.Name + " of " + m.Person2.Type);
            Console.ReadLine();
        }
    }
}

Which results in the same output as the previous example. Now we can register all dependancies in a single container. But remember we will not register implementations of the IPerson object.

15 comments:

  1. It's really a matter of taste. Factory classes can be abstracted/interfaced away as necessary, whereas factory methods are lighter weight and also tend to be testable, since they don't have a defined type, but they will require a well-known registration point, akin to a service locator but for locating factory methods.
    http://www.dapfor.com/en/net-suite/net-grid/features/performance

    ReplyDelete
  2. http://bantalsilikon01.blogspot.com/2014/12/a.html
    http://jualsangkarpleci.blogspot.com/2014/12/a.html
    http://kopiluwakliar01.blogspot.com/2014/12/a.html
    http://vvty.in/uncategorized/marinir-seo/

    http://marinirseo.blogspot.com/2014/12/a.html
    http://marinir-seo.blogspot.com/2014/12/a.html
    http://bumbu-pecel-malang.blogspot.com/2014/12/a.html
    http://distro-seo.blogspot.com/2014/12/a.html
    http://restoran-seo.blogspot.com/2014/12/a.html
    http://bantalmalangmurah.blogspot.com/2014/12/a.html
    http://distrobantal.blogspot.com/2014/12/a.html
    http://kesethandukmalang.blogspot.com/2014/12/a.html

    http://gurunyaitu.blogspot.com/2014/12/b.htmlhttp://kedirinyaindah.blogspot.com/2014/12/a.htmlhttp://bumbupecel617.blogspot.com/2014/12/w.htmlhttp://akaeoleh.blogspot.com/2014/12/v.htmlhttp://satudiasaja.blogspot.com/2014/12/u.htmlhttp://goodluckyaa.blogspot.com/2014/12/t.htmlhttp://songolas11.blogspot.com/2014/12/r.htmlhttp://yudistiramlg.blogspot.com/2014/12/q.htmlhttp://arjunapnh.blogspot.com/2014/12/p.htmlhttp://marinirseo987.blogspot.com/2014/12/o.htmlhttp://ayoikut1.blogspot.com/2014/12/n.htmlhttp://sempelblogger.blogspot.com/2014/12/m.htmlhttp://hondajass.blogspot.com/2014/12/k.htmlhttp://kesetnyakaki.blogspot.com/2014/12/j.htmlhttp://alwayssate.blogspot.com/2014/12/i.htmlhttp://bumbupecelmlg7.blogspot.com/2014/12/h.htmlhttp://bumbunyaseo.blogspot.com/2014/12/g.htmlhttp://bumbupecel100.blogspot.com/2014/12/f.htmlhttp://smartfren1.blogspot.com/2014/12/e.htmlhttp://bumbumalang1.blogspot.com/2014/12/d.htmlhttp://bumbupecelmalang7.blogspot.com/2014/12/c.htmlhttp://marinirseo7.blogspot.com/2014/12/b.htmlhttp://bloggermalang1.blogspot.com/2014/12/a.htmlhttp://asfrt111.blogspot.com/2015/01/a.htmlhttp://fgrt445.blogspot.com/2015/01/a.htmlhttp://grte231.blogspot.com/2015/01/a.htmlhttp://htyu412.blogspot.com/2015/01/asdgt.htmlhttp://fgr453.blogspot.com/2015/01/a.htmlhttp://asw145.blogspot.com/2015/01/a.htmlhttp://gtry145.blogspot.com/2015/01/a.htmlhttp://ase476.blogspot.com/2015/01/a.htmlhttp://10ouy.blogspot.com/2015/01/a.htmlhttp://s9yt.blogspot.com/2015/01/a.html,http://marinir-seo-konsultan-12a.blogspot.com/2014/10/aaa.htmlhttp://asdf-14.blogspot.com/2014/10/hhh.htmlhttp://vvtiin.blogspot.com/2014/10/iii.htmlhttp://marinir-seo-8.blogspot.com/2014/10/jjj.htmlhttp://bantaljkt1.blogspot.com/2014/10/sss.htmlhttp://dfes12.blogspot.com/2014/10/rrr.htmlhttp://seo-guru01.blogspot.com/2014/10/qqq.htmlhttp://jksbindo.blogspot.com/2014/10/ppp.htmlhttp://jkt-49.blogspot.com/2014/10/ooo.htmlhttp://seokonsultan1.blogspot.com/2014/10/nnn.htmlhttp://guru-seo-malang.blogspot.com/2014/10/mmm.htmlhttp://seo-aaa.blogspot.com/2014/10/lll.htmlhttp://seo-konsultasi-mudah.blogspot.com/2014/10/kkk.htmlhttp://bantal-silikon15.blogspot.com/2014/10/ggg.htmlhttp://agen-seo16.blogspot.com/2014/10/fff.htmlhttp://agen-seo17.blogspot.com/2014/10/eee_30.htmlhttp://agen-seo18.blogspot.com/2014/10/ddd.htmlhttp://agen-seo19.blogspot.com/2014/10/ccc.htmlhttp://agen-seo20.blogspot.com/2014/10/bbb.htmlhttp://bumbu-pecel01.blogspot.com/2014/10/xxx.htmlhttp://bantalasdf.blogspot.com/2014/12/seo-sem-seo.htmlhhttp://seo-malang-19.blogspot.com/2014/10/bbb.htmlhttp://agenseomalang18.blogspot.com/2014/10/ccc.htmlhttp://marinirseo17.blogspot.com/2014/10/ddd.htmlhttp://bumbupecel16.blogspot.com/2014/10/eee.htmlhttp://domainhosting15.blogspot.com/2014/10/fff.htmlhttp://bumbupecel15.blogspot.com/2014/10/ggg.htmlhttp://marinirseo14.blogspot.com/2014/10/hhh.htmlhttp://bantalsilikon12a.blogspot.com/2014/10/iii.htmlhttp://marinirseo12.blogspot.com/2014/10/jjj.htmlhttp://bumbu-pecel-hz.blogspot.com/2014/10/kkk.htmlhttp://marinir-seo-001.blogspot.com/2014/10/lll.htmlhttp://bantal-silikon-murah.blogspot.com/2014/10/mmm.htmlhttp://vvty-ine.blogspot.com/2014/10/nnn.htmlhttp://give-a-power.blogspot.com/2014/10/ooo.htmlhttp://bumbupecel-12.blogspot.com/2014/10/ppp.htmlhttp://marinir-seo-konsultan.blogspot.com/2014/10/qqq.htmlhttp://guru-seo-praktisi-123.blogspot.com/2014/10/rrr.htmlhttp://guru-seminar-online01.blogspot.com/2014/10/sss.htmlhttp://kopiluwakmalang11.blogspot.com/2014/10/ttt.html

    ReplyDelete
  3. http://bantal-silikon15.blogspot.com/2015/05/welcome-to-wordpress.html
    http://bumbu-pecel01.blogspot.com/2015/05/a.html
    http://seo-guru01.blogspot.com/2015/05/asf.html
    http://seo-konsultasi-mudah.blogspot.com/2015/05/fgre.html
    http://marinir-seo-konsultan-12a.blogspot.com/2015/05/asf.html
    http://dfes12.blogspot.com/2015/05/gre.html
    http://agen-seo19.blogspot.com/2015/05/wer.html
    http://agen-seo20.blogspot.com/2015/05/as.html
    http://agen-seo16.blogspot.com/2015/05/er.html
    http://agen-seo18.blogspot.com/2015/05/rqw.html
    http://jksbindo.blogspot.com/2015/05/rte.html
    http://jkt-49.blogspot.com/2015/05/tyu.html
    http://seokonsultan1.blogspot.com/2015/05/asrw.html
    http://guru-seo-malang.blogspot.com/2015/05/arte.html
    http://seo-aaa.blogspot.com/2015/05/rty.html
    http://marinir-seo-8.blogspot.com/2015/05/rty.html
    http://vvtiin.blogspot.com/2015/05/rqw.html
    http://agen-seo17.blogspot.com/2015/05/rtq.html
    http://asdf-14.blogspot.com/2015/05/qwr.html
    http://bantaljkt1.blogspot.com/2015/05/qwa.html
    http://larismanisyaluk.blogspot.com/2015/05/q.html
    http://bantalku1.blogspot.com/2015/05/b.html
    http://bantal999.blogspot.com/2015/05/r.html

    http://distrobantal.blogspot.com/
    http://bantalsilikon01.blogspot.com/
    http://sabunherbalya.blogspot.com/
    http://kesethandukmalang.blogspot.com/
    http://bantalmalangmurah.blogspot.com/
    http://restoran-seo.blogspot.com/
    http://distro-seo.blogspot.com/
    http://jualsangkarpleci.blogspot.com/
    http://marinir-seo.blogspot.com/
    http://kopiluwakliar01.blogspot.com/
    http://bumbu-pecel-malang.blogspot.com/

    ReplyDelete
  4. SEO Techniques that can help you to get high rank your website in all Search engine like Google, Bing, Yahoo etc and Alexa Ranking. For more Detail visit Click Here
    SEO Techniques

    Search Engine Optimization

    ReplyDelete
  5. How did you can avail the give your dream a shape with the help of make your move Adword India's No 1 services.

    http://www.adword.co.in/

    ReplyDelete
  6. How Packers and Movers India can assist you with your long-distance relocation services provide packing moving services at the make you best local shifting services exoprt amazing only.
    Packers and Movers Pune
    Packers and Movers Bangalore
    Packers and Movers Chennai
    Packers and Movers Hyderabad
    Packers and Movers Mumbai

    ReplyDelete
  7. Quester is a Pakistan-based questioning answering website where people can ask questions and we try our best to provide them with the best answers. Anyone can ask any legit question in English or Roman Urdu and we provide answers in the same language format. www.quester.pk

    ReplyDelete