Update MapEntity Helper in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 4m48s
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 4m48s
This commit is contained in:
parent
cedaeb3888
commit
ee3a76ce3f
@ -1,10 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Domain.Entities
|
||||
namespace Domain.Entities
|
||||
{
|
||||
public class EDocumentType
|
||||
{
|
||||
|
||||
66
Models/Helpers/EntityMapper.cs
Normal file
66
Models/Helpers/EntityMapper.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Models.Helpers
|
||||
{
|
||||
public static class EntityMapper
|
||||
{
|
||||
public static TDestination MapEntity<TSource, TDestination>(TSource source)
|
||||
where TDestination : new()
|
||||
{
|
||||
var destination = new TDestination();
|
||||
var sourceProps = typeof(TSource).GetProperties();
|
||||
var destProps = typeof(TDestination).GetProperties();
|
||||
|
||||
foreach (var sourceProp in sourceProps)
|
||||
{
|
||||
var destProp = destProps.FirstOrDefault(p => p.Name == sourceProp.Name);
|
||||
if (destProp == null || !destProp.CanWrite) continue;
|
||||
|
||||
var sourceValue = sourceProp.GetValue(source);
|
||||
|
||||
if (sourceValue == null)
|
||||
{
|
||||
destProp.SetValue(destination, null);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (IsGenericCollection(destProp.PropertyType) &&
|
||||
IsGenericCollection(sourceProp.PropertyType))
|
||||
{
|
||||
var sourceElementType = sourceProp.PropertyType.GenericTypeArguments[0];
|
||||
var destElementType = destProp.PropertyType.GenericTypeArguments[0];
|
||||
|
||||
var mappedList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(destElementType))!;
|
||||
|
||||
foreach (var item in (IEnumerable)sourceValue)
|
||||
{
|
||||
var mapMethod = typeof(EntityMapper)
|
||||
.GetMethod(nameof(MapEntity), BindingFlags.Public | BindingFlags.Static)!
|
||||
.MakeGenericMethod(sourceElementType, destElementType);
|
||||
|
||||
var mappedItem = mapMethod.Invoke(null, new[] { item });
|
||||
mappedList.Add(mappedItem);
|
||||
}
|
||||
|
||||
destProp.SetValue(destination, mappedList);
|
||||
}
|
||||
else if (destProp.PropertyType.IsAssignableFrom(sourceProp.PropertyType))
|
||||
{
|
||||
destProp.SetValue(destination, sourceValue);
|
||||
}
|
||||
}
|
||||
|
||||
return destination;
|
||||
}
|
||||
|
||||
private static bool IsGenericCollection(Type type)
|
||||
{
|
||||
if (!type.IsGenericType) return false;
|
||||
var genericDef = type.GetGenericTypeDefinition();
|
||||
return typeof(IEnumerable<>).IsAssignableFrom(genericDef) ||
|
||||
type.GetInterfaces().Any(i => i.IsGenericType &&
|
||||
i.GetGenericTypeDefinition() == typeof(IEnumerable<>));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
namespace Models.Interfaces
|
||||
{
|
||||
public interface IGenericRepository<T> where T : class
|
||||
{
|
||||
Task<T?> GetByIdAsync(int id);
|
||||
Task<IEnumerable<T>> GetAllAsync();
|
||||
Task AddAsync(T entity);
|
||||
void Update(T entity);
|
||||
void Delete(T entity);
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Models.Models;
|
||||
namespace Models.Models;
|
||||
|
||||
public partial class PhSDocumentType
|
||||
{
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Models.Helpers;
|
||||
using Models.Interfaces;
|
||||
using Models.Models;
|
||||
|
||||
@ -14,25 +15,13 @@ namespace Models.Repositories
|
||||
public async Task<IEnumerable<EAccountType>> GetAllAsync()
|
||||
{
|
||||
var accountTypes = await _context.PhSAccountTypes.ToListAsync();
|
||||
return accountTypes.Select(at => MapEntity<PhSAccountType, EAccountType>(at));
|
||||
return accountTypes.Select(EntityMapper.MapEntity<PhSAccountType, EAccountType>);
|
||||
}
|
||||
|
||||
public async Task<EAccountType?> GetByNameAsync(string name)
|
||||
{
|
||||
var accountType = await _context.PhSAccountTypes.FirstOrDefaultAsync(a => a.Name.Contains(name));
|
||||
return accountType != null ? MapEntity<PhSAccountType, EAccountType>(accountType) : null;
|
||||
}
|
||||
#endregion
|
||||
#region Métodos Auxiliares
|
||||
private static TDestination MapEntity<TSource, TDestination>(TSource source) where TDestination : new()
|
||||
{
|
||||
var destination = new TDestination();
|
||||
foreach (var propertyInfo in typeof(TSource).GetProperties())
|
||||
{
|
||||
var value = propertyInfo.GetValue(source);
|
||||
typeof(TDestination).GetProperty(propertyInfo.Name)?.SetValue(destination, value);
|
||||
}
|
||||
return destination;
|
||||
return accountType != null ? EntityMapper.MapEntity<PhSAccountType, EAccountType>(accountType) : null;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Models.Helpers;
|
||||
using Models.Interfaces;
|
||||
using Models.Models;
|
||||
|
||||
@ -14,26 +15,14 @@ namespace Models.Repositories
|
||||
public async Task<IEnumerable<EDocumentType>> GetAllAsync()
|
||||
{
|
||||
var documentTypes = await _context.PhSDocumentTypes.ToListAsync();
|
||||
return documentTypes.Select(at => MapEntity<PhSDocumentType, EDocumentType>(at));
|
||||
return documentTypes.Select(EntityMapper.MapEntity<PhSDocumentType, EDocumentType>);
|
||||
}
|
||||
|
||||
public async Task<EDocumentType?> GetByNameAsync(string name)
|
||||
{
|
||||
var documentType = await _context.PhSDocumentTypes.FirstOrDefaultAsync(a => a.Name.Contains(name));
|
||||
return documentType != null ? MapEntity<PhSDocumentType, EDocumentType>(documentType) : null;
|
||||
}
|
||||
#endregion
|
||||
#region Métodos Auxiliares
|
||||
private static TDestination MapEntity<TSource, TDestination>(TSource source) where TDestination : new()
|
||||
{
|
||||
var destination = new TDestination();
|
||||
foreach (var propertyInfo in typeof(TSource).GetProperties())
|
||||
{
|
||||
var value = propertyInfo.GetValue(source);
|
||||
typeof(TDestination).GetProperty(propertyInfo.Name)?.SetValue(destination, value);
|
||||
}
|
||||
return destination;
|
||||
return documentType != null ? EntityMapper.MapEntity<PhSDocumentType, EDocumentType>(documentType) : null;
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user