Подтвердить что ты не робот

Как настроить Automapper для автоматического игнорирования свойств с помощью атрибута ReadOnly?

Контекст:

Скажем, у меня есть следующий класс "destination":

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

    public String ReadOnlyProperty { get; set; }
}

и "исходный" класс с атрибутом ReadOnly для одного из его свойств:

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

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

Это очевидно, но чтобы быть ясным: я собираюсь отобразить класс Source в класс Destination следующим образом:

Mapper.Map(source, destination);

Проблема:

Какими способами можно настроить Automapper автоматически игнорировать свойство с атрибутом ReadOnly(true)?

Ограничения:

Я использую классы Automapper Profile для конфигурации. Я не хочу загрязнять классы атрибутами, специфичными для Automapper. Я не хочу настраивать Automapper для каждого отдельного свойства только для чтения и вызывать много дублирования таким образом.

Возможные (но не подходящие) решения:

1) Добавить атрибут IgnoreMap в свойство:

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

Я не хочу загрязнять классы с помощью атрибутов autoapper и делать их зависимыми от него. Также я не хочу добавлять дополнительный атрибут вместе с атрибутом ReadOnly.

2) Настройте Automapper, чтобы игнорировать свойство:

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

Это не способ, потому что он заставляет меня делать это для каждого отдельного свойства всюду, а также вызывает много дублирования.

4b9b3361

Ответ 1

Напишите Метод расширения, как показано ниже:

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;
    }
}

Чтобы вызвать метод расширения:

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

Ответ 2

Теперь вы можете также использовать ForAllPropertyMaps, чтобы отключить его глобально:

configure.ForAllPropertyMaps(map =>
    map.SourceMember.GetCustomAttributes().OfType<ReadOnlyAttribute>().Any(x => x.IsReadOnly),
    (map, configuration) =>
    {
        configuration.Ignore();
    });

Ответ 3

Если вы хотите отображать только свойства с определенным атрибутом, в моем случае атрибут [DataMember], я написал метод, основанный на превосходном ответе выше, чтобы обработать это как для источника, так и для назначение:

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;
    }
}

Он будет вызываться по другому методу:

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