c# - dealing with dynamic properties on oData client -
i have following class on both server , client
public class entity { public string id {get; set;} public string name {get; set;} public dictionary<string, object> dynamicproperties {get; set;} }
as far have seen examples of open type describes having dynamic properties on server side, properties on client needs explicitly declared.when send post request client how send dynamic properties ?. can't declare dynamic properties on client side. there numerous properties , each object contain different set of dynamic properties in client side. these dynamic properties stored in dynamicproperties dictionary in client side. how send object of above entity class server side, server interpret contents of dynamicproperties dictionary dynamic properties ?. appreciated.
===========================follow-up sam's answer=======================
static void main(string[] args1) { container.customers.tolist(); customer newcustomer = new customer(); newcustomer.id = 19; newcustomer.properties = new dictionary<string, object> { {"intprop", 9}, {"datetimeoffsetprop", new datetimeoffset(2015, 7, 16, 1, 2, 3, 4, timespan.zero)}, {"blah","ha"} }; try { addcustomer(newcustomer); container.addtocustomers(newcustomer); container.savechanges(); } catch (exception) { } customer newcustomer1 = new customer(); newcustomer1.id = 20; newcustomer1.properties = new dictionary<string, object> { {"intprop", 10}, {"dir","north"} }; addcustomer(newcustomer1); container.addtocustomers(newcustomer1); container.savechanges(); newcustomer1.properties["dir"] = "south"; container.updateobject(newcustomer1); container.savechanges(); console.readkey(); } private static void addcustomer(customer customer) { container.configurations.requestpipeline.onentrystarting(args => { foreach (var property in customer.properties) { args.entry.addproperties(new odataproperty { name = property.key, value = property.value // enum, complex type, should create odataenumvalue , odatacomplexvalue. }); } }); }
i getting error stating multiple properties name 'intprop' detected in entry or complex value. in odata, duplicate property names not allowed. also, doubt if creating action each time before sending object how doing valid approach lot of objects source , send server. if create action each object might blow memory odata client holds these actions in memory. how handle scenario ?. kindly me.
also, 1 more question if comment container.customers.tolist() fails stating trying add undeclared properties. why ?
if using odata client code generator, can use partial class
define/retrieve/save dyanmic properties.
for example, in client side, can define partial class entity
public partial class entity { // dynamic property "email" [global::microsoft.odata.client.originalnameattribute("email")] public string email { { return this._email; } set { this.onemailchanging(value); this._email = value; this.onemailchanged(); this.onpropertychanged("email"); } } private string _email; partial void onemailchanging(string value); partial void onemailchanged(); }
then, can use insert/retrieve/save dynamic property "email".
you can this:
container container = new container(new uri("http://...")); entity entity = new entity(); ... entity.email = "xxxx"; container.addtoentities(entity); container.savechanges();
for similar implementation, can refer my sample project.
========== iteration 2 ================
for client entity
class idictionary<string,object>
, think hook you're looking for.
for example, on client side:
public partial class entity { public idictionary<string, object> properties { get; set; } ..... }
it should work if insert following codes before
container.addtoentities(entity);
for example:
entity entity = new entity(); ... entity.properties = new dictionary<string, object> { {"intprop", 9}, {"datetimeoffsetprop", new datetimeoffset(2015, 7, 16, 1, 2, 3, 4, timespan.zero)} }; container.configurations.requestpipeline.onentrystarting(args => { foreach (var property in entity.properties) { args.entry.addproperties(new odataproperty { name = property.key, value = property.value }); } }); container.addtoentities(entity); container.savechanges();
where, addproperties
extension method. can find in my sample project , the latest commit
besides, hood method works odata client v6.12 or above.
hope can you.
========== iteration 3 ================
first, call following method,
container.configurations.requestpipeline.onentrystarting(...);
it means add action called in later execution. in codes, call twice, so, there two actions added. these 2 actions called 1 one when execute save
newcustomer1
that's,newcustomer1
havenewcustomer
's dynamic properties (action 1), meanwhile, have own dynamic properties (action 2). that's why got duplicate property name exception.
to resolve it, can renew container
. see project's update.
- for
container.customers.tolist()
, seems odata client issue.
Comments
Post a Comment