Skip to content

Blueprint41 Query

Link edited this page Apr 25, 2024 · 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 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.

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)
    {
        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.");
        }
    }
  2. 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 method accepts a CompileQuery and an optional Parameters.

    List<Movie> movies = Movie.LoadWhere(query, new Parameter("ActorName", actorName));
  3. 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($"\n 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.ReadKey(), call the DisplayMoviesForActorName(string) method with actorName 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();
    }
  1. Press F5 to run the "MovieGraph" application.

image

  1. Change the actorName parameter value to "Johnny Depp" on calling DisplayMoviesForActorName(string) method.

    DisplayMoviesForActorName("Johnny Depp");
  2. Press F5 to run the "MovieGraph" application again.

image

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