views:

21

answers:

1

This is similar to This question but the answer doesn't solve my problem.

I want to register a default implementation for an interface in code but be able to override that in a config that is read BEFORE the code is run.

I want to do something like

if (!container.IsImplementationRegistered(typeof(TInterface))
{
  container.RegisterType<TInterface, TImplementation>();
}

I can't use TryResolve as I will be registering several types in a row that might have dependencies of their own that aren't yet registered, leading to an erroneous override.

Will Unity 2.0 enable this or is there a way to do it in 1.2?

A: 

Maybe you can try with a child container.

  1. Create a container in the code
  2. Create a child container using CreateChildContainer()
  3. Configure the child container reading from the config file
  4. Use the child container in the application.

If the child container cannot resolve a type, Unity goes to the parent container to resolve.

onof
It would be a lot easier if the reference to pass around to resolve and to register would be the same. As it stands I'll have to pass the child to things that need to resolve and the parent to things that need to register. As I have modules that need to do both they now need two references or special code to get the parent before calling register. Either way they need to know about the structure of the container. I wanted to avoid that but this will have to do until we move to Unity 2.0 or Windsor.