Skip to content

Blueprint41 Query

CIRCLES ARROWS LIMITED edited this page Aug 2, 2019 · 15 revisions

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 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.

Create Compiled Query

  1. On the MovieGraph project, select the file Program.cs, then create the static method DisplayMoviesForActorName(string). In the method, create a transaction and the compiled-query to get the Movie nodes by Name of the Person (Actor).
private static void DisplayMoviesForActorName(string actorName)
{
    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();
    }
    
    List<Movie> 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($@"Movies for actor ""{ actorName }"" not found.");
    }
}
  1. 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 the LoadWhere method.

The LoadWhere methods accepts a CompileQuery and an optional Parameters.

List<Movie> movies = Movie.LoadWhere(query, new Parameter("ActorName", actorName));
  1. The LoadWhere method will return a typesafe value of List<Movie>. So we can iterate all the movies the actorName 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($@"Movies for actor ""{ actorName }"" not found.");
}

You've successfully created the method DisplayMoviesForActorName(string) that queries the Movie records by the parameter actorName.

  1. On the Main method, before the Console.Read(), call the DisplayMoviesForActorName(string) method with actorName parameter value "Keanu Reeves".
DisplayMoviesForActorName("Keanu Reeves");

Note: Comment the methods: CreateRecords, UpdateRecord, DeleteRecord and ReadAllRecords.

static void Main(string[] args)
{
    PersistenceProvider.CurrentPersistenceProvider =
      new Neo4JPersistenceProvider($"bolt://localhost:7687", $"neo4j", $"neo");

    Datastore model = new Datastore();
    model.Execute(true);

    //CreateRecords();

    //UpdateRecord();
    
    //DeleteRecord();
    
    //ReadAllRecords();

    DisplayMoviesForActorName("Keanu Reeves");
    
    Console.ReadKey();
}
  1. Press F5 to run the "MovieGraph" application.

Blueprint41 Query

  1. Change the actorName parameter value to "Johnny Depp" on calling DisplayMoviesForActorName(string) method.
DisplayMoviesForActorName("Johnny Depp");
  1. Press F5 to run the "MovieGraph" application again.

Blueprint41 Query Not Found

You've successfully created the Blueprint41 query and used to get the Movie records by Person.name (actor's name).

Next topic will guide you on how to refactor the MovieGraph datastore model.