This is your first challenge of this course. Your challenge here is to modify the code to create a new instance of the Driver that can be used across the application.
As we discussed in the About the Driver lesson, it is best practice to create a single instance of the driver in our application per Neo4j cluster or DBMS.
In the Neo4j
class inside Neoflix/Neo4j.cs
, you will see an InitDriverAsync()
method.
public static Task InitDriverAsync(string uri, string username, string password)
{
// TODO: Create an instance of the driver here
return Task.CompletedTask;
}
This method accepts the parameters required to create a new Driver
instance with basic authentication.
Challenge: Implement the initDriver function.
Your first challenge is to modify this method to create an instance of the driver and verify that the connection details are correct.
To do this, we will need to:
-
Install the
Neo4j.Driver
dependency. -
Use the
GraphDatabase.Driver
from the dependency method to create a new Driver instance and assign it to the local_driver
variable. -
Call the
VerifyConnectivityAsync()
method on the newly created driver instance to check the connection details.
1. Add the Neo4j.Driver Dependency
Firstly, run the following command in Visual Studio to install the Neo4j.Driver
dependency.
PM> Install-Package Neo4j.Driver
Alternatively, you can run the dotnet add
command.
dotnet add Neoflix package Neo4j.Driver
2. Importing the Dependency
To include the Driver dependency in our module, you will add an using
directive to the top of the file.
Copy and paste the following code into the top of main/Neoflix/Neo4j.cs
.
// Import all relevant classes from neo4j-dotnet-driver
using Neo4j.Driver;
3. Creating the Driver Instance
Create the driver instance by calling the GraphDatabase.Driver()
method.
The first argument will be the uri
passed as the first parameter.
To create an authentication token, call the AuthTokens.basic()
function with the username
and password
arguments passed to the method.
Assign the value returned by the GraphDatabase.Driver()
method to the _driver
variable and call _driver.verifyConnectivityAsync()
to verify connectivity
public static async Task InitDriverAsync(string uri, string username, string password)
{
_driver = GraphDatabase.Driver(uri, AuthTokens.Basic(username, password));
await _driver.VerifyConnectivityAsync();
}
If the connection cannot be made for any reason, call will fail with an Exception. If this occurs, your application will be unable to communicate with Neo4j. Manual investigation will be required to diagnose the issue.
If the connection has been successfully verified, it will return an instance of the driver.
Testing
To test that this functionality has been correctly implemented, run the following code in a new terminal session:
dotnet test --logger "console;verbosity=detailed" --filter "Neoflix.Challenges._01_ConnectToNeo4j"
The test file is located at Neoflix.Challenges/01-ConnectToNeo4j.cs
.
Are you stuck? Click here for help
If you get stuck, you can see a working solution by checking out the 01-connect-to-neo4j
branch by running:
git checkout 01-connect-to-neo4j
You may have to commit or stash your changes before checking out this branch. You can also click here to expand the Support pane.
Check Your Understanding
1. How many tests are run in the test you ran?
-
❏ 1
-
❏ 2
-
✓ 3
-
❏ 4
Hint
You can run the Neoflix.Challenges/01-ConnectToNeo4j.cs
test by running the following command:
dotnet test --logger "console;verbosity=detailed" --filter "Neoflix.Challenges._01_ConnectToNeo4j"
Solution
There are 3 tests in the test suite.
Lesson Summary
You have implemented the code to create a new driver instance with environment variables read from the appsettings.json
file which verifies that the driver can connect to the DBMS before the application starts.
In the next module we will look at how to use the driver to query the DBMS.