-
Notifications
You must be signed in to change notification settings - Fork 0
Remove runtime generic construction from module hosting path #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
65 changes: 65 additions & 0 deletions
65
src/Hosuto.Hosting/Modules/Hosting/Internal/ModuleRegistration.cs
This file contains hidden or 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,65 @@ | ||
| using System; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Dbosoft.Hosuto.Modules.Hosting.Internal | ||
| { | ||
| /// <summary> | ||
| /// Non-generic handle for a registered module. Concrete instances are | ||
| /// <see cref="ModuleRegistration{TModule}"/>, which keeps the module type as a static generic | ||
| /// parameter so all closed generic instantiations (module host, hosted service, module type) are | ||
| /// visible to the (AOT) compiler instead of being reconstructed at runtime via reflection. | ||
| /// </summary> | ||
| internal interface IModuleRegistration | ||
| { | ||
| Type ModuleType { get; } | ||
|
|
||
| ModuleHostingOptions Options { get; } | ||
|
|
||
| void RegisterModule(IServiceCollection services, IModuleHostServiceProviderFactory serviceProviderFactory); | ||
|
|
||
| void RegisterHost(IServiceCollection services, IServiceProvider frameworkServices); | ||
|
|
||
| void Bootstrap(IServiceProvider moduleHostServices, IServiceProvider frameworkServices); | ||
| } | ||
|
|
||
| internal sealed class ModuleRegistration<TModule> : IModuleRegistration where TModule : class | ||
| { | ||
| public ModuleRegistration(ModuleHostingOptions options) | ||
| { | ||
| Options = options; | ||
| } | ||
|
|
||
| public Type ModuleType => typeof(TModule); | ||
|
|
||
| public ModuleHostingOptions Options { get; } | ||
|
|
||
| public void RegisterModule(IServiceCollection services, IModuleHostServiceProviderFactory serviceProviderFactory) | ||
| { | ||
| if (serviceProviderFactory != null) | ||
| { | ||
| serviceProviderFactory.ConfigureModule(typeof(TModule), Options.ModuleFactory); | ||
| return; | ||
| } | ||
|
|
||
| if (Options.ModuleFactory == null) | ||
| services.AddSingleton<TModule>(); | ||
| else | ||
| services.AddSingleton(sp => (TModule)Options.ModuleFactory(sp)); | ||
| } | ||
|
|
||
| public void RegisterHost(IServiceCollection services, IServiceProvider frameworkServices) | ||
| { | ||
| var internalHost = frameworkServices.GetRequiredService<IModuleHost<TModule>>(); | ||
|
|
||
| services.AddSingleton<Dbosoft.Hosuto.Modules.Hosting.IModuleHost<TModule>>(_ => internalHost); | ||
| services.AddTransient<IHostedService, ModulesHostService<TModule>>(); | ||
| } | ||
|
|
||
| public void Bootstrap(IServiceProvider moduleHostServices, IServiceProvider frameworkServices) | ||
| { | ||
| frameworkServices.GetRequiredService<IModuleHost<TModule>>() | ||
| .Bootstrap(moduleHostServices, Options); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing behavior, not changed by this PR — the diff here only swaps the adapter construction, not this delegate (same pattern in
UseSimpleInjector/ConfigureServices). In practice the container/options filters mutate the passed instance rather than substituting a different one, soc== the capturedcontainer. Leaving it out of scope of this no-behavior-change refactor; can be cleaned up separately.