yojimbo87 / ArangoDB-NET

C#/.NET/Mono driver for ArangoDB
MIT License
107 stars 66 forks source link

Support for Polymorphism? #37

Open cocowalla opened 8 years ago

cocowalla commented 8 years ago

can't see any way to work with class hierarchies.

For example, let's say I had an Animal class, and Dog and Cat subclasses. Working with MongoDB I'd create an Animal collection, then I would insert Dog and Cat instances and a type discriminator field would automatically be added. I could then use Linq's .OfType<Dog> to query specifically on dogs, for example.

Is this supported?

yojimbo87 commented 8 years ago

Hi,

currently this functionality is not supported since, as you wrote, there would need to be a field which would identify a record type based on which some logic can retrieve required data. I don't know how it works in MongoDB, meaning that if it's the database engine or the driver, which take care of this field, but as far as I know there is no such thing in ArangoDB therefore it would need to be done by the driver in this case.

cocowalla commented 8 years ago

Yes, it would have to be done by the driver. In MongoDB a type discriminator field is added, which contains the name of the class being stored. The JSON serializer uses this to deserialize the correct type. I've seen this same solution used in other systems too, such as Marten (which has a column mt_dotnet_type that stores the type name).

darinb commented 8 years ago

No need to overly complicate the driver. Simply add a single line to any subclasses of Animal.

string type = (GetType().Name);

this will add the subclass type as a string value to the attribute 'type' in the document when inserted or updated. Then query as needed. Why overload a DB driver with data manipulation when it should be handled inside the class. A DB should not know anything about polymorphism.

cocowalla commented 8 years ago

A DB should not know anything about polymorphism

For a relational database, perhaps not. But perhaps so for a graph database such as ArangoDB.

But in anycase, when I said 'driver', I should have said 'client' - I meant this ArangoDB-NET client, not ArangoDB itself.

Why overload a DB driver with data manipulation when it should be handled inside the class

I would argue, why pollute my POCOs with database storage concerns that should be handled by the database client?

Other database clients do handle this sort of thing, like some of the MongoDB clients, or Marten.

jeff-pang commented 7 years ago

Support for Polymorphism is not a good idea. This causes the software to be tightly coupled to the driver and database, and also changes to your class structure or names will mean breaking a bunch of stuff. RavenDb does this, at first it seems like a neat feature but after awhile you realize that it makes changing stuff harder.