-
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.
Moved value object configuration for EFCore to the entity builder lev…
…el. (#31)
- Loading branch information
Showing
3 changed files
with
52 additions
and
55 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/Fluxera.ValueObject.EntityFrameworkCore/EntityTypeBuilderExtensions.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,50 @@ | ||
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.Builders; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="ModelBuilder" /> type. | ||
/// </summary> | ||
[PublicAPI] | ||
public static class EntityTypeBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configure the <see cref="EntityTypeBuilder" /> to use the | ||
/// <see cref="PrimitiveValueObjectConverter{TValueObject,TValue}" />. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"></param> | ||
public static void UsePrimitiveValueObject(this EntityTypeBuilder entityTypeBuilder) | ||
{ | ||
Guard.Against.Null(entityTypeBuilder); | ||
|
||
IEnumerable<PropertyInfo> properties = entityTypeBuilder.Metadata | ||
.ClrType | ||
.GetProperties() | ||
.Where(type => type.PropertyType.IsPrimitiveValueObject()); | ||
|
||
foreach(PropertyInfo property in properties) | ||
{ | ||
Type enumerationType = property.PropertyType; | ||
Type valueType = enumerationType.GetPrimitiveValueObjectValueType(); | ||
|
||
Type converterTypeTemplate = typeof(PrimitiveValueObjectConverter<,>); | ||
|
||
Type converterType = converterTypeTemplate.MakeGenericType(enumerationType, valueType); | ||
|
||
ValueConverter converter = (ValueConverter)Activator.CreateInstance(converterType); | ||
|
||
entityTypeBuilder | ||
.Property(property.Name) | ||
.HasConversion(converter); | ||
} | ||
} | ||
} | ||
} |
53 changes: 0 additions & 53 deletions
53
src/Fluxera.ValueObject.EntityFrameworkCore/ModelBuilderExtensions.cs
This file was deleted.
Oops, something went wrong.
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