Click here to Skip to main content
Click here to Skip to main content

Another Example for Object Factory

, 10 Dec 2012
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Ivan Krivyakov
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? PinmemberEnrique Albert17-Dec-12 0:52 
AnswerRe: Concurrency scenario - does it work? PinmemberIvan Krivyakov17-Dec-12 3:40 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid