Windsor Castle. Free configuration: is it possible to create a certain lifestyle for this service without using a specific implementation?

I have a set of services that I want to register with Castle Windsor (version 3.0 RC1) using the free registration technique.

I want all of them, except one, to use a temporary lifestyle, and I want to be single, so I do this:

container.Register(AllTypes .FromThisAssembly() .InSameNamespaceAs<IMyService>() .WithServiceDefaultInterfaces() .ConfigureIf(s => s.Implementation == typeof(MyService), s => s.LifestyleSingleton(), s => s.LifestyleTransient())); 

The problem with this is that I use typeof ( MyService ) in the first ConfigureIf parameter, but I would prefer that I could use IMyService to determine if it is single or not (i.e. it doesn't matter what the implementation is , it should always be singleton). Is it possible?

+4
source share
1 answer

Thanks a lot to oleksii, who suggested looking at this question: How to determine if a type implements a specific type of interface in the comments to the question, the answer to this should do the following:

 container.Register(AllTypes .FromThisAssembly() .InSameNamespaceAs<IMyService>() .WithServiceDefaultInterfaces() .ConfigureIf(s => typeof(IMyService).IsAssignableFrom(s.Implementation), s => s.LifestyleSingleton(), s => s.LifestyleTransient())); 
+4
source

Source: https://habr.com/ru/post/1386352/


All Articles