Skip to content

Commit

Permalink
Adding configuration binder extension
Browse files Browse the repository at this point in the history
  • Loading branch information
wAsnk committed Aug 30, 2024
1 parent 9a3a6f0 commit 16fca23
Showing 1 changed file with 25 additions and 0 deletions.
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;
}
}

0 comments on commit 16fca23

Please sign in to comment.