Check null or empty fields in an entity without consecutive use of "if-Else"

Asked

Viewed 544 times

2

In a system integration there are data updates on both sides, where you get the record of Side A and the record of side B. If there are null fields in A and these exist in B then A is supplemented with B the inverse is also true, it is as if it were a merge between the records. To resolve this issue I tried two ways that worked, however, at least for me, 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 - Entity instantiation verification

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

The entities have more than 20 fields, so my goal is to reduce as much as possible if-else. Is there any other way to do this? The second way, if the entity is used in a serialization for JSON or XML could generate problems?

  • Do you have several different entities? Each with 20+ fields?

  • No no, it’s just a few... if I’m not mistaken 2 or 3...

  • Did the answer resolve what was in doubt? Something else needs to be improved?

1 answer

1

There is always another way. The question is whether it is suitable for current need and will be for others that may arise. You need to do something that’s conceptually right.

I see no problems in the second form. A serialization will call the get and receive the result.

That one camposNull in the class is seeming me gambiarra, no matter how it is populated, but it may be correct, I have no further information to affirm.

Stranger is a property that uses another property as a field. It can, but I don’t know if it’s the intention.

I would avoid reflection, actually, whenever I can. The performance is bad and opens gaps for code failures even security. When it’s needed, it’s a great tool, but I wouldn’t abuse it. In languages like C# if you need to automate something I prefer to generate code or do it by hand.

  • camposNull is a list containing the fields that are null in the entity, to check later if there are null fields with, for example, if(entidadeA.camposNull.count > 0)and so can "treat" only fields that are null from the names that are in camposNull

  • Yes, it seems to gambiarra. This information is auxiliary and is not part of the object.

  • How conceptually correct it would be to implement ?

  • Without this field :P Without knowing the whole problem it is difficult to say, but maybe I shouldn’t have been, just get the information when I need it, or have an auxiliary object for it. It may even be correct, I don’t know, it just doesn’t seem like a member to facilitate a "dirty work" and no need for the object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.