Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

c# - How to configure Automapper to automatically ignore properties with ReadOnly attribute?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...