Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 18 additions & 10 deletions StronglyTypedId.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
# Visual Studio Version 17
VisualStudioVersion = 17.3.32728.150
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EE1258BD-3422-4F55-B9CF-B4D6C95DAD68}"
EndProject
Expand All @@ -15,19 +15,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.props = version.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds", "src\StronglyTypedIds\StronglyTypedIds.csproj", "{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Tests", "test\StronglyTypedIds.Tests\StronglyTypedIds.Tests.csproj", "{00B5ED3F-827D-41CD-9AF2-A9A20A6604E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.IntegrationTests", "test\StronglyTypedIds.IntegrationTests\StronglyTypedIds.IntegrationTests.csproj", "{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{E13FB452-2D47-4719-8BAA-7B695D79AF3A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Attributes", "src\StronglyTypedIds.Attributes\StronglyTypedIds.Attributes.csproj", "{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.IntegrationTests", "test\StronglyTypedIds.Nuget.IntegrationTests\StronglyTypedIds.Nuget.IntegrationTests.csproj", "{A7355210-7DDC-4968-84B7-79002113EA6E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StronglyTypedIds.Nuget.Attributes.IntegrationTests", "test\StronglyTypedIds.Nuget.Attributes.IntegrationTests\StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj", "{19A9B323-8C0B-4D1B-A20C-8CECFFD37F23}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,8 +39,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C0F3A36-ED47-4D0F-B736-EFC559C9E2DA}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -77,6 +75,16 @@ Global
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x64.Build.0 = Release|Any CPU
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.ActiveCfg = Release|Any CPU
{09F7364F-8CE9-4E9D-9BB7-B4CEBF682904}.Release|x86.Build.0 = Release|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.ActiveCfg = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x64.Build.0 = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.ActiveCfg = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Debug|x86.Build.0 = Debug|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.ActiveCfg = Release|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x64.Build.0 = Release|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.ActiveCfg = Release|Any CPU
{E13FB452-2D47-4719-8BAA-7B695D79AF3A}.Release|x86.Build.0 = Release|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F25F6E67-E62A-4075-86CF-4C4EDD7E4883}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public enum StronglyTypedIdBackingType
/// Use the default backing type (either the globally configured default, or Guid)
/// </summary>
Default = 0,

Guid = 1,
Int = 2,
String = 3,
Long = 4,
NullableString = 5,
MassTransitNewId = 6,
Short = 7,
Byte = 8,
}
}
24 changes: 24 additions & 0 deletions src/StronglyTypedIds/EmbeddedSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ internal static class EmbeddedSources
false
);

internal static readonly ResourceCollection ByteResources = new(
AutoGeneratedHeader,
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_Base.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_NewtonsoftJsonConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_SystemTextJsonConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_TypeConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Byte.Byte_IComparable.cs"),
false
);

internal static readonly ResourceCollection ShortResources = new(
AutoGeneratedHeader,
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_Base.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_NewtonsoftJsonConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_SystemTextJsonConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_TypeConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_EfCoreValueConverter.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_DapperTypeHandler.cs"),
LoadEmbeddedResource("StronglyTypedIds.Templates.Short.Short_IComparable.cs"),
false
);

internal static readonly ResourceCollection IntResources = new(
AutoGeneratedHeader,
LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_Base.cs"),
Expand Down
2 changes: 2 additions & 0 deletions src/StronglyTypedIds/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static string CreateId(
StronglyTypedIdBackingType.String => EmbeddedSources.StringResources,
StronglyTypedIdBackingType.NullableString => EmbeddedSources.NullableStringResources,
StronglyTypedIdBackingType.MassTransitNewId => EmbeddedSources.NewIdResources,
StronglyTypedIdBackingType.Short => EmbeddedSources.ShortResources,
StronglyTypedIdBackingType.Byte => EmbeddedSources.ByteResources,
_ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)),
};

Expand Down
23 changes: 23 additions & 0 deletions src/StronglyTypedIds/Templates/Byte/Byte_Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
 readonly partial struct TESTID : INTERFACES
{
public byte Value { get; }

public TESTID(byte value)
{
Value = value;
}

public static readonly TESTID Empty = new TESTID(0);

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);
21 changes: 21 additions & 0 deletions src/StronglyTypedIds/Templates/Byte/Byte_DapperTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler<TESTID>
{
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
}

public override TESTID Parse(object value)
{
return value switch
{
byte byteValue => new TESTID(byteValue),
short shortValue when shortValue < byte.MaxValue => new TESTID((byte)shortValue),
int intValue when intValue < byte.MaxValue => new TESTID((byte)intValue),
long longValue when longValue < byte.MaxValue => new TESTID((byte)longValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new TESTID(result),
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Byte/Byte_EfCoreValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, byte>
{
public EfCoreValueConverter() : this(null) { }
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
id => id.Value,
value => new TESTID(value),
mappingHints
)
{ }
}
1 change: 1 addition & 0 deletions src/StronglyTypedIds/Templates/Byte/Byte_IComparable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 public int CompareTo(TESTID other) => Value.CompareTo(other.Value);
Original file line number Diff line number Diff line change
@@ -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);
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<byte?>(reader);
return result.HasValue ? new TESTID(result.Value) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<TESTID>
{
public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new TESTID(reader.GetByte());
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}
41 changes: 41 additions & 0 deletions src/StronglyTypedIds/Templates/Byte/Byte_TypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

class TESTIDTypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
return sourceType == typeof(byte) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return value switch
{
byte byteValue => new TESTID(byteValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && byte.TryParse(stringValue, out var result) => new TESTID(result),
_ => base.ConvertFrom(context, culture, value),
};
}

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
return sourceType == typeof(byte) || 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(byte))
{
return idValue.Value;
}

if (destinationType == typeof(string))
{
return idValue.Value.ToString();
}
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
23 changes: 23 additions & 0 deletions src/StronglyTypedIds/Templates/Short/Short_Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
 readonly partial struct TESTID : INTERFACES
{
public short Value { get; }

public TESTID(short value)
{
Value = value;
}

public static readonly TESTID Empty = new TESTID(0);

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);
21 changes: 21 additions & 0 deletions src/StronglyTypedIds/Templates/Short/Short_DapperTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler<TESTID>
{
public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value)
{
parameter.Value = value.Value;
}

public override TESTID Parse(object value)
{
return value switch
{
short shortValue => new TESTID(shortValue),
byte byteValue => new TESTID(byteValue),
int intValue when intValue < byte.MaxValue => new TESTID((short)intValue),
long longValue when longValue < byte.MaxValue => new TESTID((short)longValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new TESTID(result),
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
}
12 changes: 12 additions & 0 deletions src/StronglyTypedIds/Templates/Short/Short_EfCoreValueConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, short>
{
public EfCoreValueConverter() : this(null) { }
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
id => id.Value,
value => new TESTID(value),
mappingHints
)
{ }
}
1 change: 1 addition & 0 deletions src/StronglyTypedIds/Templates/Short/Short_IComparable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 public int CompareTo(TESTID other) => Value.CompareTo(other.Value);
Original file line number Diff line number Diff line change
@@ -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);
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<short?>(reader);
return result.HasValue ? new TESTID(result.Value) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<TESTID>
{
public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new TESTID(reader.GetInt16());
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}
Loading