Skip to content

JoinCollectionsAggregationPipeline: retarget net10.0, bump MongoDB.Driver 3.10.0, fix equality contract - #2124

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103750-csharp-join-mongodb-collections-with-aggregation-pipeline
Open

JoinCollectionsAggregationPipeline: retarget net10.0, bump MongoDB.Driver 3.10.0, fix equality contract#2124
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103750-csharp-join-mongodb-collections-with-aggregation-pipeline

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the dotnet-mongo-db/JoinCollectionsAggregationPipeline sample for the .NET 10 refresh of Join MongoDB Collections With .NET and Aggregation Pipeline, and fixes the code defects that article rewrite turned up.

Retarget and package updates

  • Both projects retargeted to net10.0.
  • MongoDB.Driver 2.22.0 to 3.10.0. The four-string Lookup() overload the sample's pipeline uses is unchanged at that version, confirmed by compiling.
  • Testcontainers.MongoDb 3.6.0 to 4.14.0. The old version pulled in SSH.NET 2023.0.0 (NU1903, high severity) and BouncyCastle.Cryptography 2.2.1 (NU1902) transitively; 4.x drops both. The parameterless MongoDbBuilder constructor is obsolete in 4.x, so the image is pinned explicitly to mongo:8.0 (the implicit default was the end-of-life mongo:6.0).
  • Test packages bumped to what the recently retargeted samples use: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 2.8.2, coverlet.collector 10.0.1.

Code fixes

  • StudentRepository.GetAllUsers() renamed to GetAllStudentsAsync(). Nothing in this sample is a user, and the method is async.
  • Student.Equals rewritten. It compared course lists with StudentCourses.All(course => course.Equals(student.StudentCourses.ElementAt(StudentCourses.IndexOf(course)))), which throws ArgumentOutOfRangeException when the other student has fewer courses, and is O(n^2) because of the IndexOf inside the All. SequenceEqual does the same job in one line and uses the Course.Equals override that is already there.
  • Equality contract repaired. Course.GetHashCode() combined Id while Course.Equals ignores it, and Student had the same asymmetry (Equals on FirstName/LastName/StudentCourses, GetHashCode also combining Id and Major). Equal objects could therefore hash differently. Id is now out of both hash codes, and Student.GetHashCode walks StudentCourses rather than hashing the List<Course> by reference, so two students that compare equal now hash equally.
  • The five [Required] DataAnnotations removed from Course and Student, with the now-unused using System.ComponentModel.DataAnnotations;. The MongoDB driver does not read DataAnnotations and this is a console app with no model binding, so the attributes implied a validation guarantee that never existed.

Verification

dotnet build on SDK 10.0.302: 0 warnings, 0 errors.

dotnet test cannot run on the machine this was prepared on because the live test needs a Docker daemon for the MongoDB Testcontainer and none is installed there; the folder is not in .github/ci-skip-folders.txt, so PR CI runs it on the Ubuntu runner where Docker is available.

…iver 3.10.0, fix equality contract

- Retarget both projects to net10.0.
- MongoDB.Driver 2.22.0 -> 3.10.0; the four-string Lookup() overload the
  sample uses is unchanged at that version.
- Testcontainers.MongoDb 3.6.0 -> 4.14.0. The old version dragged in
  SSH.NET 2023.0.0 (NU1903, high severity) and BouncyCastle 2.2.1 (NU1902);
  4.x drops both. The parameterless MongoDbBuilder ctor is obsolete in 4.x,
  so the image is now pinned explicitly to mongo:8.0.
- Test packages bumped to the versions the recently retargeted samples use
  (Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio
  2.8.2, coverlet.collector 10.0.1).
- StudentRepository.GetAllUsers() -> GetAllStudentsAsync(). Nothing in this
  sample is a user, and the method is async.
- Student.Equals compared course lists with All(...ElementAt(IndexOf(...))),
  which throws ArgumentOutOfRangeException when the other student has fewer
  courses and is O(n^2). Replaced with SequenceEqual, which uses the
  Course.Equals override already present.
- Equality contract: Course.GetHashCode combined Id while Course.Equals
  ignores it, and Student had the same asymmetry. Id is now out of both hash
  codes, and Student's hash walks StudentCourses so equal students hash
  equally instead of hashing the list by reference.
- Dropped the five [Required] DataAnnotations from Course and Student, plus
  the now-unused using. The MongoDB driver does not read DataAnnotations and
  this is a console app with no model binding, so they implied a validation
  guarantee that never existed.

Build and test verified on net10.0 (SDK 10.0.302), 0 warnings, 0 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant