Share objects

May 12, 2010 at 1:22 PM


I have a general question about the Unity Pattern (or more the IoC-Pattern). I don't quite understand, how this all works if you had to share an object over many objects - but not as a singleton in the unity container. Let's say you have a class that instantiates a logger, and this logger should be used by all objects instantiated through this class. But this logger shouldn't be a singleton as other classes could use another instance of the logger which is configured differently and stores it's messages at some other location.

In this case I would pass the logger from the instantiating class to the objects over the constructor. But with Unity I shouldn't use the constructer, I should resolve everything. How should this conceptually be done?

I hope someone understands what I mean and I'd be happy for answers!



May 13, 2010 at 2:05 AM

With Unity, you can register as many instance of the logger class to the container as you want.  You can register multiple mappings for it using registration names.  You have the ability to configure each registration to be resolved using different values for its constructor and properties. 

Does this answer your question or I misunderstood it?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

May 14, 2010 at 5:44 AM

Hello Sarah

Yes this answers my question.

Thank you