A couple of days ago I spent some time trying to find how to make Unity call a factory method when user requests an object. Then I forgot about it, and tried to Google it again. This stuff is surprisingly hard to find, especially given the fact that the method used in Unity 1.x (StaticFactoryExtension
) was declared obsolete in favor of InjectionFactory
which for whatever reason Unity authors consider “much easier” to use.
Anyway, here’s a full working example, mostly for my future reference:
using System; using Microsoft.Practices.Unity; namespace UnityObjectFactory { class Program { static void Main(string[] args) { var container = new UnityContainer(); var factory = container.Resolve<OrderFactory>(); container.RegisterType<IOrder>( new InjectionFactory(unused_container=>factory.CreateOrder())); var order1 = container.Resolve<IOrder>(); Console.WriteLine("Fisrt order has number " + order1.Number); var order2 = container.Resolve<IOrder>(); Console.WriteLine("Second order has number " + order2.Number); } } interface IOrder { int Number { get; } } class Order : IOrder { public Order(int number) { Number = number; } public int Number { get; private set; } } class OrderFactory { int _lastOrder; public IOrder CreateOrder() { return new Order(++_lastOrder); } } }
Permalink
Fascinating blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make my
blog stand out. Please let me know where you got your design.
Thanks a lot
Permalink
The theme is called “boxy but gold”, downloadable from WordPress web site (or at least it used to be).
Permalink
Do you have a spam issue on this blog; I also am a blogger, and I was curious about your situation;
many of us have created some nice practices and we are looking to
exchange techniques with others, why not shoot me an email if
interested.
Permalink
Hi there! I realize this is kind of off-topic but I needed to ask.
Does managing a well-established website such as yours require a lot of work?
I am completely new to blogging but I do write in my diary daily.
I’d like to start a blog so I will be able to share my personal experience and feelings online. Please let me know if you have any kind of recommendations or tips for new aspiring blog owners. Thankyou!