Контекст:
Скажем, у меня есть следующий класс "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())
Это не способ, потому что он заставляет меня делать это для каждого отдельного свойства всюду, а также вызывает много дублирования.