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.
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 :)
Parse your XML with XPATH and find a value of an element based on the value of another element
When parsing and reading XML files in your C# code, you can use the XDocument or XElement objects to access it with LINQ and the Descendants or Elements properties. However, there is another option; you can use XPATH for traversing the XML. In this example we are going to see how you access a value of an XML-element, by matching the value of another element with a value given from the user.
How to create an empty or with one element IReadOnlyCollection for your unit-tests
When using the Moq framework for mocking your C# classes, you will have do define what dummy value or values a method of the mocked classes returns. For that we use the Setup method and in the Returns method we define the returned value. The question now is, which dummy value to return when your methods return a IReadOnlyCollection collection.
How to pass parameters to an AutoMapper Profile constructor aka Dependency Injection for AutoMapper
While using AutoMapper for mapping different objects in your application, you might come into a situation where you need business logic from other services while you are mapping. For that we will have to inject these services into our Profile class.