-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the EFCore entity configuration to the entity builder level. (#74)
- Loading branch information
Showing
11 changed files
with
140 additions
and
136 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
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
110 changes: 110 additions & 0 deletions
110
src/Fluxera.Repository.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,110 @@ | ||
namespace Fluxera.Repository.EntityFrameworkCore | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Fluxera.Entity; | ||
using Fluxera.Enumeration.EntityFrameworkCore; | ||
using Fluxera.Guards; | ||
using Fluxera.StronglyTypedId; | ||
using Fluxera.StronglyTypedId.EntityFrameworkCore; | ||
using Fluxera.ValueObject.EntityFrameworkCore; | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="ModelBuilder" /> type. | ||
/// </summary> | ||
[PublicAPI] | ||
public static class EntityTypeBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configure the <see cref="EntityTypeBuilder" /> with default settings. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"></param> | ||
/// <returns></returns> | ||
public static void UseRepositoryDefaults(this EntityTypeBuilder entityTypeBuilder) | ||
{ | ||
Guard.Against.Null(entityTypeBuilder); | ||
|
||
entityTypeBuilder.UseEnumeration(); | ||
entityTypeBuilder.UsePrimitiveValueObject(); | ||
entityTypeBuilder.UseStronglyTypedId(); | ||
entityTypeBuilder.UseStronglyTypedIdValueGenerator(); | ||
entityTypeBuilder.UseSequentialGuidStringIdValueGenerator(); | ||
entityTypeBuilder.UseReferences(); | ||
} | ||
|
||
/// <summary> | ||
/// Configure the <see cref="EntityTypeBuilder" /> to use the | ||
/// <see cref="StronglyTypedIdValueGenerator{TStronglyTypedId,TValue}" />. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"></param> | ||
public static void UseStronglyTypedIdValueGenerator(this EntityTypeBuilder entityTypeBuilder) | ||
{ | ||
Guard.Against.Null(entityTypeBuilder); | ||
|
||
IEnumerable<PropertyInfo> properties = entityTypeBuilder.Metadata | ||
.ClrType | ||
.GetProperties() | ||
.Where(propertyInfo => propertyInfo.PropertyType.IsStronglyTypedId()); | ||
|
||
foreach(PropertyInfo property in properties) | ||
{ | ||
Type idType = property.PropertyType; | ||
Type valueType = idType.GetStronglyTypedIdValueType(); | ||
|
||
Type generatorTypeTemplate = typeof(StronglyTypedIdValueGenerator<,>); | ||
Type generatorType = generatorTypeTemplate.MakeGenericType(idType, valueType); | ||
|
||
entityTypeBuilder | ||
.Property(property.Name) | ||
.HasValueGenerator(generatorType); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Configure the <see cref="EntityTypeBuilder" /> to use the <see cref="SequentialGuidStringValueGenerator" /> for | ||
/// string IDs. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"></param> | ||
public static void UseSequentialGuidStringIdValueGenerator(this EntityTypeBuilder entityTypeBuilder) | ||
{ | ||
Guard.Against.Null(entityTypeBuilder); | ||
|
||
IEnumerable<PropertyInfo> properties = entityTypeBuilder.Metadata | ||
.ClrType | ||
.GetProperties() | ||
.Where(propertyInfo => propertyInfo.PropertyType == typeof(string) && propertyInfo.Name == "ID"); | ||
|
||
foreach(PropertyInfo property in properties) | ||
{ | ||
entityTypeBuilder | ||
.Property(property.Name) | ||
.HasValueGenerator<SequentialGuidStringValueGenerator>(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Disables the delete cascading for references aggregate roots. | ||
/// </summary> | ||
/// <param name="entityTypeBuilder"></param> | ||
public static void UseReferences(this EntityTypeBuilder entityTypeBuilder) | ||
{ | ||
IMutableEntityType entityType = entityTypeBuilder.Metadata; | ||
|
||
if(!entityType.IsOwned() && entityType.ClrType.IsAggregateRoot()) | ||
{ | ||
IEnumerable<IMutableForeignKey> foreignKeys = entityType.GetForeignKeys(); | ||
|
||
foreach(IMutableForeignKey relationship in foreignKeys) | ||
{ | ||
relationship.DeleteBehavior = DeleteBehavior.Restrict; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
118 changes: 0 additions & 118 deletions
118
src/Fluxera.Repository.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
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
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