Another 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();
        }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *