c# - Design Pattern for Object Modification with Timestamp -
i have colleciton of objects need maintain several time-stamps last time properties within object updated (one time-stamp per property).
i implement time-stamp update in setter except deserialization library being used first creates object, updates of properties (using object's setter). means time-stamps invalidated every time program deserializes them.
i'm thinking need singleton class or update method handles updating properties , controls time-stamp update. there better way implement behavior? design pattern exist behavior?
if separate serialization concerns business layer, should find flexibility hammer out solution. have 99% of api work business object (which updates timestamps when properties update), convert to/from data-transfer-object (dto) serialization purposes only.
for example, given business object this:
public class myobject { public datetime somevalueupdated { get; private set; } private double _somevalue; public double somevalue { { return _somevalue; } set { somevalueupdated = datetime.now; _somevalue = value; } } public myobject() { } //for deserialization purposes public myobject(double somevalue, datetime somevalueupdated) { this.somevalue = somevalue; this.somevalueupdated = somevalueupdated; } }
you have matching dto this:
public class myobjectdto { public datetime somevalueupdated { get; set; } public double somevalue { get; set; } }
your dto can specially adorned various xml schema altering attributes, or can manage timestamps see fit , business layer doesn't know , doesn't care.
when comes time serialize or deserialize objects, run them through converter utility:
public static class myobjectdtoconverter { public static myobjectdto toserializable(myobject myobj) { return new myobjectdto { somevalue = myobj.somevalue, somevalueupdated = myobj.somevalueupdated }; } public static myobject fromserializable(myobjectdto myobjserialized) { return new myobject( myobjserialized.somevalue, myobjserialized.somevalueupdated ); } }
if wish, can make of properties or constructors of myobject
internal
conversion utility can access them. (for example, maybe don't want have public myobject(double somevalue, datetime somevalueupdated)
constructor publicly accessible)
Comments
Post a Comment