Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

c# - Casting nodes of an unknown type

In using Neo4j I'm able to create an array of nodes with labels and then create relationships between those nodes. The labels are essentially mappings to my POCOs (the Dog label relates to a Dog POCO in C#) and these POCOs implement from a simple base POCO containing only an ID property.

When I know the type/label of the node to retrieve, I'm able to cast it using the node.As < T > syntax within the return statement. However, when doing things such as traversing a path between nodes, I will not know the type of the node that I am traversing. While it is technically possible to cast the node as the base type that my POCOs implement from, I lose all of the properties that are specific to the the super class.

Any ideas on how to get started with this one?

Graph

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could (depending on how you feel about it) try using dynamic, for example, you can set it up like so:

var dog = new Dog {Name = "Woofer", Breed = "Afghan Hound"};
var owner = new Person {Name = "Jeff", PhoneNumber = "01234567890"};

//CREATE
gc.Cypher.
    Create("(owner:Person {ownerParams})")
    .WithParam("ownerParams", owner)
    .With("owner")
    .Create("(owner)-[:HAS_PET]->(dog:Dog {dogParams})")
    .WithParam("dogParams", dog)
    .ExecuteWithoutResults();

and retrieve with:

//RETURN
var query = gc.Cypher
    .Match("(p:Person)-[:HAS_PET]->(d:Dog)")
    .Return((p, d) => new {Person = p.As<Node<string>>(), Dog = d.As<Node<string>>()});

var results = query.Results.ToList();
foreach (var result in results)
{
    dynamic p = JsonConvert.DeserializeObject<dynamic>(result.Person.Data);
    dynamic d = JsonConvert.DeserializeObject<dynamic>(result.Dog.Data);

    Console.WriteLine("If you find {0} (a {1}) please call {2} on {3}.", d.Name, d.Breed, p.Name, p.PhoneNumber);
}

Obviously in this case I would know the types I was returning. Now, you'll notice I'm using Node<string> in this - which generally is frowned upon - the reason I'm using it is that it strips out all the normal stuff neo4j returns back, and separates the Data out - which is really all I'm interested in.

You might be tempted to try doing:

.Return((p,d) => new {Person = p.As<dynamic>(), Dog = d.As<dynamic>()});

but the problem you'll end up with here is that the Neo4jClient doesn't deal with dynamic and will actually return it as an object which loses all your properties.

This should at least give you a starting point, if you need help with a specific type of query it'd be worth putting the query up for reference.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...