What is the safest way to prevent a duplicate registration with Funq.Container? I have a utility class which loads several common items and need to protect against accidentally re-registering one of the same resources. It would be fine if the registration would REPLACE the first, or be ignored. Throwing an exception would not be acceptable.
Drazen Dotlic:
Some other DI implementations I’ve used do exactly what you want - latest registration wins. You can of course make it so that multiple registrations work too.
Adapting ServiceStack to use a different DI container is trivial, so if you’re not against it in principle, I’d consider using a different product (I know it’s convenient to have Funq included ‘in the box’).
I deliberately won’t say which DI product I’m using to avoid a holy war about whose DI product is the best
Phillip Powers:
I’m interested in knowing what DI containers people are using - not for any sort of argument, but just for the information.
Additionally, I’d be interested in knowing whether you are/have used the container with Mono.
Currently, I’m using the built-in Funq, but have traditionally used Windsor.
Fredrick Lackey:
+Phillip Powers +Stephen Brannan All good info, but please create a new thread. Still looking for input on the safest way to prevent a duplicate registration with Funq.Container.
You could always do something like create your own Safe registration extension methods, e.g:
void SafeRegister<T>(this Container container, Func<Container, T> factory)
{
var exists = container.TryResolve<T>();
using (exists as IDisposable){}
if (!Equals(exists, default(T))) return;
container.Register(factory);
}
This wont register it if it already exists.