It is hard to use mongodb and to write tests. The two solutions for this are
Write mocks and use in memory lists in tests
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.
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.
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`.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Moving away from memory persistance to mongodb.
@@ -133,3 +133,5 @@ $RECYCLE.BIN/_NCrunch*.vscodethis 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>No idea why this change is here. I hope it does not break anything.
It is hard to use mongodb and to write tests. The two solutions for this are
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){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);fixed typing.
@@ -2,3 +5,3 @@public class Robot{public int Id { get; set;}This is important. I add custom annotation to make sure the robot ID matches the _id in mongodb.
I need to allow null ids as it is mongodb that adds the ID to the document.
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 settingsbuilder.Services.Configure<RobotsDatabaseSettings>(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>();Little quirk to keep NotEmptyRobotsService working.
@@ -6,4 +6,13 @@<ImplicitUsings>enable</ImplicitUsings></PropertyGroup><ItemGroup>This adds mongodb as a dependency to the project. This file was changed automagically via
@@ -10,3 +10,3 @@string country);public Robot? GetRobotById(int id);public Robot? GetRobotById(string id);Migrated ids from int to string.
@@ -13,0 +18,4 @@private void fillDefaultRobotsWhenUnderlyingRobotServiceIsEmpty(){if (underlyingRobotsService.Robots.Count() != 0)We changed a little bit how we build
NotEmptyRobotsService. Now we ask ASP to auto wire theRobotsServicefor us. We need to do this becauseRobotsServicedepends on mongodb which can only be build via ASP auto wiring mechanism.@@ -15,3 +27,4 @@this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan");}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(); }So instead of a
List<robot>we now depend on aIMongoCollection<Robot>. The api ofIMongoCollection<Robot>is quite nice.@@ -12,1 +12,4 @@public List<Robot> Robots { get => robotsCollection.AsQueryable().ToList(); }public RobotsService(IOptions<RobotsDatabaseSettings> bookStoreDatabaseSettings){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 isToList.@@ -19,0 +19,4 @@var mongoDatabase = mongoClient.GetDatabase(bookStoreDatabaseSettings.Value.DatabaseName);this.robotsCollection = mongoDatabase.GetCollection<Robot>(bookStoreDatabaseSettings.Value.RobotsCollectionName);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 == idselect robot;return q.First();}To insert, we now need to call InsertOne, as in mongodb
this is cool. This is how to query mongodb via linq. This rocks.
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": {This is the configuration needed to run the program using a mongodb instance running locally on mongodb's default port.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.