JoinCollectionsAggregationPipeline: retarget net10.0, bump MongoDB.Driver 3.10.0, fix equality contract - #2124
Open
vladimir-pecanac-main wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Updates the
dotnet-mongo-db/JoinCollectionsAggregationPipelinesample 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
net10.0.MongoDB.Driver2.22.0 to 3.10.0. The four-stringLookup()overload the sample's pipeline uses is unchanged at that version, confirmed by compiling.Testcontainers.MongoDb3.6.0 to 4.14.0. The old version pulled inSSH.NET2023.0.0 (NU1903, high severity) andBouncyCastle.Cryptography2.2.1 (NU1902) transitively; 4.x drops both. The parameterlessMongoDbBuilderconstructor is obsolete in 4.x, so the image is pinned explicitly tomongo:8.0(the implicit default was the end-of-lifemongo:6.0).Microsoft.NET.Test.Sdk18.9.0,xunit2.9.3,xunit.runner.visualstudio2.8.2,coverlet.collector10.0.1.Code fixes
StudentRepository.GetAllUsers()renamed toGetAllStudentsAsync(). Nothing in this sample is a user, and the method is async.Student.Equalsrewritten. It compared course lists withStudentCourses.All(course => course.Equals(student.StudentCourses.ElementAt(StudentCourses.IndexOf(course)))), which throwsArgumentOutOfRangeExceptionwhen the other student has fewer courses, and is O(n^2) because of theIndexOfinside theAll.SequenceEqualdoes the same job in one line and uses theCourse.Equalsoverride that is already there.Course.GetHashCode()combinedIdwhileCourse.Equalsignores it, andStudenthad the same asymmetry (EqualsonFirstName/LastName/StudentCourses,GetHashCodealso combiningIdandMajor). Equal objects could therefore hash differently.Idis now out of both hash codes, andStudent.GetHashCodewalksStudentCoursesrather than hashing theList<Course>by reference, so two students that compare equal now hash equally.[Required]DataAnnotations removed fromCourseandStudent, with the now-unusedusing 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 buildon SDK 10.0.302: 0 warnings, 0 errors.dotnet testcannot 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.