How to ignore all non existing properties on the destination?

See original GitHub issue

Hello guys,

We have a huge project using AutoMapper 2.x with static scope, and we are on a refactoring time and one of the itens on our backlog is to update AutoMapper to the last version (5.2). We saw that the static scope still exists (we want to keep it) and it is difficult to change now! 😦

In the AutoMapper 2.0, we have an extension method called IgnoreAllNonExisting, which just add a Ignore statement on the properties in the TSource that does not exists in the TDestionation. Look the code:


public static IMappingExpression<TSource, TDestination>IgnoreAllNonExisting<TSource, TDestination>
            (this IMappingExpression<TSource, TDestination> expression)
{
	var sourceType = typeof(TSource);
	var destinationType = typeof(TDestination);
	
	var existingMaps = Mapper.Configuration.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
	
	foreach (var property in existingMaps.GetUnmappedPropertyNames())
		expression.ForMember(property, opt => opt.Ignore());

	return expression;
}

the problem is that the Mapper.Configuration throws an exception when the program hits it:

ā€œMapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you’re using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.ā€

Then, following some instructions on another threads, I try to modify this method to this:


public static IMappingExpression<TSource, TDestination>IgnoreAllNonExisting<TSource, TDestination>
            (this IMappingExpression<TSource, TDestination> expression)
{
	expression.ForAllOtherMembers(x => x.Ignore());

	return expression;
}

But this method just add the default value for each property on the destination result, making the destination result object get like an empty object with all null properties and 0 (for numbers).

My question: is there any new way to implement this method to ignore properties that does not exists in the TDestination? Maybe a native method? Or how can I fix the existent method?

Thank you

PS: Help/Save us Jimmy 😦 haha

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
jbogardcommented, Dec 5, 2016

Yeah keep that static stuff. And this seems to be a very popular post from Stack Overflow that describes a wrong way to do things.

What you really want to do is either:

  1. Call ReverseMap after CreateMap
  2. Call CreateMap, passing in the desired MemberList enum

This ā€œIgnoreAllNonExistingā€ was a hack pretty much, so instead I have the ability to tell AutoMapper which list of members you want to validate against. That’s the intent of IgnoreAllNonExisting anyway. So just get rid of that extension method, and everywhere it’s being used, I’d pass in to CreateMap, ā€œMemberList.Noneā€.

5reactions
jbogardcommented, Dec 5, 2016

I’ve tried to up vote this on SO:

http://stackoverflow.com/a/31182390/58508

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoMapper: "Ignore the rest"?
This is an extension method I wrote that ignores all non existing properties on the destination. Not sure if it will still be...
Read more >
Auto-ignore non existing destination properties with AutoMapper
By default, AutoMapper tries to map all properties of the source type to the destination type. If some of the properties are not...
Read more >
AutoMapper Ignore Method in C# with Examples
By default, AutoMapper tries to map all the properties from the source type to the destination type when both source and destination type...
Read more >
Automapper 4.2 IgnoreAllNonExisting – ignore missing ...
A 4.2+ version of a popular extension method for ignoring properties which exist on the destination type but not on the source type...
Read more >
Automapper Ignore property mapping -Guidelines
Automapper Ignore property mapping one or multiple properties with Automapper in C# .NET with examples. DoNotValidate or Ignore method to ignore properties.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found