Add conditional compilation for .NET Framework support
Updated code to support conditional compilation for .NET Framework and .NET. Introduced nullable reference types in `DbRepository.cs` and ensured proper initialization of service registration queues in `DependencyInjection.cs`. Modified `DbRepositoryFactory.cs` and `DbSetFactory.cs` to maintain consistent structure across frameworks. These changes enhance compatibility and improve type safety.
This commit is contained in:
parent
e0ca11ffc0
commit
05568b1551
@ -4,8 +4,20 @@ using DigitalData.Core.Abstractions.Interfaces;
|
||||
using DigitalData.Core.Infrastructure.Factory;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
#if NETFRAMEWORK
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
namespace DigitalData.Core.Infrastructure
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
|
||||
{
|
||||
@ -13,9 +25,17 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
protected internal readonly DbSet<TEntity> Entities;
|
||||
|
||||
public IMapper? Mapper { get; }
|
||||
public IMapper
|
||||
#if NET
|
||||
?
|
||||
#endif
|
||||
Mapper { get; }
|
||||
|
||||
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper? mapper = null)
|
||||
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper
|
||||
#if NET
|
||||
?
|
||||
#endif
|
||||
mapper = null)
|
||||
{
|
||||
Context = context;
|
||||
Entities = factory.Create(context);
|
||||
@ -37,10 +57,19 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
return entities;
|
||||
}
|
||||
|
||||
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default) => CreateAsync(Mapper!.Map<TEntity>(dto), cancel);
|
||||
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default)
|
||||
=> CreateAsync(Mapper
|
||||
#if NET
|
||||
!
|
||||
#endif
|
||||
.Map<TEntity>(dto), cancel);
|
||||
|
||||
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default)
|
||||
=> CreateAsync(Mapper!.Map<IEnumerable<TEntity>>(dtos), cancel);
|
||||
=> CreateAsync(Mapper
|
||||
#if NET
|
||||
!
|
||||
#endif
|
||||
.Map<IEnumerable<TEntity>>(dtos), cancel);
|
||||
#endregion Create
|
||||
|
||||
#region Read
|
||||
@ -60,7 +89,11 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
||||
|
||||
for (int i = entities.Count - 1; i >= 0; i--)
|
||||
{
|
||||
Mapper!.Map(dto, entities[i]);
|
||||
Mapper
|
||||
#if NET
|
||||
!
|
||||
#endif
|
||||
.Map(dto, entities[i]);
|
||||
Entities.Update(entities[i]);
|
||||
}
|
||||
|
||||
@ -104,3 +137,7 @@ public class DbRepository : IRepository
|
||||
|
||||
public IRepository<TEntity> Entity<TEntity>() where TEntity : IEntity => _factory.Get<TEntity>();
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
@ -4,8 +4,18 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
#if NETFRAMEWORK
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Linq;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Infrastructure;
|
||||
namespace DigitalData.Core.Infrastructure
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
@ -28,13 +38,13 @@ public static class DependencyInjection
|
||||
public class RepositoryConfiguration
|
||||
{
|
||||
// 1. register from assembly
|
||||
private readonly Queue<Action<IServiceCollection>> RegsFromAssembly = new();
|
||||
private readonly Queue<Action<IServiceCollection>> RegsFromAssembly = new Queue<Action<IServiceCollection>>();
|
||||
|
||||
// 2. register entities (can overwrite)
|
||||
private readonly Queue<Action<IServiceCollection>> RegsEntity = new();
|
||||
private readonly Queue<Action<IServiceCollection>> RegsEntity = new Queue<Action<IServiceCollection>>();
|
||||
|
||||
// 3. register db set factories (can overwrite)
|
||||
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new();
|
||||
private readonly Queue<Action<IServiceCollection>> RegsDbSetFactory = new Queue<Action<IServiceCollection>>();
|
||||
|
||||
internal void RegisterAllServices(IServiceCollection services)
|
||||
{
|
||||
@ -76,7 +86,11 @@ public static class DependencyInjection
|
||||
.GetMethod(nameof(AddDbSetFactory),
|
||||
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
|
||||
|
||||
var genericMethod = addDbSetFactoryMethod!.MakeGenericMethod(typeof(TDbContext), entityType);
|
||||
var genericMethod = addDbSetFactoryMethod
|
||||
#if NET
|
||||
!
|
||||
#endif
|
||||
.MakeGenericMethod(typeof(TDbContext), entityType);
|
||||
genericMethod.Invoke(null, new [] { services, null });
|
||||
#endregion DbSetFactory
|
||||
}
|
||||
@ -85,7 +99,11 @@ public static class DependencyInjection
|
||||
RegsFromAssembly.Enqueue(reg);
|
||||
}
|
||||
|
||||
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>? dbSetFactory = null)
|
||||
public void RegisterEntity<TDbContext, TEntity>(Func<TDbContext, DbSet<TEntity>>
|
||||
#if NET
|
||||
?
|
||||
#endif
|
||||
dbSetFactory = null)
|
||||
where TDbContext : DbContext
|
||||
where TEntity : class
|
||||
{
|
||||
@ -109,12 +127,25 @@ public static class DependencyInjection
|
||||
queue.Dequeue().Invoke(services);
|
||||
}
|
||||
|
||||
internal static IServiceCollection AddDbSetFactory<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>>? create = null)
|
||||
internal static IServiceCollection AddDbSetFactory<TDbContext, TEntity>(this IServiceCollection services, Func<TDbContext, DbSet<TEntity>>
|
||||
#if NET
|
||||
?
|
||||
#endif
|
||||
create = null)
|
||||
where TDbContext : DbContext
|
||||
where TEntity : class
|
||||
{
|
||||
#if NET
|
||||
create ??= ctx => ctx.Set<TEntity>();
|
||||
#elif NETFRAMEWORK
|
||||
if(create is null)
|
||||
create = ctx => ctx.Set<TEntity>();
|
||||
#endif
|
||||
services.AddSingleton(_ => new DbSetFactory<TDbContext, TEntity>(create));
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
@ -1,7 +1,15 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
#if NETFRAMEWORK
|
||||
using System;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Infrastructure.Factory;
|
||||
namespace DigitalData.Core.Infrastructure.Factory
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public class DbRepositoryFactory : IRepositoryFactory
|
||||
{
|
||||
@ -14,3 +22,7 @@ public class DbRepositoryFactory : IRepositoryFactory
|
||||
|
||||
public IRepository<TEntity> Get<TEntity>() => _provider.GetRequiredService<IRepository<TEntity>>();
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
@ -1,6 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
#if NETFRAMEWORK
|
||||
using System;
|
||||
#endif
|
||||
|
||||
namespace DigitalData.Core.Infrastructure.Factory;
|
||||
namespace DigitalData.Core.Infrastructure.Factory
|
||||
#if NET
|
||||
;
|
||||
#elif NETFRAMEWORK
|
||||
{
|
||||
#endif
|
||||
|
||||
public class DbSetFactory<TDbContext,TEntity> where TDbContext : DbContext where TEntity : class
|
||||
{
|
||||
@ -11,3 +19,7 @@ public class DbSetFactory<TDbContext,TEntity> where TDbContext : DbContext where
|
||||
Create = create;
|
||||
}
|
||||
}
|
||||
|
||||
#if NETFRAMEWORK
|
||||
}
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user