Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Mapping #871

Merged
merged 1 commit into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/Application/Common/Mappings/IMapFrom.cs

This file was deleted.

4 changes: 1 addition & 3 deletions src/Application/Common/Mappings/MappingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Application.Common.Models;
using CleanArchitecture.Application.Common.Models;
using Microsoft.EntityFrameworkCore;

namespace CleanArchitecture.Application.Common.Mappings;
Expand Down
51 changes: 0 additions & 51 deletions src/Application/Common/Mappings/MappingProfile.cs

This file was deleted.

15 changes: 11 additions & 4 deletions src/Application/Common/Models/LookupDto.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Domain.Entities;
using CleanArchitecture.Domain.Entities;

namespace CleanArchitecture.Application.Common.Models;

// Note: This is currently just used to demonstrate applying multiple IMapFrom attributes.
public class LookupDto : IMapFrom<TodoList>, IMapFrom<TodoItem>
public class LookupDto
{
public int Id { get; init; }

public string? Title { get; init; }

private class Mapping : Profile
{
public Mapping()
{
CreateMap<TodoList, LookupDto>();
CreateMap<TodoItem, LookupDto>();
}
}
}
1 change: 0 additions & 1 deletion src/Application/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Reflection;
using CleanArchitecture.Application.Common.Behaviours;
using CleanArchitecture.Application.Common.Exceptions;
using FluentValidation;
using MediatR;

Expand Down
2 changes: 2 additions & 0 deletions src/Application/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using AutoMapper;
global using AutoMapper.QueryableExtensions;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Application.Common.Models;
using MediatR;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Domain.Entities;
using CleanArchitecture.Domain.Entities;

namespace CleanArchitecture.Application.TodoItems.Queries.GetTodoItemsWithPagination;

public class TodoItemBriefDto : IMapFrom<TodoItem>
public class TodoItemBriefDto
{
public int Id { get; init; }

Expand All @@ -12,4 +11,12 @@ public class TodoItemBriefDto : IMapFrom<TodoItem>
public string? Title { get; init; }

public bool Done { get; init; }

private class Mapping : Profile
{
public Mapping()
{
CreateMap<TodoItem, TodoItemBriefDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down

This file was deleted.

18 changes: 18 additions & 0 deletions src/Application/TodoLists/Queries/ExportTodos/TodoItemRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using CleanArchitecture.Domain.Entities;

namespace CleanArchitecture.Application.TodoLists.Queries.ExportTodos;

public class TodoItemRecord
{
public string? Title { get; init; }

public bool Done { get; init; }

private class Mapping : Profile
{
public Mapping()
{
CreateMap<TodoItem, TodoItemRecord>();
}
}
}
4 changes: 1 addition & 3 deletions src/Application/TodoLists/Queries/GetTodos/GetTodosQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Security;
using CleanArchitecture.Domain.Enums;
using MediatR;
Expand Down
15 changes: 8 additions & 7 deletions src/Application/TodoLists/Queries/GetTodos/TodoItemDto.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using AutoMapper;
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Domain.Entities;
using CleanArchitecture.Domain.Entities;

namespace CleanArchitecture.Application.TodoLists.Queries.GetTodos;

public class TodoItemDto : IMapFrom<TodoItem>
public class TodoItemDto
{
public int Id { get; init; }

Expand All @@ -18,9 +16,12 @@ public class TodoItemDto : IMapFrom<TodoItem>

public string? Note { get; init; }

public void Mapping(Profile profile)
private class Mapping : Profile
{
profile.CreateMap<TodoItem, TodoItemDto>()
.ForMember(d => d.Priority, opt => opt.MapFrom(s => (int)s.Priority));
public Mapping()
{
CreateMap<TodoItem, TodoItemDto>().ForMember(d => d.Priority,
opt => opt.MapFrom(s => (int)s.Priority));
}
}
}
13 changes: 10 additions & 3 deletions src/Application/TodoLists/Queries/GetTodos/TodoListDto.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Domain.Entities;
using CleanArchitecture.Domain.Entities;

namespace CleanArchitecture.Application.TodoLists.Queries.GetTodos;

public class TodoListDto : IMapFrom<TodoList>
public class TodoListDto
{
public TodoListDto()
{
Expand All @@ -17,4 +16,12 @@ public TodoListDto()
public string? Colour { get; init; }

public IReadOnlyCollection<TodoItemDto> Items { get; init; }

private class Mapping : Profile
{
public Mapping()
{
CreateMap<TodoList, TodoListDto>();
}
}
}
11 changes: 8 additions & 3 deletions tests/Application.UnitTests/Common/Mappings/MappingTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Runtime.Serialization;
using System.Reflection;
using System.Runtime.Serialization;
using AutoMapper;
using CleanArchitecture.Application.Common.Mappings;
using CleanArchitecture.Application.Common.Interfaces;
using CleanArchitecture.Application.Common.Models;
using CleanArchitecture.Application.TodoItems.Queries.GetTodoItemsWithPagination;
using CleanArchitecture.Application.TodoLists.Queries.ExportTodos;
using CleanArchitecture.Application.TodoLists.Queries.GetTodos;
using CleanArchitecture.Domain.Entities;
using NUnit.Framework;
Expand All @@ -16,7 +19,7 @@ public class MappingTests
public MappingTests()
{
_configuration = new MapperConfiguration(config =>
config.AddProfile<MappingProfile>());
config.AddMaps(Assembly.GetAssembly(typeof(IApplicationDbContext))));

_mapper = _configuration.CreateMapper();
}
Expand All @@ -32,6 +35,8 @@ public void ShouldHaveValidConfiguration()
[TestCase(typeof(TodoItem), typeof(TodoItemDto))]
[TestCase(typeof(TodoList), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(LookupDto))]
[TestCase(typeof(TodoItem), typeof(TodoItemRecord))]
[TestCase(typeof(TodoItem), typeof(TodoItemBriefDto))]
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
{
var instance = GetInstanceOf(source);
Expand Down