Skip to content

Commit

Permalink
Moved the EFCore entity configuration to the entity builder level. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Dec 13, 2022
1 parent 603860a commit 1f1043d
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 136 deletions.
2 changes: 1 addition & 1 deletion samples/Sample.API/Sample.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Fluxera.StronglyTypedId.SystemTextJson" Version="7.0.3" />
<PackageReference Include="Fluxera.StronglyTypedId.SystemTextJson" Version="7.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample.EntityFrameworkCore/SampleDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Company>(entity =>
{
entity.ToTable("Companies");
});

modelBuilder.UseRepositoryDefaults();
entity.UseRepositoryDefaults();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PackageReference Include="Fluxera.Entity" Version="7.0.5" />
<PackageReference Include="Fluxera.Extensions.Validation.Abstractions" Version="7.0.3" />
<PackageReference Include="Fluxera.Linq.Expressions" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject" Version="7.0.4" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
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;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Enumeration.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Fluxera.Enumeration.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Fluxera.Guards" Version="7.0.3" />
<PackageReference Include="Fluxera.StronglyTypedId.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Fluxera.StronglyTypedId.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Fluxera.ValueObject.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
118 changes: 0 additions & 118 deletions src/Fluxera.Repository.EntityFrameworkCore/ModelBuilderExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fluxera.Enumeration.LiteDB" Version="7.0.4" />
<PackageReference Include="Fluxera.Enumeration.LiteDB" Version="7.0.5" />
<PackageReference Include="Fluxera.Spatial.LiteDB" Version="7.0.4" />
<PackageReference Include="Fluxera.StronglyTypedId.LiteDB" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject.LiteDB" Version="7.0.3" />
<PackageReference Include="Fluxera.StronglyTypedId.LiteDB" Version="7.0.4" />
<PackageReference Include="Fluxera.ValueObject.LiteDB" Version="7.0.4" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

<ItemGroup>
<PackageReference Include="Fluxera.ComponentModel.Annotations" Version="7.0.4" />
<PackageReference Include="Fluxera.Enumeration.MongoDB" Version="7.0.4" />
<PackageReference Include="Fluxera.Enumeration.MongoDB" Version="7.0.5" />
<PackageReference Include="Fluxera.Spatial.MongoDB" Version="7.0.4" />
<PackageReference Include="Fluxera.StronglyTypedId.MongoDB" Version="7.0.3" />
<PackageReference Include="Fluxera.StronglyTypedId.MongoDB" Version="7.0.4" />
<PackageReference Include="Fluxera.Temporal.MongoDB" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject.MongoDB" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject.MongoDB" Version="7.0.4" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/Fluxera.Repository/Fluxera.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<ItemGroup>
<PackageReference Include="Fluxera.Entity" Version="7.0.5" />
<PackageReference Include="Fluxera.Enumeration" Version="7.0.4" />
<PackageReference Include="Fluxera.Enumeration" Version="7.0.5" />
<PackageReference Include="Fluxera.Extensions.Caching" Version="7.0.3" />
<PackageReference Include="Fluxera.Extensions.Common" Version="7.0.3" />
<PackageReference Include="Fluxera.Extensions.DependencyInjection" Version="7.0.3" />
Expand All @@ -36,7 +36,7 @@
<PackageReference Include="Fluxera.Spatial" Version="7.0.4" />
<PackageReference Include="Fluxera.Temporal" Version="7.0.3" />
<PackageReference Include="Fluxera.Utilities" Version="7.0.4" />
<PackageReference Include="Fluxera.ValueObject" Version="7.0.3" />
<PackageReference Include="Fluxera.ValueObject" Version="7.0.4" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
entity.ToTable("People");
entity.OwnsOne(x => x.Address);

entity.UseRepositoryDefaults();
});

modelBuilder.Entity<Company>(entity =>
{
entity.UseRepositoryDefaults();
});

modelBuilder.Entity<Employee>(entity =>
Expand All @@ -45,9 +52,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity
.Property(x => x.SalaryNullableDecimal)
.HasConversion<double?>();

entity.UseRepositoryDefaults();
});

modelBuilder.UseRepositoryDefaults();
modelBuilder.Entity<Reference>(entity =>
{
entity.UseRepositoryDefaults();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
entity.ToTable("People");
entity.OwnsOne(x => x.Address);
});

modelBuilder.UseRepositoryDefaults();
entity.UseRepositoryDefaults();
});
}
}
}

0 comments on commit 1f1043d

Please sign in to comment.