< Summary

Information
Class: Elsa.Persistence.EFCore.EntityHandlers.SetTenantIdFilter
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/EntityHandlers/SetTenantIdFilter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 44
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Handle(...)0%620%
CreateTenantFilterExpression(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/EntityHandlers/SetTenantIdFilter.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Elsa.Common.Entities;
 3using Microsoft.EntityFrameworkCore;
 4using Microsoft.EntityFrameworkCore.Metadata;
 5
 6namespace Elsa.Persistence.EFCore.EntityHandlers;
 7
 8/// <summary>
 9/// Represents a class that applies a filter to set the TenantId for entities.
 10/// </summary>
 11public class SetTenantIdFilter : IEntityModelCreatingHandler
 12{
 13    /// <inheritdoc />
 14    public void Handle(ElsaDbContextBase dbContext, ModelBuilder modelBuilder, IMutableEntityType entityType)
 15    {
 016        if (!typeof(Entity).IsAssignableFrom(entityType.ClrType))
 017            return;
 18
 019        modelBuilder
 020            .Entity(entityType.ClrType)
 021            .HasQueryFilter(CreateTenantFilterExpression(dbContext, entityType.ClrType));
 022    }
 23
 24    private LambdaExpression CreateTenantFilterExpression(ElsaDbContextBase dbContext, Type clrType)
 25    {
 026        var parameter = Expression.Parameter(clrType, "e");
 27
 28        // e => EF.Property<string>(e, "TenantId") == this.TenantId
 029        var tenantIdProperty = Expression.Call(
 030            typeof(EF),
 031            nameof(EF.Property),
 032            [typeof(string)],
 033            parameter,
 034            Expression.Constant("TenantId"));
 35
 036        var tenantIdOnContext = Expression.Property(
 037            Expression.Constant(dbContext),
 038            nameof(ElsaDbContextBase.TenantId));
 39
 040        var body = Expression.Equal(tenantIdProperty, tenantIdOnContext);
 41
 042        return Expression.Lambda(body, parameter);
 43    }
 44}