//
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
//
namespace Astral.Services.Helpers;
///
/// Entity helpers.
///
public static class EntityHelpers
{
///
/// Apply changes only when new entity property is different and not null.
///
/// Old entity to compare against.
/// New entity with new values.
/// The type of entity to compare.
/// The new entity with changes applied.
public static TEntity ApplyChanges(TEntity oldEntity, TEntity newEntity)
{
var entityType = typeof(TEntity);
var properties = entityType.GetProperties();
foreach (var property in properties)
{
if (property.GetValue(newEntity) != null && property.GetValue(newEntity) != property.GetValue(oldEntity))
{
property.SetValue(oldEntity, property.GetValue(newEntity));
}
else
{
property.SetValue(oldEntity, property.GetValue(oldEntity));
}
}
return oldEntity;
}
}