Update MapEntity Helper in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 4m48s

This commit is contained in:
Leandro Hernan Rojas 2025-04-04 00:45:23 -03:00
parent cedaeb3888
commit ee3a76ce3f
6 changed files with 75 additions and 51 deletions

View File

@ -1,10 +1,4 @@
using System; namespace Domain.Entities
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Domain.Entities
{ {
public class EDocumentType public class EDocumentType
{ {

View 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<>));
}
}
}

View File

@ -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);
}
}

View File

@ -1,7 +1,4 @@
using System; namespace Models.Models;
using System.Collections.Generic;
namespace Models.Models;
public partial class PhSDocumentType public partial class PhSDocumentType
{ {

View File

@ -1,5 +1,6 @@
using Domain.Entities; using Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces; using Models.Interfaces;
using Models.Models; using Models.Models;
@ -14,25 +15,13 @@ namespace Models.Repositories
public async Task<IEnumerable<EAccountType>> GetAllAsync() public async Task<IEnumerable<EAccountType>> GetAllAsync()
{ {
var accountTypes = await _context.PhSAccountTypes.ToListAsync(); 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) public async Task<EAccountType?> GetByNameAsync(string name)
{ {
var accountType = await _context.PhSAccountTypes.FirstOrDefaultAsync(a => a.Name.Contains(name)); var accountType = await _context.PhSAccountTypes.FirstOrDefaultAsync(a => a.Name.Contains(name));
return accountType != null ? MapEntity<PhSAccountType, EAccountType>(accountType) : null; return accountType != null ? EntityMapper.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;
} }
#endregion #endregion
} }

View File

@ -1,5 +1,6 @@
using Domain.Entities; using Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces; using Models.Interfaces;
using Models.Models; using Models.Models;
@ -14,26 +15,14 @@ namespace Models.Repositories
public async Task<IEnumerable<EDocumentType>> GetAllAsync() public async Task<IEnumerable<EDocumentType>> GetAllAsync()
{ {
var documentTypes = await _context.PhSDocumentTypes.ToListAsync(); 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) public async Task<EDocumentType?> GetByNameAsync(string name)
{ {
var documentType = await _context.PhSDocumentTypes.FirstOrDefaultAsync(a => a.Name.Contains(name)); var documentType = await _context.PhSDocumentTypes.FirstOrDefaultAsync(a => a.Name.Contains(name));
return documentType != null ? MapEntity<PhSDocumentType, EDocumentType>(documentType) : null; return documentType != null ? EntityMapper.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;
} }
#endregion #endregion
} }
} }