-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ nested class
ReplaceParameterTypeVisitor
* move param `keySelector` from method `HasKey()` & `SplittingHasKey()` to primary ctor and rename methods to `HasBaseTable()` & `SplitToTable()` * renamed from `ModelBuilderExtension` @ `RevisionWithSplitting.ModelBuilder` @ c#/crawler
- Loading branch information
Showing
2 changed files
with
68 additions
and
27 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using System.Collections.ObjectModel; | ||
|
||
namespace tbm.Crawler.Db.Revision.Splitting; | ||
|
||
public abstract class RevisionWithSplitting<TBaseRevision> : BaseRevisionWithSplitting | ||
|
@@ -23,16 +25,51 @@ protected void SetSplitEntityValue<TSplitEntity, TValue> | |
_splitEntities[typeof(TSplitEntity)] = entityFactory(); | ||
} | ||
|
||
public class ModelBuilderExtension(ModelBuilder builder, string baseTableName) | ||
public class ModelBuilder( | ||
Microsoft.EntityFrameworkCore.ModelBuilder builder, | ||
string baseTableName, | ||
Expression<Func<TBaseRevision, object?>> keySelector) | ||
{ | ||
public void HasKey<TRevision>(Expression<Func<TRevision, object?>> keySelector) | ||
where TRevision : class, TBaseRevision => | ||
builder.Entity<TRevision>().ToTable(baseTableName).HasKey(keySelector); | ||
|
||
public void SplittingHasKey<TRevisionWithSplitting> | ||
(string tableNameSuffix, Expression<Func<TRevisionWithSplitting, object?>> keySelector) | ||
where TRevisionWithSplitting : RevisionWithSplitting<TBaseRevision> => | ||
builder.Entity<TRevisionWithSplitting>().Ignore(e => e.NullFieldsBitMask) | ||
.ToTable($"{baseTableName}_{tableNameSuffix}").HasKey(keySelector); | ||
public ModelBuilder HasBaseTable<TRevision>() where TRevision : TBaseRevision | ||
{ | ||
var visitor = new ReplaceParameterTypeVisitor<TBaseRevision, TRevision>(); | ||
_ = builder.Entity<TRevision>().ToTable(baseTableName) | ||
.HasKey((Expression<Func<TRevision, object?>>) | ||
Check failure on line 37 in c#/crawler/src/Db/Revision/Splitting/RevisionWithSplitting.cs GitHub Actions / build (crawler)
Check failure on line 37 in c#/crawler/src/Db/Revision/Splitting/RevisionWithSplitting.cs GitHub Actions / build (crawler)
|
||
visitor.Visit(keySelector)); | ||
return this; | ||
} | ||
|
||
public ModelBuilder SplitToTable<TRevisionWithSplitting>(string tableNameSuffix) | ||
where TRevisionWithSplitting : RevisionWithSplitting<TBaseRevision> | ||
{ | ||
var visitor = new ReplaceParameterTypeVisitor<TBaseRevision, TRevisionWithSplitting>(); | ||
_ = builder.Entity<TRevisionWithSplitting>() | ||
.Ignore(e => e.NullFieldsBitMask) | ||
.ToTable($"{baseTableName}_{tableNameSuffix}") | ||
.HasKey((Expression<Func<TRevisionWithSplitting, object?>>) | ||
Check failure on line 49 in c#/crawler/src/Db/Revision/Splitting/RevisionWithSplitting.cs GitHub Actions / build (crawler)
Check failure on line 49 in c#/crawler/src/Db/Revision/Splitting/RevisionWithSplitting.cs GitHub Actions / build (crawler)
|
||
visitor.Visit(keySelector)); | ||
return this; | ||
} | ||
|
||
/// <see>https://stackoverflow.com/questions/38316519/replace-parameter-type-in-lambda-expression/38345590#38345590</see> | ||
private sealed class ReplaceParameterTypeVisitor<TSource, TTarget> : ExpressionVisitor | ||
{ | ||
private ReadOnlyCollection<ParameterExpression>? _parameters; | ||
|
||
protected override Expression VisitParameter(ParameterExpression node) => | ||
_parameters?.FirstOrDefault(p => p.Name == node.Name) ?? | ||
(node.Type == typeof(TSource) ? Expression.Parameter(typeof(TTarget), node.Name) : node); | ||
|
||
protected override Expression VisitLambda<T>(Expression<T> node) | ||
{ | ||
_parameters = VisitAndConvert(node.Parameters, nameof(VisitLambda)); | ||
return Expression.Lambda(Visit(node.Body), _parameters); | ||
} | ||
|
||
protected override Expression VisitMember(MemberExpression node) => | ||
node.Member.DeclaringType == typeof(TSource) | ||
? Expression.Property(Visit(node.Expression)!, node.Member.Name) | ||
: base.VisitMember(node); | ||
} | ||
} | ||
} |