Skip to content

Commit

Permalink
[SOA-006][Feature] Build DTOs and apply AutoMapperLib (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
LyQuocCuong authored Jul 18, 2023
1 parent 2f85a55 commit 68c5486
Show file tree
Hide file tree
Showing 31 changed files with 640 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Cores/Contracts/Contracts.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
24 changes: 24 additions & 0 deletions Cores/Contracts/Utilities/Mapper/IMapperService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Contracts.Utilities.Mapper
{
public interface IMapperService
{
/// <summary>
/// Execute a mapping from the Source object to a [NEW] Destination object.
/// </summary>
/// <typeparam name="TSource">Source type to use, regardless of the runtime type</typeparam>
/// <typeparam name="TDestination">Destination type to create</typeparam>
/// <param name="source">Source object to map from</param>
/// <returns>Mapped Destination object</returns>
public TDestination Execute<TSource, TDestination>(TSource source);

/// <summary>
/// Execute a mapping from the Source object to the [EXISTING] Destination object.
/// </summary>
/// <typeparam name="TSource">Source type to use</typeparam>
/// <typeparam name="TDestination">Destination type</typeparam>
/// <param name="source">Source object to map from</param>
/// <param name="destination">Destination object to map into</param>
/// <returns>The mapped Destination object, [SAME] instance as the <paramref name="destination"/> object</returns>
public TDestination Execute<TSource, TDestination>(TSource source, TDestination destination);
}
}
11 changes: 11 additions & 0 deletions Cores/Shared/Abstracts/AbstractEntityDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Shared.Abstracts
{
public abstract class AbstractEntityDto
{
public bool IsDeleted { get; set; }

public DateTime CreatedDate { get; set; }

public DateTime UpdatedDate { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Shared.DTOs.Inputs.FromBody.CreationDtos
{
public sealed class CustomerForCreationDto
{
public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Shared.DTOs.Inputs.FromBody.CreationDtos
{
public sealed class EmployeeForCreationDto
{
public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Shared.DTOs.Inputs.FromBody.CreationDtos
{
public sealed class ProductForCreationDto
{
public string? Code { get; set; }

public string? Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Shared.DTOs.Inputs.FromBody.CreationDtos
{
public sealed class StoreForCreationDto
{
public Guid CompanyId { get; set; }

public Guid? ManagerId { get; set; }

public string? Code { get; set; }

public string? Name { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Shared.DTOs.Inputs.FromBody.UpdateDtos
{
public sealed class CompanyForUpdateDto
{
public string? Name { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Shared.DTOs.Inputs.FromBody.UpdateDtos
{
public sealed class CustomerForUpdateDto
{
public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Shared.DTOs.Inputs.FromBody.UpdateDtos
{
public sealed class EmployeeForUpdateDto
{
public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Shared.DTOs.Inputs.FromBody.UpdateDtos
{
public sealed class ProductForUpdateDto
{
public string? Code { get; set; }

public string? Name { get; set; }
}
}
15 changes: 15 additions & 0 deletions Cores/Shared/DTOs/Inputs/FromBody/UpdateDtos/StoreForUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Shared.DTOs.Inputs.FromBody.UpdateDtos
{
public sealed class StoreForUpdateDto
{
public Guid? ManagerId { get; set; }

public string? Code { get; set; }

public string? Name { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
13 changes: 13 additions & 0 deletions Cores/Shared/DTOs/Outputs/EntityDtos/CompanyDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Shared.DTOs.Outputs.EntityDtos
{
public sealed class CompanyDto : AbstractEntityDto
{
public Guid Id { get; set; }

public string? Name { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
17 changes: 17 additions & 0 deletions Cores/Shared/DTOs/Outputs/EntityDtos/CustomerDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Shared.DTOs.Outputs.EntityDtos
{
public sealed class CustomerDto : AbstractEntityDto
{
public Guid Id { get; set; }

public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
19 changes: 19 additions & 0 deletions Cores/Shared/DTOs/Outputs/EntityDtos/EmployeeDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Shared.DTOs.Outputs.EntityDtos
{
public sealed class EmployeeDto : AbstractEntityDto
{
public Guid Id { get; set; }

public Guid? WorkingStoreId { get; set; }

public string? Code { get; set; }

public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
11 changes: 11 additions & 0 deletions Cores/Shared/DTOs/Outputs/EntityDtos/ProductDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Shared.DTOs.Outputs.EntityDtos
{
public sealed class ProductDto : AbstractEntityDto
{
public Guid Id { get; set; }

public string? Code { get; set; }

public string? Name { get; set; }
}
}
19 changes: 19 additions & 0 deletions Cores/Shared/DTOs/Outputs/EntityDtos/StoreDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Shared.DTOs.Outputs.EntityDtos
{
public sealed class StoreDto : AbstractEntityDto
{
public Guid Id { get; set; }

public Guid CompanyId { get; set; }

public Guid? ManagerId { get; set; }

public string? Code { get; set; }

public string? Name { get; set; }

public string? Address { get; set; }

public string? Phone { get; set; }
}
}
9 changes: 9 additions & 0 deletions Cores/Shared/Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Cores/Shared/Using.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Shared.Abstracts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using AutoMapper;
using Contracts.Utilities.Mapper;

namespace eShopOnlineUtilities.AutoMapper
{
public sealed class AutoMapperService : IMapperService
{
private readonly IMapper _autoMapper;

public AutoMapperService(IMapper autoMapper)
{
_autoMapper = autoMapper;
}

public TDestination Execute<TSource, TDestination>(TSource source)
{
return _autoMapper.Map<TSource, TDestination>(source);
}

public TDestination Execute<TSource, TDestination>(TSource source, TDestination destination)
{
return _autoMapper.Map(source, destination);
}
}
}
16 changes: 16 additions & 0 deletions Implementations/eShopOnlineUtilities/AutoMapper/MappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using AutoMapper;

namespace eShopOnlineUtilities.AutoMapper.Profiles
{
public partial class MappingProfiles : Profile
{
public MappingProfiles()
{
MappingCompanyProfile();
MappingCustomerProfile();
MappingEmployeeProfile();
MappingProductProfile();
MappingStoreProfile();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace eShopOnlineUtilities.AutoMapper.Profiles
{
public partial class MappingProfiles : Profile
{
private void MappingCompanyProfile()
{
ReverseMapping_Company_And_CompanyDto();
ReverseMapping_CompanyForUpdateDto_And_Company();
}

private void ReverseMapping_Company_And_CompanyDto()
{
CreateMap<Company, CompanyDto>().ReverseMap()
.ForMember(right => right.Id,
right_prop => right_prop.MapFrom(left => left.Id))
.ForMember(right => right.Name,
right_prop => right_prop.MapFrom(left => left.Name))
.ForMember(right => right.Address,
right_prop => right_prop.MapFrom(left => left.Address))
.ForMember(right => right.Phone,
right_prop => right_prop.MapFrom(left => left.Phone))
.ForMember(right => right.IsDeleted,
right_prop => right_prop.MapFrom(left => left.IsDeleted))
.ForMember(right => right.CreatedDate,
right_prop => right_prop.MapFrom(left => left.CreatedDate))
.ForMember(right => right.UpdatedDate,
right_prop => right_prop.MapFrom(left => left.UpdatedDate));
}

private void ReverseMapping_CompanyForUpdateDto_And_Company()
{
CreateMap<CompanyForUpdateDto, Company>().ReverseMap()
.ForMember(right => right.Name,
right_prop => right_prop.MapFrom(left => left.Name))
.ForMember(right => right.Address,
right_prop => right_prop.MapFrom(left => left.Address))
.ForMember(right => right.Phone,
right_prop => right_prop.MapFrom(left => left.Phone));
}

}
}
Loading

0 comments on commit 68c5486

Please sign in to comment.