.net core tutorials

Using AutoMapper in Domain Objects with DotNet Core

A principle from Computer Science followed by most developers in software projects is the Separation of Concerns design principle.

This principle typically involves attempting to break down a software application into distinct sections addresses separate area of concern or buisness functionaliity.

In Software Engineering, this is often taking a step further and we attempt to break components down to conceptual models.  Hence we have what is defined as Logical Data Models.

A logical Data Model is a model of a specific problem domain of particular database management product or storage technology.  These usually include Relational tables, columns and object oriented classes.

This can also be further abstracted to a Conceptual Data model , which is a high-level description of the business’s informational needs. Typically including main concepts and main relationships between them.

In his book Domain Driven Design – Eric Evans ,  advocates the following design principle:

  • Placing the projects primary focus on core domain and domain logic
  • basing complex designs on a model of the domain
  • Initiate creative collaborations between technical and domain experts
  • Iterativaely refine conceptual models that address particular domain problems

The result of in two different objects models being defined in a project.

Entity Model

The Entity Data Model (EDM) is a set of concepts that describe the structure of data, regardless of its stored form. The EDM borrows from the Entity-Relationship Model.

Typically in .net projects this is implemented using the Entity Framework, which is an Object Relational Mapper (ORM)

Domain Model

Objects from the business specific area that represent something meaningful to the domain expert. Domain objects are mostly represented by entities and value objects.

A challenge for most software developers is managing the mapping between these often wildly different objects.

Fortunately great tools like Automapper  make this task infinitely easier .  One of the issue I found with most projects on which I have worked on which utilise Automapper, is that they have enevitably decided to implement all the Mapping logic in one central file, which is often referenced in the StartUp.cs and this file usually becomes a large file and very difficult to locate the actual mapping you may be interested in or even find a suitable place to insert a mapping logic.

A few years ago, I came across a strategy on a blog post some years back and have continued to evolve it.

In a nutshell the core concept is to store all the mapping logic a Domain object requires within the class. The benefits to this are that the class is infnitely easier to unit test and by simply opening the class you can in an instant see all the mapping logic that is related to it.

There is no longer the need the need hunt down several files in order to identify and fix bugs which may have resulted due to a mapping error.

There is a certain amount of set up required to achieve this, although not difficult. I will provide an example how I make use of this in developing my series of blogs post, regarding developing an ASP.net Core API for my Stop Web Crawlers WordPress Plugin.

I have also taken the time to wrap the code explained below into a re-usable nuget package for you to download and use in your projects.

Can’t wait to see it in action ?

Check out the code on GitHub

Get Code

Philosophica lMonkey

I have developed a custom library which is essentially a wrapper and a group of interfaces which makes use of the best named software products of all time Philosophical Monkey.

Philosophical Monkey is reflection helper that helps reflect on code in your project.

We’ve implemented this in a library we have called rather unimaginatively Threenine.Map

We have 3 interfaces in this library

We also have a MappingConfigurationFactory class which implements the Reflection logic making use of Philosophical Monkey

We simply add a reference to that project to the Api project. We now have to add the implementation of that class to the StartUp.cs

Create Domain Object

We can now create our Domain Object Classes. We will implement these in their own Class library project. For the most part they will be Plain Old CSharp Objects (POCO), however you will notice that they will implement at least one of the mapping interfaces we have defined.

Create a new class library project dotnet new classlib --name Api.Domain

We now need to create an empty Marker class which serves as an entry point to point for our reflector. We’ll just rename the autogenerated Class1 to Marker

In our Api.Domain project we’ll create a folder and name it Bots we’ll add new class and call it Referer.

Add a reference to AutoMapper and Api.Database.Entity

We are now ready to add the Attributes of the class and the mapping logic that is associated with this class.

Test it out

We have basically completed our mapping implementation so we should go ahead and test it.

I have added an additional controller to the project and called it RefereController and added a simple Get Method.

If we now start the application dotnet run and navigate to the URl http://localhost:5000/api/referer/get we will see that our application now returns the our Domain objects.




We explored how to make use of Automapper and reflection within code to enable placing mapping logic with Domain Object classes.

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee