AutoMapper - Conditional Mapping
Automapper has got another feature called conditional mapping which provides you to do mapping based on conditions. For example if you have a custom mapping for FullName property which uses FirstName and properties of source instance. In that case you want to make sure that these two fields are not null or whitespace, otherwise it will produce some undesirable results as shown in the example below.
Here we have created a custom mapper to produce the Name in the destination as a combination of FirstName and LastName from the source. But in the Person instance the value of FirstName is null and when the mapping is done it's showing only the value from LastName. But our intention was to show the value in Name field if and only if there is value in both FirstName and LastName field
In this scenario we can use the Condition method to do this validation and produce the output as desired
In this example I have done mapping two times to show the conditional mapping at work. While doing the first mapping, provided null for the FirstName property and provided value for LastName. Our conditional mapping got validated and failed, so the resulting value for the Name property in personVM object was null. For the second mapping gave values to FirstName and LastName in person object and since the conditional validation passed, the value for Name property in personVM got correctly mapped.
Note: All these examples are based on 4.2.1 version of AutoMapper and there's some significant changes in library by removing the static API. Please refer here for futher details
Connecting Azure Blob Storage account using Managed Identity
Securing Azure KeyVault connections using Managed Identity
Manage application settings with Azure KeyVault
Adding Serilog to Azure Functions created using .NET 5