-1

I need to named dynamically when Resolve methods called like below.

My registration:

container.Register(Component.For<IWorker>()
                                   .ImplementedBy<Worker>()
                                   .LifeStyle.Singleton);

My example resolve process:

container.Resolve<IWorker>("Singleton instance 1"); //Singleton instance 1 not defined but it can definable dynamically when Resolve called
container.Resolve<IWorker>("Singleton instance 2");
container.Resolve<IWorker>("Singleton instance 1"); // same instance with first resolved instance

How can I named dynamically a component in Windsor?

oguzh4n
  • 682
  • 1
  • 10
  • 29
  • Can you tell us what you're *really* trying to solve with this? I.e. what led you to want this? – Mauricio Scheffer Dec 01 '11 at 14:52
  • i want to create singleton instance each different named call – oguzh4n Dec 01 '11 at 15:04
  • where do you intend to use such a thing? What are you trying to implement/solve with it? – Mauricio Scheffer Dec 01 '11 at 15:18
  • I have a big string[]. I need to create a Worker for each item in the array (distinct), but it created before I will use it. (like singleton) – oguzh4n Dec 01 '11 at 15:56
  • 1
    Well, a singleton is a singleton. An IoC container is not supposed to manage your application state. If you need many instances of a component you probably rely on some state that should be there the next time you use that specific instance. Try to use stateless components. You can create them when needed (not before) and dispose of them afterwards. – Gert Arnold Dec 01 '11 at 16:04
  • Have you considered the pooled lifestyle? Or as @GertArnold suggests, make it stateless. – Mauricio Scheffer Dec 02 '11 at 02:41

1 Answers1

1

It sounds like you need a custom lifestyle -- something like one instance per "key" (whatever this key is). I would look into that approach versus trying to force one of the predefined lifestyles to fit your scenario or naming a component after it has been created.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54