-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathConfigurationExtensions.cs
133 lines (120 loc) · 4.99 KB
/
ConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//-----------------------------------------------------------------------
// <copyright file="BlazorConfigurationExtensions.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>Implement extension methods for .NET Core configuration</summary>
//-----------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
using Csla.Blazor;
using Csla.Blazor.State;
using Csla.Core;
using Csla.State;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Csla.Configuration
{
/// <summary>
/// Implement extension methods for .NET Core configuration
/// </summary>
public static class BlazorConfigurationExtensions
{
/// <summary>
/// Configures services to provide CSLA Blazor server support
/// </summary>
/// <param name="config">CslaOptions instance</param>
public static CslaOptions AddServerSideBlazor(this CslaOptions config)
{
return AddServerSideBlazor(config, null);
}
/// <summary>
/// Configures services to provide CSLA Blazor server support
/// </summary>
/// <param name="config">CslaOptions instance</param>
/// <param name="options">Options object</param>
public static CslaOptions AddServerSideBlazor(this CslaOptions config, Action<BlazorServerConfigurationOptions> options)
{
var blazorOptions = new BlazorServerConfigurationOptions();
options?.Invoke(blazorOptions);
config.BindingOptions.PropertyChangedMode = ApplicationContext.PropertyChangedModes.Windows;
// set context manager
string managerTypeName;
if (blazorOptions.UseInMemoryApplicationContextManager)
managerTypeName = "Csla.AspNetCore.Blazor.ApplicationContextManagerInMemory,Csla.AspNetCore";
else
managerTypeName = "Csla.AspNetCore.Blazor.ApplicationContextManagerBlazor,Csla.AspNetCore";
var managerType = Type.GetType(managerTypeName);
if (managerType is null)
throw new TypeLoadException(managerTypeName);
config.Services.AddScoped(typeof(IContextManager), managerType);
if (blazorOptions.UseInMemoryApplicationContextManager)
{
// do not use any Blazor state management
config.Services.AddSingleton<ISessionManager, NoOpSessionManager>();
}
else
{
// use Blazor state management
config.Services.AddTransient(typeof(ISessionIdManager), blazorOptions.SessionIdManagerType);
config.Services.AddSingleton(typeof(ISessionManager), blazorOptions.SessionManagerType);
config.Services.TryAddTransient(typeof(ISessionStore), blazorOptions.SessionStoreType);
config.Services.AddTransient<StateManager>();
}
// use Blazor viewmodel
config.Services.TryAddTransient(typeof(ViewModel<>), typeof(ViewModel<>));
if (blazorOptions.UseCslaPermissionsPolicy)
{
config.Services.AddTransient<IAuthorizationPolicyProvider, CslaPermissionsPolicyProvider>();
config.Services.AddTransient<IAuthorizationHandler, CslaPermissionsHandler>();
}
return config;
}
}
/// <summary>
/// Options for Blazor server-rendered and server-interactive.
/// </summary>
public class BlazorServerConfigurationOptions
{
/// <summary>
/// Gets or sets a value indicating whether the app
/// should be configured to use CSLA permissions
/// policies (default = true).
/// </summary>
public bool UseCslaPermissionsPolicy { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to use a
/// scoped DI container to manage the ApplicationContext;
/// false to use the Blazor 8 state management subsystem.
/// </summary>
public bool UseInMemoryApplicationContextManager { get; set; } = true;
/// <summary>
/// Gets or sets the type of the ISessionManager service.
/// </summary>
public Type SessionManagerType { get; set; } = Type.GetType("Csla.Blazor.State.SessionManager, Csla.AspNetCore", true);
/// <summary>
/// Gets or sets the type of the ISessionIdManager service.
/// </summary>
public Type SessionIdManagerType { get; set; } = Type.GetType("Csla.Blazor.State.SessionIdManager, Csla.AspNetCore", true);
/// <summary>
/// Gets or sets the type of the SessionStore.
/// </summary>
#if NET8_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
internal Type SessionStoreType { get; set; } = typeof(DefaultSessionStore);
/// <summary>
/// Sets the type of the SessionStore.
/// </summary>
/// <typeparam name="T"></typeparam>
public BlazorServerConfigurationOptions RegisterSessionStore<
#if NET8_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>() where T: ISessionStore
{
SessionStoreType = typeof(T);
return this;
}
}
}