| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | namespace ZhEaIsNsAaBn.Infrastructure.Specifications; |
| | | 4 | | |
| | | 5 | | /// <summary>Core <see cref="BaseUnitOfWork{TDbContext}"/> that also hands out specification repositories from DI.</summ |
| | | 6 | | /// <typeparam name="TDbContext">The application's context.</typeparam> |
| | | 7 | | public class SpecificationUnitOfWork< TDbContext > : BaseUnitOfWork<TDbContext>, ISpecificationUnitOfWork<TDbContext> |
| | | 8 | | where TDbContext : DbContext |
| | | 9 | | { |
| | | 10 | | private readonly IServiceProvider _serviceProvider; |
| | | 11 | | |
| | | 12 | | |
| | | 13 | | /// <summary>Creates the unit of work.</summary> |
| | | 14 | | /// <param name="serviceProvider">The service provider repositories are resolved from.</param> |
| | | 15 | | /// <param name="dbContextProvider">The context provider (same one the repositories use).</param> |
| | 8 | 16 | | public SpecificationUnitOfWork( IServiceProvider serviceProvider, IDbContextProvider<TDbContext> dbContextProvider ) |
| | 8 | 17 | | => _serviceProvider = serviceProvider; |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public ISpecificationRepository<TEntity, TId> Repository< TEntity, TId >() |
| | | 22 | | where TEntity : class, IBaseEntity<TId> |
| | | 23 | | where TId : struct, IEquatable<TId> |
| | 4 | 24 | | => _serviceProvider.GetService<ISpecificationRepository<TEntity, TId>>() |
| | 4 | 25 | | ?? _serviceProvider.GetService<ISpecificationRepository<TDbContext, TEntity, TId>>() |
| | 4 | 26 | | ?? throw new InvalidOperationException( $"No specification repository is registered for {typeof( TEntity ).Na |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public ISpecificationRepository<TEntity> Repository< TEntity >() |
| | | 30 | | where TEntity : class, IBaseEntity |
| | 4 | 31 | | => _serviceProvider.GetService<ISpecificationRepository<TEntity>>() |
| | 4 | 32 | | ?? _serviceProvider.GetService<ISpecificationIntRepository<TDbContext, TEntity>>() |
| | 4 | 33 | | ?? throw new InvalidOperationException( $"No specification repository is registered for {typeof( TEntity ).Na |
| | | 34 | | } |