views:

202

answers:

1
 Mapper.CreateMap<BusinessObject, Proxy.DataContacts.DCObject>()
 .ForMember(x => x.ExtensionData, y => y.Ignore())
 .ForMember(z => z.ValidPlaces, a=> a.ResolveUsing(typeof(ValidPlaces)));
 Mapper.AssertConfigurationIsValid();  

 proxydcObject = Mapper.Map<BusinessObject, Proxy.DataContracts.DCObject>(_instanceOfBusinessObject); //throws an exception saying ValidPlaces could not be resolved

 public class BusinessObject
 {
     public Enum1 Enum1 { get; set; }
     public List<ValidPlaces> ValidPlaces{ get; set; }
 }

 public class ValidPlaces
 {
     public int No { get; set; }
     public string Name { get; set; }
}

public class DCObject
{
    [DataMember]
    public Enum1 Enum1 { get; set; }
    [DataMember]
    public List<ValidPlaces> ValidPlaces{ get; set; }
}

Mapper.CreateMap works find when Mapper.AssertConfigurationIsValid(); (No exceptions thrown on this line) is called but when I actually call into the WCF service on the next line which is not shown here the Automapper throws and exception saying ValidPlaces could not be mapped.Works fine if I put Ignore() on the ValidPlaces property but ideally want that passed.

Any AutoMapper experts out there pls help.

+1  A: 

You should be able to ditch the line for ValidPlaces:

Mapper.CreateMap<BusinessObject, Proxy.DataContacts.DCObject>()
 .ForMember(x => x.ExtensionData, y => y.Ignore());

Value resolvers are for a custom class to do the value resolution, and must be of type IValueResolver. That's some defensive coding I should put in place. But for List of T -> List of U, as long as AutoMapper can map T -> U, it will work. In your situation, since T == U, then you don't have to do anything extra. Otherwise, you have to map T to U (but not List of T -> List of U).

Jimmy Bogard
My DCObject has extensiondata but business object doesnt have this property and hence I put that in place else I get an error. Also T==U in my case but I want List<T> translated to List<U>
chugh97
That's fine. AutoMapper supports List<T> automatically, without any extra configuration. I can do this: Mapper.Map<int[], List<int>>(new [] {1, 2, 3});With zero configuration. You only need to register element types, and since the element types are identical in your case, no extra configuration is needed. AutoMapper supports assignable types out-of-the-box.
Jimmy Bogard