TypeIsAssignable in BuildUp

Mar 28, 2008 at 2:56 AM
Consider the following scenario:

I want to create an object that retrieves its property values based off the properties of another arbitrary runtime object.

I'd like to do the following:
IUnityContainer container = UnityHelper.Create();
EntityView view = container.Resolve<EntityView>();
 
Entity entity = container.BuildUp<Entity>( view );

I've created an extension method that allows the passing of view, However, when the container attempts to build, an exception throws when the type of view is checked.

What is the recommended way of getting around this? Is it possible to remove that Guard?
Mar 29, 2008 at 5:11 AM
So let me paraphrase what you're saying so I understand:

You want to use the Buildup method to pass a constructor parameter into the creation of another object?

That is not what BuildUp does. BuildUp is used to perform injection on an object that somebody else created (like a web page or web service instance).

What you're doing is outside the current behavior of the container. The easiest thing I could think of would be to keep your extension method, or write an extension, and in your "Buildup" method, create a child container, stuff the extra object into the child, do a Resolve, and then throw away the child container.

Child container creation should be reasonably cheap,
Mar 29, 2008 at 2:19 PM


ctavares wrote:
What you're doing is outside the current behavior of the container. The easiest thing I could think of would be to keep your extension method, or write an extension, and in your "Buildup" method, create a child container, stuff the extra object into the child, do a Resolve, and then throw away the child container.


That's exactly what I ended up doing. Still learning the ropes here... :)