27

Context:

Let's say I have the following "destination" class:

public class Destination
{
    public String WritableProperty { get; set; }

    public String ReadOnlyProperty { get; set; }
}

and a "source" class with the ReadOnly attribute on one of it's properties:

public class Source
{
    public String WritableProperty { get; set; }

    [ReadOnly(true)]
    public String ReadOnlyProperty { get; set; }
}

It's obvious, but to be clear: I am going to map from Source class to Destination class in the following way:

Mapper.Map(source, destination);

Problem:

What are the ways to configure Automapper to automatically ignore property with ReadOnly(true) attribute?

Constraints:

I use Automapper's Profile classes for configuration. I don't want to dirty up classes with Automapper-specific attributes. I don't want to configure Automapper for every single read-only property and cause a lot of duplication by this way.

Possible (but not suited) solutions:

1) Add attribute IgnoreMap to the property:

    [ReadOnly(true)]
    [IgnoreMap]
    public String ReadOnlyProperty { get; set; }

I don't want to dirty up classes with automapper-specific attributes and make it dependent from it. Also I don't want to add additional attribute along with ReadOnly attribute.

2) Configure Automapper to ignore the property:

CreateMap<Source, Destination>()
.ForSourceMember(src => src.ReadOnlyProperty, opt => opt.Ignore())

It is not a way because it forces me to do that for every single property everywhere and also causes a lot of duplication.

Deilan
  • 4,740
  • 3
  • 39
  • 52
  • So just to be clear, you want to ignore `ReadOnly` properties when they're the *source* properties? – Andrew Whitaker Feb 07 '15 at 16:10
  • @AndrewWhitaker, exactly. – Deilan Feb 07 '15 at 20:32
  • Just a wild question - why do you have the property on the destination type if you don't want it mapped? Why not just not have the property on the destination type? – Jimmy Bogard Feb 11 '15 at 13:56
  • @JimmyBogard, because the destination type is a domain model and the source type is a view model. As far as I have learned recently, mapping from a view model back to a domain model is not a good practice to use AutoMapper, right? :) – Deilan Feb 13 '15 at 08:58

3 Answers3

27

Write Extension Method as shown below:

public static class IgnoreReadOnlyExtensions
{
    public static IMappingExpression<TSource, TDestination> IgnoreReadOnly<TSource, TDestination>(
               this IMappingExpression<TSource, TDestination> expression)
    {
        var sourceType = typeof(TSource);

        foreach (var property in sourceType.GetProperties())
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(sourceType)[property.Name];
            ReadOnlyAttribute attribute = (ReadOnlyAttribute) descriptor.Attributes[typeof(ReadOnlyAttribute)];
            if(attribute.IsReadOnly == true)
                expression.ForMember(property.Name, opt => opt.Ignore());
        }
        return expression;
    }
}

To call extension method:

Mapper.CreateMap<ViewModel, DomainModel>().IgnoreReadOnly();

Vinkal
  • 2,964
  • 1
  • 19
  • 19
11

Now you could also use ForAllPropertyMaps to disable it globally:

configure.ForAllPropertyMaps(map =>
    map.SourceMember.GetCustomAttributes().OfType<ReadOnlyAttribute>().Any(x => x.IsReadOnly),
    (map, configuration) =>
    {
        configuration.Ignore();
    });
Dresel
  • 2,375
  • 1
  • 28
  • 44
  • really great.. but please check if is null.. ForAllPropertyMaps(m => m.SourceMember != null && m.SourceMember.GetCustomAttributes().OfType().Any(), (m, c) => { c.Ignore(); }); – alhpe Nov 07 '18 at 15:15
2

If you wanted to only map properties that have a certain attribute, in my case the [DataMember] attribute, I wrote a method based on the excellent reply above to handle this for both the source and destination:

public static class ClaimMappingExtensions
{
    public static IMappingExpression<TSource, TDestination> IgnoreAllButMembersWithDataMemberAttribute<TSource, TDestination>(
               this IMappingExpression<TSource, TDestination> expression)
    {
        var sourceType = typeof(TSource);
        var destinationType = typeof(TDestination);

        foreach (var property in sourceType.GetProperties())
        {
            var descriptor = TypeDescriptor.GetProperties(sourceType)[property.Name];
            var hasDataMemberAttribute = descriptor.Attributes.OfType<DataMemberAttribute>().Any();
            if (!hasDataMemberAttribute)
                expression.ForSourceMember(property.Name, opt => opt.Ignore());
        }

        foreach (var property in destinationType.GetProperties())
        {
            var descriptor = TypeDescriptor.GetProperties(destinationType)[property.Name];
            var hasDataMemberAttribute = descriptor.Attributes.OfType<DataMemberAttribute>().Any();
            if (!hasDataMemberAttribute)
                expression.ForMember(property.Name, opt => opt.Ignore());
        }

        return expression;
    }
}

It will be called as the other method did:

Mapper.CreateMap<ViewModel,DomainModel>().IgnoreAllButMembersWithDataMemberAttribute();
matsemann
  • 83
  • 1
  • 5