forked from dnnsoftware/Dnn.Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add singleton IScopeAccessor service as an abstraction over HttpContext.
- Loading branch information
1 parent
c0b727b
commit 14190aa
Showing
10 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DotNetNuke.Web.Common | ||
{ | ||
public class LazyServiceProvider : IServiceProvider | ||
{ | ||
private IServiceProvider _serviceProvider; | ||
|
||
public object GetService(Type serviceType) | ||
{ | ||
if (_serviceProvider is null) | ||
throw new Exception("Cannot resolve services until the service provider is built."); | ||
|
||
return _serviceProvider.GetService(serviceType); | ||
} | ||
|
||
internal void SetProvider(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
} | ||
} |
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
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
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
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
33 changes: 33 additions & 0 deletions
33
DNN Platform/Library/Services/DependencyInjection/IScopeAccessor.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,33 @@ | ||
using DotNetNuke.Common.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
|
||
namespace DotNetNuke.Services.DependencyInjection { | ||
public interface IScopeAccessor { | ||
IServiceScope GetScope(); | ||
} | ||
|
||
public class ScopeAccessor : IScopeAccessor { | ||
private static Func<IDictionary> fallbackGetContextItems = () => HttpContext.Current?.Items; | ||
|
||
private Func<IDictionary> _getContextItems; | ||
|
||
public ScopeAccessor() { | ||
_getContextItems = fallbackGetContextItems; | ||
} | ||
|
||
public IServiceScope GetScope() { | ||
return HttpContextDependencyInjectionExtensions.GetScope(_getContextItems()); | ||
} | ||
|
||
internal void SetContextItemsFunc(Func<IDictionary> getContextItems) { | ||
_getContextItems = getContextItems ?? fallbackGetContextItems; | ||
} | ||
} | ||
} |