0

I have a relatively complex entity model that is based on Contracts (Interfaces) and am looking for thoughts on the best way to use Neo4j to store the entities. Has anyone tried to do this?

Example model. The idea would be to support storing the user object which contains an IAddress.

public interface IAddress {
    string Line1 { get; set; }
    string Line2 { get; set; }
}

public interface IUser {
    string Name { get; set; }
    IAddress Address { get; set; }
}

public Address : IAddress {
    public string Line1 { get; set; }
    public string Line2 { get; set; }
}

public class User : IUser {
    public User() {
        Address = new Address();
    }

    public string Name { get; set; }
    public IAddress Address { get; set; }
}
Shawn
  • 115
  • 2
  • 13
  • So I have no experience at all with `neo4jclient` and things may go deeper than your example, but from what you have shown above, wouldn't you want to store the `Address` as a separate node that is then related to the user? – JohnMark13 Jan 21 '15 at 12:16

1 Answers1

0

The problem is that you can't store complex types into Neo4j, it just doesn't accept them - regardless of whether you're using interfaces or just classes (I imagine you're getting a CypherTypeException if you try to store this in the DB).

To get around that, the best route I've come across is to use a custom serializer (for which Neo4jClient is totally capable of using), as in the answer to this question: Can Neo4j Store a Dictionary in a Node.

Obviously in your case, replace the Dictionary with your types. You can probably make a more generic version to cover multiple types if you need that.

Community
  • 1
  • 1
Charlotte Skardon
  • 6,220
  • 2
  • 31
  • 42
  • Why wouldn't mapping these "complex types" to relationships work? If you think about it we have a few key entities above (User and Address in this example). The above example works fine with concrete types using the Relationship class/concept. My question is more around supporting something like an IoC container to resolve the interfaces to concrete objects when they are loaded from the database. From reviewing the library, it looks like this is all handled by the Json.NET library which actually does have a dependency resolver mechanism. Has anyone tried using it? – Shawn Feb 01 '15 at 16:01
  • I was thinking about your answer on "complex types" and I am not sure I agree with the concept that all complex "properties" should be relationships. At the end of the day, the entire "node" should be something that can be serialized and stored. While create nodes for Addresses can be done, I would ask the question -- why should the developer be forced to create nodes and relationships. If I never intend to do any direct queries on the properties of the Address class independently of the main user, then why not just store it all -- after all it is just a serialized string when it is stored. – Shawn Feb 02 '15 at 02:50
  • Erm, that's not what my answer says - my answer gives you a way to serialize your node and store it, at no point do I mention changing your structure. In my response what I suggest is adding a custom serializer that under-the-hood is using JSON.NET to serialize as you mention. – Charlotte Skardon Feb 02 '15 at 11:04