Resolving Unity

Feb 25, 2010 at 10:24 AM


We have started to use Unity in our project and got stuck in an issue.

We have several class library (dlls) which call methods of each other and we needed a master service (windows service) that calls the methods of all the class librarys.

We have stored the mappings in app.config of the master service and create a container in this master service, resolve objects (which are mostly singleton) and call their methods. all works great (we have referenced all the class libs in this service).

Now the problem arises when we want to call a method of a class say "A" in class lib 1 from a class say "B" in class lib 2. We need a object of a class "A"  in class "B" , but since this class is singleton and the object is maintained by the container of the master service we need to access the instance of the unity container (of the master service ) in class "B".

Now the situation is equivalent to resolving unity, how do we accomplish this.

Any advise will be appreciated.



Feb 25, 2010 at 4:17 PM

Are you getting your instance of B out of the container too? In that case, all you need to do is add a dependency on A - a constructor parameter works well. Since everything's set as singleton, the container will make sure that B gets the current singleton instance of A.

The container can inject itself too - just have a dependency of type IUnityContainer and you'll get the current container injected. But that's generally something you should avoid if possible. In this case, in order to inject the container you'd have to be going through the container and at that point you might as well just inject the A and be more explicit about what you actually need.



Feb 26, 2010 at 10:07 AM

No, we are not getting the instance of the class "B" out of the container.

Let me clarify the scenario.

Class Library 1 (dll) - class "A" (implements interface "IA")

 Class Library 2 (dll) - class "MainClass" (implements interface "IMain") and Class "B"

Windows Service  - contains reference to Class Library 1 and Class Library 2.

This service instantiates the  Unity container. Container contains singleton instances of class "A" and "MainClass".

Now, we want to call a method of class "A" from class "B" (we want to use the active instance of class "A" contained in the Unity container).

Hope, this clarifies the requirements.



Feb 27, 2010 at 3:58 AM

So who creates the B that you want to call? Is it created and discarded each time, or acquired once and reused? Could you get B from the container?



Feb 27, 2010 at 5:07 AM
Edited Feb 27, 2010 at 5:11 AM

"B" is created by a client request. It is created on every client request. We cannot  get "B" from the container (if we get it from the container, we won't be able to use the same object).

To clarify, Class lib 2 is a server and listens for client requests on a port. "StartServer" and "StopServer" methods are present in "MainClass", using these methods we start the server from the Windows service. Now this server creates a thread for each user request, while serving the request it creates an object of Request class (which is class "B"). In this Request ("B") class we want to call a method of class "A" from class lib 1.



Mar 3, 2010 at 3:32 AM

Can you suggest something.

Any help would be appreciated.




Mar 31, 2010 at 7:21 PM

You need to make the Unity container accessible globally, such as from a singleton.  Then your class B can find the unity container, even though class B was not created by Unity.