[Neo] Dynamic properties...
Tobias Ivarsson
tobias.ivarsson at neotechnology.com
Fri Apr 17 11:30:55 CEST 2009
On Fri, Apr 17, 2009 at 10:25 AM, Mattias Ask <mattias.ask at jayway.se> wrote:
> Hi,
>
> I have a new problem which I've never encountered before... Code, and
> not the persistence, restricts what I want to do.
>
> I love Neo4j since it matches how I think of informations, but due to
> that I now I find that Java doesn't. What is it I want to do?
>
> Let's say you have a Teenager. In some cases you are interested in
> Teens with children. Should your Teenager class have an method called
> getChildren() or isParent()? I say no, since the vast majority of
> Teens do not have children... Down in Neo this isn't a problem, since
> you just add relations to the Child, or add a hasChildren property, to
> the specific Teenager that has kids. But in Java...
>
> Things that comes to mind are mix-ins, AOP, Qi4j, dynamic languages
> etc.... but I'm just a simple Java programmer that happens to think in
> nodes. What would you say is the easiest way for me to handle this
> flexibility in Java code?
I would suggest adding some sort of "casting" ability to your objects.
Let the base class / interface for (probably all of) your domain objects.
This could for example be something like:
<code>
public interface DomainObjectBase {
<T> T cast(AspectImplementor<T> implementor);
}
import org.neo4j.api.core.Node;
public interface AspectImplementor<T> {
T implement(Node node);
}
Then do something like:
Parent teenWithChildren = teenager.cast(new AspectImplementor<Parent>(){
public Parent implement(Node node) {
if (node.hasRelationship(DomainRelations.CHILD, Direction.OUTGOING))
{
return new ParentImplementation(node);
}
return null;
}
});
</code>
Please note that this is just some ideas i threw together in 5 minutes, with
more thought behind it you can probably come up with something that fits
better with the rest of your code.
I believe Qi4j implements something similar to this.
> I think Rickard Öberg says "Classes are dead. Long live Objects!" when
> he talks about Qi4j (www.qi4j.org) and I find myself very attracted to
> that thought...
So do I.
--
Tobias Ivarsson <tobias.ivarsson at neotechnology.com>
Hacker, Neo Technology
www.neotechnology.com
Cellphone: +46 706 534857
More information about the User
mailing list