Skip to content

Code Generation and Blueprint41

Link edited this page Apr 5, 2024 · 4 revisions

Code Generation and Blueprint41 Project

In this tutorial, we will see how to generate codes and use it on a Blueprint41 project.

  1. Launch Visual Studio 2017, create a blueprint41 project "MovieGraph". Select the OK button.

Code Generation

Code Generation

  1. On the Graph Modeller, open the "Code Generation" dialog.

Code Generation

Code Generation

  1. On the "Code Generation" dialog, check the "Movie". You will see the generated code for "Movie" entity on the right side of the window.

Code Generation

  1. Click the "Select All" button to generate codes for all entities in the model.

Code Generation

  1. You can export the generated code by clicking the Export All button.

Code Generation

  1. Click "Copy to Clipboard" button to copy the generated codes.

Code Generation

  1. On the "MovieGraph" project, open the "Datastore.cs" and paste the copied code to the Initial method.

Code Generation

Code Generation

  1. Rebuild the "MovieGraph.Model" project to generate type-safe generated models..

Rebuild Project

Code Generation

As you can see, you have now the generated files based on the graph modeller's generated codes.

Code Generation

  1. Rebuild the "MovieGraph.Generated" project.

  2. Now, let's use our datastore MovieGraph.Model. Add a new project "MovieGraph" console application. Select File > Add > New Project from the menu bar. In the Add New Project* dialog, select the Visual C# node. Then select the Console App (.NET Framework) project template. In the Name text box, type "MovieGraph". Select the Browse button.

Code Generation

  1. Select the MovieGraph folder and click Select Folder. In the Add New Project* dialog, select the OK button to create the project.

Code Generation

  1. Set the "MovieGraph" console application as a startup project, right-click the "MovieGraph" project and click Set as StartUp Project.

Code Generation

  1. Add References to MovieGraph Console Application. Right-click MovieGraph project, select Add > Reference. In the Reference Manager* dialog, select Projects node, check the MovieGraph.Generated and MovieGraph.Model projects. Select the OK button.

Code Generation

  1. Add Blueprint41 Nuget package reference to "MovieGraph" Console Application. Select Tools > NuGet Package Manager > Package Manager Console from the menu bar. In the NuGet Package Manager Console, type Install-Package Blueprint41 and wait until the Blueprint41 package successfully installs.

Code Generation

  1. After you've created the "MovieGraph" project, select the file Program.cs, then include the following namespaces:
using Blueprint41;
using Blueprint41.Core;
using Blueprint41.Neo4j.Persistence;
using Domain.Data.Manipulation;
using MovieGraph.Model;
using System;
  1. On the Main method, setup your neo4j database connection. Specify the database server and its authentication credentials.
    PersistenceProvider.CurrentPersistenceProvider =
        new Neo4JPersistenceProvider($"bolt://localhost:7687", $"neo4j", $"neo");
  1. On the Main method, create an instance of your datastore MovieGraph.Model and call the Execute method to perform a database upgrade if it exists or creates the database if it's not.
    Datastore model = new Datastore();
    model.Execute(true);
  1. To insert records on MovieGraph database, create the InsertRecords method.
private static void InsertRecords()
{
    using (Transaction.Begin())
    {
        Movie movie1 = new Movie()
        {
            Title = "The Matrix",
            Tagline = "Welcome to the Real World",
            Released = new DateTime(1999, 3, 31)
        };

        Movie movie2 = new Movie()
        {
            Tagline = "Free your mind",
            Title = "The Matrix Reloaded",
            Released = new DateTime(2003, 5, 15)
        };

        Person person1 = new Person()
        {
            Name = "Keanu Reeves",
            Born = new DateTime(1964, 9, 2)
        };

        Person person2 = new Person()
        {
            Name = "Laurence Fishburne",
            Born = new DateTime(1961, 7, 30)
        };

        Person person3 = new Person()
        {
            Name = "Lana Wachowski",
            Born = new DateTime(1961, 7, 30)
        };

        Person person4 = new Person()
        {
            Name = "Lilly Wachowski",
            Born = new DateTime(1967, 12, 29)
        };

        Person person5 = new Person()
        {
            Name = "Joel Silver",
            Born = new DateTime(1952, 7, 14)
        };

        movie1.Actors.Add(person1);
        movie1.Actors.Add(person2);
        movie1.Directors.Add(person3);
        movie1.Directors.Add(person4);
        movie1.ScreenWriters.Add(person3);
        movie1.ScreenWriters.Add(person4);
        movie1.Producers.Add(person5);
        movie1.Genre.Add(Genre.Lookup(Genre.StaticData.Name.Action));
        movie1.Genre.Add(Genre.Lookup(Genre.StaticData.Name.Sci_Fi));

        movie2.Actors.Add(person1);
        movie2.Actors.Add(person2);
        movie2.Directors.Add(person3);
        movie2.Directors.Add(person4);
        movie2.ScreenWriters.Add(person3);
        movie2.ScreenWriters.Add(person4);
        movie2.Producers.Add(person5);
        movie2.Genre.Add(Genre.Lookup(Genre.StaticData.Name.Action));
        movie2.Genre.Add(Genre.Lookup(Genre.StaticData.Name.Sci_Fi));

        Transaction.Commit();
    }
}
  1. Invoke the InsertRecords method from the Main method.
static void Main(string[] args)
{
    PersistenceProvider.CurrentPersistenceProvider =
        new Neo4JPersistenceProvider($"bolt://localhost:7687", $"neo4j", $"neo");

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

    InsertRecords();
    
    Console.WriteLine("Inserting MovieGraph records...");
    Console.WriteLine("Press any key to continue...");
    Console.ReadKey(true);
}
  1. Rebuild the solution and run the MovieGraph application.

Code Generation

  1. Open the Neo4j browser and execute the following script:
MATCH p=()-->() RETURN p LIMIT 25

Code Generation

Congratulations! 🎉 You have successfully used the graph modeller's generated code on the Blueprint41 "MovieGraph" project and inserted records on the "MovieGraph" database.

Next, we'll take a look at how to create to create sub-models.