4

I've begun implementing this;
Automapper, mapping to a complex object but figured there must be a better way.

So I created this;

Mapper.CreateMap<StoreTransportWindow, CSVWindow>()
    .ForMember(dest => dest.DC, opt => opt.ResolveUsing(fa => fa.DC.number))
    .ForMember(dest => dest.Type, opt => opt.ResolveUsing(fa => fa.Type))
;

Mapper.CreateMap<Store, CSVStore>()
    .ForMember(dest => dest.StoreName, opt => opt.ResolveUsing(fa => fa.name))
    .ForMember(dest => dest.StoreNumber, opt => opt.ResolveUsing(fa => fa.number))
;

Now I'd like to use the above mappings in the primary map;

Mapper.CreateMap<Store, CSVLineObject>()
    .ForMember( dest => dest.store, opt => opt.ResolveUsing(/* This is where I'd like to use the above Store to  CSVStore mapping */)) 
;

Is this possible?

edit

public class CSVStore
{
    public string StoreNumber { get; set; }
    public string StoreName { get; set; }
}

public class CSVWindow
{
    public string Type { get; set; }
    public string DC { get; set; }
    public string TPC { get; set; }

public class CSVLineObject
{
    public CSVStore store { get; set; }
    public List<CSVWindow> storeWindows { get; set; }
Community
  • 1
  • 1
griegs
  • 22,624
  • 33
  • 128
  • 205
  • Can you show what you're trying to map? (ie, what is `CSVLineObject`, and what is the type for `dest.store`). Also note that it looks like you should be using `MapFrom` instead of `ResolveUsing`, and the second property in the first mapping (`dest.Type`) is automatically mapped by convention. – Mightymuke Apr 19 '13 at 01:50
  • Whats `Store`? I'm trying to work out why you're mapping `Store` to `CSVLineObject` instead of doing something like `CSVLineObject.store = Mapper.Map(store)`. I guess if you wanted to you could do something like `Mapper.CreateMap().ForMember(dest => dest.store, opt => opt.MapFrom(src => Mapper.Map(src)));` – Mightymuke Apr 19 '13 at 02:26
  • Store is simply another object. It has a number, a name and a collection of StoreTransportWindow. I think the actual objects are kinda irrelevant. I'm more after an idea of how I can use a map inside another map – griegs Apr 19 '13 at 02:42
  • You might also find this interesting: http://stackoverflow.com/questions/14875075/automapper-what-is-the-difference-between-mapfrom-and-resolveusing – Mightymuke Apr 19 '13 at 03:11

1 Answers1

1

As mentioned in the comment, the initial mappings should probably be more like:

Mapper.CreateMap<StoreTransportWindow, CSVWindow>()
    .ForMember(dest => dest.DC, opt => opt.MapFrom(src => src.DC.number));
// Mapping for property Type not required

Mapper.CreateMap<Store, CSVStore>()
    .ForMember(dest => dest.StoreName, opt => opt.MapFrom(src => src.name))
    .ForMember(dest => dest.StoreNumber, opt => opt.MapFrom(src => src.number));

Now say you have the following:

public class Source
{
    public Store Store { get; set; }
}

public class Destination
{
    public CSVStore Store { get; set; }
}

Then the following mapping will suffice (as you've already defined the nested mapping Store to CSVStore):

Mapper.CreateMap<Source, Destination>();

However if Destination was more like this:

public class Destination
{
    public CSVStore CSVStore { get; set; }
}

Then you'll need to explicitly define the properties to be mapped:

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.CSVStore, opt => opt.MapFrom(src => src.Store));

(Note that the mapping from Store to CVStore is applied automatically.)

If for some reason you do need to explicitly define a nested mapping, you can do something like this:

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.destproperty,
               opt => opt.MapFrom(
                   src => Mapper.Map<SrcType, DestType>(src.srcproperty));

I have needed to use that at times, but not very often as the default functionality takes care of it for you automatically.

I can provide more details if required if you can expand on your requirements.

Mightymuke
  • 5,094
  • 2
  • 31
  • 42
  • That last snippet seems to make more sense in the context of the question i think. What you are essentially doing is creating the map within the map rather than referencing another map previously created right? Thanks for all your help so far btw. – griegs Apr 19 '13 at 03:02
  • Yes, but as mentioned you don't normally need to do that. Its referenced automatically based upon the nested types being mapped. – Mightymuke Apr 19 '13 at 03:06
  • This is one situation where I had to explicitly define the mapping: http://stackoverflow.com/questions/13334938/automapper-flattening-of-nested-mappings-asks-for-a-custom-resolver – Mightymuke Apr 19 '13 at 03:08