How Autofac’s Dynamic instantiation solves the service locator problem for ViewModels

Yesterday ive blogged about why I moved to Autofac. Now I want to show a really nice feature of Autofac which solves the service locator problem when you work with child ViewModels.

Let me give this example:

public class OrderViewModel
{
    public ObservableCollection<OrderLineViewModel> OrderLines { get; set; }

    public OrderViewModel(OrderDao orderDao)
    {
        OrderLines = new ObservableCollection<OrderLineViewModel>();

        foreach(var orderLine in orderDao.GetOrderLines())
        {
            var viewModel = IoC.Resolve<OrderLineViewModel>(orderLine);
            OrderLines.Add(viewModel);
        }
    }
}

What is the problem here? We need to create an OrderLineViewModel for each OrderLine we get from the database. We can not know how much OrderLines we need at build time of the OrderViewModel so we can not use constructor inject for that.

We have to use IoC.Resolve which is commonly known as service locator pattern. The real problem with that is that such a call only fails (because of missing dependencies) when it happens not on that point where the constructor of the root is resolved. In this example weights not much, but if this happens to code which is only used once in a month you have a problem.

So how Autofac can solve that:

public class OrderViewModel
{
    public ObservableCollection<OrderLineViewModel> OrderLines { get; set; }

    public OrderViewModel(OrderDao orderDao, Func<OrderLineViewModel,OrderLine> orderLineViewModelFactory)
    {
        OrderLines = new ObservableCollection<OrderLineViewModel>();

        foreach(var orderLine in orderDao.GetOrderLines())
        {
            var viewModel = orderLineViewModelFactory(orderLine);
            OrderLines.Add(viewModel);
        }
    }
}

What is the difference? The func is a dynamic service locator provided by Autofac, but instead at call time, Autofac has now the chance to know what types you need at runtime and now it can satisfy all dependencies at resolve time of the root objects constructor.

So what happens is that it fails now at the root of you application. Not one month later when your client calls this one view with the missing dependencies.

The same way Autofac supports Lazy<T> which is new in .Net 4.0 and resolves once on the first call. You can read more about that in this blog post from Nicholas Blumhardt which is the author of Autofac.