In an integration between systems there are data updates on both sides, where the A-side record and the B-side record are obtained. If there are null fields in A and these exist in B then A is complemented with B the reverse is also true, it's like a merge between records. To solve this question I tried two ways that worked, but at least for me, it is "gambiarrado".
1st Approach - Reflection
foreach (var propertyInfo in entidadeA.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
var valor = propertyInfo.GetValue(entidadeA, null);
camposNull.Add(nameof(propertyInfo.Name))
}
2nd Approach - Verification on entity instantiation
public class EntidadeA{
private string _campoA {get; set;}
public string CampoA
{
get { return _campoA; }
set
{
_campoA = value;
if(string.IsNullOrEmpty(value))
camposNull.Add(nameof(CampoA))
}
}
Entities have more than 20 fields, so my goal is to reduce as much as possible from if-else
. Is there any other way to do this? The second form, if the entity is used in a serialization for JSON or XML could generate problems?