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);
What are the ways to configure Automapper to automatically ignore property with ReadOnly(true)
attribute?
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.
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.
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.
So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.
If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
AutoMapper is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types. As an example, you might need to map the DTOs (Data Transfer Objects) in your application to the model objects.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With