Skip to content

Commit

Permalink
Enable lazy DI registration
Browse files Browse the repository at this point in the history
  • Loading branch information
bdukes committed May 30, 2023
1 parent 7469c02 commit 8bdee27
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootDirectory>$(MSBuildProjectDirectory)\..\..</RootDirectory>
<DocumentationFile>bin/$(Configuration)/$(TargetFramework)/DotNetNuke.DependencyInjection.xml</DocumentationFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
18 changes: 18 additions & 0 deletions DNN Platform/DotNetNuke.DependencyInjection/LazyWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.DependencyInjection;
using System;
using Microsoft.Extensions.DependencyInjection;

/// <summary>A <see cref="Lazy{T}"/> wrapper which allows requesting <see cref="Lazy{T}"/> via dependency injection and delaying instantiation.</summary>
/// <typeparam name="T">The type of service to conditionally instantiate.</typeparam>
public class LazyWrapper<T> : Lazy<T>
{
/// <summary>Initializes a new instance of the <see cref="LazyWrapper{T}"/> class.</summary>
/// <param name="serviceProvider">The DI container.</param>
public LazyWrapper(IServiceProvider serviceProvider)
: base(serviceProvider.GetService<T>)
{
}
}
3 changes: 3 additions & 0 deletions DNN Platform/Library/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information
namespace DotNetNuke
{
using System;
using DotNetNuke.Abstractions;
using DotNetNuke.Abstractions.Application;
using DotNetNuke.Abstractions.Logging;
Expand All @@ -26,6 +27,8 @@ public class Startup : IDnnStartup
/// <inheritdoc />
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient(typeof(Lazy<>), typeof(LazyWrapper<>));

services.AddSingleton<WebFormsModuleControlFactory>();
services.AddSingleton<Html5ModuleControlFactory>();
services.AddSingleton<ReflectedModuleControlFactory>();
Expand Down

0 comments on commit 8bdee27

Please sign in to comment.