- 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.
49 lines
1.0 KiB
C#
49 lines
1.0 KiB
C#
#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 |