I'm a visual person which means that I see pictures in my head when I think about abstract things like structure, code and information. My guess is that most people do, but anyway...
When I first started looking at Neo4j I was blown away by how precise the graph database structure matched my mental model of information. You have blobs of informations (nodes with properties) that relates to other blobs of information. Perfect!
When I started playing around with the API I couldn't do what I wanted, namely extend NodeImpl and make the objects I want to persist Nodes. Now, Neo4j has a very nice API which is hard to abuse (if you don't place your classes in the same namespace as they use), and understandably they don't want you to extend NodeImpl. The design guide gives this example of use:
public class CustomerImpl implements Customer{ private final Node underlyingNode; private static final String KEY_FIRST_NAME = "firstName"; CustomerImpl( Node underlyingNode ){ this.underlyingNode = underlyingNode; } public void setFirstName( String firstName ){ underlyingNode.setProperty( KEY_FIRST_NAME, firstName ); } public String getFirstName(){ return ( String ) underlyingNode.getProperty( KEY_FIRST_NAME ); } // ... }
That's great, but it doesn't match what I see in my head... In order to make things match I made a subtle change.
public class NodeDelegate implements Node { private Node delegate; protected NodeDelegate(Node node) { delegate = node; } // Delegate all methods of the Node interface to the // Node received in the constructor... } public class CustomerNode extends NodeDelegate implements Customer { private static final String KEY_FIRST_NAME = "firstName"; CustomerNode(Node node) { super(node); } public void setFirstName( String firstName ){ setProperty( KEY_FIRST_NAME, firstName ); } public String getFirstName(){ return ( String ) getProperty( KEY_FIRST_NAME ); } // ... }
By doing this I can work with my domain objects as Nodes. I don't know if it is "correct" but with this small change I can say that "A Customer IS a Node" and this is what I see in my head. Good? Bad? You be the judge, but it fits me
Mattias Ask
Consultant at Jayway

0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment