Kramer00
Kramer00

Reputation: 1187

Instantiating a Unity Named Type Registration with ASP.NET MVC 4 Application

I would like to understand how i can resolve a unity named type registration within an MVC4 application.

I am using "Unity 3.0" and the "Unity boostrapper for ASP.NET MVC".

I understand that unity is registered with the DependencyResolver and so i can use the dependency resolver methods to get an instance of an registered type.

This works fine when I want to get a simple registration back.

e.g

//Registration
container.RegisterType<IFootballer, VanPersey>();

//Instantiation 
var footballer1 = DependencyResolver.Current.GetService<IFootballer>();

But on an occasion when I want to get a reference to a named type registration it doesn't appear that the IDependencyResolver interface provides the appropriate methods to get named type registration instances.

If I had access to the unity container directly I would do something like the following:

//Registration 
container.RegisterType<IFootballer, VanPersey>("Attacker" );
container.RegisterType<IFootballer, NemanjaVidic>("Defender");

//Instantiation 
unityContainer.Resolve<IFootballer, Footballer>("Attacker")

So my question is , what is the most appropriate way to use unity in an MVC app in order to get an instance of a named registration. ie is there access to the unity comtainer?

Note that this question is not for any production implementation, I am looking at Unity (and IoC containers for the first time) and tying to get a better understanding.

I understand in production I would more than likely be passing in the dependencies through the controllers constructors, which actually leads me to another question.

How would it be possible to indicate which named type you wanted to resolve when using controller constructor injection in the following example:

//Registration 
container.RegisterType<IFootballer, VanPersey>("Attacker" );
container.RegisterType<IFootballer, NemanjaVidic>("Defender");

//Instantiation 
public HomeController(IFootballer footballer)

Upvotes: 2

Views: 5718

Answers (2)

Kramer00
Kramer00

Reputation: 1187

Incase this helps anyone else wanting to do the same thing, I found the following:

When using "Unity 3.0" and the "Unity boostrapper for AP.NET MVC" I found that un the UnityConfig class that it exposes a GetConfiguredContainer method in which you can directly access the unity container without having to use the dependency resolver.

Snippet from UnityConfig

/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    /// <summary>
    /// Gets the configured Unity container.
    /// </summary>
    public static IUnityContainer GetConfiguredContainer()
    {
        return container.Value;
    }
    #endregion
}

So that allowed me to access the Unity container directly to obtain named type instances.

var footballer1 = UnityConfig.GetConfiguredContainer().Resolve<IFootballer>("Attacker");
var footballer2 = UnityConfig.GetConfiguredContainer().Resolve<IFootballer>("Defender");

Upvotes: 4

Wiktor Zychla
Wiktor Zychla

Reputation: 48240

Most probably the container itself is also registered so that the resolver can resolve it:

var container = DependencyResolver.Current.GetService<IUnityContainer>();
var vanPersey = container.RegisterType<IFootballer, VanPersey>("Attacker" );

Haven't tried it with the resolver but it always worked like that with the ServiceLocator.

Upvotes: 5

Related Questions