Null vs Value not set

We wrote a web service that uses a simple object translator to map DTO values ​​to "real" server-side business objects. As part of this exercise. We are faced with an “interesting” difference between explicitly set null values and clients that do not have a value set .

The problem is that we want to set the default value for the real business object if the client has not explicitly set the value, however using standard types with the NULL option, there is no way to determine if the client really means "set this to null" or just don't install it.

The solution here is obviously a kind of “flag”.

In the business object, we can internally monitor the state of the field using the IsDirty private flags set in the property settings, but the DTO only really indicates the interface, so this means publishing this data to the public. This leaves a number of implementation options. C # language (so statically typed), so ...

  • Can we set the "IsSet" flag for each property?
  • Could we expose each property as a class with the .Value and .IsSet properties? etc. etc.

How would you decide to expose these “flags” in the Data Contract? What do you think is the best practice for this?

Any opinions on this would be greatly appreciated.

+5
source share
4 answers

, bool . , / .

+3

, :

public class DtoData<T> 
{
  T data;
  bool IsSet { get; private set; }
  T Data 
  { 
    get { return data; }
    set { data = value; IsSet = true; } 
  }
}


public class XyzDto 
{
  // only private setters, initialize in constructor
  DtoData<int?> SomeInt { get; private set; }
  DtoData<string> SomeString { get; private set; }
}

. , , DtoData , . .


. " " ? , Dtos ? , , . "field-filter".

+3

, , , , , . ; , , . , , , ; , , , . , , , ; ; , . .

+1
source

I ran into this problem. You can get around this by entering some default value for building a DTO, but it is not ideal. I wrote in more detail on the blog here, this can help someone understand the problem further.

0
source

All Articles