Skip to content

Commit

Permalink
feat: update Templating DependencyInjectionExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Jan 3, 2024
1 parent 98b1701 commit 5ea0f27
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

Console.WriteLine("----------DotNetCoreSample----------");

InvokeHelper.OnInvokeException = ex => ConsoleHelper.ErrorWriteWithColor(ex.ToString(), ConsoleColor.DarkRed);

// ServiceDecoratorTest.MainTest();

// var dataLogger = LogHelper.GetLogger(typeof(DataExtension));
Expand Down
12 changes: 8 additions & 4 deletions src/WeihanLi.Common/Templating/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ public static class DependencyInjectionExtensions
public static IServiceCollection AddTemplating(this IServiceCollection services, Action<TemplateEngineOptions>? optionsConfigure = null)
{
Guard.NotNull(services);
if (services.Any(x => x.ServiceType == typeof(ITemplateEngine)))
throw new InvalidOperationException("Templating services had been registered");

if (optionsConfigure != null)
services.Configure(optionsConfigure);
services.AddOptions().Configure(optionsConfigure);

services.TryAddSingleton<ITemplateParser, DefaultTemplateParser>();
services.TryAddSingleton<ITemplateRenderer, DefaultTemplateRenderer>();
services.TryAddSingleton<ITemplateEngine, TemplateEngine>();

services.TryAddEnumerable(ServiceDescriptor.Singleton<IRenderMiddleware, DefaultRenderMiddleware>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<IRenderMiddleware, EnvRenderMiddleware>());
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IRenderMiddleware), sp =>
services.AddSingleton<IRenderMiddleware>(sp =>
{
var configuration = sp.GetRequiredService<IOptions<TemplateEngineOptions>>().Value.Configuration
var configuration = sp.GetService<IOptions<TemplateEngineOptions>>()?.Value.Configuration
?? sp.GetService<IConfiguration>();
return new ConfigurationRenderMiddleware(configuration);
}));
});

services.TryAddSingleton(sp =>
{
var pipelineBuilder = PipelineBuilder.CreateAsync<TemplateRenderContext>();
Expand Down

0 comments on commit 5ea0f27

Please sign in to comment.