c# - Serialize and Deserialize Property Based on DB Value -
i have simple class has bool property. 'get' logic property executes stored procedure return bit field database.
i serialize class , save xml field in database. saves class , bool property fine, no problem.
the problem seem having when deserialize class. class deserilizes fine, when data drives bool field has been updated, seems class recognizes serialized in xml, , not looking database new bool value (does not execute procedure newly update bit field).
my solution has been add xmlignoreattribute attribute field isn't serialized begin with. i'm wondering if noticed and/or can me understand inner working of .net xmlserializer class.
thanks!
[xmlignoreattribute] public bool isupdated { { datatable dtresults = mclssqlservertool.loaddatatable("exec stp_rl_sel_nameisupdated '" + mstrname + "'"); bool blnisupdated = convert.toboolean(dtresults.rows[0]["ru_bitisupdated"]); return blnisupdated; } }
the first thing note here [xmlignore] redundant; xmlserializer not interested in get-only properties (except lists), because knows can't deserialize them. example:
public class sometype { public string foo { get; set; } public string bar { { console.writeline("get_bar"); return "abc"; } } static void main() { var ser = new xmlserializer(typeof (sometype)); ser.serialize(console.out, new sometype { foo = "def" }); } } outputs (minus namespaces aliases etc):
<sometype> <foo>def</foo> </sometype> (note bar not called)
for deserialization, process (for simple values, not lists) simple: values found in the incoming xml stream, resolve them members, , assign them - i.e. xml-deserializer glorified switch statement based on incoming xml nodes.
it never randomly call "set" unless data in incoming xml (and property read/write); , when does, expects assign value.
the interesting thing in scenario "get" doesn't assign value anywhere - there no cache. actually, doesn't matter xmlserializer doesn't touch - every time access isupdated query. suspect mistake, , lead aggressive , unpredictable data querying.
many serializers support concept of serialization callbacks, allow perform code @ end of serialization; however, xmlserializer does not support this. isn't option.
it isn't clear want achieve, i'd call method @ point.
Comments
Post a Comment