Basic Mapping Question

Mar 19, 2007 at 7:25 AM
Hi, I'm just starting to get my head around the object builder.

I've got an interface defined in an assembly and want to use Object Builder to construct a concrete version of it. What I want to do is to be able to set the concrete class based on a configuration file. Essentially provide a mapping from the interface to the concrete class to instantiate.

Ultimately I want to be able to do this: WorkItem.SmartParts.AddNew<IProduct> and configure the object to create. This all goes down to a BuildUp call and I think that can do the mapping.

How can I do that?

Mar 19, 2007 at 6:10 PM
Hi, the following code sets the Type Mapping policy and says that when a "MyFoo" IFoo Interface is requested a new instance of Foo will be created.

public class TypeMappingFixture
public void BuildInterfaceReturnItsConfiguredImplementation()
Builder builder = new Builder();
builder.Policies.Set <ITypeMappingPolicy>(new TypeMappingPolicy(typeof(Foo), "MyFoo"),
typeof(IFoo), "MyFoo");

IFoo result = builder.BuildUp<IFoo>(new Locator(), "MyFoo", null, new PolicyList[] { });

public interface IFoo { }
public class Foo : IFoo { }

You can use the test code including it in test project directly and see the results.


Adrian Alonso
Mar 20, 2007 at 2:52 AM
Thanks, but I can't get it to work.

My code looks like this
Builder builder = new Builder();
    (new TypeMappingPolicy(typeof(ProductOverviewView), "MYProductOverviewView"),
        typeof(IProductOverviewView), "MYProductOverviewView");
IProductOverviewView ipoView = builder.BuildUp<IProductOverviewView>
    (new Locator(),"MYProductOverviewView", null, new PolicyList[] {});

I end up getting a NullReferenceException. I'm doing this in the context of a SCSF & CAB project. I can see that the type mapping policy is going to be needed, but I'm not sure where to put it to affect the applicaiton as a whole.
Mar 23, 2007 at 1:46 AM
Try using the overload of builder.BuildUp that does not include the empty transient PolicyList. In my experience, always use the overload of BuildUp that takes the fewest number of arguments that you need to express your intent. The transient PolicyList has caused me problems when it was empty.