-
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.
Added serializer support fpr primitive value objects. (#15)
* Updated packages. * Added serialization support for JSON.NET and System.Text.Json. * Added serializer support for LiteDB. * Added serializer support for MongoDB. * Added EF Core serializer support. * Fixed warnings.
- Loading branch information
Showing
45 changed files
with
1,358 additions
and
10 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
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
37 changes: 37 additions & 0 deletions
37
src/Fluxera.ValueObject.EntityFrameworkCore/Fluxera.ValueObject.EntityFrameworkCore.csproj
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,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Title>Fluxera.ValueObject.EntityFrameworkCore</Title> | ||
<Description>A libary that provides serializer support for EF Core for value objects.</Description> | ||
<PackageTags>fluxera;library;ddd;value-object;ef-core</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md" Link="Properties\README.md"> | ||
<Pack>true</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\icon.png" Link="Properties\icon.png"> | ||
<Pack>true</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="GitVersion.MsBuild" Version="5.10.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Fluxera.ValueObject\Fluxera.ValueObject.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
53 changes: 53 additions & 0 deletions
53
src/Fluxera.ValueObject.EntityFrameworkCore/ModelBuilderExtensions.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace Fluxera.ValueObject.EntityFrameworkCore | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Fluxera.Guards; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="ModelBuilder" /> type. | ||
/// </summary> | ||
[PublicAPI] | ||
public static class ModelBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configure the module builder to use the <see cref="PrimitiveValueObjectConverter{TValueObject,TValue}" />. | ||
/// </summary> | ||
/// <param name="modelBuilder"></param> | ||
public static void UsePrimitiveValueObject(this ModelBuilder modelBuilder) | ||
{ | ||
Guard.Against.Null(modelBuilder, nameof(modelBuilder)); | ||
|
||
foreach(IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes()) | ||
{ | ||
IEnumerable<PropertyInfo> properties = entityType | ||
.ClrType | ||
.GetProperties() | ||
.Where(type => type.PropertyType.IsPrimitiveValueObject()); | ||
|
||
foreach(PropertyInfo property in properties) | ||
{ | ||
Type enumerationType = property.PropertyType; | ||
Type valueType = enumerationType.GetValueType(); | ||
|
||
Type converterTypeTemplate = typeof(PrimitiveValueObjectConverter<,>); | ||
|
||
Type converterType = converterTypeTemplate.MakeGenericType(enumerationType, valueType); | ||
|
||
ValueConverter converter = (ValueConverter)Activator.CreateInstance(converterType); | ||
|
||
modelBuilder | ||
.Entity(entityType.ClrType) | ||
.Property(property.Name) | ||
.HasConversion(converter); | ||
} | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Fluxera.ValueObject.EntityFrameworkCore/PrimitiveValueObjectConverter.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace Fluxera.ValueObject.EntityFrameworkCore | ||
{ | ||
using System; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <inheritdoc /> | ||
[PublicAPI] | ||
public sealed class PrimitiveValueObjectConverter<TValueObject, TValue> : ValueConverter<TValueObject, TValue> | ||
where TValueObject : PrimitiveValueObject<TValueObject, TValue> | ||
where TValue : IComparable | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PrimitiveValueObjectConverter{TValueObject,TValue}" /> type. | ||
/// </summary> | ||
public PrimitiveValueObjectConverter() | ||
: base(valueObject => Serialize(valueObject), value => Deserialize(value)) | ||
{ | ||
} | ||
|
||
private static TValue Serialize(TValueObject valueObject) | ||
{ | ||
TValue value = valueObject.Value; | ||
return value; | ||
} | ||
|
||
private static TValueObject Deserialize(TValue value) | ||
{ | ||
if(value is null) | ||
{ | ||
return null; | ||
} | ||
|
||
object instance = Activator.CreateInstance(typeof(TValueObject), BindingFlags.Public | BindingFlags.Instance, null, new object[] { value }, null); | ||
return (TValueObject)instance; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Fluxera.ValueObject.JsonNet/CompositeContractResolver.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace Fluxera.ValueObject.JsonNet | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Fluxera.Guards; | ||
using JetBrains.Annotations; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
/// <summary> | ||
/// A <see cref="IContractResolver" /> that allows to have multiple other resolver instances added. | ||
/// </summary> | ||
[PublicAPI] | ||
public sealed class CompositeContractResolver : IContractResolver, IEnumerable<IContractResolver> | ||
{ | ||
private readonly IList<IContractResolver> contractResolvers = new List<IContractResolver>(); | ||
private readonly DefaultContractResolver defaultContractResolver = new DefaultContractResolver(); | ||
|
||
/// <inheritdoc /> | ||
public JsonContract ResolveContract(Type type) | ||
{ | ||
return this.contractResolvers | ||
.Select(x => x.ResolveContract(type)) | ||
.FirstOrDefault(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<IContractResolver> GetEnumerator() | ||
{ | ||
return this.contractResolvers.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return this.GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Add a resolver instance. | ||
/// </summary> | ||
/// <param name="contractResolver"></param> | ||
public void Add(IContractResolver contractResolver) | ||
{ | ||
Guard.Against.Null(contractResolver); | ||
|
||
if(this.contractResolvers.Contains(this.defaultContractResolver)) | ||
{ | ||
this.contractResolvers.Remove(this.defaultContractResolver); | ||
} | ||
|
||
this.contractResolvers.Add(contractResolver); | ||
this.contractResolvers.Add(this.defaultContractResolver); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Fluxera.ValueObject.JsonNet/Fluxera.ValueObject.JsonNet.csproj
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,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Title>Fluxera.ValueObject.JsonNet</Title> | ||
<Description>A libary that provides serializer support for JSON.NET for value objects.</Description> | ||
<PackageTags>fluxera;library;ddd;value-object;json</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md" Link="Properties\README.md"> | ||
<Pack>true</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\icon.png" Link="Properties\icon.png"> | ||
<Pack>true</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="GitVersion.MsBuild" Version="5.10.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Fluxera.ValueObject\Fluxera.ValueObject.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
22 changes: 22 additions & 0 deletions
22
src/Fluxera.ValueObject.JsonNet/JsonSerializerSettingsExtensions.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Fluxera.ValueObject.JsonNet | ||
{ | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="JsonSerializerSettings" /> type. | ||
/// </summary> | ||
public static class JsonSerializerSettingsExtensions | ||
{ | ||
/// <summary> | ||
/// Configure the serializer to use the <see cref="PrimitiveValueObjectConverter{TValueObject,TValue}" />. | ||
/// </summary> | ||
/// <param name="settings"></param> | ||
public static void UsePrimitiveValueObject(this JsonSerializerSettings settings) | ||
{ | ||
settings.ContractResolver = new CompositeContractResolver | ||
{ | ||
new PrimitiveValueObjectContractResolver() | ||
}; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Fluxera.ValueObject.JsonNet/PrimitiveValueObjectContractResolver.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Fluxera.ValueObject.JsonNet | ||
{ | ||
using System; | ||
using JetBrains.Annotations; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
/// <inheritdoc /> | ||
[PublicAPI] | ||
public sealed class PrimitiveValueObjectContractResolver : DefaultContractResolver | ||
{ | ||
/// <inheritdoc /> | ||
protected override JsonConverter ResolveContractConverter(Type objectType) | ||
{ | ||
if(objectType.IsPrimitiveValueObject()) | ||
{ | ||
Type valueType = objectType.GetValueType(); | ||
Type converterTypeTemplate = typeof(PrimitiveValueObjectConverter<,>); | ||
Type converterType = converterTypeTemplate.MakeGenericType(objectType, valueType); | ||
|
||
return (JsonConverter)Activator.CreateInstance(converterType); | ||
} | ||
|
||
return base.ResolveContractConverter(objectType); | ||
} | ||
} | ||
} |
Oops, something went wrong.