Search code examples
domain-driven-designautomapperabp-framework

Does AutoMapper pattern violate principle of DDD?


I am trying Abp framework recently and happily found that it is a wonderful implementation of DDD. But since it uses AutoMapper to translate DTOs into Entities/Aggregates, I have noticed it's able to short-circuit my private setters of Aggregates, which obviously violated the main rule of DDD. Although the goal of AutoMapper is to reduce manual operations, but DDD emphasizes invariant through private setters.

How can I make there two seemingly conflicting concept clear and use this framework smoothly? Does that mean I have to give up AutoMapper to keep DDD principles or vice versa?

I believe AutoMapper is not an anti-pattern of DDD since it's very popular in the community. In another word, if AutoMapper can use reflection (as I know) to set private setters, anybody else can. Does that means private setters is essentially unsafe?

Thanks for anyone could help me or have me a hint.


Solution

  • AutoMapper is: A convention-based object-object mapper in .NET.

    In itself, AutoMapper does not violate the principle of DDD. It is how you use it that possibly does.

    How can I make there two seemingly conflicting concept clear and use this framework smoothly? Does that mean I have to give up AutoMapper to keep DDD principles or vice versa?

    No, you don't have to give up AutoMapper.
    You can specify .IgnoreAllPropertiesWithAnInaccessibleSetter for each map.

    Related: How to configure AutoMapper to globally Ignore all Properties With Inaccessible Setter(private or protected)?

    In another word, if AutoMapper can use reflection (as I know) to set private setters, anybody else can. Does that means private setters is essentially unsafe?

    No, that means that reflection is very powerful.