c# - How to differentiate between not set and set to null during XML Serialization? -


i have wcf service allows external application update database. have update operation contract accepts data contract external application should set. problem cannot distinguish set null , not set because when data contract serialized value null. consider following data contract:

[datacontract, xmlroot("person")] public class person: baseentity {     [datamember, xmlelement]     public string prefix { get; set; }     [datamember, xmlelement]     public string firstname { get; set; }     [datamember, xmlelement]     public string middlename { get; set; }     [datamember, xmlelement]     public string lastname { get; set; }     [datamember, xmlelement]     public string maidenname { get; set; }     [datamember, xmlelement] } 

one external application can set firstname , lastname , ignore rest of properties. when wcf service receive request, other properties set null. update statememt in wcf service think properties set null. wish find way determine properties not set update statement ignore properties.

you can not distinguish 2 cases. every variable starts life time initial value, null reference types, 0 integers , floating point numbers , on. in consequence there absolutely no difference between

string firstname; 

and

string firstname;  firstname = null; 

this means have perform tracking manually if need information.

string firstname; boolean firstnameset;  firstname = null; firstnameset = true; 

note manually mean have keep track of information manually, can of course add tracking code property setters users of class don't have aware of this.

public class person {    private string firstname = null;     private boolean firstnameset = false;     private string lastname = null;     private boolean lastnameset = false;     public string firstname    {       { return this.firstname; }       set       {          this.firstname = value;          this.firstnameset = true;       }    }     public string lastname    {       { return this.lastname; }       set       {          this.lastname = value;          this.lastnameset = true;       }    } } 

there lot of other alternatives implement this, example can use set track set properties. best solution depends on requirements.

public class person {    private readonly iset<string> setproperties = new hashset<string>();     private string firstname = null;     private string lastname = null;     public string firstname    {       { return this.firstname; }       set       {          this.firstname = value;          this.setproperties.add("firstname");       }    }     public string lastname    {       { return this.lastname; }       set       {          this.lastname= value;          this.setproperties.add("lastname");       }    } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -