-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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,52 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace Implementation.Converters | ||
{ | ||
/// <summary> | ||
/// Converts a <see cref="Guid"/> to and from its <see cref="System.String"/> representation. | ||
/// </summary> | ||
public class GuidConverter : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <returns>Returns <c>true</c> if this instance can convert the specified object type; otherwise <c>false</c>.</returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType.IsAssignableFrom(typeof(Guid)); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <param name="existingValue">The existing value of object being read.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
/// <returns>The object value.</returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
try | ||
{ | ||
return serializer.Deserialize<Guid>(reader); | ||
} | ||
catch | ||
{ | ||
return Guid.Empty; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> | ||
/// <param name="value">The value.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
serializer.Serialize(writer, value); | ||
} | ||
} | ||
} |
This file contains 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,21 @@ | ||
using System; | ||
using Implementation.Converters; | ||
using Infrastructure.Repositories; | ||
using Newtonsoft.Json; | ||
|
||
namespace Implementation.Repositories | ||
{ | ||
public abstract class AEntity : IEntity | ||
{ | ||
[JsonConverter(typeof(GuidConverter))] | ||
public Guid Id { get; set; } | ||
|
||
protected AEntity() | ||
{ | ||
if (Id == Guid.Empty) | ||
{ | ||
Id = Guid.NewGuid(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Infrastructure.Repositories | ||
{ | ||
|
This file contains 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 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 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
16 changes: 14 additions & 2 deletions
16
_src/_Tests/ImplementationTest/RepositoryTests/Model/User.cs
This file contains 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 |
---|---|---|
@@ -1,12 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Implementation.Converters; | ||
using Implementation.Repositories; | ||
using Infrastructure.Repositories; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace ImplementationTest.RepositoryTests.Model | ||
{ | ||
public class User : IUser | ||
public class User : AEntity, IUser | ||
{ | ||
public Guid Id { get; } = Guid.NewGuid(); | ||
// [JsonConverter(typeof(GuidConverter))] | ||
// public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public int Age { get; set; } | ||
|
||
public User() : base() | ||
{ | ||
|
||
} | ||
|
||
} | ||
} |