-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding configuration binder extension
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Lombiq.HelpfulLibraries.OrchardCore/Mvc/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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Environment.Shell.Configuration; | ||
|
||
namespace Lombiq.HelpfulLibraries.OrchardCore.Mvc; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Binds a configuration section to a class and configures it in the service collection. | ||
/// </summary> | ||
public static IServiceCollection BindAndConfigureSection<T>( | ||
this IServiceCollection services, | ||
IShellConfiguration shellConfiguration, | ||
string sectionName) | ||
where T : class, new() | ||
{ | ||
var options = new T(); | ||
var configSection = shellConfiguration.GetSection(sectionName); | ||
configSection.Bind(options); | ||
services.Configure<T>(configSection); | ||
|
||
return services; | ||
} | ||
} |