-1

I've come across these questions: Merge two objects using AutoMapper , C# Merge objects with automapper, and this one seemed to the most helpful: Automapper - Multi object source and one destination, however it does not seem to be using version 10 of the AutoMapper code. Everything has it's own errors with the latest version of AutoMapper.

Additionally, I just started on StackOverflow so I cannot comment yet on that question. I can only ask my own.

It appears in AutoMapper that you have to use something called a "Profile" now (see their documentation here: https://docs.automapper.org/en/stable/Dependency-injection.html#asp-net-core). Therefore, my question is, how would one create a "Merge" type of mapping in version 10 of AutoMapper in .Net 5 or greater (so using Dependency Injection, etc.)

As a request, if you give specific code snippets, can you give where that code should go as well, such as "In Startup.cs" or "Create a new class", etc.

Since AutoMapper is so new to me, some of the library lingo is still new to me.

Thank you all for your help.


UPDATE on 9/21/2021 at 11 am EDT Some context on why I'm trying to merge: This is the scenario that I have generalized to highlight the main points:

I have 2 data sources. One I contact via API. The other is via MsSql.

The API stores Person Information (names, addresses), whereas the Sql stores ProgramPerson (PersonId from above, License # for Program, DateOfLicense, etc.)

Therefore, I would like to get information from the API, then information from the Sql Server and end up with one C# Model. That way when I need to update a Person, I can pass around 1 model that implements the interfaces for the Sql and the Api calls.

Kevin
  • 26
  • 5
  • The model of the person shouldn't know or care *anything* about where the data came from. Just get the data, and set the appropriate fields on your person object. AutoMapper seems like overkill and, as Piotr says in their answer, not recommended for this use case. – Peter K. Sep 21 '21 at 15:07
  • He @Peter K. I appreciate your feedback. So the reasoning I why I wanted this is that we're still in active development and there is a possibility we'd have to change fields on the Shared API, or on the Sql side. My goal was to allow AutoMapper to dynamically create the maps while avoiding Reflection every time. Also, to help make sure every field is mapped when possible and not neglected. I figured AutoMapper would always identify the mapping sequence if a change on a model occurred, versus examining a data service or more after a change on one model or the other. – Kevin Sep 21 '21 at 15:11
  • Sounds dangerous, with everything in flux. I think you're better off having a human review all changes to mappings, rather than relying on the "magic" of AutoMapper. YMMV – Peter K. Sep 21 '21 at 15:26
  • 1
    Fair enough. We're making a shared api within our organization, sharing the library of interfaces and models (via versioned Nuget Packages). But nevertheless, I do appreciate your input and feedback. The historical versions of this question were well-received, so I didn't expect that this was a dangerous thing. Thanks again. – Kevin Sep 21 '21 at 15:37

1 Answers1

1

I don't think it's a very good idea to use Automapper to merge two objects. I think it works best for simple cases. Some advices from the author of the library: https://jimmybogard.com/automapper-usage-guidelines/

"X DO NOT use AutoMapper except in cases where the destination type is a flattened subset of properties of the source type"

Piotr Kammer
  • 81
  • 1
  • 4
  • I appreciate the reply. So this is the scenario that I have generalized to highlight the main points. I have 2 data sources. One I contact via API. The other is via Sql. The API stores Person Information (names, addresses), whereas the Sql stores ProgramPerson( PersonId from above, License # for Program, DateOfLicense, etc.) Therefore, I would like to get information from the API, then information from the Sql Server and end up with one C# Model. That way when I need to update, I can pass around 1 model that implements the interfaces for the Sql and the Api calls. – Kevin Sep 21 '21 at 14:59
  • Therefore, in some sense, the two things I want to merge together both have an interface (IModel1 and IModel2), that the MERGED Model would implement (ExampleClass: IModel1, IModel2). Model 1 would provide properties 1-5, and Model 2 would provide properties 6-10. – Kevin Sep 21 '21 at 15:04