'How to check an empty object?

I have a number of objects. Some of them have string properties and some of them have bool properties. I'm using them in multi-step form. so each step is bound to each object.

Now, if the user fills the first two sections and saves the data as a user wants to fill rest of the data later. At this point, I need to identify how much data is filled and how much is left. So that next time when the form gets loaded, based on the previously filled data I can identify from where to start filling the form.

I'm trying to identify at the time of saving that how many objects have values. In other words, if I find an object with all the values e.g. empty strings, I can skip that object to be saved into the database. I referred this https://stackoverflow.com/a/22683141/10037521 which shows how to check empty object which has all the string properties.

How to include boolean properties as well in this check? e.g. If the object has 10 bool properties and if all of them are false, I need to identify that object as empty.

So to summarize above question, how to identify if the object is blank which have bool or string properties?



Solution 1:[1]

Technically, you can combine Reflection and Linq into something like this:

  Object obj = ...

  // All Empty (false or null) public properties of either string or bool type
  PropertyInfo[] emptyProps = obj
    .GetType()
    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .Where(prop => prop.CanRead) // you may want to check prop.CanWrite as well
    .Where(prop => prop.PropertyType == typeof(bool) || 
                   prop.PropertyType == typeof(string))
    .Where(prop => object.Equals(prop.GetValue(obj), 
                                 prop.PropertyType.IsValueType 
                                   ? Activator.CreateInstance(prop.PropertyType) 
                                   : null))
    .ToArray();

However, I doubt if you should do this: some properties should not be saved:

   // Should be saved
   public string FirstName {...} 
   // Should be saved
   public string SecondName {...} 
   // Should NOT be saved
   public string Name {get {return $"{FirstName} {SecondName}";}}

You can have elaborated criteria which data should be saved, e.g. in case of FirstName you may want to check (at least!)

   // FirstName must be not null, not empty and not blank
   bool shouldSave = !string.IsNullOrWhiteSpace(FirstName);

That's why I suggest implementing a custom property / method within the class:

   public class MyClass {
     ...
     // virtual: you may want to add some properties in a derived class 
     public virtual bool IsEmpty {
       get {
         return string.IsNullOrWhiteSpace(FirstName) &&
                string.IsNullOrWhiteSpace(SecondName) &&
                ...
       }
     } 
   }

Solution 2:[2]

You may serialize it to a json then test against the string to achieve a similar checking:

public static object? NullIfEmptyValues(object obj) =>
    JsonSerializer.Serialize(obj) is "\"\"" or "[]" or "null" ? null : obj;

Solution 3:[3]

You can check it with a simple check:

obj == null || obj == String.Empty

This will check for both null and "empty" conditions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 n0099
Solution 3 zcoop98