Sam
Sam

Reputation: 15770

Using AutoMapper on shared properties?

Can AutoMapper map shared properties?

Entity

public class Entity
{
    public static string SomeProperty {get; set;}
    ...
    ...
    ...
}

View Model

public class ViewModel
{
    public string SomeProperty {get; set;}
    ...
    ...
    ...
}

Upvotes: 0

Views: 70

Answers (2)

bloparod
bloparod

Reputation: 1726

You should use a code like this:

Mapper.CreateMap<Entity, ViewModel>()
    .ForMember(
        dest => dest.SomeProperty,
        opt => opt.MapFrom(src => Entity.SomeProperty));

Upvotes: 0

Attila
Attila

Reputation: 28772

Although I have not used AutoMapper yet, I see no reason why you would not be able to achieve what you are looking for. Based on the project's documentation on Projection, you can write a projector:

Mapper.CreateMap<Entity, ViewModel>()
  .ForMember(dest => dest.SomeProperty, opt => opt.MapFrom(src => src.SomeProperty));

// Perform mapping
ViewModel form = Mapper.Map<Entity, ViewModel>(entity);

Upvotes: 1

Related Questions