Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Another Example for Object Factory

0.00/5 (No votes)
10 Dec 2012Apache 7.1K  
A closer to real life example for object factory.

As one of the commentators correctly pointed out, it does not make much sense to create value objects such as Order via dependency injection, so my previous example was perhaps too contrived. The real life need for object factory was to encapsulate NHibernate session factory. So, a closer to real life example for object factory would look like this:

using System;
using Microsoft.Practices.Unity;

namespace UnityObjectFactory
{
    class Program
    {
        static IUnityContainer _container = new UnityContainer();
        static void Main(string[] args)
        {
            var factory = _container.Resolve<SessionFactory>();
            _container.RegisterType<ISession>(new InjectionFactory(unused_container=>factory.CreateSession()));

            DoDbWork();
            DoDbWork();
        }

        private static void DoDbWork()
        {
            using (var dbWorker = _container.Resolve<DbWorker>())
            {
                dbWorker.Work();
            }
        }
    }

    interface ISession : IDisposable
    {
        int SessionId { get; }
        void ExecuteQuery(string query);
    }

    class Session : ISession
    {
        public Session(int id) 
        {
            Console.WriteLine("Created session " + id);
            SessionId = id; 
        }

        public int SessionId { get; private set; }

        public void ExecuteQuery(string query)
        {
            Console.WriteLine(SessionId + ": " + query);
        }

        public void Dispose()
        {
            Console.WriteLine("Closed session " + SessionId);
        }
    }

    class SessionFactory
    {
        private int _lastSessionId;

        public ISession CreateSession()
        {
            Console.WriteLine("Session factory invoked. Working hard to create a session..");
            return new Session(++_lastSessionId);
        }
    }

    class DbWorker : IDisposable
    {
        [Dependency]
        public ISession Session { get; set; }

        public void Work()
        {
            Session.ExecuteQuery("SELECT * FROM SomeBigTable");
        }

        public void Dispose()
        {
            Session.Dispose();
        }
    }
}


License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0