-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Back-port to 1.13] Expose configuration to customizations (#378)
* Expose IConfiguration in ViewModelCompositionOptions (#374) * Expose IConfiguration in ViewModelCompositionOptions * Throw if configuration is not set * Approved API * Tests * # Customizing ViewModel Composition options from dependent assemblies * Link to Customizing ViewModel Composition options from dependent assemblies (cherry picked from commit 8dd8e73) * Add directives required to compile with .NET Standard
- Loading branch information
1 parent
77d9aaa
commit 1e2cf78
Showing
8 changed files
with
225 additions
and
14 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,7 @@ | ||
# Customizing ViewModel Composition options from dependent assemblies | ||
|
||
Assemblies containing types participating in the composition process can customize the current application `ViewModelCompositionOptions` by defining a type that implements the `IViewModelCompositionOptionsCustomization` interface. At runtime, when the application starts, types implementing the `IViewModelCompositionOptionsCustomization` will be instantiated and the `Customize` method will be invoked. | ||
|
||
> Note: Types implementing `IViewModelCompositionOptionsCustomization` are not managed by IoC container. Dependency injection is not available. | ||
`ViewModelCompositionOptions` offers the ability to access the application `IConfiguration` insance. By default the `ViewModelCompositionOptions.Configuration` property is null. If accessed it throws an `ArgumentException`. To enable configuration support, pass the `IConfiguration` instance when configuring ServiceComposer via the `AddViewModelComposition` extension method. |
15 changes: 15 additions & 0 deletions
15
src/ServiceComposer.AspNetCore.Endpoints.Tests/Utils/IsNestedTypeOfExtension.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,15 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace ServiceComposer.AspNetCore.Endpoints.Tests; | ||
|
||
public static class IsNestedTypeOfExtension | ||
{ | ||
public static bool IsNestedTypeOf<T>(this Type type) where T : class | ||
{ | ||
return type.IsNested | ||
&& typeof(T).GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public) | ||
.Contains(type); | ||
} | ||
} |
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
20 changes: 19 additions & 1 deletion
20
src/ServiceComposer.AspNetCore/ServiceCollectionExtensions.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 |
---|---|---|
@@ -1,21 +1,39 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
#if NETCOREAPP3_1 || NET5_0_OR_GREATER | ||
using Microsoft.Extensions.Configuration; | ||
#endif | ||
|
||
namespace ServiceComposer.AspNetCore | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
#if NETCOREAPP3_1 || NET5_0_OR_GREATER | ||
public static void AddViewModelComposition(this IServiceCollection services, IConfiguration configuration = null) | ||
{ | ||
AddViewModelComposition(services, null, configuration); | ||
} | ||
|
||
public static void AddViewModelComposition(this IServiceCollection services, Action<ViewModelCompositionOptions> config, IConfiguration configuration = null) | ||
{ | ||
var options = new ViewModelCompositionOptions(services, configuration); | ||
config?.Invoke(options); | ||
|
||
options.InitializeServiceCollection(); | ||
} | ||
#else | ||
public static void AddViewModelComposition(this IServiceCollection services) | ||
{ | ||
AddViewModelComposition(services, null); | ||
} | ||
|
||
public static void AddViewModelComposition(this IServiceCollection services, Action<ViewModelCompositionOptions> config) | ||
{ | ||
var options = new ViewModelCompositionOptions(services); | ||
config?.Invoke(options); | ||
|
||
options.InitializeServiceCollection(); | ||
} | ||
#endif | ||
} | ||
} |
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,29 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace TestClassLibraryWithHandlers; | ||
|
||
public class FakeConfig : IConfiguration | ||
{ | ||
public IEnumerable<IConfigurationSection> GetChildren() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public IChangeToken GetReloadToken() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public IConfigurationSection GetSection(string key) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
|
||
public string this[string key] | ||
{ | ||
get => throw new System.NotImplementedException(); | ||
set => throw new System.NotImplementedException(); | ||
} | ||
} |