简体   繁体   English

如何配置Automapper以自动忽略具有ReadOnly属性的属性?

[英]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: 和一个“源”类,其中一个属性具有ReadOnly属性:

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: 很明显,但要明确:我将通过以下方式从Source类映射到Destination类:

Mapper.Map(source, destination);

Problem: 问题:

What are the ways to configure Automapper to automatically ignore property with ReadOnly(true) attribute? 有哪些方法可以将Automapper配置为使用ReadOnly(true)属性自动忽略属性?

Constraints: 约束:

I use Automapper's Profile classes for configuration. 我使用Automapper的Profile类进行配置。 I don't want to dirty up classes with Automapper-specific attributes. 我不想弄脏具有Automapper特定属性的类。 I don't want to configure Automapper for every single read-only property and cause a lot of duplication by this way. 我不想为每个只读属性配置Automapper,并且通过这种方式导致大量重复。

Possible (but not suited) solutions: 可能的(但不适合)解决方案:

1) Add attribute IgnoreMap to the property: 1)将属性IgnoreMap添加到属性:

    [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. 另外,我不想添加其他属性以及ReadOnly属性。

2) Configure Automapper to ignore the property: 2)配置Automapper忽略该属性:

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. 这不是一种方式,因为它迫使我为每个地方的每一处房产做这件事,也造成很多重复。

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

Now you could also use ForAllPropertyMaps to disable it globally: 现在您还可以使用ForAllPropertyMaps全局禁用它:

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

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: 如果你只想映射具有特定属性的属性,在我的情况下是[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;
    }
}

It will be called as the other method did: 它将被调用为另一种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM