My articles about the .NET AutoMapper tool

How to map a property of a parent object to a property inside a list of objects with AutoMapper

Mapping objects with AutoMapper can save you a lot of time and repeated code. However, some mappings are more tricky than others. Consider the example where we want to map two lists of objects and moreover, a specific property of each of these list-objects should contain the value of a property from a parent object.

Let us see this scenario with a concrete example:

Read the complete article

Map one C# Record to another with AutoMapper

Records were introduced in C# 9.0 and are a handy way of holding data, instead of using properties of classes. I recently came along the task of mapping one record of one type to one of another type by using AutoMapper. At first I thought I will map them exactly as I would map the properties of two different classes. However, it was trickier than that :)

Read the complete article

How to skip the mapping between properties of a source and a target object in AutoMapper

As I did in my previous article, I focus again on the AutoMapper framework.

When I map one object to another, I often deal with a destination object that contains LESS properties than the source object. If I take no action, an exception is going to be thrown. For that we will have to declare the skipped properties by using the DoNotValidate method when we define the mapping (CreateMap) between the two objects:

Read the complete article

Use a custom function when mapping one property to another with Automapper

Automapper is a handful tool for mapping the properties of two different type objects together. Such mappings are very common when you are dealing with a multi-tier architecture in your program. For example you want to map the entity object, which contains data from the database, with the UI-model object of your WebApi action.

For basic mapping examples refer to the Automapper documentation. In this article we will consider the following advanced example:

  • Your entity model contains a property with a non-serialized string that you store in a table of your database

  • Based on some condition checking, you want to or do not want to deserialize this string and store it into a property of different type of your UI-model object

For that you need a mapping between two properties of different type and also a function that runs every time to do this transformation:

Read the complete article