简体   繁体   中英

mapping dto to entities? maybe automapper? should properties match? linq selection

I am trying to create a LINQ-to-SQL solution using what I will call basic linq-to-sql. It is not a full ORM solution via EF or anything like that. The point right now is to leverage the power of LINQ and reduce stored procedures (as well as some other benefits).

I have done some work with DTOs and entities before... but that too was a custom implementation. In that implementation automapper was used. I know automapper "works best" when entitiy properties match DTO properties. But what if they don't? Can you configure it in a custom way?

I suppose a bigger question is... is this a real solution for the fact that you cannot instantiate "LINQ-mapped" "entities" in a query. To be clear you cannot use linq and select a new "entity" you would have to select an unmapped DTO. What I am ultimately trying to do is avoid "mapping" the entity to the DTO in a given query. I suppose this is not that big of a deal but does something like automapper solve this problem?

I'm not exactly sure what you're asking but:

I know automapper "works best" when entitiy properties match DTO properties. But what if they don't? Can you configure it in a custom way?

If you're asking what I think you're asking you should look into .ForMember and overriding the Convert function. There are examples of both on the wiki: .ForMember: https://github.com/AutoMapper/AutoMapper/wiki/Projection Convert: https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

Automapper does work "best" when dtos and entities match up because it's super easy to set up a map between the two is their property names and types match. When they don't match you need to manually select properties with .ForMember or manually do the whole conversion by overriding the Convert function.

I hope this is a little helpful. Pop a comment below if it wasn't or you need more info.

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