The tutorial by the msdn (source) recommends using an IDependencyResolver:
IDependencyResolver resolver = DependencyResolver.Current;
IDependencyResolver newResolver = new UnityDependencyResolver(container, resolver);
DependencyResolver.SetResolver(newResolver);
I was under the impression that IDependencyResolver does not properly manage object lifetime because it lacks a release method, and also that is conceptually a service locator anti-pattern (source).
How could I refactor this tutorial to not use IDependencyResolver?
Use Unity.Mvc3, there is a HierarchicalLifetimeManager that can manage lifetime for objects implementing IDispoable.
Its not an anti pattern if you resolve only at the composition root here, which basically with MVC is via constructor injection in the controller.
http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx
Note this doesn’t have to be a custom controller factory you create, unity will inject automatically for you. See my code here:
http://completedevelopment.blogspot.com/2011/12/using-dependency-injection-with-mvc.html