-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ 86 replace automapper with manual mappers (#439)
- Loading branch information
1 parent
5b55536
commit ae77958
Showing
29 changed files
with
108 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
docs/adr/20241118-replace-automapper-with-manual-mapping.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Replace AutoMapper with manual mapping | ||
|
||
- Status: Approved | ||
- Deciders: Daniel Mackay, Matt Goldman | ||
- Date: 2024-11-18 | ||
- Tags: mappers | ||
|
||
Technical Story: https://github.com/SSWConsulting/SSW.CleanArchitecture/issues/86 | ||
|
||
## Context and Problem Statement | ||
|
||
We currently use AutoMapper to map the output of queries in the CA Template. While saving some work, this can also lead more complicate mappers, and runtime issues due to missing fields or mappings. | ||
|
||
While mappers solve a problem in a certain set of cases, they can also introduce complexity and runtime issues, and are not a sensible default. | ||
|
||
## Decision Drivers | ||
|
||
- Reduce runtime errors | ||
- Reduce tooling removing 'unused' properties | ||
|
||
## Considered Options | ||
|
||
1. AutoMapper | ||
2. Manual Mapper | ||
|
||
## Decision Outcome | ||
|
||
Chosen option: "Option 2 - Manual Mapper", because it reduces the runtime errors, and makes both simple and complex mapping scenarios easier to understand. | ||
|
||
### Consequences <!-- optional --> | ||
|
||
- ✅ Once Automapper is removed, we can remove the mapping profiles from the code | ||
- ✅ DTOs can now use records, making the code much simpler | ||
- ✅ With much more concise code, we can fit everything in one file | ||
- ✅ With everything in one file, we can remove a layer of folders | ||
|
||
## Pros and Cons of the Options | ||
|
||
### Option 1 - AutoMapper | ||
|
||
- ✅ Less code to write for simple mapping scenarios | ||
- ❌ Mapping becomes complex for complicated scenarios | ||
- ❌ Can lead to runtime issues due to missing fields or mappings | ||
- ❌ Need to learn a new library | ||
|
||
### Option 2 - Manual Mapper | ||
|
||
- ✅ Mapping becomes simple for both simple and complicated scenarios | ||
- ✅ Reduced runtime issues due to missing fields or mappings | ||
- ✅ No need to learn a new library | ||
- ❌ More code needed for mapping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/Application/UseCases/Heroes/Commands/CreateHero/CreateHeroPowerDto.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/Application/UseCases/Heroes/Commands/UpdateHero/UpdateHeroPowerDto.cs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/Application/UseCases/Heroes/Queries/GetAllHeroes/GetAllHeroesMapping.cs
This file was deleted.
Oops, something went wrong.
17 changes: 12 additions & 5 deletions
17
src/Application/UseCases/Heroes/Queries/GetAllHeroes/GetAllHeroesQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
using AutoMapper.QueryableExtensions; | ||
using SSW.CleanArchitecture.Application.Common.Interfaces; | ||
|
||
namespace SSW.CleanArchitecture.Application.UseCases.Heroes.Queries.GetAllHeroes; | ||
|
||
public record GetAllHeroesQuery : IRequest<IReadOnlyList<HeroDto>>; | ||
|
||
public sealed class GetAllHeroesQueryHandler( | ||
IMapper mapper, | ||
IApplicationDbContext dbContext) : IRequestHandler<GetAllHeroesQuery, IReadOnlyList<HeroDto>> | ||
public record HeroDto(Guid Id, string Name, string Alias, int PowerLevel, IEnumerable<HeroPowerDto> Powers); | ||
|
||
public record HeroPowerDto(string Name, int PowerLevel); | ||
|
||
internal sealed class GetAllHeroesQueryHandler(IApplicationDbContext dbContext) | ||
: IRequestHandler<GetAllHeroesQuery, IReadOnlyList<HeroDto>> | ||
{ | ||
public async Task<IReadOnlyList<HeroDto>> Handle( | ||
GetAllHeroesQuery request, | ||
CancellationToken cancellationToken) | ||
{ | ||
return await dbContext.Heroes | ||
.ProjectTo<HeroDto>(mapper.ConfigurationProvider) | ||
.Select(h => new HeroDto( | ||
h.Id.Value, | ||
h.Name, | ||
h.Alias, | ||
h.PowerLevel, | ||
h.Powers.Select(p => new HeroPowerDto(p.Name, p.PowerLevel)))) | ||
.ToListAsync(cancellationToken); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
src/Application/UseCases/Heroes/Queries/GetAllHeroes/HeroDto.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
src/Application/UseCases/Teams/Queries/GetAllTeams/GetAllHeroesQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
using AutoMapper.QueryableExtensions; | ||
using SSW.CleanArchitecture.Application.Common.Interfaces; | ||
|
||
namespace SSW.CleanArchitecture.Application.UseCases.Teams.Queries.GetAllTeams; | ||
|
||
public record GetAllTeamsQuery : IRequest<IReadOnlyList<TeamDto>>; | ||
|
||
public sealed class GetAllTeamsQueryHandler( | ||
IMapper mapper, | ||
IApplicationDbContext dbContext) : IRequestHandler<GetAllTeamsQuery, IReadOnlyList<TeamDto>> | ||
public record TeamDto(Guid Id, string Name); | ||
|
||
internal sealed class GetAllTeamsQueryHandler(IApplicationDbContext dbContext) | ||
: IRequestHandler<GetAllTeamsQuery, IReadOnlyList<TeamDto>> | ||
{ | ||
public async Task<IReadOnlyList<TeamDto>> Handle( | ||
GetAllTeamsQuery request, | ||
CancellationToken cancellationToken) | ||
{ | ||
return await dbContext.Teams | ||
.ProjectTo<TeamDto>(mapper.ConfigurationProvider) | ||
.Select(t => new TeamDto(t.Id.Value, t.Name)) | ||
.ToListAsync(cancellationToken); | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
src/Application/UseCases/Teams/Queries/GetAllTeams/GetAllTeamsMapping.cs
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
src/Application/UseCases/Teams/Queries/GetAllTeams/TeamDto.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
templates/command/Commands/CommandName/CommandNameCommandValidator.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.