Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit 864bfac

Browse files
committed
compile time warnings fixed
ICanFlush vs. IPersistenceSession
1 parent ac187ca commit 864bfac

File tree

22 files changed

+118
-68
lines changed

22 files changed

+118
-68
lines changed

src/abstractions/Backend.Fx/Environment/MultiTenancy/TenantActivated.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public class TenantActivated : TenantStatusChanged
44
{
5-
public TenantActivated(int tenantId, string name, string description, bool isDemoTenant)
6-
: base(tenantId, name, description, isDemoTenant)
5+
public TenantActivated(string name, string description, bool isDemoTenant)
6+
: base(name, description, isDemoTenant)
77
{
88
}
99
}

src/abstractions/Backend.Fx/Environment/MultiTenancy/TenantDeactivated.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
public class TenantDeactivated : TenantStatusChanged
44
{
5-
public TenantDeactivated(int tenantId, string name, string description, bool isDemoTenant)
6-
: base(tenantId, name, description, isDemoTenant)
5+
public TenantDeactivated(string name, string description, bool isDemoTenant)
6+
: base(name, description, isDemoTenant)
77
{
88
}
99
}

src/abstractions/Backend.Fx/Environment/MultiTenancy/TenantService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public TenantId CreateTenant(string name, string description, bool isDemonstrati
6464
var tenant = new Tenant(name, description, isDemonstrationTenant) {Configuration = configuration};
6565
_tenantRepository.SaveTenant(tenant);
6666
var tenantId = new TenantId(tenant.Id);
67-
_messageBus.Publish(new TenantActivated(tenant.Id, tenant.Name, tenant.Description, tenant.IsDemoTenant));
67+
_messageBus.Publish(new TenantActivated(tenant.Name, tenant.Description, tenant.IsDemoTenant));
6868
return tenantId;
6969
}
7070

@@ -74,7 +74,7 @@ public void ActivateTenant(TenantId tenantId)
7474
Tenant tenant = _tenantRepository.GetTenant(tenantId);
7575
tenant.State = TenantState.Active;
7676
_tenantRepository.SaveTenant(tenant);
77-
_messageBus.Publish(new TenantActivated(tenant.Id, tenant.Name, tenant.Description, tenant.IsDemoTenant));
77+
_messageBus.Publish(new TenantActivated(tenant.Name, tenant.Description, tenant.IsDemoTenant));
7878
}
7979

8080
public void DeactivateTenant(TenantId tenantId)
@@ -83,7 +83,7 @@ public void DeactivateTenant(TenantId tenantId)
8383
Tenant tenant = _tenantRepository.GetTenant(tenantId);
8484
tenant.State = TenantState.Inactive;
8585
_tenantRepository.SaveTenant(tenant);
86-
_messageBus.Publish(new TenantDeactivated(tenant.Id, tenant.Name, tenant.Description, tenant.IsDemoTenant));
86+
_messageBus.Publish(new TenantDeactivated(tenant.Name, tenant.Description, tenant.IsDemoTenant));
8787
}
8888

8989
public void DeleteTenant(TenantId tenantId)

src/abstractions/Backend.Fx/Environment/MultiTenancy/TenantStatusChanged.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Backend.Fx.Environment.MultiTenancy
44
{
55
public abstract class TenantStatusChanged : IntegrationEvent
66
{
7-
protected TenantStatusChanged(int tenantId, string name, string description, bool isDemoTenant) : base(tenantId)
7+
protected TenantStatusChanged(string name, string description, bool isDemoTenant)
88
{
99
Name = name;
1010
Description = description;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Security.Principal;
2+
using Backend.Fx.Environment.DateAndTime;
3+
using Backend.Fx.Patterns.DependencyInjection;
4+
5+
namespace Backend.Fx.Environment.Persistence
6+
{
7+
public interface IPersistenceSession : ICanFlush
8+
{
9+
ICurrentTHolder<IIdentity> IdentityHolder { get; }
10+
11+
IClock Clock { get; }
12+
13+
void MakeReadonly();
14+
}
15+
}

src/abstractions/Backend.Fx/Patterns/DataGeneration/DataGenerated.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ namespace Backend.Fx.Patterns.DataGeneration
66
/// Will appear on the message bus when the data generation process has been completed
77
/// </summary>
88
public class DataGenerated : IntegrationEvent
9-
{
10-
public DataGenerated(int tenantId) : base(tenantId)
11-
{
12-
}
13-
}
9+
{ }
1410
}

src/abstractions/Backend.Fx/Patterns/DataGeneration/GenerateDataOnBoot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ private void SeedDataForAllActiveTenants()
6161
foreach (TenantId prodTenantId in prodTenantIds)
6262
{
6363
DataGenerationContext.SeedDataForTenant(prodTenantId, false);
64-
_application.MessageBus.Publish(new DataGenerated(prodTenantId.Value));
64+
_application.MessageBus.Publish(new DataGenerated());
6565
}
6666

6767
var demoTenantIds = _tenantIdProvider.GetActiveDemonstrationTenantIds();
6868
foreach (TenantId demoTenantId in demoTenantIds)
6969
{
7070
DataGenerationContext.SeedDataForTenant(demoTenantId, true);
71-
_application.MessageBus.Publish(new DataGenerated(demoTenantId.Value));
71+
_application.MessageBus.Publish(new DataGenerated());
7272
}
7373
}
7474
}

src/abstractions/Backend.Fx/Patterns/DependencyInjection/Pure/ScopedServices.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Backend.Fx.Patterns.DependencyInjection.Pure
2121
/// It is very useful in testing scenarios.</remarks>
2222
public interface IScopedServices : IDisposable
2323
{
24-
ICanFlush CanFlush { get; }
24+
IPersistenceSession PersistenceSession { get; }
2525
AdjustableClock Clock { get; }
2626
IDomainEventAggregator EventAggregator { get; }
2727
IMessageBusScope MessageBusScope { get; }
@@ -34,7 +34,7 @@ public interface IScopedServices : IDisposable
3434
public abstract class ScopedServices : IScopedServices
3535
{
3636
private readonly Assembly[] _domainAssemblies;
37-
private readonly Lazy<ICanFlush> _canFlush;
37+
private readonly Lazy<IPersistenceSession> _persistenceSession;
3838
private bool _doAutoFlush = true;
3939

4040
protected ScopedServices(
@@ -48,10 +48,12 @@ protected ScopedServices(
4848
TenantIdHolder = CurrentTenantIdHolder.Create(tenantId);
4949
IdentityHolder = CurrentIdentityHolder.Create(identity);
5050
CorrelationHolder = new CurrentCorrelationHolder();
51-
_canFlush = new Lazy<ICanFlush>(CreateCanFlush);
51+
_persistenceSession = new Lazy<IPersistenceSession>(() => CreatePersistenceSession());
5252
}
5353

54-
public ICanFlush CanFlush => _canFlush.Value;
54+
public ICanFlush CanFlush => _persistenceSession.Value;
55+
56+
public IPersistenceSession PersistenceSession => _persistenceSession.Value;
5557

5658
public ICurrentTHolder<TenantId> TenantIdHolder { get; }
5759

@@ -96,7 +98,7 @@ public T GetRandom<T>() where T : AggregateRoot
9698

9799
public void Flush()
98100
{
99-
CanFlush.Flush();
101+
PersistenceSession.Flush();
100102
}
101103

102104
public void Complete()
@@ -150,9 +152,9 @@ protected object GetAggregateAuthorization(ICurrentTHolder<IIdentity> identityHo
150152
continue;
151153
}
152154

153-
constructorParameterTypes[i] = ProvideInstance(constructorParameterTypes[i].ParameterType);
155+
constructorParameters[i] = ProvideInstance(constructorParameterTypes[i].ParameterType);
154156

155-
if (constructorParameterTypes[i] == null)
157+
if (constructorParameters[i] == null)
156158
{
157159
throw new InvalidOperationException($"No implementation for {constructorParameterTypes[i].ParameterType.Name} provided");
158160
}
@@ -170,6 +172,6 @@ protected virtual void Dispose(bool disposing)
170172
{
171173
}
172174

173-
protected abstract ICanFlush CreateCanFlush();
175+
protected abstract IPersistenceSession CreatePersistenceSession();
174176
}
175177
}

src/environments/Backend.Fx.AspNetCore/BackendFxApplicationStartup.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Backend.Fx.Patterns.DependencyInjection;
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Mvc.Controllers;
8+
using Microsoft.AspNetCore.Mvc.ViewComponents;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Hosting;
1011

@@ -17,8 +18,8 @@ public static void AddBackendFxApplication<THostedService>(this IServiceCollecti
1718
{
1819
services.AddSingleton<THostedService>();
1920
services.AddSingleton<IHostedService>(provider => provider.GetRequiredService<THostedService>());
20-
services.AddSingleton(provider => provider.GetRequiredService<THostedService>().Application);
2121
services.AddSingleton<IControllerActivator, BackendFxApplicationControllerActivator>();
22+
services.AddSingleton<IViewComponentActivator, BackendFxApplicationViewComponentActivator>();
2223
}
2324

2425
public static void UseBackendFxApplication<THostedService, TTenantMiddleware>(this IApplicationBuilder app)
@@ -28,19 +29,23 @@ public static void UseBackendFxApplication<THostedService, TTenantMiddleware>(th
2829

2930
app.Use(async (context, requestDelegate) =>
3031
{
31-
IBackendFxApplication application = app.ApplicationServices.GetRequiredService<THostedService>().Application;
32-
application.WaitForBoot();
33-
34-
// set the instance provider for the controller activator
35-
context.SetCurrentInstanceProvider(application.CompositionRoot.InstanceProvider);
36-
3732
// the ambient tenant id has been set before by a TenantMiddleware
3833
var tenantId = context.GetTenantId();
3934

4035
// the invoking identity has been set before by an AuthenticationMiddleware
4136
IIdentity actingIdentity = context.User.Identity;
37+
38+
IBackendFxApplication application = app.ApplicationServices.GetRequiredService<THostedService>().Application;
39+
application.WaitForBoot();
40+
41+
await application.AsyncInvoker.InvokeAsync(ip =>
42+
{
43+
// set the instance provider for activators being called inside the requestDelegate (everything related to MVC
44+
// for example, like ControllerActivator and ViewComponentActivator etc.)
45+
context.SetCurrentInstanceProvider(ip);
4246

43-
await application.AsyncInvoker.InvokeAsync(_ => requestDelegate.Invoke(), actingIdentity, tenantId);
47+
return requestDelegate.Invoke();
48+
}, actingIdentity, tenantId);
4449
});
4550
}
4651
}

src/implementations/Backend.Fx.EfCorePersistence/EfFlush.cs renamed to src/implementations/Backend.Fx.EfCorePersistence/EfCorePersistenceSession.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@
1515

1616
namespace Backend.Fx.EfCorePersistence
1717
{
18-
public class EfFlush : ICanFlush
18+
public class EfCorePersistenceSession : IPersistenceSession
1919
{
20-
private static readonly ILogger Logger = LogManager.Create<EfFlush>();
20+
private static readonly ILogger Logger = LogManager.Create<EfCorePersistenceSession>();
21+
private bool _isReadonly;
2122
public DbContext DbContext { get; }
23+
2224
public ICurrentTHolder<IIdentity> IdentityHolder { get; }
2325
public IClock Clock { get; }
26+
27+
public void MakeReadonly()
28+
{
29+
Logger.Debug("Making this DbContext readonly. Changes won't be detected when flushing.");
30+
DbContext.ChangeTracker.AutoDetectChangesEnabled = false;
31+
DbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
32+
_isReadonly = true;
33+
}
2434

25-
public EfFlush(DbContext dbContext, ICurrentTHolder<IIdentity> identityHolder, IClock clock)
35+
public EfCorePersistenceSession(DbContext dbContext, ICurrentTHolder<IIdentity> identityHolder, IClock clock)
2636
{
2737
DbContext = dbContext;
2838
Logger.Debug("Disabling auto detect changes on this DbContext. Changes will be detected explicitly when flushing.");
@@ -33,6 +43,12 @@ public EfFlush(DbContext dbContext, ICurrentTHolder<IIdentity> identityHolder, I
3343

3444
public void Flush()
3545
{
46+
if (_isReadonly)
47+
{
48+
Logger.Info("skipping flush because this instance was marked as readonly");
49+
return;
50+
}
51+
3652
DetectChanges();
3753
UpdateTrackingProperties();
3854
DbContext.TraceChangeTrackerState();

0 commit comments

Comments
 (0)