From 05568b1551bc2806903a01b375787363dda6b9fb Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 30 Sep 2025 18:36:15 +0200 Subject: [PATCH] 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. --- .../DbRepository.cs | 55 ++++++++++++++++--- .../DependencyInjection.cs | 49 ++++++++++++++--- .../Factory/DbRepositoryFactory.cs | 16 +++++- .../Factory/DbSetFactory.cs | 16 +++++- 4 files changed, 114 insertions(+), 22 deletions(-) diff --git a/DigitalData.Core.Infrastructure/DbRepository.cs b/DigitalData.Core.Infrastructure/DbRepository.cs index ddf5aef..e79a658 100644 --- a/DigitalData.Core.Infrastructure/DbRepository.cs +++ b/DigitalData.Core.Infrastructure/DbRepository.cs @@ -4,8 +4,20 @@ using DigitalData.Core.Abstractions.Interfaces; using DigitalData.Core.Infrastructure.Factory; using Microsoft.EntityFrameworkCore; using System.Linq.Expressions; - -namespace DigitalData.Core.Infrastructure; +#if NETFRAMEWORK +using System.Collections.Generic; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +#endif + +namespace DigitalData.Core.Infrastructure +#if NET + ; +#elif NETFRAMEWORK + { +#endif public class DbRepository : IRepository where TDbContext : DbContext where TEntity : class { @@ -13,9 +25,17 @@ public class DbRepository : IRepository where TDbC protected internal readonly DbSet Entities; - public IMapper? Mapper { get; } - - public DbRepository(TDbContext context, DbSetFactory factory, IMapper? mapper = null) + public IMapper +#if NET + ? +#endif + Mapper { get; } + + public DbRepository(TDbContext context, DbSetFactory factory, IMapper +#if NET + ? +#endif + mapper = null) { Context = context; Entities = factory.Create(context); @@ -37,10 +57,19 @@ public class DbRepository : IRepository where TDbC return entities; } - public virtual Task CreateAsync(TDto dto, CancellationToken cancel = default) => CreateAsync(Mapper!.Map(dto), cancel); + public virtual Task CreateAsync(TDto dto, CancellationToken cancel = default) + => CreateAsync(Mapper +#if NET + ! +#endif + .Map(dto), cancel); public virtual Task> CreateAsync(IEnumerable dtos, CancellationToken cancel = default) - => CreateAsync(Mapper!.Map>(dtos), cancel); + => CreateAsync(Mapper +#if NET + ! +#endif + .Map>(dtos), cancel); #endregion Create #region Read @@ -60,7 +89,11 @@ public class DbRepository : IRepository 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]); } @@ -103,4 +136,8 @@ public class DbRepository : IRepository } public IRepository Entity() where TEntity : IEntity => _factory.Get(); -} \ No newline at end of file +} + +#if NETFRAMEWORK + } +#endif \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/DependencyInjection.cs b/DigitalData.Core.Infrastructure/DependencyInjection.cs index e3ab1c6..8440e58 100644 --- a/DigitalData.Core.Infrastructure/DependencyInjection.cs +++ b/DigitalData.Core.Infrastructure/DependencyInjection.cs @@ -4,8 +4,18 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; - -namespace DigitalData.Core.Infrastructure; +#if NETFRAMEWORK +using System.Collections.Generic; +using System; +using System.Linq; +#endif + +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> RegsFromAssembly = new(); + private readonly Queue> RegsFromAssembly = new Queue>(); // 2. register entities (can overwrite) - private readonly Queue> RegsEntity = new(); + private readonly Queue> RegsEntity = new Queue>(); // 3. register db set factories (can overwrite) - private readonly Queue> RegsDbSetFactory = new(); + private readonly Queue> RegsDbSetFactory = new Queue>(); 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(Func>? dbSetFactory = null) + public void RegisterEntity(Func> +#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(this IServiceCollection services, Func>? create = null) + internal static IServiceCollection AddDbSetFactory(this IServiceCollection services, Func> +#if NET + ? +#endif + create = null) where TDbContext : DbContext where TEntity : class { +#if NET create ??= ctx => ctx.Set(); +#elif NETFRAMEWORK + if(create is null) + create = ctx => ctx.Set(); +#endif services.AddSingleton(_ => new DbSetFactory(create)); return services; } -} \ No newline at end of file +} + +#if NETFRAMEWORK + } +#endif \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs b/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs index b2c44aa..9cd19e7 100644 --- a/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs +++ b/DigitalData.Core.Infrastructure/Factory/DbRepositoryFactory.cs @@ -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 { @@ -13,4 +21,8 @@ public class DbRepositoryFactory : IRepositoryFactory } public IRepository Get() => _provider.GetRequiredService>(); -} \ No newline at end of file +} + +#if NETFRAMEWORK + } +#endif \ No newline at end of file diff --git a/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs b/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs index 5e518e1..697ecba 100644 --- a/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs +++ b/DigitalData.Core.Infrastructure/Factory/DbSetFactory.cs @@ -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 where TDbContext : DbContext where TEntity : class { @@ -10,4 +18,8 @@ public class DbSetFactory where TDbContext : DbContext where { Create = create; } -} \ No newline at end of file +} + +#if NETFRAMEWORK + } +#endif \ No newline at end of file