Conditional registration

vmelamed asked for this feature about 1 year ago — 1 comment

vmelamed commented about 1 year ago

I would like to see a registration method that allows for conditional registration. Let's call it RegisterTypeIfNot. I.e. Unity will register the typeFrom/typeTo/resolveName combination only if it is not already registered. This can be very useful when you want to register types and implementations generally in code but in some instances you would like to override the implementation type from configuration. E.g. we have different providers for postal address verification; or some provider is not accessible at the moment or is too expensive to be called from dev or qa environment - replace it with a mock provider. Unity currently provides the method IsRegistered that could be used in combination with RegisterType but 1. it is slow (unless you guys fixed it already) and 2. RegisterTypeIfNot should be a thread safe method because you might have a number of registrars that try to register one and the same common type (e.g. each service registers only the types that it needs and some may be used everywhere, e.g. IRepository or something like that).

Join the discussion!

with GitHub to comment