Developer 02 ebf79309d1 Refactor Static class to be generic
Updated the `Static` class in the `DigitalData.Core.Infrastructure` namespace to a generic version `Static<TEntity>`. Removed the original non-generic class and the `GetRepository<TEntity>` method. The new generic class provides a method to retrieve a repository for a specific entity type `TEntity`, enhancing type safety and usability.
2025-09-30 19:58:22 +02:00

37 lines
1.1 KiB
C#

#if NETFRAMEWORK
using System;
#endif
using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.Core.Infrastructure
#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