How to simply map an NHibernate ISet to IList using AutoMapper

后端 未结 2 1511
温柔的废话
温柔的废话 2020-12-16 08:39

I\'m trying to use AutoMapper to map from DTO\'s to my Domain.

My DTO\'s might look like this:

public class MyDTO
{
    public string Name { get; set         


        
相关标签:
2条回答
  • 2020-12-16 08:46

    The answer:

    1. You have to create your own IObjectMapper to map a custom collection like ISet
    2. Create your own configuration instance with all the standard objectmappers and your new setobjectmapper.
    3. Use an IMappingEngine instance created with the configuration with your own objectmapper instead of the static AutoMapper.Mapper class.

    Some remarks:

    • It's easy to configure the IMappingEngine construction in a inversion of control container.
    • The source of automapper itself might help you with creating the IObjectMapper implementation.
    • You are using automapper on the opposite way for what it is designed for: It's designed to map complex objects to simple objects. You try to map a simple DTO to a complex entity. (This does not mean that what you want is hard to do with automapper, but you might get different problems in the future)
    • You are using the anemic domain model anti pattern. Domain should hold all the business logic, so it should not expose a complex collection like ISet (and no public setters for collections at all)
    0 讨论(0)
  • 2020-12-16 09:12

    If your persistence layer is simple, using UseDestinationValue() will tell AutoMapper to not replace the underlying collection:

    ForMember(dest => dest.Children, opt => opt.UseDestinationValue())

    However, if it's not simple, we just do the updating manually back into the domain. The logic generally gets more complex to update the domain model. Doing reverse mapping puts constraints on the shape of your domain model, which you might not want.

    0 讨论(0)
提交回复
热议问题