Dmitriy Sosunov
Dmitriy Sosunov

Reputation: 1113

AutoMapper and different objects

I have list of objects like the following:

A: IBaseType
{
   string X
   DateTime Y
   int Z
}

and I want to get after mapping three different objects that are produced based on their properties.

For instance:

Mapper.Map<IList<A>, IList<IBaseType>>(list); 

and in output collection get different objects

X: IBaseType
{
  string X;
}

Y: IBaseType
{
   DateTime Y
}

and so on.

Or in something like this:

Mapper.CreateMap<Item, ItemModel>().FromMap(d=>d.Conditions, opt=>(there some like to IValueResovler );

Upvotes: 0

Views: 320

Answers (1)

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

I think that best way is to implement custom mapping functionality.

Something like this:

public IBaseType Map(A item)
{
     if ( /*your condition*/ )
          return new X(){ X = item.X}

     else if ( /*your condition*/ )
          return new Y(){ Y = item.X}
}

and use this method

List<IBaseType> result = new List<IBaseType>() ;
list.Foreach(x => result.Add(Map(x)));

Upvotes: 1

Related Questions