1

Ive recently switched up to Enterprise Library 5 and Unity. I was quite happy with the examples that were provided until I surfed around and started reading the hiss about "Service Locator is an Anti Pattern". From there I stumbled into writings on Composite Root and the Hollywood Principal of "dont call us, we'll call you".

I think I understand the benefits of Composite Root but I dont understand how we can avoid calling it in order to get instances of the classes the application needs. So I think I might be misusderstanding the whole idea.

Im approaching this as a new way to do a centralised factory pattern. But at some point I still have to call into the Factory Pattern to get my instances? So for the code below from what Ive read the Unity Container should be created somewhere near the root of the app (not in a form). That in turn should take care of my DI for exception handling and logging. (Note this is just 2 min play code so it's meant to be pointless)

 public partial class Form1 : Form
    {
        public IUnityContainer Container { get; protected set; }

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Container = new UnityContainer().AddNewExtension<EnterpriseLibraryCoreExtension>();
            try
            {
                throw new NullReferenceException("Mucking around.");
            }
            catch (Exception ex)
            {
                    TaxCalculator calc = this.Container.Resolve<TaxCalculator>(new ParameterOverride("fire", 888));
                    calc.LogWriter.Write(calc.Fire, "muckaround");
            }
        }


public class TaxCalculator
{
    private ExceptionManager _exceptionManager;
    public LogWriter LogWriter { get; protected set; }

    public string Fire { get; set; }

    public TaxCalculator(ExceptionManager em, LogWriter lw, int fire)
    {
        this._exceptionManager = em;
        this.LogWriter = lw;
        this.Fire = fire.ToString();
    }
}

Trouble is, if I cant call a TaxCalculatorFactory which in turn calls the container, or just call the container itself, how do I get a specific type of Tax Calculator?

How/ where do I get my instances from? Do I start the app, build a bunch of factories that I may or may not need during the apps lifetime and then have the various layers of the app request instances from the factories? Seems like I'd be passing around alot of factory references so Im not sure what the benefit is? Or do I initialise static classes in the container and just call them?

As you can see Im in a muddle re: the idea of Hollywood Principal.

rism
  • 11,932
  • 16
  • 76
  • 116

1 Answers1

0

One alternative to a Service Locator is to use an IoC container that can inject dependencies during object creation.

This SO question asks a similar question, which points to this Autofac article on object reference types. It should give you some ideas about how an IoC container can address this issue.

Community
  • 1
  • 1
neontapir
  • 4,698
  • 3
  • 37
  • 52