mfenniak / rethinkdb-net

A C# / .NET client driver for RethinkDB.
Other
247 stars 37 forks source link

Dynamic properties not supported #217

Open smurfpandey opened 9 years ago

smurfpandey commented 9 years ago

I am trying to insert an object with dynamic property. But the code hangs at insert. I also tried replacing dynamic with JObject, but then, only id is getting inserted.

Object definition

public class Banana
    {

        [DataMember(Name = "id", EmitDefaultValue = false)]
        public Guid Id { get; set; }

        public dynamic flesh { get; set; }
    }

Code to Insert

public static async Task<string> PlantBanana(Banana objBanana)
        {
            string firstError = string.Empty;            
            using (IConnection conn = connectionFactory.Get())
            {
                var response = await conn.RunAsync(Banana.Table.Insert(objBanana));

                if (response.Errors > 0)
                {
                    firstError = response.FirstError;
                    return firstError;
                }                
            }

            return firstError;
        }

I am using Newtonsoft serializer.

mfenniak commented 9 years ago

Hey @smurfpandey. Thanks for the bug report; neither of the object serializers in rethinkdb-net currently support using dynamic properties or fields. But, our recently added support for key-value dictionaries might have laid the groundwork for this to be feasible... I will take a look into it and see what it would take to implement.

mfenniak commented 9 years ago

Hrm, there are limitations on the capabilities of dynamic with expression trees that seem like they would prevent rethinkdb-net from using them in any serious way. Nothing with a dynamic type can be used in any way inside of an expression tree, and, we use expression trees to convert C# logic into RethinkDB logic.

Take your example of a Banana class, and then try adding a query filter.

var response = await conn.RunAsync(Banana.Table.Filter(b => b.flesh > 100));

This will come up with a compiler error, error CS1963: An expression tree cannot contain a dynamic operation. If the compiler can't support this then we couldn't either, unless we introduced a whole new, non-type-safe mechanism to construct RethinkDB queries. I'm not sure that'd be worth the effort.

Is there a specific use-case that you had in mind for dynamic? I think I could support it as a return value, but, not as a value that can be interacted with in a query in any way.

smurfpandey commented 9 years ago

@mfenniak I am working on a simple mock server which will output JSON response. That's why I am using the dynamic flesh property to hold the JSON provided by the user. Since this mock server should accept any kind of valid JSON, I am using dynamic. Anyways, I replaced rethinkdb with arangodb, and got it working with it's .net driver. But the interesting part was, dynamic was not working there either, but that driver supports JSON string as input, so I serialized my object then stored it, same thing during fetching, serialize the output, then again deserialize as my Banana object.

You can check what i did here: https://github.com/smurfpandey/api-monkey/blob/master/APIMonkey/Code/DataManager.cs Driver I am using: https://github.com/yojimbo87/ArangoDB-NET