Comments (6)
With full respect for Jimmy's Bogard work and his Automapper library - do not use it (and any of mapping libraries) in more complex projects - this is my opinion and advice.
Commands and Domain Events should be immutable. Public setters and parameterless constructors remove encapsulation - this is nightmare of most codebases which I see regularly. Don't do it.
Private setter is much better solution but still not ideal. It breaks immutability *intention". Additionally, as far as I know, there is performance difference in CLR between no setter and private setter (I cannot find any reference now).
I used Automapper long time ago (6-7 years maybe) but I stopped. For me creating mapping in code is not a deal. It is easy and quick and with removed "primitive obssesion" I have a lot of compile-time checks and support. Sometimes more code is not bad. I don't want to use external library in this case.
To sum up - don't use mapping library or use mapping library which supports private setters. I strongly recommend former.
from modular-monolith-with-ddd.
There was a discussion on AutoMapper on Twitter once - the quick summary was that AM is worth using in Domain ->ViewModels (you minimize the data that needs to be downloaded from the database) but not in Request -> Domain (because you start to store your business logic in mappings).
from modular-monolith-with-ddd.
I think we all agree that using mapping library in Command handling is not good idea. Let's move to Queries :)
If you use microORM like Dapper you have everything out of the box - projection to type, selecting only needed columns etc. Mapping library is not needed.
So maybe one of the reason to use it is mapping Domain Entity to DTO? I don't think so. Domain Model is optimized for writes, not reads. You should not use it for reading. In this application you cannot even do it because all fields are private.
So if you don't have true Domain Model and you use EF as data access tool to Data Model why not to return data models (EF entities) directly? EF supports projections I think.
Maybe I am missing something so please add some reference to repository or good article why is better to use Automapper in cases which I described.
from modular-monolith-with-ddd.
command is an intention which shouldn't be modified so it is good idea to keep it immutable. i think most current mappers should deal with private setters so the best way to achive this is to explicity implements private setters
from modular-monolith-with-ddd.
There's an argument that commands are statements about something that happened and therefore should be immutable, and it allows for the command (or any MediatR IRequest) to be passed around without worrying about it being modified.
I'm a fan of defensive programming and making such object immutable is a good idea IMO, but I've seen many implementations of CQRS that use mutable 'requests', even Jimmy Bogard uses public setters on IRequests in his examples.
from modular-monolith-with-ddd.
It might be worth adding that Jimmy Bogard uses AutoMapper to map to commands which then act as a Viewmodel , which is different usecase (and a violation of Single Responsibility Principle), but for his simplistic quick architecture demo it's sufficient, anything more than that could be reasonably considered overengineering.
I would not cast AutoMapper away completely, it's a perfectly valid tool to use even in complex projects, but on Read Model side (when mapping/projecting to viewmodels or DTOs). It should not be used on write side.
from modular-monolith-with-ddd.
Related Issues (20)
- 无政治倾向类型用途 HOT 3
- Issue with 2 or more DomainNotification<> with thesame DomainEvent
- How to achieve the domain event of new entity before saving when create new entity,please?
- Contracting services HOT 1
- Getting Sql Server connection issue while building using NUKE HOT 1
- How to secure API resources (e.g. user registration endpoint) HOT 2
- Update to .NET 6.0 / 7.0? HOT 9
- Infrastructure Module and DRY
- error: User context is not available while creating meetingrouppropsala
- Handling exceptions, validation exception
- Identity Changes HOT 12
- how to execute .\build MigrateDatabase "connection_string" HOT 2
- Remove Unused `using` Directives Following .NET 7/8 Update HOT 1
- CI/CD Pipeline for Docker Image Build Validation
- is john wick ukranian?
- Why does the Member Aggregate in the Meetings module need the login information? HOT 1
- ProcessInternalCommandsCommandHandler does not work if result is success
- RUN dotnet restore "DatabaseMigrator.csproj" fails HOT 2
- OAuth2 Resource Owner Password Grant Type has been deprecated
- swagger error: API.xml file not found HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modular-monolith-with-ddd.