-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modular mapping configuration (#162)
* Modular configuration for property mappings * Update Readme, unit tests and sample Co-authored-by: Steven Decoodt <[email protected]>
- Loading branch information
Showing
9 changed files
with
312 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Sieve.Services; | ||
|
||
namespace Sieve.Sample.Entities | ||
{ | ||
public class SieveConfigurationForPost : ISieveConfiguration | ||
{ | ||
public void Configure(SievePropertyMapper mapper) | ||
{ | ||
mapper.Property<Post>(p => p.Title) | ||
.CanSort() | ||
.CanFilter() | ||
.HasName("CustomTitleName"); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#nullable enable | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Sieve.Services | ||
{ | ||
/// <summary> | ||
/// Use this interface to create SieveConfiguration (just like EntityTypeConfigurations are defined for EF) | ||
/// </summary> | ||
public interface ISieveConfiguration | ||
{ | ||
/// <summary> | ||
/// Configures sieve property mappings. | ||
/// </summary> | ||
/// <param name="mapper"> The mapper used to configure the sieve properties on. </param> | ||
void Configure(SievePropertyMapper mapper); | ||
} | ||
|
||
/// <summary> | ||
/// Configuration extensions to the <see cref="SievePropertyMapper" /> | ||
/// </summary> | ||
public static class SieveConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Applies configuration that is defined in an <see cref="ISieveConfiguration" /> instance. | ||
/// </summary> | ||
/// <param name="mapper"> The mapper to apply the configuration on. </param> | ||
/// <typeparam name="T">The configuration to be applied. </typeparam> | ||
/// <returns> | ||
/// The same <see cref="SievePropertyMapper" /> instance so that additional configuration calls can be chained. | ||
/// </returns> | ||
public static SievePropertyMapper ApplyConfiguration<T>(this SievePropertyMapper mapper) where T : ISieveConfiguration, new() | ||
{ | ||
var configuration = new T(); | ||
configuration.Configure(mapper); | ||
return mapper; | ||
} | ||
|
||
/// <summary> | ||
/// Applies configuration from all <see cref="ISieveConfiguration" /> | ||
/// instances that are defined in provided assembly. | ||
/// </summary> | ||
/// <param name="mapper"> The mapper to apply the configuration on. </param> | ||
/// <param name="assembly"> The assembly to scan. </param> | ||
/// <returns> | ||
/// The same <see cref="SievePropertyMapper" /> instance so that additional configuration calls can be chained. | ||
/// </returns> | ||
public static SievePropertyMapper ApplyConfigurationsFromAssembly(this SievePropertyMapper mapper, Assembly assembly) | ||
{ | ||
foreach (var type in assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)) | ||
{ | ||
// Only accept types that contain a parameterless constructor, are not abstract. | ||
var noArgConstructor = type.GetConstructor(Type.EmptyTypes); | ||
if (noArgConstructor is null) | ||
{ | ||
continue; | ||
} | ||
|
||
if (type.GetInterfaces().Any(t => t == typeof(ISieveConfiguration))) | ||
{ | ||
var configuration = (ISieveConfiguration)noArgConstructor.Invoke(new object?[] { }); | ||
configuration.Configure(mapper); | ||
} | ||
} | ||
|
||
return mapper; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
SieveUnitTests/Abstractions/Entity/SieveConfigurationForIPost.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,37 @@ | ||
using Sieve.Services; | ||
|
||
namespace SieveUnitTests.Abstractions.Entity | ||
{ | ||
public class SieveConfigurationForIPost : ISieveConfiguration | ||
{ | ||
public void Configure(SievePropertyMapper mapper) | ||
{ | ||
mapper.Property<IPost>(p => p.ThisHasNoAttributeButIsAccessible) | ||
.CanSort() | ||
.CanFilter() | ||
.HasName("shortname"); | ||
|
||
mapper.Property<IPost>(p => p.TopComment.Text) | ||
.CanFilter(); | ||
|
||
mapper.Property<IPost>(p => p.TopComment.Id) | ||
.CanSort(); | ||
|
||
mapper.Property<IPost>(p => p.OnlySortableViaFluentApi) | ||
.CanSort(); | ||
|
||
mapper.Property<IPost>(p => p.TopComment.Text) | ||
.CanFilter() | ||
.HasName("topc"); | ||
|
||
mapper.Property<IPost>(p => p.FeaturedComment.Text) | ||
.CanFilter() | ||
.HasName("featc"); | ||
|
||
mapper | ||
.Property<IPost>(p => p.DateCreated) | ||
.CanSort() | ||
.HasName("CreateDate"); | ||
} | ||
} | ||
} |
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 @@ | ||
using Sieve.Services; | ||
|
||
namespace SieveUnitTests.Entities | ||
{ | ||
public class SieveConfigurationForPost : ISieveConfiguration | ||
{ | ||
public void Configure(SievePropertyMapper mapper) | ||
{ | ||
mapper.Property<Post>(p => p.ThisHasNoAttributeButIsAccessible) | ||
.CanSort() | ||
.CanFilter() | ||
.HasName("shortname"); | ||
|
||
mapper.Property<Post>(p => p.TopComment.Text) | ||
.CanFilter(); | ||
|
||
mapper.Property<Post>(p => p.TopComment.Id) | ||
.CanSort(); | ||
|
||
mapper.Property<Post>(p => p.OnlySortableViaFluentApi) | ||
.CanSort(); | ||
|
||
mapper.Property<Post>(p => p.TopComment.Text) | ||
.CanFilter() | ||
.HasName("topc"); | ||
|
||
mapper.Property<Post>(p => p.FeaturedComment.Text) | ||
.CanFilter() | ||
.HasName("featc"); | ||
|
||
mapper | ||
.Property<Post>(p => p.DateCreated) | ||
.CanSort() | ||
.HasName("CreateDate"); | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
SieveUnitTests/Services/ModularConfigurationSieveProcessor.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,26 @@ | ||
using Microsoft.Extensions.Options; | ||
using Sieve.Models; | ||
using Sieve.Services; | ||
using SieveUnitTests.Abstractions.Entity; | ||
using SieveUnitTests.Entities; | ||
|
||
namespace SieveUnitTests.Services | ||
{ | ||
public class ModularConfigurationSieveProcessor : SieveProcessor | ||
{ | ||
public ModularConfigurationSieveProcessor( | ||
IOptions<SieveOptions> options, | ||
ISieveCustomSortMethods customSortMethods, | ||
ISieveCustomFilterMethods customFilterMethods) | ||
: base(options, customSortMethods, customFilterMethods) | ||
{ | ||
} | ||
|
||
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper) | ||
{ | ||
return mapper | ||
.ApplyConfiguration<SieveConfigurationForPost>() | ||
.ApplyConfiguration<SieveConfigurationForIPost>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.