This commit introduces two new properties in the `Static` class: `Repository` and `GetRepository<TEntity>()`. The `Repository` property allows retrieval of an `IRepository` instance from the service provider, while `GetRepository<TEntity>()` provides access to a specific `IRepository<TEntity>`. These additions improve the ease of accessing repository instances for data operations.
34 lines
1.0 KiB
C#
34 lines
1.0 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 class Static
|
|
{
|
|
private readonly static Lazy<IServiceCollection> LazyServices = new Lazy<IServiceCollection>(() => new ServiceCollection());
|
|
|
|
public static IServiceCollection Services => LazyProvider.IsValueCreated
|
|
? LazyServices.Value
|
|
: throw new InvalidOperationException("Service collection is not available after the service provider has been built.");
|
|
|
|
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 IRepository<TEntity> GetRepository<TEntity>() => Provider.GetRequiredService<IRepository<TEntity>>();
|
|
}
|
|
|
|
#if NETFRAMEWORK
|
|
}
|
|
#endif |