Skip to content

Commit

Permalink
#371 Refactor entity configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjstone committed Sep 5, 2020
1 parent 9d8d76c commit b0c34d2
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

namespace RapidField.SolidInstruments.Example.Domain.AccessControl
{
using UserModelConfiguration = Models.User.AggregateDataAccessModelConfiguration;
using UserRoleAssignmentModelConfiguration = Models.UserRoleAssignment.AggregateDataAccessModelConfiguration;
using UserRoleModelConfiguration = Models.UserRole.AggregateDataAccessModelConfiguration;

/// <summary>
/// Represents a connection to the AccessControl database.
/// </summary>
Expand Down Expand Up @@ -52,19 +56,9 @@ protected override void OnModelCreating(IConfiguration applicationConfiguration,
try
{
_ = modelBuilder
.Entity<UserModel>(entityType =>
{
entityType.HasMany(entity => entity.UserRoleAssignments).WithOne(entity => entity.User);
})
.Entity<UserRoleModel>(entityType =>
{
entityType.HasMany<UserRoleAssignmentModel>().WithOne(entity => entity.UserRole).HasForeignKey(entity => entity.UserRoleIdentifier);
})
.Entity<UserRoleAssignmentModel>(entityType =>
{
entityType.HasOne(entity => entity.User).WithMany(entity => entity.UserRoleAssignments).HasForeignKey(entity => entity.UserIdentifier);
entityType.HasOne(entity => entity.UserRole).WithMany().HasForeignKey(entity => entity.UserRoleIdentifier);
});
.ApplyConfiguration(new UserModelConfiguration(applicationConfiguration))
.ApplyConfiguration(new UserRoleAssignmentModelConfiguration(applicationConfiguration))
.ApplyConfiguration(new UserRoleModelConfiguration(applicationConfiguration));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;

namespace RapidField.SolidInstruments.Example.Domain.Models.User
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="AggregateDataAccessModel" /> class.
/// </summary>
public sealed class AggregateDataAccessModelConfiguration : DataAccessModelConfiguration<AggregateDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="AggregateDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public AggregateDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<AggregateDataAccessModel> entityType, IConfiguration applicationConfiguration) => _ = entityType.HasMany(entity => entity.UserRoleAssignments).WithOne(entity => entity.User);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;

namespace RapidField.SolidInstruments.Example.Domain.Models.User
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="ValueDataAccessModel" /> class.
/// </summary>
public sealed class ValueDataAccessModelConfiguration : DataAccessModelConfiguration<ValueDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="ValueDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public ValueDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<ValueDataAccessModel> entityType, IConfiguration applicationConfiguration)
{
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;
using UserRoleAssignmentModel = RapidField.SolidInstruments.Example.Domain.Models.UserRoleAssignment.AggregateDataAccessModel;

namespace RapidField.SolidInstruments.Example.Domain.Models.UserRole
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="AggregateDataAccessModel" /> class.
/// </summary>
public sealed class AggregateDataAccessModelConfiguration : DataAccessModelConfiguration<AggregateDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="AggregateDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public AggregateDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<AggregateDataAccessModel> entityType, IConfiguration applicationConfiguration) => _ = entityType.HasMany<UserRoleAssignmentModel>().WithOne(entity => entity.UserRole).HasForeignKey(entity => entity.UserRoleIdentifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;

namespace RapidField.SolidInstruments.Example.Domain.Models.UserRole
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="ValueDataAccessModel" /> class.
/// </summary>
public sealed class ValueDataAccessModelConfiguration : DataAccessModelConfiguration<ValueDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="ValueDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public ValueDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<ValueDataAccessModel> entityType, IConfiguration applicationConfiguration)
{
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;

namespace RapidField.SolidInstruments.Example.Domain.Models.UserRoleAssignment
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="AggregateDataAccessModel" /> class.
/// </summary>
public sealed class AggregateDataAccessModelConfiguration : DataAccessModelConfiguration<AggregateDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="AggregateDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public AggregateDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<AggregateDataAccessModel> entityType, IConfiguration applicationConfiguration)
{
_ = entityType.HasOne(entity => entity.User).WithMany(entity => entity.UserRoleAssignments).HasForeignKey(entity => entity.UserIdentifier);
_ = entityType.HasOne(entity => entity.UserRole).WithMany().HasForeignKey(entity => entity.UserRoleIdentifier);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Configuration;
using RapidField.SolidInstruments.DataAccess.EntityFramework;
using System;

namespace RapidField.SolidInstruments.Example.Domain.Models.UserRoleAssignment
{
/// <summary>
/// Encapsulates entity type configuration for the <see cref="ValueDataAccessModel" /> class.
/// </summary>
public sealed class ValueDataAccessModelConfiguration : DataAccessModelConfiguration<ValueDataAccessModel>
{
/// <summary>
/// Initializes a new instance of the <see cref="ValueDataAccessModelConfiguration" /> class.
/// </summary>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="applicationConfiguration" /> is <see langword="null" />.
/// </exception>
public ValueDataAccessModelConfiguration(IConfiguration applicationConfiguration)
: base(applicationConfiguration)
{
return;
}

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<ValueDataAccessModel> entityType, IConfiguration applicationConfiguration)
{
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected DataAccessModelConfiguration(IConfiguration applicationConfiguration)
/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="builder">
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected override void Configure(EntityTypeBuilder<TDataAccessModel> builder, IConfiguration applicationConfiguration) => builder.HasKey(model => model.Identifier);
protected override void Configure(EntityTypeBuilder<TDataAccessModel> entityType, IConfiguration applicationConfiguration) => entityType.HasKey(model => model.Identifier);

/// <summary>
/// Represents the type of the value that uniquely identifies the data access model.
Expand Down Expand Up @@ -89,21 +89,21 @@ protected DataAccessModelConfiguration(IConfiguration applicationConfiguration)
/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="builder">
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
public void Configure(EntityTypeBuilder<TDataAccessModel> builder) => Configure(builder, ApplicationConfiguration);
public void Configure(EntityTypeBuilder<TDataAccessModel> entityType) => Configure(entityType, ApplicationConfiguration);

/// <summary>
/// Configures the entity.
/// </summary>
/// <param name="builder">
/// <param name="entityType">
/// The builder that is used to configure the entity.
/// </param>
/// <param name="applicationConfiguration">
/// Configuration information for the application.
/// </param>
protected abstract void Configure(EntityTypeBuilder<TDataAccessModel> builder, IConfiguration applicationConfiguration);
protected abstract void Configure(EntityTypeBuilder<TDataAccessModel> entityType, IConfiguration applicationConfiguration);

/// <summary>
/// Represents the type of the data access model.
Expand Down

0 comments on commit b0c34d2

Please sign in to comment.