Resolve container registered name from BuilderStrategy

Jun 5, 2008 at 6:19 PM
Edited Jun 5, 2008 at 6:58 PM
I would like to be able to resolve the container registered name of the object being built from within a BuilderStrategy. For example:

container.RegisterType(fromType, toType, "MyName");

then from within a BuilderStrategy, perhaps use the IBuildContext to find "MyName":

public class MyBuilderStrategy : BuilderStrategy
     public override void PreBuildUp(IBuilderContext context)
         string name = context.OriginalBuildKey.Name; // which would be "MyName" from above 

Is there a mechanism to do this?
Jun 5, 2008 at 9:36 PM
You can do:

NamedTypeBuildKey key = context.BuildKey as NamedTypeBuildKey;
if(key != null) {
    string name = key.Name;

Something like that should do what you want. But I'm not sure why you want this? What are you actually trying to accomplish?

Jun 8, 2008 at 7:53 PM
Thanks, that's exactly what I was looking for!

I have an existing application that loads services from app.config using Activator. I was working on one of the services and wanted DI so I made a container extension that reads the existing xml config and registers the services with Unity, then swapped the Activator code with container.Resolve. The issue is that at build-up I need to find the original app.config element that I used to register the service so I can get additional config info for processing in a BuildStrategy.
Jun 9, 2008 at 6:24 PM
Edited Jun 9, 2008 at 6:24 PM
Why are IBuilderContext.BuildKey & OriginalBuildKey typed as 'object' and not 'IBuildKey'?

Jun 9, 2008 at 7:47 PM
Historical. OB2 is used for other projects too. In many of them, the build key is just a Type. If OB was just for Unity, I just would have used NamedTypeBuildKey everywhere.