简体   繁体   中英

Automapper vs Dapper for mapping

This question is to verify if the current implementation is the right way to go about in terms of best practices and performance. So far in all my previous companies I have been using Auto Mapper to map relational objects to domain model entities and domain model entities to Dtos. The ORM tools have been Entity framework. In my current company they are using Dapper as ORM tool and do not use AutoMapper as they say Dapper does the mapping for you internally. So the way they have structured the project is create a separate class library project that contains Dtos and reference the Dtos in Dataccess and Business layer. The query returned by Dapper is internally mapped to the Dtos. These Dtos are returned to the Business layer and so on.

For example

In the code below the Participant function is Dto.

Repository file in DataAccess layer

 public List<ParticipantFunction> GetParticipantFunctions(int workflowId)
        {
            // Update the Action for Participant
            string selectSql = @"SELECT [WFPatFunc_ID] AS WFPatFuncID
                        ,[WFFunction]
                        ,[SubIndustryID]
                        ,[DepartmentID]
                    FROM [dbo].[WF_ParticipantsFunctions]
                    WHERE [DepartmentID] = (SELECT TOP 1 [DepartmentID] FROM [dbo].[WF] WHERE [WF_ID] = @workflowId)";

            return _unitOfWork.GetConnection().Query<ParticipantFunction>(selectSql, new
            {
                workflowId = workflowId
            }).ToList();
        }

The reason what I have been told by the developers is that AutoMapper would be just an overhead and reduce speed and since Dapper does mapping internally, there is no need for it.

I would like to know if the practice they are following is fine and have no issues.

There is no right or wrong here. If the current system works and solves all their requirements, then great: use that! If you have an actual need for something where auto-mapper would be useful, then great: use that!

But: if you don't have a need for the thing that auto-mapper does (and it appears that they do not), then... don't use that?

Perhaps one key point / question is: what is your ability to refactor the code if your requirements change later . For many people, the answer there is "sure, we can change stuff" - so in that case I would say: defer on adding an additional layer until you actually have a requirement for an additional layer.

If you will absolutely not be able to change the code later, perhaps due to lots of public-facing APIs (software as a product), then it makes sense to de-couple everything now so there is no coupling / dependency in the public API. But: most people don't have that . Besides which, dapper makes no demands on your type model whatseover other than: it must look kinda like the tables. If it does that, then again: why add an additional layer if you don't need it?

This is more of an architecture problem and there is no good or bad.

- You are not directly using data object so you can use Attributes for mapping and stuff not needed in your UI. - 您不是直接使用数据对象,因此您可以使用属性进行映射以及UI中不需要的内容。 That way you can have the DTO's in a library that has no dependencies to your data access stuff.( Note you could do this with fluent mapping but this way you can use what you like )

- If you domain model changes your public interface will stay the same. - 如果域模型更改,您的公共接口将保持不变。 Lets say you add a property to the model all the stuff you already build wont be getting the new field in you JSON for no reason.

- This is why Microsoft started pushing dto's if I rember correctly I think CodePlex( Not 100% sure it was them ) was using EF entitles directly to add stuff to the database. - 这就是为什么微软开始推动dto的原因如果我正确地使用,我认为CodePlex( 不是100%确定它们是他们 )直接使用EF权限来添加数据库。 Someone figure this out and just expanded the Json with stuff that he was not allowed to access, for example if you have a post that has a reference to a user you could change the role of the user by adding a new post because of change tracking. There are ways to protect you self from this but security should always be an opt-out not opt-in.

I like to use dto's when I need to expose BI level to a public interface. For example if I have an API that has System operations like or .

- Generally not using dto's will run faster. - 一般不使用dto会运行得更快。 No extra step of mapping. Projections help with this but you can really optimize when you know what you need to do.

- Sometime a large architecture is an overkill and you just want to get things done. - 有时大型架构是一种矫枉过正,你只是想完成任务。 And you are not just doing copy paste of your properties for most of you time.

- This is opposite to the security sometimes you want to use the same api to do more then one thing. - 这与安全性相反,有时您希望使用相同的api来做更多事情。 For example if you want to have the UI decide what it wants to see from a big object. Like select and expand. (Note this can be done with dto's but if you ever tried to do expand with dtos you know how tricky it can get)

I use it when I need to expose the data access level to the client, if you need to use Breeze or JayData and/or Odata. With api like and

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM