-
Notifications
You must be signed in to change notification settings - Fork 91
Added MongoSerializer and Mongo ObjectId backing type #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marspion
wants to merge
12
commits into
andrewlock:master
Choose a base branch
from
marspion:feature/object-id-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e90315a
Added MongoObjectIdSerializer for String and NullableString
96e24fa
Refactored MongoSerializer and implemented it for all templates
88be638
Implemented ObjectId backing type.
2d2b2e0
Added some integration tests for ObjectId backing type.
608aae8
Implemented MongoSerializer tests for all backing types
60bb3e6
Added missing link folder in nuget integration tests and simplified c…
625a115
Resolved discrepancy of handling null/empty values in serializers.
de0c801
Reverted CanSerializeToNullable test.
f2d63d1
Added missing empty line to ObjectId_Base template and generated snap…
6fdaeab
Remove extra spaces
andrewlock a6d0b1b
CodeReview suggestion about returing TESTID.Empty
6854795
Overrode Mongo2Go binaries for Linux platform.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,5 +18,6 @@ public enum StronglyTypedIdBackingType | |
| Long = 4, | ||
| NullableString = 5, | ||
| MassTransitNewId = 6, | ||
| ObjectId = 7 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) | ||
| { | ||
| return new TESTID(MassTransit.NewId.FromGuid(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.ToGuid().ToString()); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); |
17 changes: 17 additions & 0 deletions
17
src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| | ||
| public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler<TESTID> | ||
| { | ||
| public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) | ||
| { | ||
| parameter.Value = 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"), | ||
| }; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| | ||
| public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<TESTID, string> | ||
| { | ||
| 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 | ||
| ) { } | ||
| } |
1 change: 1 addition & 0 deletions
1
src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| public int CompareTo(TESTID other) => Value.CompareTo(other.Value); |
12 changes: 12 additions & 0 deletions
12
src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| | ||
| 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) | ||
| { | ||
| return new TESTID(new MongoDB.Bson.ObjectId(serializer.Deserialize<string>(reader))); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(new MongoDB.Bson.ObjectId(reader.GetString())); | ||
| } | ||
|
|
||
| public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStringValue(value.Value.ToString()); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<TESTID> | ||
| { | ||
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to go through Guid here? You could use new
NewId(context.Reader.ReadString())I think 🙂 (similar forSerialize())There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have right :) I simplified it