Skip to content

Refactoring Model

Link edited this page Apr 25, 2024 · 20 revisions

Refactoring Model

This topic will guide you on how to refactor the Blueprint41 datastore model.

The database meta-model is defined in C# code and represents a stricter version of the neo4j schema. It defines the types of nodes, which properties they have and the relations the node type can have. The relations define the multiplicity of relationships. The database meta-model feels more like a class definition than a graph definition, which makes sense when you realize that the data eventually needs to be loaded into objects. Having a database meta-model in this way has the added advantage that the refactor actions can be applied to the data in the database as well as to the type-safe model.

Refactor the Movie and Person entity

To adjust the casing of the properties for the Movie and Person entity, go to "MovieGraph.Model" project, and select the file "Datastore.cs".

  1. Create a new method RefactorProperties and implement the code for refactoring the Movie and Person entity properties. Please refer to the table below for the property changes:

    Entity CurrentProperty NewProperty
    Movie title Title
    tagline Tagline
    released Released
    Person name Name
    born Born

Important

Don't forget to set a Version attribute for the RefactorProperties method so that it will be included in the database update when running the application.

    [Version(1, 0, 1)]
    protected void RefactorProperties()
    {
        Entities["Movie"].Properties["title"].Refactor.Rename("Title");
        Entities["Movie"].Properties["tagline"].Refactor.Rename("Tagline");
        Entities["Movie"].Properties["released"].Refactor.Rename("Released");

        Entities["Person"].Properties["name"].Refactor.Rename("Name");
        Entities["Person"].Properties["born"].Refactor.Rename("Born");
    }
  1. After implementing the code for refactoring the datastore model, the next step is to generate the type-safe models based on the updated data store model. Make sure to Build the MovieGraph.Model project to generate type-safe generated models. See generate type-safe models.

  2. Run the "MovieGraph" application. Select Debug > Start Debugging or you can press F5.

    Oops! There are errors while compiling the "MovieGraph" project. 😢

Refactor-Compile Error

Since the database meta-model has changed by generating the type-safe models, the Movie and Person entity properties have now changed. Now, you need to update the codes that still uses the previous properties.

  1. To fix the errors, click each item in the error list and you will go directly to the line of codes that needs to be fixed.

image

To fix the query in the DisplayMoviesForActorName method, focus the cursor to the name and press Ctrl + Space to display the available members of the Person entity and select Name. You can also do the same to fix other properties of Person and Movie entity.

image

To fix the UpdateMovie and DeleteMovie methods, it uses the method LoadBytitle(string) but since the title property has changed to Title, the method Movie.LoadBytitle(string) will also changed its name to Movie.LoadByTitle(string).

Focus the cursor to the Movie.LoadBytitle(string) method and press Ctrl + Space, then select the Movie.LoadByTitle(string) method.

image

image

For the other errors, you can fix it similarly by applying the steps mentioned above.

After updating the codes, build the "MovieGraph" project to check if there are still errors.

  1. After the errors have been fixed and successfully build the project, perform this statement MATCH (n:Movie) RETURN n LIMIT 25 in the Neo4j browser to check the current properties of the Movie entity.

image

As you can see, the Movie property names have not yet changed.

  1. Comment other methods call except the GetMoviesAndTheActors method, then press F5 to run the "MovieGraph" application.

image

Voila! Your console application works perfectly again.

  1. In the Neo4j browser, perform this statement MATCH (r:RefactorVersion) RETURN r to verify that your upgrade script Version 1.0.1 has successfully executed in the database.

image

  1. Perform this statement MATCH (n:Movie) RETURN n LIMIT 25 and MATCH (n:Person) RETURN n LIMIT 25 in the Neo4j browser to check if the Movie and Person entity successfully updated its property names.

image

image

Congratulations! 🎉 You've executed your upgrade script and refactored the Movie and Person entity successfully.