Created
December 10, 2013 08:08
-
-
Save cskardon/7887178 to your computer and use it in GitHub Desktop.
Same as https://gist.github.com/cskardon/7870411 but with a generic Create extension method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Neo4jClient; | |
using Neo4jClient.Cypher; | |
using Newtonsoft.Json; | |
#region Data objects | |
public enum Relationship | |
{ | |
LIKES, | |
DISLIKES | |
} | |
public abstract class Neo4jObject | |
{ | |
protected Neo4jObject(string labels) | |
{ | |
Labels = labels; | |
} | |
[JsonIgnore] | |
public string Labels { get; private set; } | |
} | |
public class Person : Neo4jObject | |
{ | |
public const string TypeLabels = "Person"; | |
public Person() | |
: base(TypeLabels) | |
{ | |
} | |
public string Name { get; set; } | |
} | |
public class Reason | |
{ | |
public string Value { get; set; } | |
} | |
#endregion Data objects | |
public class LikesAndDislikes | |
{ | |
public static void Main() | |
{ | |
var client = new GraphClient(new Uri("http://localhost.:7474/db/data")); | |
client.Connect(); | |
Creates(client); | |
WhoDislikes(client, new Person { Name = "Person D" }); | |
} | |
#region Getting | |
private static void WhoDislikes(IGraphClient Client, Person disliked) | |
{ | |
var query = Client.Cypher | |
.Match(string.Format("(p:{0})<-[r:DISLIKES]-(other:{0})", disliked.Labels)) | |
.Where((Person p) => p.Name == disliked.Name) | |
.Return((other, r) => | |
new | |
{ | |
other.As<Person>().Name, | |
Reason = r.As<Reason>().Value | |
}); | |
var res = query.Results.ToList(); | |
Console.WriteLine("{0} disliked by:", disliked.Name); | |
foreach (var result in res) | |
Console.WriteLine("\t{0} because {1}", result.Name, result.Reason); | |
} | |
#endregion Getting | |
#region Creation | |
private static void Creates(IGraphClient client) | |
{ | |
var pA = new Person { Name = "Person A" }; | |
var pB = new Person { Name = "Person B" }; | |
var pC = new Person { Name = "Person C" }; | |
var pD = new Person { Name = "Person D" }; | |
client.Create(pA); | |
client.Create(pB); | |
client.Create(pC); | |
client.Create(pD); | |
CreateRelationship(client, pA, pB, Relationship.LIKES); | |
CreateRelationship(client, pB, pC, Relationship.LIKES); | |
CreateRelationship(client, pB, pD, Relationship.DISLIKES, new Reason { Value = "Crazy guy" }); | |
CreateRelationship(client, pC, pD, Relationship.DISLIKES, new Reason { Value = "Don't know why..." }); | |
CreateRelationship(client, pD, pA, Relationship.LIKES); | |
} | |
private static void CreateRelationship(IGraphClient client, Person from, Person to, Relationship relationship, Reason reason = null) | |
{ | |
ICypherFluentQuery query = client.Cypher | |
.Match(string.Format("(f:{0}), (t:{1})", from.Labels, to.Labels)) | |
.Where((Person f) => f.Name == from.Name) | |
.AndWhere((Person t) => t.Name == to.Name); | |
if (reason == null) | |
query | |
.CreateUnique(string.Format("(f)-[:{0}]->(t)", relationship)).ExecuteWithoutResults(); | |
else | |
query | |
.CreateUnique(string.Format("(f)-[:{0} {{relParam}}]->(t)", relationship)) | |
.WithParam("relParam", reason) | |
.ExecuteWithoutResults(); | |
} | |
#endregion Creation | |
} | |
public static class Neo4jClientExtensions | |
{ | |
public static void Create<T>(this IGraphClient client, T t) | |
where T: Neo4jObject | |
{ | |
client.Cypher | |
.Create(string.Format("(t:{0} {{params}})", t.Labels)) | |
.WithParam("params", t) | |
.ExecuteWithoutResults(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment