Click here to Skip to main content
15,885,824 members
Articles / Programming Languages / C#

Another Example for Object Factory

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Dec 2012Apache 6.9K   5   2
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();
        }
    }
}


This article was originally posted at http://www.ikriv.com/blog?p=1154

License

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


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
QuestionConcurrency scenario - does it work? Pin
Enrique Albert17-Dec-12 0:52
Enrique Albert17-Dec-12 0:52 
AnswerRe: Concurrency scenario - does it work? Pin
Ivan Krivyakov17-Dec-12 3:40
Ivan Krivyakov17-Dec-12 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.