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:
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
|
||||
Reference in New Issue
Block a user