Skip to content

Commit

Permalink
Merge pull request #95 from neozhu/mapperly
Browse files Browse the repository at this point in the history
use mappery instead of automapper
  • Loading branch information
neozhu authored Nov 7, 2024
2 parents f81d933 + 8dd524c commit dd050da
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 195 deletions.
3 changes: 3 additions & 0 deletions src/CleanArchitectureCodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<DependentUpon>source.extension.vsixmanifest</DependentUpon>
</Compile>
<Compile Include="TemplateMap.cs" />
<Content Include="Templates\Mappers\.mapper.cs.txt">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Templates\Pages\.create.razor.txt">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
Expand Down
1 change: 1 addition & 0 deletions src/CodeGeneratorPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private void ExecuteAsync(object sender, EventArgs e)
$"{nameofPlural}/Commands/Import/Import{nameofPlural}CommandValidator.cs",
$"{nameofPlural}/Caching/{name}CacheKey.cs",
$"{nameofPlural}/DTOs/{name}Dto.cs",
$"{nameofPlural}/Mappers/{name}Mapper.cs",
$"{nameofPlural}/EventHandlers/{name}CreatedEventHandler.cs",
$"{nameofPlural}/EventHandlers/{name}UpdatedEventHandler.cs",
$"{nameofPlural}/EventHandlers/{name}DeletedEventHandler.cs",
Expand Down
1 change: 1 addition & 0 deletions src/Templatemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static async Task<string> GetTemplateFilePathAsync(Project project, Intel
"Commands\\AddEdit",
"Commands\\Import",
"DTOs",
"Mappers",
"Caching",
"EventHandlers",
"Events",
Expand Down
75 changes: 30 additions & 45 deletions src/Templates/Commands/AddEdit/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using {selectns}.{nameofPlural}.DTOs;
using {selectns}.{nameofPlural}.Caching;
using {selectns}.{nameofPlural}.Mappers;

namespace {namespace};

public class AddEdit{itemname}Command: ICacheInvalidatorRequest<Result<int>>
Expand All @@ -13,58 +15,41 @@ public class AddEdit{itemname}Command: ICacheInvalidatorRequest<Result<int>>

public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
}

private class Mapping : Profile
public class AddEdit{itemname}CommandHandler : IRequestHandler<AddEdit{itemname}Command, Result<int>>
{
private readonly IApplicationDbContext _context;
public AddEdit{itemname}CommandHandler(
IApplicationDbContext context)
{
public Mapping()
{
CreateMap<{itemname}Dto,AddEdit{itemname}Command>(MemberList.None);
CreateMap<AddEdit{itemname}Command,{itemname}>(MemberList.None);

}
_context = context;
}
}

public class AddEdit{itemname}CommandHandler : IRequestHandler<AddEdit{itemname}Command, Result<int>>
public async Task<Result<int>> Handle(AddEdit{itemname}Command request, CancellationToken cancellationToken)
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<AddEdit{itemname}CommandHandler> _localizer;
public AddEdit{itemname}CommandHandler(
IApplicationDbContext context,
IStringLocalizer<AddEdit{itemname}CommandHandler> localizer,
IMapper mapper
)
if (request.Id > 0)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(AddEdit{itemname}Command request, CancellationToken cancellationToken)
{
if (request.Id > 0)
{
var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
else
var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
var item = _mapper.Map<{itemname}>(request);
// raise a create domain event
item.AddDomainEvent(new {itemname}CreatedEvent(item));
_context.{nameofPlural}.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}

{itemname}Mapper.ApplyChangesFrom(request,item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
else
{
var item = {itemname}Mapper.FromEditCommand(request);
// raise a create domain event
item.AddDomainEvent(new {itemname}CreatedEvent(item));
_context.{nameofPlural}.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}

}
}

23 changes: 4 additions & 19 deletions src/Templates/Commands/Create/.cs.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using {selectns}.{nameofPlural}.DTOs;

using {selectns}.{nameofPlural}.Caching;
using {selectns}.{nameofPlural}.Mappers;

namespace {namespace};

Expand All @@ -13,34 +13,19 @@ public class Create{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{dtoFieldDefinition}
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
private class Mapping : Profile
{
public Mapping()
{
CreateMap<{itemname}Dto,Create{itemname}Command>(MemberList.None);
CreateMap<Create{itemname}Command,{itemname}>(MemberList.None);
}
}
}

public class Create{itemname}CommandHandler : IRequestHandler<Create{itemname}Command, Result<int>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<Create{itemname}Command> _localizer;
public Create{itemname}CommandHandler(
IApplicationDbContext context,
IStringLocalizer<Create{itemname}Command> localizer,
IMapper mapper
)
IApplicationDbContext context)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(Create{itemname}Command request, CancellationToken cancellationToken)
{
var item = _mapper.Map<{itemname}>(request);
var item = {itemname}Mapper.FromCreateCommand(request);
// raise a create domain event
item.AddDomainEvent(new {itemname}CreatedEvent(item));
_context.{nameofPlural}.Add(item);
Expand Down
65 changes: 29 additions & 36 deletions src/Templates/Commands/Delete/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,39 @@ using {selectns}.{nameofPlural}.Caching;

namespace {namespace};

public class Delete{itemname}Command: ICacheInvalidatorRequest<Result<int>>
public class Delete{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{
public int[] Id { get; }
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
public Delete{itemname}Command(int[] id)
{
Id = id;
}
}

public class Delete{itemname}CommandHandler :
IRequestHandler<Delete{itemname}Command, Result<int>>

{
private readonly IApplicationDbContext _context;
public Delete{itemname}CommandHandler(
IApplicationDbContext context)
{
public int[] Id { get; }
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
public Delete{itemname}Command(int[] id)
{
Id = id;
}
_context = context;
}

public class Delete{itemname}CommandHandler :
IRequestHandler<Delete{itemname}Command, Result<int>>

public async Task<Result<int>> Handle(Delete{itemname}Command request, CancellationToken cancellationToken)
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<Delete{itemname}CommandHandler> _localizer;
public Delete{itemname}CommandHandler(
IApplicationDbContext context,
IStringLocalizer<Delete{itemname}CommandHandler> localizer,
IMapper mapper
)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(Delete{itemname}Command request, CancellationToken cancellationToken)
var items = await _context.{nameofPlural}.Where(x=>request.Id.Contains(x.Id)).ToListAsync(cancellationToken);
foreach (var item in items)
{
var items = await _context.{nameofPlural}.Where(x=>request.Id.Contains(x.Id)).ToListAsync(cancellationToken);
foreach (var item in items)
{
// raise a delete domain event
item.AddDomainEvent(new {itemname}DeletedEvent(item));
_context.{nameofPlural}.Remove(item);
}
var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);
// raise a delete domain event
item.AddDomainEvent(new {itemname}DeletedEvent(item));
_context.{nameofPlural}.Remove(item);
}

var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);
}

}

9 changes: 3 additions & 6 deletions src/Templates/Commands/Import/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using {selectns}.{nameofPlural}.DTOs;
using {selectns}.{nameofPlural}.Caching;
using {selectns}.{nameofPlural}.Mappers;

namespace {namespace};

Expand All @@ -28,22 +29,18 @@ namespace {namespace};
IRequestHandler<Import{nameofPlural}Command, Result<int>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<Import{nameofPlural}CommandHandler> _localizer;
private readonly IExcelService _excelService;
private readonly {itemname}Dto _dto = new();

public Import{nameofPlural}CommandHandler(
IApplicationDbContext context,
IExcelService excelService,
IStringLocalizer<Import{nameofPlural}CommandHandler> localizer,
IMapper mapper
)
IStringLocalizer<Import{nameofPlural}CommandHandler> localizer)
{
_context = context;
_localizer = localizer;
_excelService = excelService;
_mapper = mapper;
}
#nullable disable warnings
public async Task<Result<int>> Handle(Import{nameofPlural}Command request, CancellationToken cancellationToken)
Expand All @@ -60,7 +57,7 @@ namespace {namespace};
var exists = await _context.{nameofPlural}.AnyAsync(x => x.Name == dto.Name, cancellationToken);
if (!exists)
{
var item = _mapper.Map<{itemname}>(dto);
var item = {itemname}Mapper.FromDto(dto);
// add create domain events if this entity implement the IHasDomainEvent interface
// item.AddDomainEvent(new {itemname}CreatedEvent(item));
await _context.{nameofPlural}.AddAsync(item, cancellationToken);
Expand Down
60 changes: 23 additions & 37 deletions src/Templates/Commands/Update/.cs.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using {selectns}.{nameofPlural}.DTOs;

using {selectns}.{nameofPlural}.Caching;
using {selectns}.{nameofPlural}.Mappers;

namespace {namespace};

Expand All @@ -13,44 +13,30 @@ public class Update{itemname}Command: ICacheInvalidatorRequest<Result<int>>
{dtoFieldDefinition}
public string CacheKey => {itemname}CacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => {itemname}CacheKey.GetOrCreateTokenSource();
private class Mapping : Profile
{
public Mapping()
{
CreateMap<{itemname}Dto,Update{itemname}Command>(MemberList.None);
CreateMap<Update{itemname}Command,{itemname}>(MemberList.None);
}
}

}

public class Update{itemname}CommandHandler : IRequestHandler<Update{itemname}Command, Result<int>>
public class Update{itemname}CommandHandler : IRequestHandler<Update{itemname}Command, Result<int>>
{
private readonly IApplicationDbContext _context;
public Update{itemname}CommandHandler(
IApplicationDbContext context)
{
_context = context;
}
public async Task<Result<int>> Handle(Update{itemname}Command request, CancellationToken cancellationToken)
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<Update{itemname}CommandHandler> _localizer;
public Update{itemname}CommandHandler(
IApplicationDbContext context,
IStringLocalizer<Update{itemname}CommandHandler> localizer,
IMapper mapper
)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(Update{itemname}Command request, CancellationToken cancellationToken)
{

var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
var item = await _context.{nameofPlural}.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"{itemname} with id: [{request.Id}] not found.");
}
{itemname}Mapper.ApplyChangesFrom(request, item);
// raise a update domain event
item.AddDomainEvent(new {itemname}UpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
}

7 changes: 0 additions & 7 deletions src/Templates/DTOs/.dto.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ public class {itemname}Dto
public int Id { get; set; }
{dtoFieldDefinition}

private class Mapping : Profile
{
public Mapping()
{
CreateMap<{itemname}, {itemname}Dto>().ReverseMap();
}
}
}

Loading

0 comments on commit dd050da

Please sign in to comment.