diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs
index 0016511b6..be78594f2 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs
@@ -18,5 +18,6 @@ public enum StronglyTypedIdBackingType
Long = 4,
NullableString = 5,
MassTransitNewId = 6,
+ ObjectId = 7
}
}
\ No newline at end of file
diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
index 599a51409..80f40227b 100644
--- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
+++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs
@@ -46,5 +46,10 @@ public enum StronglyTypedIdConverter
/// Creates a Dapper TypeHandler for converting to and from the type
///
DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Mongo Serializer for converting string to and from type
+ ///
+ MongoSerializer = 64,
}
}
\ No newline at end of file
diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs
index e1f229779..e9df74496 100644
--- a/src/StronglyTypedIds/EmbeddedSources.cs
+++ b/src/StronglyTypedIds/EmbeddedSources.cs
@@ -25,6 +25,7 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_MongoSerializer.cs"),
false
);
@@ -37,6 +38,7 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_MongoSerializer.cs"),
false
);
@@ -49,6 +51,7 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_MongoSerializer.cs"),
false
);
@@ -61,6 +64,7 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_MongoSerializer.cs"),
false
);
@@ -73,6 +77,7 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_MongoSerializer.cs"),
true
);
@@ -85,12 +90,27 @@ internal static class EmbeddedSources
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_MongoSerializer.cs"),
+ false
+ );
+
+ internal static readonly ResourceCollection ObjectIdResources = new(
+ AutoGeneratedHeader,
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_Base.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_NewtonsoftJsonConverter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_SystemTextJsonConverter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_TypeConverter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_EfCoreValueConverter.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_DapperTypeHandler.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_IComparable.cs"),
+ LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_MongoSerializer.cs"),
false
);
internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]";
internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]";
internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]";
+ internal const string MongoSerializerAttributeSource = " [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(TESTIDMongoSerializer))]";
internal static string LoadEmbeddedResource(string resourceName)
{
@@ -116,6 +136,7 @@ public readonly struct ResourceCollection
public string TypeConverter { get; }
public string EfCoreValueConverter { get; }
public string DapperTypeHandler { get; }
+ public string Mongo { get; }
public string Comparable { get; }
public ResourceCollection(
@@ -127,6 +148,7 @@ public ResourceCollection(
string efCoreValueConverter,
string dapperTypeHandler,
string comparable,
+ string mongo,
bool nullableEnable)
{
BaseId = baseId;
@@ -137,6 +159,7 @@ public ResourceCollection(
DapperTypeHandler = dapperTypeHandler;
Comparable = comparable;
NullableEnable = nullableEnable;
+ Mongo = mongo;
Header = header;
}
}
diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs
index d08ffc1e9..071b41e45 100644
--- a/src/StronglyTypedIds/SourceGenerationHelper.cs
+++ b/src/StronglyTypedIds/SourceGenerationHelper.cs
@@ -32,6 +32,7 @@ public static string CreateId(
StronglyTypedIdBackingType.String => EmbeddedSources.StringResources,
StronglyTypedIdBackingType.NullableString => EmbeddedSources.NullableStringResources,
StronglyTypedIdBackingType.MassTransitNewId => EmbeddedSources.NewIdResources,
+ StronglyTypedIdBackingType.ObjectId => EmbeddedSources.ObjectIdResources,
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
};
@@ -69,6 +70,7 @@ static string CreateId(
var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson);
var useEfCoreValueConverter = converters.IsSet(StronglyTypedIdConverter.EfCoreValueConverter);
var useDapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.DapperTypeHandler);
+ var useMongoSerializer = converters.IsSet(StronglyTypedIdConverter.MongoSerializer);
var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable);
var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable);
@@ -121,6 +123,11 @@ static string CreateId(
{
sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource);
}
+
+ if (useMongoSerializer)
+ {
+ sb.AppendLine(EmbeddedSources.MongoSerializerAttributeSource);
+ }
sb.Append(resources.BaseId);
ReplaceInterfaces(sb, useIEquatable, useIComparable);
@@ -156,6 +163,11 @@ static string CreateId(
{
sb.AppendLine(resources.SystemTextJson);
}
+
+ if (useMongoSerializer)
+ {
+ sb.AppendLine(resources.Mongo);
+ }
sb.Replace("TESTID", idName);
sb.AppendLine(@" }");
diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs
new file mode 100644
index 000000000..812a34382
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs
new file mode 100644
index 000000000..923ef3c16
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs
new file mode 100644
index 000000000..bf5fc8c94
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs
new file mode 100644
index 000000000..716fa44a5
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs
new file mode 100644
index 000000000..0b7a8d745
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs
@@ -0,0 +1,19 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs
new file mode 100644
index 000000000..93f32aad7
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs
@@ -0,0 +1,24 @@
+ readonly partial struct TESTID : INTERFACES
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public TESTID(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static TESTID New() => new TESTID(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly TESTID Empty = new TESTID(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(TESTID other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is TESTID other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(TESTID a, TESTID b) => a.Equals(b);
+ public static bool operator !=(TESTID a, TESTID b) => !(a == b);
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs
new file mode 100644
index 000000000..6b0fb4b44
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs
@@ -0,0 +1,17 @@
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override TESTID Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
+ };
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs
new file mode 100644
index 000000000..c94a00c6c
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs
@@ -0,0 +1,11 @@
+
+ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter
+ {
+ public EfCoreValueConverter() : this(null) { }
+ public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
+ : base(
+ id => id.Value.ToString(),
+ value => new TESTID(new MongoDB.Bson.ObjectId(value)),
+ mappingHints
+ ) { }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs
new file mode 100644
index 000000000..e0696ee2a
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs
@@ -0,0 +1 @@
+ public int CompareTo(TESTID other) => Value.CompareTo(other.Value);
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs
new file mode 100644
index 000000000..74bbecaae
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs
new file mode 100644
index 000000000..2f19f4495
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs
@@ -0,0 +1,20 @@
+
+ class TESTIDNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(TESTID);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (TESTID)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new TESTID(new MongoDB.Bson.ObjectId(result));
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs
new file mode 100644
index 000000000..c09fe18b8
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs
@@ -0,0 +1,20 @@
+
+ class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter
+ {
+ public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
+ {
+ return new TESTID(new MongoDB.Bson.ObjectId(reader.GetString()));
+ }
+
+ public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options)
+ {
+ if (value.Value == MongoDB.Bson.ObjectId.Empty)
+ {
+ writer.WriteNullValue();
+ }
+ else
+ {
+ writer.WriteStringValue(value.Value.ToString());
+ }
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs
new file mode 100644
index 000000000..0ff4a7c57
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs
@@ -0,0 +1,42 @@
+
+ class TESTIDTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MongoDB.Bson.ObjectId objectIdValue => new TESTID(objectIdValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is TESTID idValue)
+ {
+ if (destinationType == typeof(MongoDB.Bson.ObjectId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
\ No newline at end of file
diff --git a/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs b/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs
new file mode 100644
index 000000000..af3e50267
--- /dev/null
+++ b/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs
@@ -0,0 +1,12 @@
+ class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new TESTID(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs
index 0ce1ce679..b55f4227a 100644
--- a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs
+++ b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs
@@ -15,6 +15,7 @@ public static void AddHandlers()
SqlMapper.AddTypeHandler(new DapperLongId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new DapperNullableStringId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new DapperNewIdId.DapperTypeHandler());
+ SqlMapper.AddTypeHandler(new DapperObjectIdId.DapperTypeHandler());
}
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs
new file mode 100644
index 000000000..f87d4d0fd
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs
@@ -0,0 +1,38 @@
+using System;
+using System.Runtime.InteropServices;
+using Mongo2Go;
+using MongoDB.Driver;
+using Xunit;
+
+namespace StronglyTypedIds.IntegrationTests.Fixtures;
+
+public class MongoDbFixture : IDisposable
+{
+ private readonly MongoDbRunner _runner;
+
+ public MongoDbFixture()
+ {
+ _runner = MongoDbRunner.Start(binariesSearchPatternOverride: GetBinariesSearchPattern());
+ var client = new MongoClient(_runner.ConnectionString);
+ Database = client.GetDatabase("IntegrationTest");
+ }
+
+ public IMongoDatabase Database { get; }
+
+ public void Dispose()
+ {
+ _runner?.Dispose();
+ }
+
+ // Overrode default binaries search pattern because of problem on some Linux OS versions
+ private static string GetBinariesSearchPattern()
+ {
+ return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "tools/mongodb-linux*/bin" : default;
+ }
+}
+
+[CollectionDefinition(Name)]
+public class MongoDbCollection : ICollectionFixture
+{
+ public const string Name = "MongoDB collection";
+}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
index 2905f70e6..537849ca8 100644
--- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs
@@ -5,6 +5,8 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -12,8 +14,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class GuidIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public GuidIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public void SameValuesAreEqual()
{
@@ -220,6 +230,19 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(value, new DapperGuidId(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")));
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("GuidIdTestCollection");
+
+ var guidValue = Guid.NewGuid();
+ var original = new TestDocument { Id = new MongoGuidId(guidValue) };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoGuidId(guidValue)).FirstAsync();
+
+ Assert.Equal(new MongoGuidId(guidValue), retrieved.Id);
+ }
[Theory]
[InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")]
@@ -355,5 +378,10 @@ public class EntityWithNullableId
{
public NewtonsoftJsonGuidId? Id { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoGuidId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
index e2c48be64..57a30e48b 100644
--- a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs
@@ -5,6 +5,8 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -12,8 +14,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class IntIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public IntIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public void SameValuesAreEqual()
{
@@ -211,6 +221,18 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(new DapperIntId(123), value);
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("IntIdTestCollection");
+
+ var original = new TestDocument { Id = new MongoIntId(123) };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoIntId(123)).FirstAsync();
+
+ Assert.Equal(new MongoIntId(123), retrieved.Id);
+ }
[Theory]
[InlineData(123)]
@@ -348,5 +370,10 @@ public class EntityWithNullableId
{
public NewtonsoftJsonIntId? Id { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoIntId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
index b2f70dbcb..6496ff33e 100644
--- a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs
@@ -5,6 +5,8 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -12,8 +14,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class LongIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public LongIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public void SameValuesAreEqual()
{
@@ -212,6 +222,18 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(value, new DapperLongId(123));
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("LongIdTestCollection");
+
+ var original = new TestDocument { Id = new MongoLongId(123) };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoLongId(123)).FirstAsync();
+
+ Assert.Equal(new MongoLongId(123), retrieved.Id);
+ }
[Theory]
[InlineData(123L)]
@@ -349,5 +371,10 @@ public class EntityWithNullableId
{
public NewtonsoftJsonLongId? Id { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoLongId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
index e6dce6d74..300c4fa56 100644
--- a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs
@@ -6,6 +6,8 @@
using MassTransit;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -13,8 +15,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class MassTransitNewIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public MassTransitNewIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public void SameValuesAreEqual()
{
@@ -221,6 +231,19 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(new DapperNewIdId(NewId.FromGuid(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))), value);
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("NewIdTestCollection");
+
+ var guidValue = Guid.NewGuid();
+ var original = new TestDocument { Id = new MongoNewIdId(NewId.FromGuid(guidValue)) };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoNewIdId(NewId.FromGuid(guidValue))).FirstAsync();
+
+ Assert.Equal(new MongoNewIdId(NewId.FromGuid(guidValue)), retrieved.Id);
+ }
[Theory]
[InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")]
@@ -357,5 +380,10 @@ public class EntityWithNullableId
{
public NewtonsoftJsonNewIdId? Id { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoNewIdId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
index 877d3e4d1..0a23f3ece 100644
--- a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs
@@ -5,6 +5,8 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -12,8 +14,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class NullableStringIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public NullableStringIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public void SameValuesAreEqual()
{
@@ -201,6 +211,18 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(value, new DapperNullableStringId("this is a value"));
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("MongoNullableStringIdTestCollection");
+
+ var original = new TestDocument { Id = new MongoNullableStringId("some value") };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoNullableStringId("some value")).FirstAsync();
+
+ Assert.Equal(new MongoNullableStringId("some value"), retrieved.Id);
+ }
[Theory]
[InlineData("")]
@@ -407,5 +429,10 @@ public class TestEntity
public Guid Id { get; set; }
public EfCoreNullableStringId Name { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoNullableStringId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs
new file mode 100644
index 000000000..b92981bac
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs
@@ -0,0 +1,371 @@
+using System;
+using System.ComponentModel;
+using System.Linq;
+using System.Threading.Tasks;
+using Dapper;
+using Microsoft.Data.Sqlite;
+using Microsoft.EntityFrameworkCore;
+using MongoDB.Bson;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
+using StronglyTypedIds.IntegrationTests.Types;
+using Xunit;
+using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
+using SystemTextJsonSerializer = System.Text.Json.JsonSerializer;
+
+namespace StronglyTypedIds.IntegrationTests;
+
+[Collection(MongoDbCollection.Name)]
+public class ObjectIdTests
+{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public ObjectIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
+ [Fact]
+ public void SameValuesAreEqual()
+ {
+ var id = ObjectId.GenerateNewId();
+ var foo1 = new ObjectIdId1(id);
+ var foo2 = new ObjectIdId1(id);
+
+ Assert.Equal(foo1, foo2);
+ }
+
+ [Fact]
+ public void EmptyValueIsEmpty()
+ {
+ Assert.Equal(ObjectIdId1.Empty.Value, ObjectId.Empty);
+ }
+
+
+ [Fact]
+ public void DifferentValuesAreUnequal()
+ {
+ var foo1 = ObjectIdId1.New();
+ var foo2 = ObjectIdId1.New();
+
+ Assert.NotEqual(foo1, foo2);
+ }
+
+ [Fact]
+ public void OverloadsWorkCorrectly()
+ {
+ var id = ObjectId.GenerateNewId();
+ var same1 = new ObjectIdId1(id);
+ var same2 = new ObjectIdId1(id);
+ var different = ObjectIdId1.New();
+
+ Assert.True(same1 == same2);
+ Assert.False(same1 == different);
+ Assert.False(same1 != same2);
+ Assert.True(same1 != different);
+ }
+
+ [Fact]
+ public void DifferentTypesAreUnequal()
+ {
+ var bar = ObjectIdId2.New();
+ var foo = ObjectIdId1.New();
+
+ //Assert.NotEqual(bar, foo); // does not compile
+ Assert.NotEqual((object)bar, (object)foo);
+ }
+
+ [Fact]
+ public void CantCreateEmptyGeneratedId1()
+ {
+ var foo = new ObjectIdId1();
+ var bar = new ObjectIdId2();
+
+ //Assert.NotEqual(bar, foo); // does not compile
+ Assert.NotEqual((object)bar, (object)foo);
+ }
+
+ [Fact]
+ public void CanSerializeToObjectId_WithTypeConverter()
+ {
+ var foo = NewtonsoftJsonObjectIdId.New();
+
+ var serializedFoo = NewtonsoftJsonSerializer.SerializeObject(foo);
+ var serializedObjectId = NewtonsoftJsonSerializer.SerializeObject(foo.Value.ToString());
+
+ Assert.Equal(serializedFoo, serializedObjectId);
+ }
+
+ [Fact]
+ public void CanSerializeToObjectId_WithSystemTextJsonProvider()
+ {
+ var foo = SystemTextJsonObjectIdId.New();
+
+ var serializedFoo = SystemTextJsonSerializer.Serialize(foo);
+ var serializedObjectId = SystemTextJsonSerializer.Serialize(foo.Value.ToString());
+
+ Assert.Equal(serializedFoo, serializedObjectId);
+ }
+
+ [Fact]
+ public void CanDeserializeFromObjectId_WithNewtonsoftJsonProvider()
+ {
+ var value = ObjectId.GenerateNewId();
+ var foo = new NewtonsoftJsonObjectIdId(value);
+ var serializedObjectId = NewtonsoftJsonSerializer.SerializeObject(value);
+
+ var deserializedFoo = NewtonsoftJsonSerializer.DeserializeObject(serializedObjectId);
+
+ Assert.Equal(foo, deserializedFoo);
+ }
+
+ [Fact]
+ public void CanDeserializeFromObjectId_WithSystemTextJsonProvider()
+ {
+ var value = ObjectId.GenerateNewId();
+ var foo = new SystemTextJsonObjectIdId(value);
+ var serializedObjectId = SystemTextJsonSerializer.Serialize(value.ToString());
+
+ var deserializedFoo = SystemTextJsonSerializer.Deserialize(serializedObjectId);
+
+ Assert.Equal(foo, deserializedFoo);
+ }
+
+ [Fact]
+ public void CanSerializeToObjectId_WithBothJsonConverters()
+ {
+ var foo = BothJsonObjectIdId.New();
+
+ var serializedFoo1 = NewtonsoftJsonSerializer.SerializeObject(foo);
+ var serializedObjectId1 = NewtonsoftJsonSerializer.SerializeObject(foo.Value.ToString());
+
+ var serializedFoo2 = SystemTextJsonSerializer.Serialize(foo);
+ var serializedObjectId2 = SystemTextJsonSerializer.Serialize(foo.Value.ToString());
+
+ Assert.Equal(serializedFoo1, serializedObjectId1);
+ Assert.Equal(serializedFoo2, serializedObjectId2);
+ }
+
+ [Fact]
+ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty()
+ {
+ var foo = NoJsonObjectIdId.New();
+
+ var serialized = SystemTextJsonSerializer.Serialize(foo);
+
+ var expected = "{\"Value\":" + SystemTextJsonSerializer.Serialize(foo.Value) + "}";
+
+ Assert.Equal(expected, serialized);
+ }
+
+ [Fact]
+ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty()
+ {
+ var foo = NoJsonObjectIdId.New();
+
+ var serialized = NewtonsoftJsonSerializer.SerializeObject(foo);
+
+ var expected = $"\"{foo.Value.ToString()}\"";
+
+ Assert.Equal(expected, serialized);
+ }
+
+ [Fact]
+ public void WhenNoTypeConverter_SerializesWithValueProperty()
+ {
+ var foo = NoConverterObjectIdId.New();
+
+ var newtonsoft = SystemTextJsonSerializer.Serialize(foo);
+ var systemText = SystemTextJsonSerializer.Serialize(foo);
+
+ var expected = "{\"Value\":" + SystemTextJsonSerializer.Serialize(foo.Value) + "}";
+
+ Assert.Equal(expected, newtonsoft);
+ Assert.Equal(expected, systemText);
+ }
+
+ [Fact]
+ public void WhenEfCoreValueConverterUsesValueConverter()
+ {
+ var connection = new SqliteConnection("DataSource=:memory:");
+ connection.Open();
+
+ var options = new DbContextOptionsBuilder()
+ .UseSqlite(connection)
+ .Options;
+
+ using (var context = new TestDbContext(options))
+ {
+ context.Database.EnsureCreated();
+ context.Entities.Add(
+ new TestEntity { Id = EfCoreObjectIdId.New() });
+ context.SaveChanges();
+ }
+ using (var context = new TestDbContext(options))
+ {
+ var all = context.Entities.ToList();
+ Assert.Single(all);
+ }
+ }
+
+ [Fact]
+ public async Task WhenDapperValueConverterUsesValueConverter()
+ {
+ using var connection = new SqliteConnection("DataSource=:memory:");
+ await connection.OpenAsync();
+
+ var results = await connection.QueryAsync("SELECT '62758c6ee39f6ef4751bb831'");
+
+ var value = Assert.Single(results);
+ Assert.Equal(new DapperObjectIdId(new ObjectId("62758c6ee39f6ef4751bb831")), value);
+ }
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("ObjectIdIdTestCollection");
+
+ var objectIdValue = ObjectId.GenerateNewId();
+ var original = new TestDocument { Id = new MongoObjectIdId(objectIdValue) };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoObjectIdId(objectIdValue)).FirstAsync();
+
+ Assert.Equal(new MongoObjectIdId(objectIdValue), retrieved.Id);
+ }
+
+ [Theory]
+ [InlineData("62758cacb6f910fe91bd697b")]
+ public void TypeConverter_CanConvertToAndFrom(string value)
+ {
+ var converter = TypeDescriptor.GetConverter(typeof(NoJsonObjectIdId));
+ var id = converter.ConvertFrom(value);
+ Assert.IsType(id);
+ Assert.Equal(new NoJsonObjectIdId(new ObjectId(value)), id);
+
+ var reconverted = converter.ConvertTo(id, value.GetType());
+ Assert.Equal(value, reconverted);
+ }
+
+ [Fact]
+ public void CanCompareDefaults()
+ {
+ ComparableObjectIdId original = default;
+ var other = ComparableObjectIdId.Empty;
+
+ var compare1 = original.CompareTo(other);
+ var compare2 = other.CompareTo(original);
+ Assert.Equal(compare1, -compare2);
+ }
+
+ [Fact]
+ public void CanEquateDefaults()
+ {
+ EquatableObjectIdId original = default;
+ var other = EquatableObjectIdId.Empty;
+
+ var equals1 = (original as IEquatable).Equals(other);
+ var equals2 = (other as IEquatable).Equals(original);
+
+ Assert.Equal(equals1, equals2);
+ }
+
+ [Fact]
+ public void ImplementsInterfaces()
+ {
+ Assert.IsAssignableFrom>(BothObjectIdId.Empty);
+ Assert.IsAssignableFrom>(BothObjectIdId.Empty);
+
+ Assert.IsAssignableFrom>(EquatableObjectIdId.Empty);
+ Assert.IsAssignableFrom>(ComparableObjectIdId.Empty);
+
+#pragma warning disable 184
+ Assert.False(ObjectIdId1.Empty is IComparable);
+ Assert.False(ObjectIdId1.Empty is IEquatable);
+#pragma warning restore 184
+ }
+
+
+#if NET6_0_OR_GREATER
+ [Fact]
+ public void WhenConventionBasedEfCoreValueConverterUsesValueConverter()
+ {
+ var connection = new SqliteConnection("DataSource=:memory:");
+ connection.Open();
+
+ var options = new DbContextOptionsBuilder()
+ .UseSqlite(connection)
+ .Options;
+
+ using (var context = new ConventionsDbContext(options))
+ {
+ context.Database.EnsureCreated();
+ context.Entities.Add(
+ new TestEntity { Id = EfCoreObjectIdId.New() });
+ context.SaveChanges();
+ }
+ using (var context = new ConventionsDbContext(options))
+ {
+ var all = context.Entities.ToList();
+ Assert.Single(all);
+ }
+ }
+
+ public class ConventionsDbContext : DbContext
+ {
+ public DbSet Entities { get; set; }
+
+ public ConventionsDbContext(DbContextOptions options) : base(options)
+ {
+ }
+
+ protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
+ {
+ configurationBuilder
+ .Properties()
+ .HaveConversion();
+ }
+
+ protected override void OnModelCreating(ModelBuilder modelBuilder)
+ {
+ modelBuilder
+ .Entity(builder =>
+ {
+ builder
+ .Property(x => x.Id)
+ .ValueGeneratedNever();
+ });
+ }
+ }
+#endif
+
+ public class TestDbContext : DbContext
+ {
+ public DbSet Entities { get; set; }
+
+ public TestDbContext(DbContextOptions options) : base(options)
+ {
+ }
+
+ protected override void OnModelCreating(ModelBuilder modelBuilder)
+ {
+ modelBuilder
+ .Entity(builder =>
+ {
+ builder
+ .Property(x => x.Id)
+ .HasConversion(new EfCoreObjectIdId.EfCoreValueConverter())
+ .ValueGeneratedNever();
+ });
+ }
+ }
+
+ public class TestEntity
+ {
+ public EfCoreObjectIdId Id { get; set; }
+ }
+
+ public class TestDocument
+ {
+ public MongoObjectIdId Id { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
index 70508222c..ee92d2298 100644
--- a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
+++ b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs
@@ -5,6 +5,8 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
+using MongoDB.Driver;
+using StronglyTypedIds.IntegrationTests.Fixtures;
using StronglyTypedIds.IntegrationTests.Types;
using Xunit;
using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert;
@@ -12,8 +14,16 @@
namespace StronglyTypedIds.IntegrationTests
{
+ [Collection(MongoDbCollection.Name)]
public class StringIdTests
{
+ private readonly MongoDbFixture _mongoDbFixture;
+
+ public StringIdTests(MongoDbFixture mongoDbFixture)
+ {
+ _mongoDbFixture = mongoDbFixture;
+ }
+
[Fact]
public async Task ThrowsIfTryToCreateWithNull()
{
@@ -207,6 +217,18 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(value, new DapperStringId("this is a value"));
}
+
+ [Fact]
+ public async Task WhenMongoSerializerUsesSerializer()
+ {
+ var collection = _mongoDbFixture.Database.GetCollection("StringIdTestCollection");
+
+ var original = new TestDocument { Id = new MongoStringId("some value") };
+ await collection.InsertOneAsync(original);
+ var retrieved = await collection.Find(x => x.Id == new MongoStringId("some value")).FirstAsync();
+
+ Assert.Equal(new MongoStringId("some value"), retrieved.Id);
+ }
[Theory]
[InlineData("")]
@@ -343,5 +365,10 @@ public class TestEntity
public Guid Id { get; set; }
public EfCoreStringId Name { get; set; }
}
+
+ public class TestDocument
+ {
+ public MongoStringId Id { get; set; }
+ }
}
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
index 1d6f12598..1f6f3f0c1 100644
--- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj
@@ -19,6 +19,7 @@
+
@@ -26,16 +27,21 @@
+
+
+
+
+
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
index 9ad651d72..9450e9f01 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs
@@ -37,4 +37,7 @@ public partial struct EquatableGuidId { }
[StronglyTypedId(implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableGuidId { }
+
+ [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer)]
+ public partial struct MongoGuidId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
index 15316b98a..a202e4084 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs
@@ -34,4 +34,7 @@ public partial struct EquatableIntId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Int, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableIntId { }
+
+ [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.Int)]
+ public partial struct MongoIntId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
index 5a1081bb8..0a6eb0a6d 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs
@@ -34,4 +34,7 @@ public partial struct EquatableLongId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableLongId { }
+
+ [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.Long)]
+ public partial struct MongoLongId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
index 0be4ce4c9..5b949c228 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs
@@ -37,4 +37,7 @@ public partial struct EquatableNewIdId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableNewIdId { }
+
+ [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.MongoSerializer)]
+ public partial struct MongoNewIdId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
index d51e901a1..dc7c8badf 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs
@@ -34,4 +34,7 @@ public partial struct EquatableNullableStringId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.NullableString, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableNullableStringId { }
+
+ [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.NullableString)]
+ public partial struct MongoNullableStringId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs
new file mode 100644
index 000000000..5bf40ca47
--- /dev/null
+++ b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs
@@ -0,0 +1,40 @@
+namespace StronglyTypedIds.IntegrationTests.Types;
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId)]
+partial struct ObjectIdId1 { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId)]
+partial struct ObjectIdId2 { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.None)]
+public partial struct NoConverterObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.TypeConverter)]
+public partial struct NoJsonObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.NewtonsoftJson)]
+public partial struct NewtonsoftJsonObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.SystemTextJson)]
+public partial struct SystemTextJsonObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.NewtonsoftJson | StronglyTypedIdConverter.SystemTextJson)]
+public partial struct BothJsonObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.EfCoreValueConverter)]
+public partial struct EfCoreObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.DapperTypeHandler)]
+public partial struct DapperObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)]
+public partial struct BothObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IEquatable)]
+public partial struct EquatableObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IComparable)]
+public partial struct ComparableObjectIdId { }
+
+[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.MongoSerializer)]
+public partial struct MongoObjectIdId { }
\ No newline at end of file
diff --git a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
index 5e7cdfa57..fcb9fcdff 100644
--- a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
+++ b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs
@@ -34,4 +34,7 @@ public partial struct EquatableStringId { }
[StronglyTypedId(backingType: StronglyTypedIdBackingType.String, implementations: StronglyTypedIdImplementations.IComparable)]
public partial struct ComparableStringId { }
+
+ [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, converters: StronglyTypedIdConverter.MongoSerializer)]
+ public partial struct MongoStringId { }
}
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
index bb760958b..b99578794 100644
--- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj
@@ -10,6 +10,7 @@
+
@@ -23,22 +24,27 @@
+
+
+
+
+
diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
index d79579881..630f9ccb8 100644
--- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
+++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj
@@ -9,6 +9,7 @@
+
@@ -22,22 +23,27 @@
+
+
+
+
+
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt
index 4ea1be319..74d859696 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt
@@ -31,6 +31,7 @@ namespace StronglyTypedIds
Long = 4,
NullableString = 5,
MassTransitNewId = 6,
+ ObjectId = 7
}
}
#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
index 32b324616..477021e78 100644
--- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
+++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt
@@ -59,6 +59,11 @@ namespace StronglyTypedIds
/// Creates a Dapper TypeHandler for converting to and from the type
///
DapperTypeHandler = 32,
+
+ ///
+ /// Creates a Mongo Serializer for converting string to and from type
+ ///
+ MongoSerializer = 64,
}
}
#endif
\ No newline at end of file
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt
new file mode 100644
index 000000000..5716eb6fc
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt
@@ -0,0 +1,39 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ readonly partial struct MyTestId
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt
new file mode 100644
index 000000000..497986bb0
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt
@@ -0,0 +1,39 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt
new file mode 100644
index 000000000..a2c88d270
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt
@@ -0,0 +1,40 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt
new file mode 100644
index 000000000..9d0bc5c9b
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt
@@ -0,0 +1,40 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..bf245b783
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..200cfb6bf
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..1d2119e12
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..be66c0ab1
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..1b3a443af
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..440a73743
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..ed5311bc2
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..6a7c5c57a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..ba1c05dc3
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..19e7fd59f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..dd4873707
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt
new file mode 100644
index 000000000..7da8d8333
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt
@@ -0,0 +1,93 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt
new file mode 100644
index 000000000..9e72afdb0
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt
new file mode 100644
index 000000000..ac772db15
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt
new file mode 100644
index 000000000..49b36c5c8
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt
new file mode 100644
index 000000000..9f587d8d8
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt
@@ -0,0 +1,92 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt
new file mode 100644
index 000000000..bdf1ff7c5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt
@@ -0,0 +1,113 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt
new file mode 100644
index 000000000..c5d7c0307
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt
@@ -0,0 +1,113 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt
new file mode 100644
index 000000000..0445ae543
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt
@@ -0,0 +1,123 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt
new file mode 100644
index 000000000..13f601e2a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt
@@ -0,0 +1,123 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt
new file mode 100644
index 000000000..9de33416b
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt
@@ -0,0 +1,90 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt
new file mode 100644
index 000000000..e1f7eb970
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt
@@ -0,0 +1,90 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt
new file mode 100644
index 000000000..2a0733d5f
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt
new file mode 100644
index 000000000..2375b15b9
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt
@@ -0,0 +1,91 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt
new file mode 100644
index 000000000..ce53fb9fb
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt
@@ -0,0 +1,97 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(value));
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt
new file mode 100644
index 000000000..8507545d4
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt
@@ -0,0 +1,97 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(value));
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt
new file mode 100644
index 000000000..5cbc7fdfe
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt
@@ -0,0 +1,107 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(value));
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt
new file mode 100644
index 000000000..641cb0e9c
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt
@@ -0,0 +1,107 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string Value { get; }
+
+ public MyTestId(string value)
+ {
+ Value = value ?? throw new System.ArgumentNullException(nameof(value));
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt
new file mode 100644
index 000000000..76ad56067
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt
new file mode 100644
index 000000000..ec8a6cec1
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt
new file mode 100644
index 000000000..c88a7186e
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt
new file mode 100644
index 000000000..4be3c5656
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public System.Guid Value { get; }
+
+ public MyTestId(System.Guid value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(System.Guid.NewGuid());
+ public static readonly MyTestId Empty = new MyTestId(System.Guid.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(guidValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(guid.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new System.Guid(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt
new file mode 100644
index 000000000..ead99ce84
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt
new file mode 100644
index 000000000..27a1a1319
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt
new file mode 100644
index 000000000..f3dcdcc67
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt
new file mode 100644
index 000000000..40bf927e5
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public int Value { get; }
+
+ public MyTestId(int value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ long longValue when longValue < int.MaxValue => new MyTestId((int)longValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ int intValue => new MyTestId(intValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && int.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(int) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(int))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt32());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt32(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt
new file mode 100644
index 000000000..20afff557
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt
@@ -0,0 +1,136 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt
new file mode 100644
index 000000000..62bed1ede
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt
@@ -0,0 +1,136 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt
new file mode 100644
index 000000000..07ee8229d
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt
new file mode 100644
index 000000000..4e0735288
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt
@@ -0,0 +1,137 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public long Value { get; }
+
+ public MyTestId(long value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(0);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(int) || sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ long longValue => new MyTestId(longValue),
+ int intValue => new MyTestId(intValue),
+ short shortValue => new MyTestId(shortValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && long.TryParse(stringValue, out var result) => new MyTestId(result),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(long) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(long))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return result.HasValue ? new MyTestId(result.Value) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadInt64());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteInt64(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt
new file mode 100644
index 000000000..acb2feac3
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt
@@ -0,0 +1,141 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt
new file mode 100644
index 000000000..2dbfb3c91
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt
@@ -0,0 +1,141 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt
new file mode 100644
index 000000000..c6e213324
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt
@@ -0,0 +1,142 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt
new file mode 100644
index 000000000..4c8c78790
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt
@@ -0,0 +1,142 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public MassTransit.NewId Value { get; }
+
+ public MyTestId(MassTransit.NewId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MassTransit.NewId.Next());
+ public static readonly MyTestId Empty = new MyTestId(MassTransit.NewId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value.ToGuid();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) ||
+ sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MassTransit.NewId newIdValue => new MyTestId(newIdValue),
+ System.Guid guidValue => new MyTestId(MassTransit.NewId.FromGuid(guidValue)),
+ string stringValue when !string.IsNullOrEmpty(stringValue) && System.Guid.TryParse(stringValue, out var result) => new MyTestId(MassTransit.NewId.FromGuid(result)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(System.Guid) || sourceType == typeof(MassTransit.NewId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MassTransit.NewId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(System.Guid))
+ {
+ return idValue.Value.ToGuid();
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToGuid().ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToGuid());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var guid = serializer.Deserialize(reader);
+ return guid.HasValue ? new MyTestId(MassTransit.NewId.FromGuid(guid.Value)) : null;
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(new MassTransit.NewId(context.Reader.ReadString()));
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteString(value.Value.ToString());
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt
new file mode 100644
index 000000000..c14db4911
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt
@@ -0,0 +1,156 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt
new file mode 100644
index 000000000..aba992e7c
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt
@@ -0,0 +1,156 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt
new file mode 100644
index 000000000..1ad44ca71
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt
@@ -0,0 +1,166 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt
new file mode 100644
index 000000000..bbd582874
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt
@@ -0,0 +1,166 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+#nullable enable
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public string? Value { get; }
+
+ public MyTestId(string? value)
+ {
+ Value = value;
+ }
+
+ public static readonly MyTestId Empty = new MyTestId(string.Empty);
+
+ public bool Equals(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => true,
+ (null, _) => false,
+ (_, null) => false,
+ (_, _) => Value.Equals(other.Value),
+ };
+ }
+ public override bool Equals(object? obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value?.GetHashCode() ?? 0;
+ public override string? ToString() => Value;
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other)
+ {
+ return (Value, other.Value) switch
+ {
+ (null, null) => 0,
+ (null, _) => -1,
+ (_, null) => 1,
+ (_, _) => Value.CompareTo(other.Value),
+ };
+ }
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value;
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ null => new MyTestId(null),
+ System.DBNull => new MyTestId(null),
+ string stringValue => new MyTestId(stringValue),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
+ }
+
+ public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
+ {
+ if (value is null)
+ {
+ return new MyTestId(null);
+ }
+
+ var stringValue = value as string;
+ if (stringValue is not null)
+ {
+ return new MyTestId(stringValue);
+ }
+
+ return base.ConvertFrom(context, culture, value);
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
+ {
+ return sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value;
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ if (value is null)
+ {
+ serializer.Serialize(writer, null);
+ }
+ else
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value);
+ }
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ return new MyTestId(serializer.Deserialize(reader));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadString());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ if (value.Value is null)
+ {
+ context.Writer.WriteNull();
+ }
+ else
+ {
+ context.Writer.WriteString(value.Value);
+ }
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt
new file mode 100644
index 000000000..a3c10e182
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MongoDB.Bson.ObjectId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt
new file mode 100644
index 000000000..0710ccf66
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt
@@ -0,0 +1,133 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MongoDB.Bson.ObjectId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt
new file mode 100644
index 000000000..5289626ac
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MongoDB.Bson.ObjectId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize(reader);
+ return new MyTestId(new MongoDB.Bson.ObjectId(result));
+ }
+ }
+ class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase
+ {
+ public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
+ {
+ return new MyTestId(context.Reader.ReadObjectId());
+ }
+
+ public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
+ {
+ context.Writer.WriteObjectId(value.Value);
+ }
+ }
+ }
+}
diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt
new file mode 100644
index 000000000..9d2b6226a
--- /dev/null
+++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt
@@ -0,0 +1,134 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by the StronglyTypedId source generator
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+#pragma warning disable 1591 // publicly visible type or member must be documented
+
+namespace Some.Namespace
+{
+ [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))]
+ [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))]
+ [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))]
+ readonly partial struct MyTestId : System.IComparable, System.IEquatable
+ {
+ public MongoDB.Bson.ObjectId Value { get; }
+
+ public MyTestId(MongoDB.Bson.ObjectId value)
+ {
+ Value = value;
+ }
+
+ public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId());
+ public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty);
+
+ public bool Equals(MyTestId other) => this.Value.Equals(other.Value);
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj)) return false;
+ return obj is MyTestId other && Equals(other);
+ }
+
+ public override int GetHashCode() => Value.GetHashCode();
+
+ public override string ToString() => Value.ToString();
+ public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b);
+ public static bool operator !=(MyTestId a, MyTestId b) => !(a == b);
+ public int CompareTo(MyTestId other) => Value.CompareTo(other.Value);
+
+ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler
+ {
+ public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value)
+ {
+ parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString();
+ }
+
+ public override MyTestId Parse(object value)
+ {
+ return value switch
+ {
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
+ };
+ }
+ }
+
+ class MyTestIdTypeConverter : System.ComponentModel.TypeConverter
+ {
+ public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom
+ (context, sourceType);
+ }
+
+ public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
+ {
+ return value switch
+ {
+ MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue),
+ string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
+ _ => base.ConvertFrom(context, culture, value),
+ };
+ }
+
+ public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
+ {
+ return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
+ }
+
+ public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
+ {
+ if (value is MyTestId idValue)
+ {
+ if (destinationType == typeof(MongoDB.Bson.ObjectId))
+ {
+ return idValue.Value;
+ }
+
+ if (destinationType == typeof(string))
+ {
+ return idValue.Value.ToString();
+ }
+ }
+
+ return base.ConvertTo(context, culture, value, destinationType);
+ }
+ }
+
+ class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
+ {
+ public override bool CanConvert(System.Type objectType)
+ {
+ return objectType == typeof(MyTestId);
+ }
+
+ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var id = (MyTestId)value;
+ serializer.Serialize(writer, id.Value.ToString());
+ }
+
+ public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
+ {
+ var result = serializer.Deserialize