Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Unity how do I inject a named dependency into a constructor?

I have the IRespository registered twice (with names) in the following code:

// Setup the Client Repository IOC.Container.RegisterType<ClientEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository>     ("Client", new InjectionConstructor(typeof(ClientEntities)));  // Setup the Customer Repository IOC.Container.RegisterType<CustomerEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository>     ("Customer", new InjectionConstructor(typeof(CustomerEntities)));  IOC.Container.RegisterType<IClientModel, ClientModel>(); IOC.Container.RegisterType<ICustomerModel, CustomerModel>(); 

But then when I want to resolve this (to use the IRepository) I have to do a manual resolve like this:

public ClientModel(IUnityContainer container) {    this.dataAccess = container.Resolve<IRepository>(Client);     ..... } 

What I would like to do is to have it resolved in the constructor (just like IUnityContainer). I need some way to say which named type to resolve to.

Something like this: (NOTE: Not real code)

public ClientModel([NamedDependancy("Client")] IRepository dataAccess) {    this.dataAccess = dataAccess;     ..... } 

Is there a way to make my fake code work?

like image 963
Vaccano Avatar asked Aug 12 '11 21:08

Vaccano


People also ask

What is constructor dependency injection?

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.

How do you inject a dependency?

The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

Can you use dependency injection in Unity?

Scene-based dependency injection means you can setup up the Unity hierarchy and your MonoBehaviors however you like. It scans your scene and automatically wires together your MonoBehaviors. It allows you to connect objects in the Unity hierarchy without having to manually use functions such as FindObjectOfType(...) .


1 Answers

You can configure dependencies with or without names in the API, attributes, or via the config file. You didn't mention XML above, so I'll assume you're using the API.

To tell the container to resolve a named dependency, you'll need to use an InjectionParameter object. For your ClientModel example, do this:

container.RegisterType<IClientModel, ClientModel>(     new InjectionConstructor(                        // Explicitly specify a constructor         new ResolvedParameter<IRepository>("Client") // Resolve parameter of type IRepository using name "Client"     ) ); 

This tells the container "When resolving ClientModel, call the constructor that takes a single IRepository parameter. When resolving that parameter, resolve with the name 'Client' in addition to the type."

If you wanted to use attributes, your example almost works, you just need to change the attribute name:

public ClientModel([Dependency("Client")] IRepository dataAccess) {    this.dataAccess = dataAccess;     ..... } 
like image 83
Chris Tavares Avatar answered Sep 24 '22 07:09

Chris Tavares