Mar 30, 2011 at 5:09 AM
Edited Apr 1, 2011 at 4:29 AM
There are two ways you could accomplish what you want. First,do reflection to register the types ahead rather than figure things out during the actual resolving of the type. You would do the logic of the registration in the Initialize method
of a custom unity extension. In that way, configuration type mapping will override whatever you registered in the custom extension as long as you added the extension first before loading the container from the config or if you added the extension in
the config file rather than adding it programmatically.
The other way is the way you're currently trying to figure out right now. You could check if there's an existing type mapping for the current type that is to be resolved using this code:
var policy = context.Policies.Get<IBuildKeyMappingPolicy>(context.OriginalBuildKey);
If the variable policy is null, it means no type mapping for the current type exists yet. To add one and persist it, you could add a IBuildKeyMappingPolicy in the context.PersistentPolicies object:
if(policy == null)
context.PersistentPolicies.Set(new BuildKeyMappingPolicy(new NamedTypeBuildKey(mapToType)), new NamedTypeBuildKey(context.BuildKey.Type));
where mapToType variable is the type of the concrete implementation of the interface.
You may want to consider the first approach since it doesn't add a logic everytime the Resolve method executes.
Global Technologies and Solutions