andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.52k stars 80 forks source link

Hashes should match the underlying type #65

Closed MovGP0 closed 2 years ago

MovGP0 commented 2 years ago

Given we have an id like:

[StronglyTypedId(backingType: StronglyTypedIdBackingType.Guid)]
public partial struct CustomerId { }

Then the hashes of the id and the underlying type should match:

var guid = new Guid("d5d21f3b-2d6d-45ee-a1b7-bb9cc65e55c6")
var id = new CustomerId(guid);

Assert.Equal(guid.GetHash(), id.GetHash());

This is important when migrating existing code/tests to strongly typed ids.

Remarks: Currently the hash code changes when a different set of StronglyTypedIdConverter is defined.

andrewlock commented 2 years ago

This is already how it works I think? 🤔 the ID calls GetHashCode() on the backing value?

https://github.com/andrewlock/StronglyTypedId/blob/master/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns%3DSome.Namespace_converter%3D10_backingType%3DInt.verified.txt#L31