refactor: replace Static service accessor with Factory implementation
- Replaced `Static` class with `Factory` class to unify service collection and provider handling. - Introduced `Factory.Shared` singleton for centralized access to `IServiceProvider`. - Updated generic repository access to use `Factory<TEntity>` instead of `Static<TEntity>`. - Simplified lazy initialization of the service provider. - Maintains compatibility with .NET Framework and .NET conditional compilation.
This commit is contained in:
parent
b8751379c5
commit
568f5990b2
49
DigitalData.Core.Abstraction.Application/Factory.cs
Normal file
49
DigitalData.Core.Abstraction.Application/Factory.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#if NETFRAMEWORK
|
||||||
|
using System;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Abstraction.Application
|
||||||
|
#if NET
|
||||||
|
;
|
||||||
|
#elif NETFRAMEWORK
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
public interface IFactory : IServiceCollection, IServiceProvider
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Factory : ServiceCollection, IFactory, IServiceCollection, IServiceProvider
|
||||||
|
{
|
||||||
|
private readonly Lazy<IServiceProvider> _rootProvider;
|
||||||
|
|
||||||
|
private Factory()
|
||||||
|
{
|
||||||
|
_rootProvider = new Lazy<IServiceProvider>(this.BuildServiceProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
public object
|
||||||
|
#if NET
|
||||||
|
?
|
||||||
|
#endif
|
||||||
|
GetService(Type serviceType)
|
||||||
|
{
|
||||||
|
return _rootProvider.Value.GetService(serviceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRepository Repository => _rootProvider.Value.GetRequiredService<IRepository>();
|
||||||
|
|
||||||
|
public static readonly IFactory Shared = new Factory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Factory<TEntity>
|
||||||
|
{
|
||||||
|
public static IRepository<TEntity> Repository() => Factory.Shared.GetRequiredService<IRepository<TEntity>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if NETFRAMEWORK
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -1,37 +0,0 @@
|
|||||||
#if NETFRAMEWORK
|
|
||||||
using System;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstraction.Application
|
|
||||||
#if NET
|
|
||||||
;
|
|
||||||
#elif NETFRAMEWORK
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public static class Static
|
|
||||||
{
|
|
||||||
private readonly static Lazy<IServiceCollection> LazyServices = new Lazy<IServiceCollection>(() => new ServiceCollection());
|
|
||||||
|
|
||||||
public static IServiceCollection Services => LazyProvider.IsValueCreated
|
|
||||||
? LazyServices.Value
|
|
||||||
: throw new InvalidOperationException("Services cannot be accessed after the Provider has been created.");
|
|
||||||
|
|
||||||
private static readonly Lazy<IServiceProvider> LazyProvider = new Lazy<IServiceProvider>(Services.BuildServiceProvider);
|
|
||||||
|
|
||||||
public static IServiceProvider Provider => LazyProvider.Value;
|
|
||||||
|
|
||||||
public static IRepository Repository => Provider.GetRequiredService<IRepository>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Static<TEntity>
|
|
||||||
{
|
|
||||||
public static IRepository<TEntity> Repository() => Static.Provider.GetRequiredService<IRepository<TEntity>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if NETFRAMEWORK
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@ -55,6 +55,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Abstractio
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Application", "Application", "{E18417C2-D9F5-437A-9ED5-473DD6260066}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Application", "Application", "{E18417C2-D9F5-437A-9ED5-473DD6260066}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Abstraction.Repository", "DigitalData.Core.Abstraction.Repository\DigitalData.Core.Abstraction.Repository.csproj", "{07E36C52-C402-43D7-B8C5-0626D2B3E388}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -128,6 +130,10 @@ Global
|
|||||||
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Debug|Any CPU.Build.0 = Release|Any CPU
|
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Debug|Any CPU.Build.0 = Release|Any CPU
|
||||||
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{420C35A7-0EDE-4E2E-8500-484B57B0367A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{07E36C52-C402-43D7-B8C5-0626D2B3E388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{07E36C52-C402-43D7-B8C5-0626D2B3E388}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{07E36C52-C402-43D7-B8C5-0626D2B3E388}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{07E36C52-C402-43D7-B8C5-0626D2B3E388}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -154,6 +160,7 @@ Global
|
|||||||
{8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
{8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||||
{420C35A7-0EDE-4E2E-8500-484B57B0367A} = {E18417C2-D9F5-437A-9ED5-473DD6260066}
|
{420C35A7-0EDE-4E2E-8500-484B57B0367A} = {E18417C2-D9F5-437A-9ED5-473DD6260066}
|
||||||
{E18417C2-D9F5-437A-9ED5-473DD6260066} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
{E18417C2-D9F5-437A-9ED5-473DD6260066} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||||
|
{07E36C52-C402-43D7-B8C5-0626D2B3E388} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}
|
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user