Unity 2.0: Combining InjectionFactory and Automatic Factory

05/06/10

Permalink 09:52:47 pm, by truewill Email , 261 words, 2342 views   English (US)
Categories: Tips, C#, IoC, .NET

Unity 2.0: Combining InjectionFactory and Automatic Factory

Unity 2.0 is out, and has some very nice features. Two of the coolest are InjectionFactory and Automatic Factories. Nice code samples and tutorials exist, but here’s an example of combining the two features.

using System;
using Microsoft.Practices.Unity;

namespace Unity2Example
{
  class Program
  {
    static void Main()
    {
      using (var container = new UnityContainer())
      {
        container
          .RegisterType<IService, ConcreteService>(
            new InjectionFactory(c => ServiceFactory()));

        Model model = container.Resolve<Model>();

        Console.WriteLine("\nCalling Execute...");
        model.Execute();
        Console.WriteLine("\nCalling Execute...");
        model.Execute();
      }
    }

    static IService ServiceFactory()
    {
      Console.WriteLine("ServiceFactory called.");
      return new ConcreteService();
    }
  }

  interface IService
  {
    void MakeTheCall();
  }

  class ConcreteService : IService
  {
    public ConcreteService()
    {
      Console.WriteLine("ConcreteService instantiated.");
    }

    public void MakeTheCall()
    {
      Console.WriteLine(
        "ConcreteService makes the call.");
    }
  }

  class Model
  {
    public Model(Func<IService> serviceFinder)
    {
      Console.WriteLine("Model instantiated.");

      _serviceFinder = serviceFinder;
    }

    public void Execute()
    {
      _serviceFinder().MakeTheCall();
    }

    private readonly Func<IService> _serviceFinder;
  }
}

This will display:

Model instantiated.

Calling Execute...
ServiceFactory called.
ConcreteService instantiated.
ConcreteService makes the call.

Calling Execute...
ServiceFactory called.
ConcreteService instantiated.
ConcreteService makes the call.

Essentially we’re injecting a Service Locator into our model. My use case for this was injecting an object where the object reference could change (it needed to be deserialized at certain times). This way the dependent class would always get the most current version. It turned out I already had a class that could act as a Service Locator with the addition of an interface, so I injected that instead (it seemed simpler). I think this combination may come in useful someday, though.

Comments, Pingbacks:

No Comments/Pingbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
The name truewill is composed of two other words. What is the SECOND word?

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 6

powered by b2evolution free blog software