-
Notifications
You must be signed in to change notification settings - Fork 8
Blueprint41 Query
Blueprint41 Query is based on cypher query language. See Cypher Query Language.
Using the database model, we generate classes that you can use to construct cypher queries in a fully type-safe manner. Unlike the magic string equivalent of these queries, their type-safe cousins give you full IntelliSense support. The good thing is that a manually written one-off code will generate a compile-time error as soon as the database model has refactored in an incompatible manner. This means that generated code and manually added tweaks would be guaranteed to be compatible.
-
On the
MovieGraph
project, select the fileProgram.cs
, then create the static methodDisplayMoviesForActorName(string)
. In the method, create a transaction and the compiled-query to get theMovie
nodes byName
of thePerson
(Actor).private static void DisplayMoviesForActorName(string actorName) { List<Movie> movies = new List<Movie>(); using (Transaction.Begin()) { var query = Transaction.CompiledQuery .Match ( Node.Person.Alias(out var actor) .In.ACTED_IN.Out. Movie.Alias(out var movie) ) .Where(actor.name == Parameter.New<string>("ActorName")) .Return(movie) .Compile(); movies = Movie.LoadWhere(query, new Parameter("ActorName", actorName)); } if (movies.Count > 0) { Console.WriteLine($"Actor: { actorName }"); Console.WriteLine("Movies:"); foreach (var movie in movies) { Console.WriteLine($" { movie.title }"); } } else { Console.WriteLine($"\n Movies for actor \"{actorName}\" not found."); } }
-
Getting the list of movies by an actor is pretty straightforward. The
Movie
class contains lots of convenience methods. For now, we will be using theLoadWhere
method.The
LoadWhere
method accepts aCompileQuery
and an optionalParameters
.List<Movie> movies = Movie.LoadWhere(query, new Parameter("ActorName", actorName));
-
The
LoadWhere
method will return a typesafe value ofList<Movie>
. So we can iterate all the movies theactorName
was involved in.if (movies.Count > 0) { Console.WriteLine($"Actor: { actorName }"); Console.WriteLine("Movies:"); foreach (var item in movies) { Console.WriteLine($" { item.title }"); } } else { Console.WriteLine($"\n Movies for actor \"{actorName}\" not found."); }
You've successfully created the method DisplayMoviesForActorName(string)
that queries the Movie
records by the parameter actorName
.
-
On the
Main
method, before theConsole.ReadKey()
, call theDisplayMoviesForActorName(string)
method withactorName
parameter value "Keanu Reeves".DisplayMoviesForActorName("Keanu Reeves");
Note
Comment other the method calls except DisplayMoviesForActorName("Keanu Reeves")
.
static void Main(string[] args)
{
Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]");
PersistenceProvider.CurrentPersistenceProvider = provider;
Datastore model = new Datastore();
model.Execute(true);
//CreateMoviesAndActors();
//DeleteMovie("Cloud Atlas");
//UpdateMovie();
//UpdateRelationships();
//GetActorsFromRelationshipAndAddToMovie();
//GetMoviesAndTheActors();
//GetRelationshipsByPropertyValue();
DisplayMoviesForActorName("Keanu Reeves");
Console.ReadKey();
}
- Press F5 to run the "MovieGraph" application.
-
Change the
actorName
parameter value to "Johnny Depp" on callingDisplayMoviesForActorName(string)
method.DisplayMoviesForActorName("Johnny Depp");
-
Press F5 to run the "MovieGraph" application again.
🎉 You've successfully created the Blueprint41
query and used to get the Movie
records by Person.name
(actor's name).
The next topic will guide you on how to refactor the MovieGraph
datastore model.