Use mongodb as backend database #1

Open
didiercrunch wants to merge 2 commits from mongodb into main
pull from: mongodb
didiercrunch commented 2023-09-24 11:15:44 -04:00 (Migrated from github.com)

Moving away from memory persistance to mongodb.

Moving away from memory persistance to mongodb.
didiercrunch (Migrated from github.com) reviewed 2023-09-24 11:32:37 -04:00
@@ -133,3 +133,5 @@ $RECYCLE.BIN/
_NCrunch*
.vscode
didiercrunch (Migrated from github.com) commented 2023-09-24 11:16:53 -04:00

this is not useful. I used JetBrain's rider IDE to implement this feature so I needed to add couple of things to the .gitignore

this is not useful. I used JetBrain's rider IDE to implement this feature so I needed to add couple of things to the .gitignore
@@ -20,4 +20,8 @@
<ProjectReference Include="..\RobotsWantedLeague\RobotsWantedLeague.csproj" />
</ItemGroup>
didiercrunch (Migrated from github.com) commented 2023-09-24 11:17:24 -04:00

No idea why this change is here. I hope it does not break anything.

No idea why this change is here. I hope it does not break anything.
didiercrunch (Migrated from github.com) commented 2023-09-24 11:18:57 -04:00

It is hard to use mongodb and to write tests. The two solutions for this are

  1. Write mocks and use in memory lists in tests
  2. Use mongodb but clean the database between each test.

Both of these are relatively hard and I preferred to tests for now.

It is hard to use mongodb and to write tests. The two solutions for this are 1. Write mocks and use in memory lists in tests 2. Use mongodb but clean the database between each test. Both of these are relatively hard and I preferred to tests for now.
@@ -39,3 +39,2 @@
public IActionResult Robot(int id){
Robot robot = robotsService.GetRobotById(id);
public IActionResult Robot(string id){
didiercrunch (Migrated from github.com) commented 2023-09-24 11:19:17 -04:00

With mongodb, robot Ids are now strings.

With mongodb, robot Ids are now strings.
@@ -40,2 +40,2 @@
public IActionResult Robot(int id){
Robot robot = robotsService.GetRobotById(id);
public IActionResult Robot(string id){
Robot? robot = robotsService.GetRobotById(id);
didiercrunch (Migrated from github.com) commented 2023-09-24 11:19:27 -04:00

fixed typing.

fixed typing.
@@ -2,3 +5,3 @@
public class Robot{
public int Id { get; set;}
didiercrunch (Migrated from github.com) commented 2023-09-24 11:20:01 -04:00

This is important. I add custom annotation to make sure the robot ID matches the _id in mongodb.

This is important. I add custom annotation to make sure the robot ID matches the _id in mongodb.
didiercrunch (Migrated from github.com) commented 2023-09-24 11:20:38 -04:00

I need to allow null ids as it is mongodb that adds the ID to the document.

I need to allow null ids as it is mongodb that adds the ID to the document.
didiercrunch (Migrated from github.com) commented 2023-09-24 11:21:39 -04:00

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.

this is the configuration file for mongodb. It allows to extract the information from the application.json file into the proper code.
@@ -4,1 +5,4 @@
// database settings
builder.Services.Configure<RobotsDatabaseSettings>(
didiercrunch (Migrated from github.com) commented 2023-09-24 11:22:54 -04:00

This is the important part of the config change. This is what is needed to wire up mongodb as an ASP service.

This is the important part of the config change. This is what is needed to wire up mongodb as an ASP service.
@@ -5,2 +10,4 @@
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<RobotsService>();
didiercrunch (Migrated from github.com) commented 2023-09-24 11:23:22 -04:00

Little quirk to keep NotEmptyRobotsService working.

Little quirk to keep NotEmptyRobotsService working.
@@ -6,4 +6,13 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
didiercrunch (Migrated from github.com) commented 2023-09-24 11:24:51 -04:00

This adds mongodb as a dependency to the project. This file was changed automagically via

dotnet add package MongoDB.Driver
This adds mongodb as a dependency to the project. This file was changed automagically via ```shell dotnet add package MongoDB.Driver ```
@@ -10,3 +10,3 @@
string country);
public Robot? GetRobotById(int id);
public Robot? GetRobotById(string id);
didiercrunch (Migrated from github.com) commented 2023-09-24 11:25:06 -04:00

Migrated ids from int to string.

Migrated ids from int to string.
@@ -13,0 +18,4 @@
private void fillDefaultRobotsWhenUnderlyingRobotServiceIsEmpty()
{
if (underlyingRobotsService.Robots.Count() != 0)
didiercrunch (Migrated from github.com) commented 2023-09-24 11:26:38 -04:00

We changed a little bit how we build NotEmptyRobotsService. Now we ask ASP to auto wire the RobotsService for us. We need to do this because RobotsService depends on mongodb which can only be build via ASP auto wiring mechanism.

We changed a little bit how we build `NotEmptyRobotsService`. Now we ask ASP to auto wire the `RobotsService` for us. We need to do this because `RobotsService` depends on mongodb which can only be build via ASP auto wiring mechanism.
@@ -15,3 +27,4 @@
this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan");
}
didiercrunch (Migrated from github.com) commented 2023-09-24 11:26:57 -04:00

Made sure to only add new robots if the db is empty.

Made sure to only add new robots if the db is empty.
@@ -10,2 +11,3 @@
public RobotsService()
public List<Robot> Robots { get => robotsCollection.AsQueryable().ToList(); }
didiercrunch (Migrated from github.com) commented 2023-09-24 11:28:05 -04:00

So instead of a List<robot> we now depend on a IMongoCollection<Robot>. The api of IMongoCollection<Robot> is quite nice.

So instead of a `List<robot>` we now depend on a `IMongoCollection<Robot>`. The api of `IMongoCollection<Robot>` is quite nice.
@@ -12,1 +12,4 @@
public List<Robot> Robots { get => robotsCollection.AsQueryable().ToList(); }
public RobotsService(IOptions<RobotsDatabaseSettings> bookStoreDatabaseSettings)
{
didiercrunch (Migrated from github.com) commented 2023-09-24 11:29:37 -04:00

To return the list of all the robot we need to first fetch them. robotsCollection.AsQueryable() returns an object which is queryable via linq. The query here is empty (aka take all) and the method to realize the query is ToList.

To return the list of all the robot we need to first fetch them. `robotsCollection.AsQueryable()` returns an object which is queryable via linq. The query here is empty (aka take all) and the method to realize the query is `ToList`.
@@ -19,0 +19,4 @@
var mongoDatabase = mongoClient.GetDatabase(
bookStoreDatabaseSettings.Value.DatabaseName);
this.robotsCollection = mongoDatabase.GetCollection<Robot>(bookStoreDatabaseSettings.Value.RobotsCollectionName);
didiercrunch (Migrated from github.com) commented 2023-09-24 11:30:27 -04:00

This is how we manage to build the IMongoCollection. The only logic here is googlin g + copy/pasting

This is how we manage to build the `IMongoCollection`. The only logic here is googlin g + copy/pasting
@@ -40,1 +39,4 @@
where robot.Id == id
select robot;
return q.First();
}
didiercrunch (Migrated from github.com) commented 2023-09-24 11:30:51 -04:00

To insert, we now need to call InsertOne, as in mongodb

To insert, we now need to call InsertOne, as in mongodb
didiercrunch (Migrated from github.com) commented 2023-09-24 11:31:13 -04:00

this is cool. This is how to query mongodb via linq. This rocks.

this is cool. This is how to query mongodb via linq. This rocks.
didiercrunch (Migrated from github.com) commented 2023-09-24 11:31:51 -04:00

Deleting is a little bit more complicated. The weird part is the lambda function robot => robot.Id == id.

Deleting is a little bit more complicated. The weird part is the lambda function `robot => robot.Id == id`.
@@ -5,5 +5,10 @@
"Microsoft.AspNetCore": "Warning"
}
},
"RobotsDatabase": {
didiercrunch (Migrated from github.com) commented 2023-09-24 11:32:32 -04:00

This is the configuration needed to run the program using a mongodb instance running locally on mongodb's default port.

This is the configuration needed to run the program using a mongodb instance running locally on mongodb's default port.
You are not authorized to merge this pull request.
This pull request can be merged automatically.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin mongodb:mongodb
git checkout mongodb
Sign in to join this conversation.