From 9a94b06825c8c0d6042761c439498bdd4c105ca2 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sat, 28 Dec 2019 01:57:47 +0100 Subject: [PATCH 01/46] Fix candidate --- MongoDM.sln | 7 +++ .../Filters/LocalContextFilter.cs | 38 +++++++++++++++ src/MongoDM.Hangfire/MongoDM.Hangfire.csproj | 21 +++++++++ .../ServiceCollectionExtensions.cs | 23 ++++++++++ src/MongoDM.Hangfire/Tasks/TaskRunner.cs | 46 +++++++++++++++++++ src/MongoDM/MongoDM.csproj | 3 +- src/MongoDM/ServiceCollectionExtensions.cs | 12 ++--- src/MongoDM/Tasks/ITaskRunner.cs | 10 ++++ .../Tasks/IUpdateDocDependenciesTask.cs | 18 ++------ .../Tasks/UpdateDocDependenciesTask.cs | 20 +------- .../{LocalContext.cs => AsyncLocalContext.cs} | 9 ++-- .../Utility/AsyncLocalContextAccessor.cs | 31 +++++++++++++ src/MongoDM/Utility/ContextAccessorFacade.cs | 15 +----- src/MongoDM/Utility/DBCache.cs | 1 - src/MongoDM/Utility/DBMaintainer.cs | 12 ++--- .../Utility/HangfireContextAccessor.cs | 20 -------- src/MongoDM/Utility/IAsyncLocalContext.cs | 11 +++++ .../Utility/IAsyncLocalContextAccessor.cs | 13 ++++++ .../Utility/IHangfireContextAccessor.cs | 9 ---- src/MongoDM/Utility/ILocalContextAccessor.cs | 11 ----- src/MongoDM/Utility/ILocalContextFactory.cs | 9 ---- src/MongoDM/Utility/LocalContextAccessor.cs | 20 -------- src/MongoDM/Utility/LocalContextFactory.cs | 18 -------- 23 files changed, 223 insertions(+), 154 deletions(-) create mode 100644 src/MongoDM.Hangfire/Filters/LocalContextFilter.cs create mode 100644 src/MongoDM.Hangfire/MongoDM.Hangfire.csproj create mode 100644 src/MongoDM.Hangfire/ServiceCollectionExtensions.cs create mode 100644 src/MongoDM.Hangfire/Tasks/TaskRunner.cs create mode 100644 src/MongoDM/Tasks/ITaskRunner.cs rename src/MongoDM/Utility/{LocalContext.cs => AsyncLocalContext.cs} (63%) create mode 100644 src/MongoDM/Utility/AsyncLocalContextAccessor.cs delete mode 100644 src/MongoDM/Utility/HangfireContextAccessor.cs create mode 100644 src/MongoDM/Utility/IAsyncLocalContext.cs create mode 100644 src/MongoDM/Utility/IAsyncLocalContextAccessor.cs delete mode 100644 src/MongoDM/Utility/IHangfireContextAccessor.cs delete mode 100644 src/MongoDM/Utility/ILocalContextAccessor.cs delete mode 100644 src/MongoDM/Utility/ILocalContextFactory.cs delete mode 100644 src/MongoDM/Utility/LocalContextAccessor.cs delete mode 100644 src/MongoDM/Utility/LocalContextFactory.cs diff --git a/MongoDM.sln b/MongoDM.sln index cbf778ca..2d8164ed 100644 --- a/MongoDM.sln +++ b/MongoDM.sln @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM", "src\MongoDM\Mong EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM.Tests", "test\MongoDM.Tests\MongoDM.Tests.csproj", "{82789357-2EC1-4159-B3F6-0F830ABAFF4C}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM.Hangfire", "src\MongoDM.Hangfire\MongoDM.Hangfire.csproj", "{10897D0D-4898-4A4D-8D1E-B2435E93D9A1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -25,6 +27,10 @@ Global {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Debug|Any CPU.Build.0 = Debug|Any CPU {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Release|Any CPU.ActiveCfg = Release|Any CPU {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Release|Any CPU.Build.0 = Release|Any CPU + {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -32,6 +38,7 @@ Global GlobalSection(NestedProjects) = preSolution {AA9CE7D3-169F-4725-970E-4CE900E91EDD} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {82789357-2EC1-4159-B3F6-0F830ABAFF4C} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} + {10897D0D-4898-4A4D-8D1E-B2435E93D9A1} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs b/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs new file mode 100644 index 00000000..e09d3b93 --- /dev/null +++ b/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs @@ -0,0 +1,38 @@ +using Digicando.MongoDM.Utility; +using Hangfire.Server; +using System.Collections.Generic; + +namespace Digicando.MongoDM.HF.Filters +{ + class LocalContextFilter : IServerFilter + { + // Fields. + private readonly IAsyncLocalContextAccessor localContextAccessor; + private readonly Dictionary contexts = new Dictionary(); + + // Constructors. + public LocalContextFilter(IAsyncLocalContextAccessor localContextAccessor) + { + this.localContextAccessor = localContextAccessor; + } + + // Properties. + public void OnPerforming(PerformingContext filterContext) + { + lock (contexts) + { + contexts.Add(filterContext, localContextAccessor.GetNewLocalContext()); + } + } + + public void OnPerformed(PerformedContext filterContext) + { + lock (contexts) + { + var context = contexts[filterContext]; + contexts.Remove(filterContext); + context.Dispose(); + } + } + } +} diff --git a/src/MongoDM.Hangfire/MongoDM.Hangfire.csproj b/src/MongoDM.Hangfire/MongoDM.Hangfire.csproj new file mode 100644 index 00000000..a694a77d --- /dev/null +++ b/src/MongoDM.Hangfire/MongoDM.Hangfire.csproj @@ -0,0 +1,21 @@ + + + + netstandard2.0 + true + Digicando.MongoDM.HF + true + 0.19.0-dev + Digicando Srl + Linker for use MongoDB with Hangfire + + + + + + + + + + + diff --git a/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs b/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..296a2e8e --- /dev/null +++ b/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs @@ -0,0 +1,23 @@ +using Digicando.MongoDM.HF.Filters; +using Digicando.MongoDM.HF.Tasks; +using Digicando.MongoDM.ProxyModels; +using Digicando.MongoDM.Utility; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; + +namespace Digicando.MongoDM.HF +{ + public static class ServiceCollectionExtensions + { + public static void AddMongoDMHangfire(this IServiceCollection services) + where TDBContext : class, IDBContextBase + where TDBContexImpl : class, TDBContext + where TProxyGenerator: class, IProxyGenerator + { + services.AddMongoDM(); + + // Add Hangfire filters. + GlobalJobFilters.Filters.Add(new LocalContextFilter(AsyncLocalContextAccessor.Instance)); + } + } +} diff --git a/src/MongoDM.Hangfire/Tasks/TaskRunner.cs b/src/MongoDM.Hangfire/Tasks/TaskRunner.cs new file mode 100644 index 00000000..88d5e0db --- /dev/null +++ b/src/MongoDM.Hangfire/Tasks/TaskRunner.cs @@ -0,0 +1,46 @@ +using Digicando.MongoDM.Tasks; +using Hangfire; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading.Tasks; + +namespace Digicando.MongoDM.HF.Tasks +{ + public class TaskRunner : ITaskRunner + { + private readonly IBackgroundJobClient backgroundJobClient; + private readonly IServiceProvider serviceProvider; + + public TaskRunner( + IBackgroundJobClient backgroundJobClient, + IServiceProvider serviceProvider) + { + this.backgroundJobClient = backgroundJobClient; + this.serviceProvider = serviceProvider; + } + + public string Enqueue(Expression> methodCall) => + backgroundJobClient.Enqueue(methodCall); + + public void RunUpdateDocDependenciesTask(Type modelType, Type keyType, IEnumerable idPaths, object modelId) => + backgroundJobClient.Enqueue(() => RunUpdateDocDependenciesTaskHelper(modelType, keyType, idPaths, modelId)); + + // Helpers. + [Queue(Queues.DB_MAINTENANCE)] + private Task RunUpdateDocDependenciesTaskHelper( + Type modelType, + Type keyType, + IEnumerable idPaths, + object modelId) + { + var task = serviceProvider.GetService(); + return typeof(UpdateDocDependenciesTask).GetMethod(nameof(UpdateDocDependenciesTask.RunAsync), + BindingFlags.Public | BindingFlags.Instance) + .MakeGenericMethod(modelType, keyType) + .Invoke(task, new object[] { idPaths, modelId }) as Task; + } + } +} diff --git a/src/MongoDM/MongoDM.csproj b/src/MongoDM/MongoDM.csproj index 2358490d..293b8895 100644 --- a/src/MongoDM/MongoDM.csproj +++ b/src/MongoDM/MongoDM.csproj @@ -5,14 +5,13 @@ true Digicando.MongoDM true - 0.18.3-dev + 0.19.0-dev Digicando Srl ODM framework for MongoDB - diff --git a/src/MongoDM/ServiceCollectionExtensions.cs b/src/MongoDM/ServiceCollectionExtensions.cs index 4945335d..79e82b0e 100644 --- a/src/MongoDM/ServiceCollectionExtensions.cs +++ b/src/MongoDM/ServiceCollectionExtensions.cs @@ -3,28 +3,26 @@ using Digicando.MongoDM.Serialization.Modifiers; using Digicando.MongoDM.Tasks; using Digicando.MongoDM.Utility; -using Hangfire; using Microsoft.Extensions.DependencyInjection; namespace Digicando.MongoDM { public static class ServiceCollectionExtensions { - public static void AddMongoDM(this IServiceCollection services) + public static void AddMongoDM(this IServiceCollection services) where TDBContext : class, IDBContextBase where TDBContexImpl : class, TDBContext where TProxyGenerator: class, IProxyGenerator + where TTaskRunner: class, ITaskRunner { services.AddSingleton(); services.AddSingleton(provider => provider.GetService()); + services.AddSingleton(AsyncLocalContextAccessor.Instance); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); // Proxy generator. @@ -32,10 +30,8 @@ public static void AddMongoDM(this I services.AddSingleton(new Castle.DynamicProxy.ProxyGenerator()); // Add tasks. + services.AddSingleton(); services.AddScoped(); - - // Add Hangfire filters. - GlobalJobFilters.Filters.Add(new HangfireContextAccessor()); } } } diff --git a/src/MongoDM/Tasks/ITaskRunner.cs b/src/MongoDM/Tasks/ITaskRunner.cs new file mode 100644 index 00000000..7f9ae06e --- /dev/null +++ b/src/MongoDM/Tasks/ITaskRunner.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace Digicando.MongoDM.Tasks +{ + public interface ITaskRunner + { + void RunUpdateDocDependenciesTask(Type modelType, Type keyType, IEnumerable idPaths, object modelId); + } +} diff --git a/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs b/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs index d00014ef..4c97a86d 100644 --- a/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs +++ b/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs @@ -1,22 +1,14 @@ -using Hangfire; -using Hangfire.Server; -using System; +using Digicando.MongoDM.Models; using System.Collections.Generic; using System.Threading.Tasks; namespace Digicando.MongoDM.Tasks { - internal interface IUpdateDocDependenciesTask + public interface IUpdateDocDependenciesTask { - /// - /// Needed because https://github.com/sergeyzwezdin/Hangfire.Mongo/issues/165 - /// - [Queue(Queues.DB_MAINTENANCE)] - Task Run_NOTGENERICPROXY_Async( - PerformContext performContext, - Type modelType, - Type keyType, + Task RunAsync( IEnumerable idPaths, - object modelId); + TKey modelId) + where TModel : class, IEntityModel; } } \ No newline at end of file diff --git a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs b/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs index 0117b450..fd427073 100644 --- a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs @@ -1,16 +1,13 @@ using Digicando.MongoDM.Models; using Digicando.MongoDM.Repositories; using Digicando.MongoDM.Serialization.Modifiers; -using Hangfire.Server; using MongoDB.Driver; -using System; using System.Collections.Generic; -using System.Reflection; using System.Threading.Tasks; namespace Digicando.MongoDM.Tasks { - class UpdateDocDependenciesTask : IUpdateDocDependenciesTask + public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask { // Fields. private readonly IDBContextBase dbContext; @@ -26,20 +23,7 @@ public UpdateDocDependenciesTask( } // Methods. - public Task Run_NOTGENERICPROXY_Async( - PerformContext performContext, - Type modelType, - Type keyType, - IEnumerable idPaths, - object modelId) - { - return typeof(UpdateDocDependenciesTask).GetMethod(nameof(RunAsync), BindingFlags.NonPublic | BindingFlags.Instance) - .MakeGenericMethod(modelType, keyType) - .Invoke(this, new object[] { idPaths, modelId }) as Task; - } - - // Helpers. - private async Task RunAsync( + public async Task RunAsync( IEnumerable idPaths, TKey modelId) where TModel : class, IEntityModel diff --git a/src/MongoDM/Utility/LocalContext.cs b/src/MongoDM/Utility/AsyncLocalContext.cs similarity index 63% rename from src/MongoDM/Utility/LocalContext.cs rename to src/MongoDM/Utility/AsyncLocalContext.cs index f23f1f14..287425a9 100644 --- a/src/MongoDM/Utility/LocalContext.cs +++ b/src/MongoDM/Utility/AsyncLocalContext.cs @@ -1,15 +1,14 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Digicando.MongoDM.Utility { - public class LocalContext : IDisposable + public class AsyncLocalContext : IAsyncLocalContext { // Fields. - private readonly ILocalContextAccessor localContextAccessor; + private readonly IAsyncLocalContextAccessor localContextAccessor; // Constructors. - internal LocalContext(ILocalContextAccessor localContextAccessor) + internal AsyncLocalContext(IAsyncLocalContextAccessor localContextAccessor) { this.localContextAccessor = localContextAccessor; localContextAccessor.OnCreated(this); diff --git a/src/MongoDM/Utility/AsyncLocalContextAccessor.cs b/src/MongoDM/Utility/AsyncLocalContextAccessor.cs new file mode 100644 index 00000000..adf3c1cf --- /dev/null +++ b/src/MongoDM/Utility/AsyncLocalContextAccessor.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading; + +namespace Digicando.MongoDM.Utility +{ + public class AsyncLocalContextAccessor : IAsyncLocalContextAccessor + { + // Fields. + private static readonly AsyncLocal localContextCurrent = new AsyncLocal(); + + // Properties. + public AsyncLocalContext Context => localContextCurrent.Value; + + // Static properties. + public static IAsyncLocalContextAccessor Instance { get; } = new AsyncLocalContextAccessor(); + + // Methods. + public IAsyncLocalContext GetNewLocalContext() => new AsyncLocalContext(this); + + public void OnCreated(AsyncLocalContext context) + { + if (localContextCurrent.Value != null) + throw new InvalidOperationException("Only one context at time is supported"); + + localContextCurrent.Value = context; + } + + public void OnDisposed(AsyncLocalContext context) => + localContextCurrent.Value = null; + } +} diff --git a/src/MongoDM/Utility/ContextAccessorFacade.cs b/src/MongoDM/Utility/ContextAccessorFacade.cs index d13aa6e4..4c79d19a 100644 --- a/src/MongoDM/Utility/ContextAccessorFacade.cs +++ b/src/MongoDM/Utility/ContextAccessorFacade.cs @@ -9,18 +9,15 @@ class ContextAccessorFacade : IContextAccessorFacade { // Fields. private readonly IHttpContextAccessor httpContextAccessor; - private readonly ILocalContextAccessor localContextAccessor; - private readonly IHangfireContextAccessor performContextAccessor; + private readonly IAsyncLocalContextAccessor localContextAccessor; // Constructors. public ContextAccessorFacade( IHttpContextAccessor httpContextAccessor, - ILocalContextAccessor localContextAccessor, - IHangfireContextAccessor performContextAccessor) + IAsyncLocalContextAccessor localContextAccessor) { this.httpContextAccessor = httpContextAccessor; // Provided by Asp.Net, available only on client call. this.localContextAccessor = localContextAccessor; // Optional context created by user on local async method stack. - this.performContextAccessor = performContextAccessor; // Provided by HangFire, available only during task execution. } // Proeprties. @@ -32,8 +29,6 @@ public IReadOnlyDictionary Items return localContextAccessor.Context.Items.ToDictionary(pair => pair.Key as object, pair => pair.Value); if (httpContextAccessor.HttpContext != null) return httpContextAccessor.HttpContext.Items.ToDictionary(pair => pair.Key, pair => pair.Value); - if (performContextAccessor.PerformContext != null) - return performContextAccessor.PerformContext.Items.ToDictionary(pair => pair.Key as object, pair => pair.Value); throw new InvalidOperationException(); } } @@ -46,8 +41,6 @@ public object SyncRoot return localContextAccessor.Context.Items; if (httpContextAccessor.HttpContext != null) return httpContextAccessor.HttpContext.Items; - if (performContextAccessor.PerformContext != null) - return performContextAccessor.PerformContext.Items; throw new InvalidOperationException(); } } @@ -59,8 +52,6 @@ public void AddItem(string key, object value) localContextAccessor.Context.Items.Add(key, value); else if (httpContextAccessor.HttpContext != null) httpContextAccessor.HttpContext.Items.Add(key, value); - else if (performContextAccessor.PerformContext != null) - performContextAccessor.PerformContext.Items.Add(key, value); else throw new InvalidOperationException(); } @@ -71,8 +62,6 @@ public bool RemoveItem(string key) return localContextAccessor.Context.Items.Remove(key); else if (httpContextAccessor.HttpContext != null) return httpContextAccessor.HttpContext.Items.Remove(key); - else if (performContextAccessor.PerformContext != null) - return performContextAccessor.PerformContext.Items.Remove(key); else throw new InvalidOperationException(); } diff --git a/src/MongoDM/Utility/DBCache.cs b/src/MongoDM/Utility/DBCache.cs index 379211a6..52cac940 100644 --- a/src/MongoDM/Utility/DBCache.cs +++ b/src/MongoDM/Utility/DBCache.cs @@ -1,7 +1,6 @@ using Digicando.MongoDM.Models; using System; using System.Collections.Generic; -using System.Linq; namespace Digicando.MongoDM.Utility { diff --git a/src/MongoDM/Utility/DBMaintainer.cs b/src/MongoDM/Utility/DBMaintainer.cs index 48ebaecb..a1eb3577 100644 --- a/src/MongoDM/Utility/DBMaintainer.cs +++ b/src/MongoDM/Utility/DBMaintainer.cs @@ -1,7 +1,6 @@ using Digicando.MongoDM.ProxyModels; using Digicando.MongoDM.Serialization; using Digicando.MongoDM.Tasks; -using Hangfire; using MongoDB.Driver; using System.Linq; @@ -10,16 +9,16 @@ namespace Digicando.MongoDM.Utility public class DBMaintainer : IDBMaintainer { // Fields. - private readonly IBackgroundJobClient backgroundJobClient; private readonly IDocumentSchemaRegister documentSchemaRegister; + private readonly ITaskRunner taskRunner; // Constructors. public DBMaintainer( - IBackgroundJobClient backgroundJobClient, - IDocumentSchemaRegister documentSchemaRegister) + IDocumentSchemaRegister documentSchemaRegister, + ITaskRunner taskRunner) { - this.backgroundJobClient = backgroundJobClient; this.documentSchemaRegister = documentSchemaRegister; + this.taskRunner = taskRunner; } // Methods. @@ -36,8 +35,7 @@ public void OnUpdatedModel(IAuditable updatedModel, TKey modelId) .Distinct(); // Enqueue call for background job. - backgroundJobClient.Enqueue( - task => task.Run_NOTGENERICPROXY_Async(null, dependencyGroup.Key, typeof(TKey), idPaths, modelId)); + taskRunner.RunUpdateDocDependenciesTask(dependencyGroup.Key, typeof(TKey), idPaths, modelId); } } } diff --git a/src/MongoDM/Utility/HangfireContextAccessor.cs b/src/MongoDM/Utility/HangfireContextAccessor.cs deleted file mode 100644 index 687a7915..00000000 --- a/src/MongoDM/Utility/HangfireContextAccessor.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Hangfire.Server; -using System.Threading; - -namespace Digicando.MongoDM.Utility -{ - class HangfireContextAccessor : IHangfireContextAccessor, IServerFilter - { - // Fields. - private static readonly AsyncLocal performContextCurrent = new AsyncLocal(); - - // Properties. - public PerformContext PerformContext => performContextCurrent.Value; - - public void OnPerformed(PerformedContext filterContext) => - performContextCurrent.Value = null; - - public void OnPerforming(PerformingContext filterContext) => - performContextCurrent.Value = filterContext; - } -} diff --git a/src/MongoDM/Utility/IAsyncLocalContext.cs b/src/MongoDM/Utility/IAsyncLocalContext.cs new file mode 100644 index 00000000..5f942b2b --- /dev/null +++ b/src/MongoDM/Utility/IAsyncLocalContext.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace Digicando.MongoDM.Utility +{ + public interface IAsyncLocalContext : IDisposable + { + // Properties. + IDictionary Items { get; } + } +} diff --git a/src/MongoDM/Utility/IAsyncLocalContextAccessor.cs b/src/MongoDM/Utility/IAsyncLocalContextAccessor.cs new file mode 100644 index 00000000..93e5b65a --- /dev/null +++ b/src/MongoDM/Utility/IAsyncLocalContextAccessor.cs @@ -0,0 +1,13 @@ +namespace Digicando.MongoDM.Utility +{ + public interface IAsyncLocalContextAccessor + { + AsyncLocalContext Context { get; } + + IAsyncLocalContext GetNewLocalContext(); + + void OnCreated(AsyncLocalContext context); + + void OnDisposed(AsyncLocalContext context); + } +} \ No newline at end of file diff --git a/src/MongoDM/Utility/IHangfireContextAccessor.cs b/src/MongoDM/Utility/IHangfireContextAccessor.cs deleted file mode 100644 index a10304ea..00000000 --- a/src/MongoDM/Utility/IHangfireContextAccessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Hangfire.Server; - -namespace Digicando.MongoDM.Utility -{ - internal interface IHangfireContextAccessor - { - PerformContext PerformContext { get; } - } -} \ No newline at end of file diff --git a/src/MongoDM/Utility/ILocalContextAccessor.cs b/src/MongoDM/Utility/ILocalContextAccessor.cs deleted file mode 100644 index 3fa3bfd3..00000000 --- a/src/MongoDM/Utility/ILocalContextAccessor.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Digicando.MongoDM.Utility -{ - internal interface ILocalContextAccessor - { - LocalContext Context { get; } - - void OnCreated(LocalContext context); - - void OnDisposed(LocalContext context); - } -} \ No newline at end of file diff --git a/src/MongoDM/Utility/ILocalContextFactory.cs b/src/MongoDM/Utility/ILocalContextFactory.cs deleted file mode 100644 index 643860d1..00000000 --- a/src/MongoDM/Utility/ILocalContextFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Digicando.MongoDM.Utility -{ - public interface ILocalContextFactory - { - IDisposable CreateNewLocalContext(); - } -} \ No newline at end of file diff --git a/src/MongoDM/Utility/LocalContextAccessor.cs b/src/MongoDM/Utility/LocalContextAccessor.cs deleted file mode 100644 index b3a4be62..00000000 --- a/src/MongoDM/Utility/LocalContextAccessor.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Threading; - -namespace Digicando.MongoDM.Utility -{ - class LocalContextAccessor : ILocalContextAccessor - { - // Fields. - private static readonly AsyncLocal localContextCurrent = new AsyncLocal(); - - // Properties. - public LocalContext Context => localContextCurrent.Value; - - // Methods. - public void OnCreated(LocalContext context) => - localContextCurrent.Value = context; - - public void OnDisposed(LocalContext context) => - localContextCurrent.Value = null; - } -} diff --git a/src/MongoDM/Utility/LocalContextFactory.cs b/src/MongoDM/Utility/LocalContextFactory.cs deleted file mode 100644 index e5340dc6..00000000 --- a/src/MongoDM/Utility/LocalContextFactory.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; - -namespace Digicando.MongoDM.Utility -{ - class LocalContextFactory : ILocalContextFactory - { - private readonly ILocalContextAccessor localContextAccessor; - - public LocalContextFactory( - ILocalContextAccessor localContextAccessor) - { - this.localContextAccessor = localContextAccessor; - } - - public IDisposable CreateNewLocalContext() => - new LocalContext(localContextAccessor); - } -} From b4d93d37ae741751936bf576daca1ebc67611813 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sat, 28 Dec 2019 19:05:09 +0100 Subject: [PATCH 02/46] Completed MODM24 --- .../Filters/LocalContextFilter.cs | 9 ++--- src/MongoDM.Hangfire/Tasks/TaskRunner.cs | 33 ++++--------------- .../Tasks/UpdateDocDependenciesTaskFacade.cs | 29 ++++++++++++++++ 3 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs diff --git a/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs b/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs index e09d3b93..e2102ae8 100644 --- a/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs +++ b/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs @@ -8,7 +8,7 @@ class LocalContextFilter : IServerFilter { // Fields. private readonly IAsyncLocalContextAccessor localContextAccessor; - private readonly Dictionary contexts = new Dictionary(); + private readonly Dictionary contexts = new Dictionary(); // Constructors. public LocalContextFilter(IAsyncLocalContextAccessor localContextAccessor) @@ -21,7 +21,7 @@ public void OnPerforming(PerformingContext filterContext) { lock (contexts) { - contexts.Add(filterContext, localContextAccessor.GetNewLocalContext()); + contexts.Add(filterContext.BackgroundJob.Id, localContextAccessor.GetNewLocalContext()); } } @@ -29,8 +29,9 @@ public void OnPerformed(PerformedContext filterContext) { lock (contexts) { - var context = contexts[filterContext]; - contexts.Remove(filterContext); + var jobId = filterContext.BackgroundJob.Id; + var context = contexts[jobId]; + contexts.Remove(jobId); context.Dispose(); } } diff --git a/src/MongoDM.Hangfire/Tasks/TaskRunner.cs b/src/MongoDM.Hangfire/Tasks/TaskRunner.cs index 88d5e0db..b9e2cc91 100644 --- a/src/MongoDM.Hangfire/Tasks/TaskRunner.cs +++ b/src/MongoDM.Hangfire/Tasks/TaskRunner.cs @@ -1,46 +1,25 @@ using Digicando.MongoDM.Tasks; using Hangfire; -using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; -using System.Linq.Expressions; -using System.Reflection; -using System.Threading.Tasks; namespace Digicando.MongoDM.HF.Tasks { public class TaskRunner : ITaskRunner { + // Fields. private readonly IBackgroundJobClient backgroundJobClient; - private readonly IServiceProvider serviceProvider; + // Constructors. public TaskRunner( - IBackgroundJobClient backgroundJobClient, - IServiceProvider serviceProvider) + IBackgroundJobClient backgroundJobClient) { this.backgroundJobClient = backgroundJobClient; - this.serviceProvider = serviceProvider; } - public string Enqueue(Expression> methodCall) => - backgroundJobClient.Enqueue(methodCall); - + // Methods. public void RunUpdateDocDependenciesTask(Type modelType, Type keyType, IEnumerable idPaths, object modelId) => - backgroundJobClient.Enqueue(() => RunUpdateDocDependenciesTaskHelper(modelType, keyType, idPaths, modelId)); - - // Helpers. - [Queue(Queues.DB_MAINTENANCE)] - private Task RunUpdateDocDependenciesTaskHelper( - Type modelType, - Type keyType, - IEnumerable idPaths, - object modelId) - { - var task = serviceProvider.GetService(); - return typeof(UpdateDocDependenciesTask).GetMethod(nameof(UpdateDocDependenciesTask.RunAsync), - BindingFlags.Public | BindingFlags.Instance) - .MakeGenericMethod(modelType, keyType) - .Invoke(task, new object[] { idPaths, modelId }) as Task; - } + backgroundJobClient.Enqueue( + task => task.RunAsync(modelType, keyType, idPaths, modelId)); } } diff --git a/src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs new file mode 100644 index 00000000..bf81ae4c --- /dev/null +++ b/src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -0,0 +1,29 @@ +using Digicando.MongoDM.Tasks; +using Hangfire; +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading.Tasks; + +namespace Digicando.MongoDM.HF.Tasks +{ + class UpdateDocDependenciesTaskFacade + { + // Fields. + private readonly IUpdateDocDependenciesTask task; + + // Constructors. + public UpdateDocDependenciesTaskFacade(IUpdateDocDependenciesTask task) + { + this.task = task; + } + + // Methods. + [Queue(Queues.DB_MAINTENANCE)] + public Task RunAsync(Type modelType, Type keyType, IEnumerable idPaths, object modelId) => + typeof(UpdateDocDependenciesTask).GetMethod( + nameof(UpdateDocDependenciesTask.RunAsync), BindingFlags.Public | BindingFlags.Instance) + .MakeGenericMethod(modelType, keyType) + .Invoke(task, new object[] { idPaths, modelId }) as Task; + } +} From 20b9883e6ab3b6f4f82033848248684e05c2877a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 5 Jan 2020 20:27:24 +0100 Subject: [PATCH 03/46] Completed MODM-27 --- .../ServiceCollectionExtensions.cs | 8 ++++---- src/MongoDM/{DBContextBase.cs => DbContext.cs} | 4 ++-- src/MongoDM/{IDBContextBase.cs => IDbContext.cs} | 2 +- src/MongoDM/Migration/MongoDocumentMigration.cs | 16 ++++++++-------- .../ProxyModels/ReferenceableInterceptor.cs | 6 +++--- .../Repositories/CollectionRepositoryBase.cs | 4 ++-- src/MongoDM/Repositories/GridFSRepositoryBase.cs | 2 +- src/MongoDM/Repositories/RepositoryBase.cs | 4 ++-- .../Serialization/DocumentSchemaRegister.cs | 4 ++-- .../Serialization/IDocumentSchemaRegister.cs | 2 +- .../Serialization/IModelSerializerCollector.cs | 2 +- .../Serializers/ExtendedClassMapSerializer.cs | 8 ++++---- .../Serializers/ReferenceSerializer.cs | 4 ++-- src/MongoDM/ServiceCollectionExtensions.cs | 10 +++++----- src/MongoDM/Tasks/UpdateDocDependenciesTask.cs | 4 ++-- .../ExtendedClassMapSerializerTest.cs | 4 ++-- .../ReferenceableInterceptorTest.cs | 4 ++-- 17 files changed, 44 insertions(+), 44 deletions(-) rename src/MongoDM/{DBContextBase.cs => DbContext.cs} (98%) rename src/MongoDM/{IDBContextBase.cs => IDbContext.cs} (97%) diff --git a/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs b/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs index 296a2e8e..ea771547 100644 --- a/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs +++ b/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs @@ -9,12 +9,12 @@ namespace Digicando.MongoDM.HF { public static class ServiceCollectionExtensions { - public static void AddMongoDMHangfire(this IServiceCollection services) - where TDBContext : class, IDBContextBase - where TDBContexImpl : class, TDBContext + public static void AddMongoDMHangfire(this IServiceCollection services) + where TDbContext : class, IDbContext + where TDbContextImpl : class, TDbContext where TProxyGenerator: class, IProxyGenerator { - services.AddMongoDM(); + services.AddMongoDM(); // Add Hangfire filters. GlobalJobFilters.Filters.Add(new LocalContextFilter(AsyncLocalContextAccessor.Instance)); diff --git a/src/MongoDM/DBContextBase.cs b/src/MongoDM/DbContext.cs similarity index 98% rename from src/MongoDM/DBContextBase.cs rename to src/MongoDM/DbContext.cs index 5b8e1f6f..7014ec86 100644 --- a/src/MongoDM/DBContextBase.cs +++ b/src/MongoDM/DbContext.cs @@ -14,13 +14,13 @@ namespace Digicando.MongoDM { - public abstract class DBContextBase : IDBContextBase + public abstract class DbContext : IDbContext { // Consts. public const string DocumentVersionElementName = "v"; // Constructor. - public DBContextBase( + public DbContext( string connectionString, IDBCache dbCache, IDBMaintainer dbMaintainer, diff --git a/src/MongoDM/IDBContextBase.cs b/src/MongoDM/IDbContext.cs similarity index 97% rename from src/MongoDM/IDBContextBase.cs rename to src/MongoDM/IDbContext.cs index c467ddc2..ca31406c 100644 --- a/src/MongoDM/IDBContextBase.cs +++ b/src/MongoDM/IDbContext.cs @@ -10,7 +10,7 @@ namespace Digicando.MongoDM { - public interface IDBContextBase + public interface IDbContext { // Properties. IMongoClient Client { get; } diff --git a/src/MongoDM/Migration/MongoDocumentMigration.cs b/src/MongoDM/Migration/MongoDocumentMigration.cs index f7c9b129..b0367ffb 100644 --- a/src/MongoDM/Migration/MongoDocumentMigration.cs +++ b/src/MongoDM/Migration/MongoDocumentMigration.cs @@ -36,26 +36,26 @@ public override async Task MigrateAsync() var filterBuilder = Builders.Filter; var filter = filterBuilder.Or( // No version in document (very old). - filterBuilder.Exists(DBContextBase.DocumentVersionElementName, false), + filterBuilder.Exists(DbContext.DocumentVersionElementName, false), // Version as string (doc.Version < "0.12.0"). //(can't query directly for string because https://docs.mongodb.com/v3.2/reference/operator/query/type/#arrays) - filterBuilder.Not(filterBuilder.Type(DBContextBase.DocumentVersionElementName, BsonType.Int32)), + filterBuilder.Not(filterBuilder.Type(DbContext.DocumentVersionElementName, BsonType.Int32)), // Version is an array with values ("0.12.0" <= doc.Version). //doc.Major < min.Major - filterBuilder.Lt($"{DBContextBase.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), //doc.Major == min.Major && doc.Minor < min.Minor filterBuilder.And( - filterBuilder.Eq($"{DBContextBase.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), - filterBuilder.Lt($"{DBContextBase.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease)), + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease)), //doc.Major == min.Major && doc.Minor == min.Minor && doc.Patch < min.Patch filterBuilder.And( - filterBuilder.Eq($"{DBContextBase.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), - filterBuilder.Eq($"{DBContextBase.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease), - filterBuilder.Lt($"{DBContextBase.DocumentVersionElementName}.2", MinimumDocumentVersion.PatchRelease))); + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.2", MinimumDocumentVersion.PatchRelease))); // Replace documents. await collection.Find(filter, new FindOptions { NoCursorTimeout = true }) diff --git a/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs b/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs index 449a7db3..0ff19a8c 100644 --- a/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs @@ -23,13 +23,13 @@ public class ReferenceableInterceptor : ModelInterceptorBase additionalInterfaces, - IDBContextBase dBContext) + IDbContext dbContext) : base(additionalInterfaces) { if (typeof(TModel).GetInterfaces().Contains(typeof(IFileModel))) - repository = dBContext.ModelGridFSRepositoryMap[typeof(TModel)] as IRepository; + repository = dbContext.ModelGridFSRepositoryMap[typeof(TModel)] as IRepository; else - repository = dBContext.ModelCollectionRepositoryMap[typeof(TModel)] as IRepository; + repository = dbContext.ModelCollectionRepositoryMap[typeof(TModel)] as IRepository; } // Protected methods. diff --git a/src/MongoDM/Repositories/CollectionRepositoryBase.cs b/src/MongoDM/Repositories/CollectionRepositoryBase.cs index c0257751..0c287b8f 100644 --- a/src/MongoDM/Repositories/CollectionRepositoryBase.cs +++ b/src/MongoDM/Repositories/CollectionRepositoryBase.cs @@ -25,7 +25,7 @@ public abstract class CollectionRepositoryBase : // Constructors. public CollectionRepositoryBase( string collectionName, - IDBContextBase dbContext) + IDbContext dbContext) : base(dbContext) { Collection = dbContext.Database.GetCollection(collectionName); @@ -61,7 +61,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis newIndexes.Add( ("ver", new CreateIndexModel( - Builders.IndexKeys.Ascending(DBContextBase.DocumentVersionElementName), + Builders.IndexKeys.Ascending(DbContext.DocumentVersionElementName), new CreateIndexOptions { Name = "ver" }))); //referenced documents diff --git a/src/MongoDM/Repositories/GridFSRepositoryBase.cs b/src/MongoDM/Repositories/GridFSRepositoryBase.cs index c460eedc..58e02368 100644 --- a/src/MongoDM/Repositories/GridFSRepositoryBase.cs +++ b/src/MongoDM/Repositories/GridFSRepositoryBase.cs @@ -23,7 +23,7 @@ public abstract class GridFSRepositoryBase : // Constructors. public GridFSRepositoryBase( string bucketName, - IDBContextBase dbContext) + IDbContext dbContext) : base(dbContext) { var bucketOptions = new GridFSBucketOptions(); diff --git a/src/MongoDM/Repositories/RepositoryBase.cs b/src/MongoDM/Repositories/RepositoryBase.cs index b5acdffa..844c0d05 100644 --- a/src/MongoDM/Repositories/RepositoryBase.cs +++ b/src/MongoDM/Repositories/RepositoryBase.cs @@ -18,10 +18,10 @@ public abstract class RepositoryBase : where TModel : class, IEntityModel { // Fields. - private readonly IDBContextBase dbContext; + private readonly IDbContext dbContext; // Constructors. - public RepositoryBase(IDBContextBase dbContext) + public RepositoryBase(IDbContext dbContext) { this.dbContext = dbContext; } diff --git a/src/MongoDM/Serialization/DocumentSchemaRegister.cs b/src/MongoDM/Serialization/DocumentSchemaRegister.cs index 9cafffdd..7a27480d 100644 --- a/src/MongoDM/Serialization/DocumentSchemaRegister.cs +++ b/src/MongoDM/Serialization/DocumentSchemaRegister.cs @@ -23,7 +23,7 @@ class DocumentSchemaRegister : IDocumentSchemaRegister private readonly Dictionary> modelEntityReferencesIdsMap = new Dictionary>(); - private IDBContextBase dbContext; + private IDbContext dbContext; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly List schemas = new List(); @@ -35,7 +35,7 @@ public DocumentSchemaRegister( } //here for circular dependency injection with DBContext - public void Initialize(IDBContextBase dbContext) + public void Initialize(IDbContext dbContext) { if (this.dbContext != null) throw new InvalidOperationException("Instance already initialized"); diff --git a/src/MongoDM/Serialization/IDocumentSchemaRegister.cs b/src/MongoDM/Serialization/IDocumentSchemaRegister.cs index 67bf52e7..f03a8c6d 100644 --- a/src/MongoDM/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongoDM/Serialization/IDocumentSchemaRegister.cs @@ -25,7 +25,7 @@ public interface IDocumentSchemaRegister /// Call before everything else. Used for avoid circular dependency injection with MongoStorage /// /// Current instance of IDBContext - void Initialize(IDBContextBase dbContext); + void Initialize(IDbContext dbContext); /// /// Register a new model schema diff --git a/src/MongoDM/Serialization/IModelSerializerCollector.cs b/src/MongoDM/Serialization/IModelSerializerCollector.cs index 88cc9e97..515520d4 100644 --- a/src/MongoDM/Serialization/IModelSerializerCollector.cs +++ b/src/MongoDM/Serialization/IModelSerializerCollector.cs @@ -3,6 +3,6 @@ public interface IModelSerializerCollector { // Methods. - void Register(IDBContextBase dbContext); + void Register(IDbContext dbContext); } } diff --git a/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs index ba2e3efb..4664a7b6 100644 --- a/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -35,14 +35,14 @@ public Func Condition // Fields. private BsonClassMapSerializer _serializer; - private readonly IDBContextBase dbContext; + private readonly IDbContext dbContext; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly ICollection extraElements; private readonly Func> fixDeserializedModelAsync; // Constructor. public ExtendedClassMapSerializer( - IDBContextBase dbContext, + IDbContext dbContext, ISerializerModifierAccessor serializerModifierAccessor, Func> fixDeserializedModelAsync = null) { @@ -51,7 +51,7 @@ public ExtendedClassMapSerializer( extraElements = new List(); this.fixDeserializedModelAsync = fixDeserializedModelAsync ?? ((m, _) => Task.FromResult(m)); documentVersionElement = new BsonElement( - DBContextBase.DocumentVersionElementName, + DbContext.DocumentVersionElementName, DocumentVersionToBsonArray(dbContext.DocumentVersion)); } @@ -86,7 +86,7 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser // Get version. DocumentVersion documentVersion = null; - if (bsonDocument.TryGetElement(DBContextBase.DocumentVersionElementName, out BsonElement versionElement)) + if (bsonDocument.TryGetElement(DbContext.DocumentVersionElementName, out BsonElement versionElement)) documentVersion = BsonValueToDocumentVersion(versionElement.Value); // Initialize localContext and bsonReader diff --git a/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs b/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs index f394471e..400c9220 100644 --- a/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs @@ -25,7 +25,7 @@ public class ReferenceSerializer : private readonly ReaderWriterLockSlim configLockClassMaps = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockSerializers = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly IDBContextBase dbContext; + private readonly IDbContext dbContext; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly IDictionary registeredAdapters = new Dictionary(); @@ -34,7 +34,7 @@ public class ReferenceSerializer : // Constructors. public ReferenceSerializer( - IDBContextBase dbContext, + IDbContext dbContext, ISerializerModifierAccessor serializerModifierAccessor, bool useCascadeDelete) { diff --git a/src/MongoDM/ServiceCollectionExtensions.cs b/src/MongoDM/ServiceCollectionExtensions.cs index 79e82b0e..138434de 100644 --- a/src/MongoDM/ServiceCollectionExtensions.cs +++ b/src/MongoDM/ServiceCollectionExtensions.cs @@ -9,14 +9,14 @@ namespace Digicando.MongoDM { public static class ServiceCollectionExtensions { - public static void AddMongoDM(this IServiceCollection services) - where TDBContext : class, IDBContextBase - where TDBContexImpl : class, TDBContext + public static void AddMongoDM(this IServiceCollection services) + where TDbContext : class, IDbContext + where TDbContextImpl : class, TDbContext where TProxyGenerator: class, IProxyGenerator where TTaskRunner: class, ITaskRunner { - services.AddSingleton(); - services.AddSingleton(provider => provider.GetService()); + services.AddSingleton(); + services.AddSingleton(provider => provider.GetService()); services.AddSingleton(AsyncLocalContextAccessor.Instance); services.AddSingleton(); diff --git a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs b/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs index fd427073..0e4144f8 100644 --- a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs @@ -10,12 +10,12 @@ namespace Digicando.MongoDM.Tasks public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask { // Fields. - private readonly IDBContextBase dbContext; + private readonly IDbContext dbContext; private readonly ISerializerModifierAccessor serializerModifierAccessor; // Constructors. public UpdateDocDependenciesTask( - IDBContextBase dbContext, + IDbContext dbContext, ISerializerModifierAccessor serializerModifierAccessor) { this.dbContext = dbContext; diff --git a/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs b/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs index cb62fe1f..78989379 100644 --- a/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs @@ -56,14 +56,14 @@ public SerializationTestElement( } // Fields. - private readonly Mock dbContextMock; + private readonly Mock dbContextMock; private readonly DocumentVersion documentVersion = new DocumentVersion("1.0.0"); private readonly Mock serializerModifierAccessorMock; // Constructor. public ExtendedClassMapSerializerTest() { - dbContextMock = new Mock(); + dbContextMock = new Mock(); dbContextMock.Setup(c => c.DocumentVersion) .Returns(() => documentVersion); dbContextMock.Setup(c => c.DBCache.LoadedModels.ContainsKey(It.IsAny())) diff --git a/test/MongoDM.Tests/ReferenceableInterceptorTest.cs b/test/MongoDM.Tests/ReferenceableInterceptorTest.cs index 87502446..96f478d9 100644 --- a/test/MongoDM.Tests/ReferenceableInterceptorTest.cs +++ b/test/MongoDM.Tests/ReferenceableInterceptorTest.cs @@ -14,7 +14,7 @@ public class ReferenceableInterceptorTest { private readonly ReferenceableInterceptor interceptor; private readonly Mock> repositoryMock; - private readonly Mock dbContextMock; + private readonly Mock dbContextMock; private readonly Mock getIsSummaryInvocationMock; private readonly Mock getLoadedMembersInvocationMock; @@ -23,7 +23,7 @@ public ReferenceableInterceptorTest() { repositoryMock = new Mock>(); - dbContextMock = new Mock(); + dbContextMock = new Mock(); dbContextMock.Setup(r => r.ModelCollectionRepositoryMap[typeof(FakeModel)]) .Returns(() => repositoryMock.Object); From 9979120bbf815f48366b64840a94ce86e6ffd565 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 5 Jan 2020 21:17:17 +0100 Subject: [PATCH 04/46] Completed MODM-28 --- MongoDM.sln => MongODM.sln | 6 +++--- .../Filters/LocalContextFilter.cs | 4 ++-- .../MongODM.Hangfire.csproj} | 4 ++-- .../ServiceCollectionExtensions.cs | 10 +++++----- .../Tasks/TaskRunner.cs | 4 ++-- .../Tasks/UpdateDocDependenciesTaskFacade.cs | 4 ++-- src/{MongoDM => MongODM}/DbContext.cs | 12 ++++++------ .../Extensions/ClassMapExtensions.cs | 6 +++--- .../Extensions/EnumerableExtensions.cs | 2 +- src/{MongoDM => MongODM}/GlobalSuppressions.cs | 4 ++-- src/{MongoDM => MongODM}/IDbContext.cs | 10 +++++----- .../Migration/MongoCollectionMigration.cs | 2 +- .../Migration/MongoDocumentMigration.cs | 6 +++--- .../Migration/MongoMigrationBase.cs | 2 +- src/{MongoDM => MongODM}/Models/IEntityModel.cs | 2 +- src/{MongoDM => MongODM}/Models/IFileModel.cs | 2 +- src/{MongoDM => MongODM}/Models/IModel.cs | 2 +- .../MongoDM.csproj => MongODM/MongODM.csproj} | 2 +- .../ProxyModels/AuditableInterceptor.cs | 2 +- src/{MongoDM => MongODM}/ProxyModels/IAuditable.cs | 2 +- .../ProxyModels/IProxyGenerator.cs | 2 +- .../ProxyModels/IReferenceable.cs | 2 +- .../ProxyModels/ReferenceableInterceptor.cs | 6 +++--- .../Repositories/CollectionRepositoryBase.cs | 10 +++++----- .../Repositories/GridFSRepositoryBase.cs | 8 ++++---- .../Repositories/ICollectionRepository.cs | 4 ++-- .../Repositories/IGridFSRepository.cs | 4 ++-- src/{MongoDM => MongODM}/Repositories/IRepository.cs | 8 ++++---- .../Repositories/RepositoryBase.cs | 10 +++++----- .../Serialization/DocumentSchema.cs | 2 +- .../Serialization/DocumentSchemaMemberMap.cs | 2 +- .../Serialization/DocumentSchemaRegister.cs | 8 ++++---- .../Serialization/DocumentVersion.cs | 2 +- .../Serialization/EntityMember.cs | 2 +- .../Serialization/ExtendedBsonDocumentReader.cs | 2 +- .../Serialization/ExtendedBsonDocumentWriter.cs | 2 +- .../Serialization/IDocumentSchemaRegister.cs | 4 ++-- .../Serialization/IModelSerializerCollector.cs | 2 +- .../Modifiers/CacheSerializerModifier.cs | 4 ++-- .../Modifiers/ISerializerModifierAccessor.cs | 2 +- .../Modifiers/ReferenceSerializerModifier.cs | 4 ++-- .../Modifiers/SerializerModifierAccessor.cs | 4 ++-- .../Serializers/DictionarySerializer.cs | 2 +- .../Serializers/EnumerableSerializer.cs | 2 +- .../Serializers/ExtendedClassMapSerializer.cs | 8 ++++---- .../Serializers/ExtraElementsSerializer.cs | 2 +- .../Serialization/Serializers/GeoPointSerializer.cs | 4 ++-- .../Serializers/HexToBinaryDataSerializer.cs | 2 +- .../Serializers/IClassMapContainerSerializer.cs | 2 +- .../Serializers/IReferenceContainerSerializer.cs | 2 +- .../Serializers/ReadOnlyDictionarySerializer.cs | 2 +- .../Serialization/Serializers/ReferenceSerializer.cs | 8 ++++---- .../Serializers/ReferenceSerializerAdapter.cs | 4 ++-- .../Serializers/ReferenceSerializerSwitch.cs | 4 ++-- .../ServiceCollectionExtensions.cs | 12 ++++++------ src/{MongoDM => MongODM}/Tasks/ITaskRunner.cs | 2 +- .../Tasks/IUpdateDocDependenciesTask.cs | 4 ++-- src/{MongoDM => MongODM}/Tasks/Queues.cs | 2 +- .../Tasks/UpdateDocDependenciesTask.cs | 8 ++++---- .../Utility/AsyncLocalContext.cs | 2 +- .../Utility/AsyncLocalContextAccessor.cs | 2 +- .../Utility/ContextAccessorFacade.cs | 2 +- src/{MongoDM => MongODM}/Utility/DBCache.cs | 4 ++-- src/{MongoDM => MongODM}/Utility/DBMaintainer.cs | 8 ++++---- .../Utility/IAsyncLocalContext.cs | 2 +- .../Utility/IAsyncLocalContextAccessor.cs | 2 +- .../Utility/IContextAccessorFacade.cs | 2 +- src/{MongoDM => MongODM}/Utility/IDBCache.cs | 4 ++-- src/{MongoDM => MongODM}/Utility/IDBMaintainer.cs | 4 ++-- .../Comparers/FakeModelComparer.cs | 4 ++-- .../ExtendedClassMapSerializerTest.cs | 12 ++++++------ .../MockHelpers/InterceptorMockHelper.cs | 2 +- .../Models/FakeEntityModelBase.cs | 2 +- .../Models/FakeModel.cs | 2 +- .../Models/FakeModelProxy.cs | 2 +- .../Models/ModelBase.cs | 2 +- .../MongODM.Tests.csproj} | 4 ++-- .../ReferenceableInterceptorTest.cs | 10 +++++----- 78 files changed, 162 insertions(+), 162 deletions(-) rename MongoDM.sln => MongODM.sln (85%) rename src/{MongoDM.Hangfire => MongODM.Hangfire}/Filters/LocalContextFilter.cs (93%) rename src/{MongoDM.Hangfire/MongoDM.Hangfire.csproj => MongODM.Hangfire/MongODM.Hangfire.csproj} (81%) rename src/{MongoDM.Hangfire => MongODM.Hangfire}/ServiceCollectionExtensions.cs (79%) rename src/{MongoDM.Hangfire => MongODM.Hangfire}/Tasks/TaskRunner.cs (90%) rename src/{MongoDM.Hangfire => MongODM.Hangfire}/Tasks/UpdateDocDependenciesTaskFacade.cs (92%) rename src/{MongoDM => MongODM}/DbContext.cs (96%) rename src/{MongoDM => MongODM}/Extensions/ClassMapExtensions.cs (91%) rename src/{MongoDM => MongODM}/Extensions/EnumerableExtensions.cs (99%) rename src/{MongoDM => MongODM}/GlobalSuppressions.cs (84%) rename src/{MongoDM => MongODM}/IDbContext.cs (85%) rename src/{MongoDM => MongODM}/Migration/MongoCollectionMigration.cs (97%) rename src/{MongoDM => MongODM}/Migration/MongoDocumentMigration.cs (96%) rename src/{MongoDM => MongODM}/Migration/MongoMigrationBase.cs (88%) rename src/{MongoDM => MongODM}/Models/IEntityModel.cs (87%) rename src/{MongoDM => MongODM}/Models/IFileModel.cs (83%) rename src/{MongoDM => MongODM}/Models/IModel.cs (80%) rename src/{MongoDM/MongoDM.csproj => MongODM/MongODM.csproj} (94%) rename src/{MongoDM => MongODM}/ProxyModels/AuditableInterceptor.cs (98%) rename src/{MongoDM => MongODM}/ProxyModels/IAuditable.cs (90%) rename src/{MongoDM => MongODM}/ProxyModels/IProxyGenerator.cs (86%) rename src/{MongoDM => MongODM}/ProxyModels/IReferenceable.cs (96%) rename src/{MongoDM => MongODM}/ProxyModels/ReferenceableInterceptor.cs (98%) rename src/{MongoDM => MongODM}/Repositories/CollectionRepositoryBase.cs (97%) rename src/{MongoDM => MongODM}/Repositories/GridFSRepositoryBase.cs (96%) rename src/{MongoDM => MongODM}/Repositories/ICollectionRepository.cs (95%) rename src/{MongoDM => MongODM}/Repositories/IGridFSRepository.cs (86%) rename src/{MongoDM => MongODM}/Repositories/IRepository.cs (90%) rename src/{MongoDM => MongODM}/Repositories/RepositoryBase.cs (96%) rename src/{MongoDM => MongODM}/Serialization/DocumentSchema.cs (93%) rename src/{MongoDM => MongODM}/Serialization/DocumentSchemaMemberMap.cs (98%) rename src/{MongoDM => MongODM}/Serialization/DocumentSchemaRegister.cs (98%) rename src/{MongoDM => MongODM}/Serialization/DocumentVersion.cs (99%) rename src/{MongoDM => MongODM}/Serialization/EntityMember.cs (92%) rename src/{MongoDM => MongODM}/Serialization/ExtendedBsonDocumentReader.cs (87%) rename src/{MongoDM => MongODM}/Serialization/ExtendedBsonDocumentWriter.cs (87%) rename src/{MongoDM => MongODM}/Serialization/IDocumentSchemaRegister.cs (97%) rename src/{MongoDM => MongODM}/Serialization/IModelSerializerCollector.cs (73%) rename src/{MongoDM => MongODM}/Serialization/Modifiers/CacheSerializerModifier.cs (94%) rename src/{MongoDM => MongODM}/Serialization/Modifiers/ISerializerModifierAccessor.cs (85%) rename src/{MongoDM => MongODM}/Serialization/Modifiers/ReferenceSerializerModifier.cs (94%) rename src/{MongoDM => MongODM}/Serialization/Modifiers/SerializerModifierAccessor.cs (92%) rename src/{MongoDM => MongODM}/Serialization/Serializers/DictionarySerializer.cs (99%) rename src/{MongoDM => MongODM}/Serialization/Serializers/EnumerableSerializer.cs (98%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ExtendedClassMapSerializer.cs (98%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ExtraElementsSerializer.cs (98%) rename src/{MongoDM => MongODM}/Serialization/Serializers/GeoPointSerializer.cs (96%) rename src/{MongoDM => MongODM}/Serialization/Serializers/HexToBinaryDataSerializer.cs (96%) rename src/{MongoDM => MongODM}/Serialization/Serializers/IClassMapContainerSerializer.cs (81%) rename src/{MongoDM => MongODM}/Serialization/Serializers/IReferenceContainerSerializer.cs (70%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ReadOnlyDictionarySerializer.cs (99%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ReferenceSerializer.cs (98%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ReferenceSerializerAdapter.cs (95%) rename src/{MongoDM => MongODM}/Serialization/Serializers/ReferenceSerializerSwitch.cs (97%) rename src/{MongoDM => MongODM}/ServiceCollectionExtensions.cs (87%) rename src/{MongoDM => MongODM}/Tasks/ITaskRunner.cs (86%) rename src/{MongoDM => MongODM}/Tasks/IUpdateDocDependenciesTask.cs (80%) rename src/{MongoDM => MongODM}/Tasks/Queues.cs (73%) rename src/{MongoDM => MongODM}/Tasks/UpdateDocDependenciesTask.cs (94%) rename src/{MongoDM => MongODM}/Utility/AsyncLocalContext.cs (94%) rename src/{MongoDM => MongODM}/Utility/AsyncLocalContextAccessor.cs (96%) rename src/{MongoDM => MongODM}/Utility/ContextAccessorFacade.cs (98%) rename src/{MongoDM => MongODM}/Utility/DBCache.cs (96%) rename src/{MongoDM => MongODM}/Utility/DBMaintainer.cs (90%) rename src/{MongoDM => MongODM}/Utility/IAsyncLocalContext.cs (84%) rename src/{MongoDM => MongODM}/Utility/IAsyncLocalContextAccessor.cs (87%) rename src/{MongoDM => MongODM}/Utility/IContextAccessorFacade.cs (89%) rename src/{MongoDM => MongODM}/Utility/IDBCache.cs (83%) rename src/{MongoDM => MongODM}/Utility/IDBMaintainer.cs (62%) rename test/{MongoDM.Tests => MongODM.Tests}/Comparers/FakeModelComparer.cs (96%) rename test/{MongoDM.Tests => MongODM.Tests}/ExtendedClassMapSerializerTest.cs (98%) rename test/{MongoDM.Tests => MongODM.Tests}/MockHelpers/InterceptorMockHelper.cs (99%) rename test/{MongoDM.Tests => MongODM.Tests}/Models/FakeEntityModelBase.cs (89%) rename test/{MongoDM.Tests => MongODM.Tests}/Models/FakeModel.cs (90%) rename test/{MongoDM.Tests => MongODM.Tests}/Models/FakeModelProxy.cs (61%) rename test/{MongoDM.Tests => MongODM.Tests}/Models/ModelBase.cs (84%) rename test/{MongoDM.Tests/MongoDM.Tests.csproj => MongODM.Tests/MongODM.Tests.csproj} (86%) rename test/{MongoDM.Tests => MongODM.Tests}/ReferenceableInterceptorTest.cs (97%) diff --git a/MongoDM.sln b/MongODM.sln similarity index 85% rename from MongoDM.sln rename to MongODM.sln index 2d8164ed..b1bd62ee 100644 --- a/MongoDM.sln +++ b/MongODM.sln @@ -7,11 +7,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{490DAED7-DAD EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CF1ABDEA-794F-4474-858D-BCB61F367D72}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM", "src\MongoDM\MongoDM.csproj", "{AA9CE7D3-169F-4725-970E-4CE900E91EDD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM", "src\MongODM\MongODM.csproj", "{AA9CE7D3-169F-4725-970E-4CE900E91EDD}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM.Tests", "test\MongoDM.Tests\MongoDM.Tests.csproj", "{82789357-2EC1-4159-B3F6-0F830ABAFF4C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Tests", "test\MongODM.Tests\MongODM.Tests.csproj", "{82789357-2EC1-4159-B3F6-0F830ABAFF4C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDM.Hangfire", "src\MongoDM.Hangfire\MongoDM.Hangfire.csproj", "{10897D0D-4898-4A4D-8D1E-B2435E93D9A1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Hangfire", "src\MongODM.Hangfire\MongODM.Hangfire.csproj", "{10897D0D-4898-4A4D-8D1E-B2435E93D9A1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs b/src/MongODM.Hangfire/Filters/LocalContextFilter.cs similarity index 93% rename from src/MongoDM.Hangfire/Filters/LocalContextFilter.cs rename to src/MongODM.Hangfire/Filters/LocalContextFilter.cs index e2102ae8..faa3d4e0 100644 --- a/src/MongoDM.Hangfire/Filters/LocalContextFilter.cs +++ b/src/MongODM.Hangfire/Filters/LocalContextFilter.cs @@ -1,8 +1,8 @@ -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Utility; using Hangfire.Server; using System.Collections.Generic; -namespace Digicando.MongoDM.HF.Filters +namespace Digicando.MongODM.HF.Filters { class LocalContextFilter : IServerFilter { diff --git a/src/MongoDM.Hangfire/MongoDM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj similarity index 81% rename from src/MongoDM.Hangfire/MongoDM.Hangfire.csproj rename to src/MongODM.Hangfire/MongODM.Hangfire.csproj index a694a77d..808e567f 100644 --- a/src/MongoDM.Hangfire/MongoDM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - Digicando.MongoDM.HF + Digicando.MongODM.HF true 0.19.0-dev Digicando Srl @@ -15,7 +15,7 @@ - + diff --git a/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs b/src/MongODM.Hangfire/ServiceCollectionExtensions.cs similarity index 79% rename from src/MongoDM.Hangfire/ServiceCollectionExtensions.cs rename to src/MongODM.Hangfire/ServiceCollectionExtensions.cs index ea771547..67855318 100644 --- a/src/MongoDM.Hangfire/ServiceCollectionExtensions.cs +++ b/src/MongODM.Hangfire/ServiceCollectionExtensions.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.HF.Filters; -using Digicando.MongoDM.HF.Tasks; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Utility; +using Digicando.MongODM.HF.Filters; +using Digicando.MongODM.HF.Tasks; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Utility; using Hangfire; using Microsoft.Extensions.DependencyInjection; -namespace Digicando.MongoDM.HF +namespace Digicando.MongODM.HF { public static class ServiceCollectionExtensions { diff --git a/src/MongoDM.Hangfire/Tasks/TaskRunner.cs b/src/MongODM.Hangfire/Tasks/TaskRunner.cs similarity index 90% rename from src/MongoDM.Hangfire/Tasks/TaskRunner.cs rename to src/MongODM.Hangfire/Tasks/TaskRunner.cs index b9e2cc91..48c16d5f 100644 --- a/src/MongoDM.Hangfire/Tasks/TaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/TaskRunner.cs @@ -1,9 +1,9 @@ -using Digicando.MongoDM.Tasks; +using Digicando.MongODM.Tasks; using Hangfire; using System; using System.Collections.Generic; -namespace Digicando.MongoDM.HF.Tasks +namespace Digicando.MongODM.HF.Tasks { public class TaskRunner : ITaskRunner { diff --git a/src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs similarity index 92% rename from src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs rename to src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs index bf81ae4c..30128113 100644 --- a/src/MongoDM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs +++ b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.Tasks; +using Digicando.MongODM.Tasks; using Hangfire; using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongoDM.HF.Tasks +namespace Digicando.MongODM.HF.Tasks { class UpdateDocDependenciesTaskFacade { diff --git a/src/MongoDM/DbContext.cs b/src/MongODM/DbContext.cs similarity index 96% rename from src/MongoDM/DbContext.cs rename to src/MongODM/DbContext.cs index 7014ec86..82768b09 100644 --- a/src/MongoDM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -1,8 +1,8 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Repositories; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization.Conventions; using MongoDB.Driver; @@ -12,7 +12,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM +namespace Digicando.MongODM { public abstract class DbContext : IDbContext { diff --git a/src/MongoDM/Extensions/ClassMapExtensions.cs b/src/MongODM/Extensions/ClassMapExtensions.cs similarity index 91% rename from src/MongoDM/Extensions/ClassMapExtensions.cs rename to src/MongODM/Extensions/ClassMapExtensions.cs index ffddb70d..2ae89332 100644 --- a/src/MongoDM/Extensions/ClassMapExtensions.cs +++ b/src/MongODM/Extensions/ClassMapExtensions.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Serialization.Serializers; +using Digicando.MongODM.Models; +using Digicando.MongODM.Serialization.Serializers; using MongoDB.Bson.Serialization; using System; using System.Linq.Expressions; -namespace Digicando.MongoDM.Extensions +namespace Digicando.MongODM.Extensions { public static class ClassMapExtensions { diff --git a/src/MongoDM/Extensions/EnumerableExtensions.cs b/src/MongODM/Extensions/EnumerableExtensions.cs similarity index 99% rename from src/MongoDM/Extensions/EnumerableExtensions.cs rename to src/MongODM/Extensions/EnumerableExtensions.cs index 838e2c44..d7510edd 100644 --- a/src/MongoDM/Extensions/EnumerableExtensions.cs +++ b/src/MongODM/Extensions/EnumerableExtensions.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Linq.Expressions; -namespace Digicando.MongoDM.Extensions +namespace Digicando.MongODM.Extensions { public static class EnumerableExtensions { diff --git a/src/MongoDM/GlobalSuppressions.cs b/src/MongODM/GlobalSuppressions.cs similarity index 84% rename from src/MongoDM/GlobalSuppressions.cs rename to src/MongODM/GlobalSuppressions.cs index dbaddd31..8537d114 100644 --- a/src/MongoDM/GlobalSuppressions.cs +++ b/src/MongODM/GlobalSuppressions.cs @@ -4,6 +4,6 @@ // Project-level suppressions either have no target or are given // a specific target and scoped to a namespace, type, member, etc. -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongoDM.Serialization.DocumentSchemaRegister.MembersDependenciesToString~System.String")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongoDM.Serialization.DocumentSchemaRegister.ModelDependenciesToString~System.String")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongODM.Serialization.DocumentSchemaRegister.MembersDependenciesToString~System.String")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongODM.Serialization.DocumentSchemaRegister.ModelDependenciesToString~System.String")] diff --git a/src/MongoDM/IDbContext.cs b/src/MongODM/IDbContext.cs similarity index 85% rename from src/MongoDM/IDbContext.cs rename to src/MongODM/IDbContext.cs index ca31406c..4d8e019a 100644 --- a/src/MongoDM/IDbContext.cs +++ b/src/MongODM/IDbContext.cs @@ -1,14 +1,14 @@ -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Repositories; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Utility; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Utility; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM +namespace Digicando.MongODM { public interface IDbContext { diff --git a/src/MongoDM/Migration/MongoCollectionMigration.cs b/src/MongODM/Migration/MongoCollectionMigration.cs similarity index 97% rename from src/MongoDM/Migration/MongoCollectionMigration.cs rename to src/MongODM/Migration/MongoCollectionMigration.cs index 5edef9b4..18735863 100644 --- a/src/MongoDM/Migration/MongoCollectionMigration.cs +++ b/src/MongODM/Migration/MongoCollectionMigration.cs @@ -2,7 +2,7 @@ using System; using System.Threading.Tasks; -namespace Digicando.MongoDM.Migration +namespace Digicando.MongODM.Migration { /// /// Migrate a collection to another diff --git a/src/MongoDM/Migration/MongoDocumentMigration.cs b/src/MongODM/Migration/MongoDocumentMigration.cs similarity index 96% rename from src/MongoDM/Migration/MongoDocumentMigration.cs rename to src/MongODM/Migration/MongoDocumentMigration.cs index b0367ffb..a2113e75 100644 --- a/src/MongoDM/Migration/MongoDocumentMigration.cs +++ b/src/MongODM/Migration/MongoDocumentMigration.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Serialization; +using Digicando.MongODM.Models; +using Digicando.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; using System.Threading.Tasks; -namespace Digicando.MongoDM.Migration +namespace Digicando.MongODM.Migration { /// /// Migrate documents of a collection from an older version to a newer diff --git a/src/MongoDM/Migration/MongoMigrationBase.cs b/src/MongODM/Migration/MongoMigrationBase.cs similarity index 88% rename from src/MongoDM/Migration/MongoMigrationBase.cs rename to src/MongODM/Migration/MongoMigrationBase.cs index 4e60585a..1b2656ef 100644 --- a/src/MongoDM/Migration/MongoMigrationBase.cs +++ b/src/MongODM/Migration/MongoMigrationBase.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; -namespace Digicando.MongoDM.Migration +namespace Digicando.MongODM.Migration { public abstract class MongoMigrationBase { diff --git a/src/MongoDM/Models/IEntityModel.cs b/src/MongODM/Models/IEntityModel.cs similarity index 87% rename from src/MongoDM/Models/IEntityModel.cs rename to src/MongODM/Models/IEntityModel.cs index f27c4310..d61c9c09 100644 --- a/src/MongoDM/Models/IEntityModel.cs +++ b/src/MongODM/Models/IEntityModel.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public interface IEntityModel : IModel { diff --git a/src/MongoDM/Models/IFileModel.cs b/src/MongODM/Models/IFileModel.cs similarity index 83% rename from src/MongoDM/Models/IFileModel.cs rename to src/MongODM/Models/IFileModel.cs index c1ccc9e4..5db30ac9 100644 --- a/src/MongoDM/Models/IFileModel.cs +++ b/src/MongODM/Models/IFileModel.cs @@ -1,6 +1,6 @@ using System.IO; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public interface IFileModel : IEntityModel { diff --git a/src/MongoDM/Models/IModel.cs b/src/MongODM/Models/IModel.cs similarity index 80% rename from src/MongoDM/Models/IModel.cs rename to src/MongODM/Models/IModel.cs index 9838eef2..1997fec3 100644 --- a/src/MongoDM/Models/IModel.cs +++ b/src/MongODM/Models/IModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public interface IModel { diff --git a/src/MongoDM/MongoDM.csproj b/src/MongODM/MongODM.csproj similarity index 94% rename from src/MongoDM/MongoDM.csproj rename to src/MongODM/MongODM.csproj index 293b8895..8d6ddd78 100644 --- a/src/MongoDM/MongoDM.csproj +++ b/src/MongODM/MongODM.csproj @@ -3,7 +3,7 @@ netstandard2.0 true - Digicando.MongoDM + Digicando.MongODM true 0.19.0-dev Digicando Srl diff --git a/src/MongoDM/ProxyModels/AuditableInterceptor.cs b/src/MongODM/ProxyModels/AuditableInterceptor.cs similarity index 98% rename from src/MongoDM/ProxyModels/AuditableInterceptor.cs rename to src/MongODM/ProxyModels/AuditableInterceptor.cs index 70e4a140..e3431644 100644 --- a/src/MongoDM/ProxyModels/AuditableInterceptor.cs +++ b/src/MongODM/ProxyModels/AuditableInterceptor.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Reflection; -namespace Digicando.MongoDM.ProxyModels +namespace Digicando.MongODM.ProxyModels { public class AuditableInterceptor : ModelInterceptorBase { diff --git a/src/MongoDM/ProxyModels/IAuditable.cs b/src/MongODM/ProxyModels/IAuditable.cs similarity index 90% rename from src/MongoDM/ProxyModels/IAuditable.cs rename to src/MongODM/ProxyModels/IAuditable.cs index b18120a2..04036f7b 100644 --- a/src/MongoDM/ProxyModels/IAuditable.cs +++ b/src/MongODM/ProxyModels/IAuditable.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Reflection; -namespace Digicando.MongoDM.ProxyModels +namespace Digicando.MongODM.ProxyModels { public interface IAuditable { diff --git a/src/MongoDM/ProxyModels/IProxyGenerator.cs b/src/MongODM/ProxyModels/IProxyGenerator.cs similarity index 86% rename from src/MongoDM/ProxyModels/IProxyGenerator.cs rename to src/MongODM/ProxyModels/IProxyGenerator.cs index e9ae4dac..da6ce880 100644 --- a/src/MongoDM/ProxyModels/IProxyGenerator.cs +++ b/src/MongODM/ProxyModels/IProxyGenerator.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongoDM.ProxyModels +namespace Digicando.MongODM.ProxyModels { public interface IProxyGenerator { diff --git a/src/MongoDM/ProxyModels/IReferenceable.cs b/src/MongODM/ProxyModels/IReferenceable.cs similarity index 96% rename from src/MongoDM/ProxyModels/IReferenceable.cs rename to src/MongODM/ProxyModels/IReferenceable.cs index 91558403..9a3ae0d3 100644 --- a/src/MongoDM/ProxyModels/IReferenceable.cs +++ b/src/MongODM/ProxyModels/IReferenceable.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.ProxyModels +namespace Digicando.MongODM.ProxyModels { public interface IReferenceable { diff --git a/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM/ProxyModels/ReferenceableInterceptor.cs similarity index 98% rename from src/MongoDM/ProxyModels/ReferenceableInterceptor.cs rename to src/MongODM/ProxyModels/ReferenceableInterceptor.cs index 0ff19a8c..2d68c264 100644 --- a/src/MongoDM/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM/ProxyModels/ReferenceableInterceptor.cs @@ -2,15 +2,15 @@ using Digicando.DomainHelper; using Digicando.DomainHelper.Attributes; using Digicando.DomainHelper.ProxyModel; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Repositories; +using Digicando.MongODM.Models; +using Digicando.MongODM.Repositories; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongoDM.ProxyModels +namespace Digicando.MongODM.ProxyModels { public class ReferenceableInterceptor : ModelInterceptorBase where TModel : class, IEntityModel diff --git a/src/MongoDM/Repositories/CollectionRepositoryBase.cs b/src/MongODM/Repositories/CollectionRepositoryBase.cs similarity index 97% rename from src/MongoDM/Repositories/CollectionRepositoryBase.cs rename to src/MongODM/Repositories/CollectionRepositoryBase.cs index 0c287b8f..c6645231 100644 --- a/src/MongoDM/Repositories/CollectionRepositoryBase.cs +++ b/src/MongODM/Repositories/CollectionRepositoryBase.cs @@ -1,7 +1,7 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Linq; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public abstract class CollectionRepositoryBase : RepositoryBase, diff --git a/src/MongoDM/Repositories/GridFSRepositoryBase.cs b/src/MongODM/Repositories/GridFSRepositoryBase.cs similarity index 96% rename from src/MongoDM/Repositories/GridFSRepositoryBase.cs rename to src/MongODM/Repositories/GridFSRepositoryBase.cs index 58e02368..cd871f9e 100644 --- a/src/MongoDM/Repositories/GridFSRepositoryBase.cs +++ b/src/MongODM/Repositories/GridFSRepositoryBase.cs @@ -1,7 +1,7 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -13,7 +13,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public abstract class GridFSRepositoryBase : RepositoryBase, diff --git a/src/MongoDM/Repositories/ICollectionRepository.cs b/src/MongODM/Repositories/ICollectionRepository.cs similarity index 95% rename from src/MongoDM/Repositories/ICollectionRepository.cs rename to src/MongODM/Repositories/ICollectionRepository.cs index 987e0289..dc3c6a45 100644 --- a/src/MongoDM/Repositories/ICollectionRepository.cs +++ b/src/MongODM/Repositories/ICollectionRepository.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using MongoDB.Driver; using MongoDB.Driver.Linq; using System; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public interface ICollectionRepository : IRepository { diff --git a/src/MongoDM/Repositories/IGridFSRepository.cs b/src/MongODM/Repositories/IGridFSRepository.cs similarity index 86% rename from src/MongoDM/Repositories/IGridFSRepository.cs rename to src/MongODM/Repositories/IGridFSRepository.cs index 29528d5f..4959eb8a 100644 --- a/src/MongoDM/Repositories/IGridFSRepository.cs +++ b/src/MongODM/Repositories/IGridFSRepository.cs @@ -1,9 +1,9 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using System.IO; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public interface IGridFSRepository : IRepository { diff --git a/src/MongoDM/Repositories/IRepository.cs b/src/MongODM/Repositories/IRepository.cs similarity index 90% rename from src/MongoDM/Repositories/IRepository.cs rename to src/MongODM/Repositories/IRepository.cs index 77227817..d2d6bb2e 100644 --- a/src/MongoDM/Repositories/IRepository.cs +++ b/src/MongODM/Repositories/IRepository.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.Migration; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Serialization; +using Digicando.MongODM.Migration; +using Digicando.MongODM.Models; +using Digicando.MongODM.Serialization; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public interface IRepository { diff --git a/src/MongoDM/Repositories/RepositoryBase.cs b/src/MongODM/Repositories/RepositoryBase.cs similarity index 96% rename from src/MongoDM/Repositories/RepositoryBase.cs rename to src/MongODM/Repositories/RepositoryBase.cs index 844c0d05..11fbbd7a 100644 --- a/src/MongoDM/Repositories/RepositoryBase.cs +++ b/src/MongODM/Repositories/RepositoryBase.cs @@ -1,8 +1,8 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.Migration; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization; +using Digicando.MongODM.Migration; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; using MoreLinq; using System; using System.Collections; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Repositories +namespace Digicando.MongODM.Repositories { public abstract class RepositoryBase : IRepository diff --git a/src/MongoDM/Serialization/DocumentSchema.cs b/src/MongODM/Serialization/DocumentSchema.cs similarity index 93% rename from src/MongoDM/Serialization/DocumentSchema.cs rename to src/MongODM/Serialization/DocumentSchema.cs index 795a818b..e0c23a8c 100644 --- a/src/MongoDM/Serialization/DocumentSchema.cs +++ b/src/MongODM/Serialization/DocumentSchema.cs @@ -1,7 +1,7 @@ using MongoDB.Bson.Serialization; using System; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class DocumentSchema { diff --git a/src/MongoDM/Serialization/DocumentSchemaMemberMap.cs b/src/MongODM/Serialization/DocumentSchemaMemberMap.cs similarity index 98% rename from src/MongoDM/Serialization/DocumentSchemaMemberMap.cs rename to src/MongODM/Serialization/DocumentSchemaMemberMap.cs index a79c176c..4c10e6f9 100644 --- a/src/MongoDM/Serialization/DocumentSchemaMemberMap.cs +++ b/src/MongODM/Serialization/DocumentSchemaMemberMap.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class DocumentSchemaMemberMap { diff --git a/src/MongoDM/Serialization/DocumentSchemaRegister.cs b/src/MongODM/Serialization/DocumentSchemaRegister.cs similarity index 98% rename from src/MongoDM/Serialization/DocumentSchemaRegister.cs rename to src/MongODM/Serialization/DocumentSchemaRegister.cs index 7a27480d..690b6fd7 100644 --- a/src/MongoDM/Serialization/DocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/DocumentSchemaRegister.cs @@ -1,6 +1,6 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Serialization.Modifiers; -using Digicando.MongoDM.Serialization.Serializers; +using Digicando.MongODM.Models; +using Digicando.MongODM.Serialization.Modifiers; +using Digicando.MongODM.Serialization.Serializers; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { class DocumentSchemaRegister : IDocumentSchemaRegister { diff --git a/src/MongoDM/Serialization/DocumentVersion.cs b/src/MongODM/Serialization/DocumentVersion.cs similarity index 99% rename from src/MongoDM/Serialization/DocumentVersion.cs rename to src/MongODM/Serialization/DocumentVersion.cs index ddb78c7b..153a27d8 100644 --- a/src/MongoDM/Serialization/DocumentVersion.cs +++ b/src/MongODM/Serialization/DocumentVersion.cs @@ -2,7 +2,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class DocumentVersion : IComparable { diff --git a/src/MongoDM/Serialization/EntityMember.cs b/src/MongODM/Serialization/EntityMember.cs similarity index 92% rename from src/MongoDM/Serialization/EntityMember.cs rename to src/MongODM/Serialization/EntityMember.cs index d8ffbd7a..f96492a3 100644 --- a/src/MongoDM/Serialization/EntityMember.cs +++ b/src/MongODM/Serialization/EntityMember.cs @@ -1,6 +1,6 @@ using MongoDB.Bson.Serialization; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class EntityMember { diff --git a/src/MongoDM/Serialization/ExtendedBsonDocumentReader.cs b/src/MongODM/Serialization/ExtendedBsonDocumentReader.cs similarity index 87% rename from src/MongoDM/Serialization/ExtendedBsonDocumentReader.cs rename to src/MongODM/Serialization/ExtendedBsonDocumentReader.cs index d2371b85..44102b07 100644 --- a/src/MongoDM/Serialization/ExtendedBsonDocumentReader.cs +++ b/src/MongODM/Serialization/ExtendedBsonDocumentReader.cs @@ -1,7 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.IO; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class ExtendedBsonDocumentReader : BsonDocumentReader { diff --git a/src/MongoDM/Serialization/ExtendedBsonDocumentWriter.cs b/src/MongODM/Serialization/ExtendedBsonDocumentWriter.cs similarity index 87% rename from src/MongoDM/Serialization/ExtendedBsonDocumentWriter.cs rename to src/MongODM/Serialization/ExtendedBsonDocumentWriter.cs index e8b6fb62..5490453f 100644 --- a/src/MongoDM/Serialization/ExtendedBsonDocumentWriter.cs +++ b/src/MongODM/Serialization/ExtendedBsonDocumentWriter.cs @@ -1,7 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.IO; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public class ExtendedBsonDocumentWriter : BsonDocumentWriter { diff --git a/src/MongoDM/Serialization/IDocumentSchemaRegister.cs b/src/MongODM/Serialization/IDocumentSchemaRegister.cs similarity index 97% rename from src/MongoDM/Serialization/IDocumentSchemaRegister.cs rename to src/MongODM/Serialization/IDocumentSchemaRegister.cs index f03a8c6d..5b8c7757 100644 --- a/src/MongoDM/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/IDocumentSchemaRegister.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.Serialization.Modifiers; +using Digicando.MongODM.Serialization.Modifiers; using MongoDB.Bson.Serialization; using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public interface IDocumentSchemaRegister { diff --git a/src/MongoDM/Serialization/IModelSerializerCollector.cs b/src/MongODM/Serialization/IModelSerializerCollector.cs similarity index 73% rename from src/MongoDM/Serialization/IModelSerializerCollector.cs rename to src/MongODM/Serialization/IModelSerializerCollector.cs index 515520d4..da289e88 100644 --- a/src/MongoDM/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM/Serialization/IModelSerializerCollector.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongoDM.Serialization +namespace Digicando.MongODM.Serialization { public interface IModelSerializerCollector { diff --git a/src/MongoDM/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs similarity index 94% rename from src/MongoDM/Serialization/Modifiers/CacheSerializerModifier.cs rename to src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs index 79673e6d..add7642d 100644 --- a/src/MongoDM/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Utility; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongoDM.Serialization.Modifiers +namespace Digicando.MongODM.Serialization.Modifiers { class CacheSerializerModifier : IDisposable { diff --git a/src/MongoDM/Serialization/Modifiers/ISerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/ISerializerModifierAccessor.cs similarity index 85% rename from src/MongoDM/Serialization/Modifiers/ISerializerModifierAccessor.cs rename to src/MongODM/Serialization/Modifiers/ISerializerModifierAccessor.cs index 36e64679..0fc2afaa 100644 --- a/src/MongoDM/Serialization/Modifiers/ISerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/ISerializerModifierAccessor.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongoDM.Serialization.Modifiers +namespace Digicando.MongODM.Serialization.Modifiers { public interface ISerializerModifierAccessor { diff --git a/src/MongoDM/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs similarity index 94% rename from src/MongoDM/Serialization/Modifiers/ReferenceSerializerModifier.cs rename to src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs index 2b6b3ccc..00604979 100644 --- a/src/MongoDM/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Utility; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongoDM.Serialization.Modifiers +namespace Digicando.MongODM.Serialization.Modifiers { class ReferenceSerializerModifier : IDisposable { diff --git a/src/MongoDM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs similarity index 92% rename from src/MongoDM/Serialization/Modifiers/SerializerModifierAccessor.cs rename to src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index 2455d892..fc73f355 100644 --- a/src/MongoDM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -1,7 +1,7 @@ -using Digicando.MongoDM.Utility; +using Digicando.MongODM.Utility; using System; -namespace Digicando.MongoDM.Serialization.Modifiers +namespace Digicando.MongODM.Serialization.Modifiers { class SerializerModifierAccessor : ISerializerModifierAccessor { diff --git a/src/MongoDM/Serialization/Serializers/DictionarySerializer.cs b/src/MongODM/Serialization/Serializers/DictionarySerializer.cs similarity index 99% rename from src/MongoDM/Serialization/Serializers/DictionarySerializer.cs rename to src/MongODM/Serialization/Serializers/DictionarySerializer.cs index 6a6f62d2..cae344ac 100644 --- a/src/MongoDM/Serialization/Serializers/DictionarySerializer.cs +++ b/src/MongODM/Serialization/Serializers/DictionarySerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class DictionarySerializer : DictionarySerializerBase, TKey, TValue>, diff --git a/src/MongoDM/Serialization/Serializers/EnumerableSerializer.cs b/src/MongODM/Serialization/Serializers/EnumerableSerializer.cs similarity index 98% rename from src/MongoDM/Serialization/Serializers/EnumerableSerializer.cs rename to src/MongODM/Serialization/Serializers/EnumerableSerializer.cs index 10dddb0d..7b7cc845 100644 --- a/src/MongoDM/Serialization/Serializers/EnumerableSerializer.cs +++ b/src/MongODM/Serialization/Serializers/EnumerableSerializer.cs @@ -2,7 +2,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class EnumerableSerializer : EnumerableSerializerBase, TItem>, diff --git a/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs similarity index 98% rename from src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs rename to src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs index 4664a7b6..98471045 100644 --- a/src/MongoDM/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -1,7 +1,7 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization.Modifiers; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization.Modifiers; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; @@ -11,7 +11,7 @@ using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class ExtendedClassMapSerializer : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IClassMapContainerSerializer diff --git a/src/MongoDM/Serialization/Serializers/ExtraElementsSerializer.cs b/src/MongODM/Serialization/Serializers/ExtraElementsSerializer.cs similarity index 98% rename from src/MongoDM/Serialization/Serializers/ExtraElementsSerializer.cs rename to src/MongODM/Serialization/Serializers/ExtraElementsSerializer.cs index c556432a..21c1a684 100644 --- a/src/MongoDM/Serialization/Serializers/ExtraElementsSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ExtraElementsSerializer.cs @@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class ExtraElementsSerializer : SerializerBase { diff --git a/src/MongoDM/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs similarity index 96% rename from src/MongoDM/Serialization/Serializers/GeoPointSerializer.cs rename to src/MongODM/Serialization/Serializers/GeoPointSerializer.cs index 04b20846..f681fe36 100644 --- a/src/MongoDM/Serialization/Serializers/GeoPointSerializer.cs +++ b/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs @@ -1,5 +1,5 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.ProxyModels; +using Digicando.MongODM.ProxyModels; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.GeoJsonObjectModel; @@ -8,7 +8,7 @@ using System.Linq.Expressions; using System.Reflection; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class GeoPointSerializer : SerializerBase where TInModel : class diff --git a/src/MongoDM/Serialization/Serializers/HexToBinaryDataSerializer.cs b/src/MongODM/Serialization/Serializers/HexToBinaryDataSerializer.cs similarity index 96% rename from src/MongoDM/Serialization/Serializers/HexToBinaryDataSerializer.cs rename to src/MongODM/Serialization/Serializers/HexToBinaryDataSerializer.cs index f1d65fd7..190fc427 100644 --- a/src/MongoDM/Serialization/Serializers/HexToBinaryDataSerializer.cs +++ b/src/MongODM/Serialization/Serializers/HexToBinaryDataSerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class HexToBinaryDataSerializer : SerializerBase { diff --git a/src/MongoDM/Serialization/Serializers/IClassMapContainerSerializer.cs b/src/MongODM/Serialization/Serializers/IClassMapContainerSerializer.cs similarity index 81% rename from src/MongoDM/Serialization/Serializers/IClassMapContainerSerializer.cs rename to src/MongODM/Serialization/Serializers/IClassMapContainerSerializer.cs index 4664b06c..4298074f 100644 --- a/src/MongoDM/Serialization/Serializers/IClassMapContainerSerializer.cs +++ b/src/MongODM/Serialization/Serializers/IClassMapContainerSerializer.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Text; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public interface IClassMapContainerSerializer { diff --git a/src/MongoDM/Serialization/Serializers/IReferenceContainerSerializer.cs b/src/MongODM/Serialization/Serializers/IReferenceContainerSerializer.cs similarity index 70% rename from src/MongoDM/Serialization/Serializers/IReferenceContainerSerializer.cs rename to src/MongODM/Serialization/Serializers/IReferenceContainerSerializer.cs index 7c3a36f2..f69c46a7 100644 --- a/src/MongoDM/Serialization/Serializers/IReferenceContainerSerializer.cs +++ b/src/MongODM/Serialization/Serializers/IReferenceContainerSerializer.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public interface IReferenceContainerSerializer : IClassMapContainerSerializer { diff --git a/src/MongoDM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs b/src/MongODM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs similarity index 99% rename from src/MongoDM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs rename to src/MongODM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs index 6ac02ed9..b777860e 100644 --- a/src/MongoDM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs +++ b/src/MongODM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class ReadOnlyDictionarySerializer : DictionarySerializerBase, TKey, TValue>, diff --git a/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs similarity index 98% rename from src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs rename to src/MongODM/Serialization/Serializers/ReferenceSerializer.cs index 400c9220..39d614d9 100644 --- a/src/MongoDM/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs @@ -1,7 +1,7 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization.Modifiers; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization.Modifiers; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Conventions; @@ -12,7 +12,7 @@ using System.Reflection; using System.Threading; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class ReferenceSerializer : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongoDM/Serialization/Serializers/ReferenceSerializerAdapter.cs b/src/MongODM/Serialization/Serializers/ReferenceSerializerAdapter.cs similarity index 95% rename from src/MongoDM/Serialization/Serializers/ReferenceSerializerAdapter.cs rename to src/MongODM/Serialization/Serializers/ReferenceSerializerAdapter.cs index a74a4730..d77f71f8 100644 --- a/src/MongoDM/Serialization/Serializers/ReferenceSerializerAdapter.cs +++ b/src/MongODM/Serialization/Serializers/ReferenceSerializerAdapter.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { class ReferenceSerializerAdapter : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongoDM/Serialization/Serializers/ReferenceSerializerSwitch.cs b/src/MongODM/Serialization/Serializers/ReferenceSerializerSwitch.cs similarity index 97% rename from src/MongoDM/Serialization/Serializers/ReferenceSerializerSwitch.cs rename to src/MongODM/Serialization/Serializers/ReferenceSerializerSwitch.cs index ad718567..1d5c9f5a 100644 --- a/src/MongoDM/Serialization/Serializers/ReferenceSerializerSwitch.cs +++ b/src/MongODM/Serialization/Serializers/ReferenceSerializerSwitch.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; using System.Collections.Generic; -namespace Digicando.MongoDM.Serialization.Serializers +namespace Digicando.MongODM.Serialization.Serializers { public class ReferenceSerializerSwitch : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongoDM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs similarity index 87% rename from src/MongoDM/ServiceCollectionExtensions.cs rename to src/MongODM/ServiceCollectionExtensions.cs index 138434de..676858e7 100644 --- a/src/MongoDM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Serialization.Modifiers; -using Digicando.MongoDM.Tasks; -using Digicando.MongoDM.Utility; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; +using Digicando.MongODM.Tasks; +using Digicando.MongODM.Utility; using Microsoft.Extensions.DependencyInjection; -namespace Digicando.MongoDM +namespace Digicando.MongODM { public static class ServiceCollectionExtensions { diff --git a/src/MongoDM/Tasks/ITaskRunner.cs b/src/MongODM/Tasks/ITaskRunner.cs similarity index 86% rename from src/MongoDM/Tasks/ITaskRunner.cs rename to src/MongODM/Tasks/ITaskRunner.cs index 7f9ae06e..8aab6903 100644 --- a/src/MongoDM/Tasks/ITaskRunner.cs +++ b/src/MongODM/Tasks/ITaskRunner.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Digicando.MongoDM.Tasks +namespace Digicando.MongODM.Tasks { public interface ITaskRunner { diff --git a/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs b/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs similarity index 80% rename from src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs rename to src/MongODM/Tasks/IUpdateDocDependenciesTask.cs index 4c97a86d..58325af7 100644 --- a/src/MongoDM/Tasks/IUpdateDocDependenciesTask.cs +++ b/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs @@ -1,8 +1,8 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using System.Collections.Generic; using System.Threading.Tasks; -namespace Digicando.MongoDM.Tasks +namespace Digicando.MongODM.Tasks { public interface IUpdateDocDependenciesTask { diff --git a/src/MongoDM/Tasks/Queues.cs b/src/MongODM/Tasks/Queues.cs similarity index 73% rename from src/MongoDM/Tasks/Queues.cs rename to src/MongODM/Tasks/Queues.cs index 3a34e7ff..2cbeb97d 100644 --- a/src/MongoDM/Tasks/Queues.cs +++ b/src/MongODM/Tasks/Queues.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongoDM.Tasks +namespace Digicando.MongODM.Tasks { public class Queues { diff --git a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs similarity index 94% rename from src/MongoDM/Tasks/UpdateDocDependenciesTask.cs rename to src/MongODM/Tasks/UpdateDocDependenciesTask.cs index 0e4144f8..6ed214d5 100644 --- a/src/MongoDM/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs @@ -1,11 +1,11 @@ -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Repositories; -using Digicando.MongoDM.Serialization.Modifiers; +using Digicando.MongODM.Models; +using Digicando.MongODM.Repositories; +using Digicando.MongODM.Serialization.Modifiers; using MongoDB.Driver; using System.Collections.Generic; using System.Threading.Tasks; -namespace Digicando.MongoDM.Tasks +namespace Digicando.MongODM.Tasks { public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask { diff --git a/src/MongoDM/Utility/AsyncLocalContext.cs b/src/MongODM/Utility/AsyncLocalContext.cs similarity index 94% rename from src/MongoDM/Utility/AsyncLocalContext.cs rename to src/MongODM/Utility/AsyncLocalContext.cs index 287425a9..d3a767d6 100644 --- a/src/MongoDM/Utility/AsyncLocalContext.cs +++ b/src/MongODM/Utility/AsyncLocalContext.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public class AsyncLocalContext : IAsyncLocalContext { diff --git a/src/MongoDM/Utility/AsyncLocalContextAccessor.cs b/src/MongODM/Utility/AsyncLocalContextAccessor.cs similarity index 96% rename from src/MongoDM/Utility/AsyncLocalContextAccessor.cs rename to src/MongODM/Utility/AsyncLocalContextAccessor.cs index adf3c1cf..84d74914 100644 --- a/src/MongoDM/Utility/AsyncLocalContextAccessor.cs +++ b/src/MongODM/Utility/AsyncLocalContextAccessor.cs @@ -1,7 +1,7 @@ using System; using System.Threading; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public class AsyncLocalContextAccessor : IAsyncLocalContextAccessor { diff --git a/src/MongoDM/Utility/ContextAccessorFacade.cs b/src/MongODM/Utility/ContextAccessorFacade.cs similarity index 98% rename from src/MongoDM/Utility/ContextAccessorFacade.cs rename to src/MongODM/Utility/ContextAccessorFacade.cs index 4c79d19a..a69d266d 100644 --- a/src/MongoDM/Utility/ContextAccessorFacade.cs +++ b/src/MongODM/Utility/ContextAccessorFacade.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { class ContextAccessorFacade : IContextAccessorFacade { diff --git a/src/MongoDM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs similarity index 96% rename from src/MongoDM/Utility/DBCache.cs rename to src/MongODM/Utility/DBCache.cs index 52cac940..59353868 100644 --- a/src/MongoDM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -1,8 +1,8 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using System; using System.Collections.Generic; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { class DBCache : IDBCache { diff --git a/src/MongoDM/Utility/DBMaintainer.cs b/src/MongODM/Utility/DBMaintainer.cs similarity index 90% rename from src/MongoDM/Utility/DBMaintainer.cs rename to src/MongODM/Utility/DBMaintainer.cs index a1eb3577..13dbf2c8 100644 --- a/src/MongoDM/Utility/DBMaintainer.cs +++ b/src/MongODM/Utility/DBMaintainer.cs @@ -1,10 +1,10 @@ -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Tasks; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Tasks; using MongoDB.Driver; using System.Linq; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public class DBMaintainer : IDBMaintainer { diff --git a/src/MongoDM/Utility/IAsyncLocalContext.cs b/src/MongODM/Utility/IAsyncLocalContext.cs similarity index 84% rename from src/MongoDM/Utility/IAsyncLocalContext.cs rename to src/MongODM/Utility/IAsyncLocalContext.cs index 5f942b2b..edf18577 100644 --- a/src/MongoDM/Utility/IAsyncLocalContext.cs +++ b/src/MongODM/Utility/IAsyncLocalContext.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public interface IAsyncLocalContext : IDisposable { diff --git a/src/MongoDM/Utility/IAsyncLocalContextAccessor.cs b/src/MongODM/Utility/IAsyncLocalContextAccessor.cs similarity index 87% rename from src/MongoDM/Utility/IAsyncLocalContextAccessor.cs rename to src/MongODM/Utility/IAsyncLocalContextAccessor.cs index 93e5b65a..25040697 100644 --- a/src/MongoDM/Utility/IAsyncLocalContextAccessor.cs +++ b/src/MongODM/Utility/IAsyncLocalContextAccessor.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public interface IAsyncLocalContextAccessor { diff --git a/src/MongoDM/Utility/IContextAccessorFacade.cs b/src/MongODM/Utility/IContextAccessorFacade.cs similarity index 89% rename from src/MongoDM/Utility/IContextAccessorFacade.cs rename to src/MongODM/Utility/IContextAccessorFacade.cs index da061c72..d4ba8a28 100644 --- a/src/MongoDM/Utility/IContextAccessorFacade.cs +++ b/src/MongODM/Utility/IContextAccessorFacade.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public interface IContextAccessorFacade { diff --git a/src/MongoDM/Utility/IDBCache.cs b/src/MongODM/Utility/IDBCache.cs similarity index 83% rename from src/MongoDM/Utility/IDBCache.cs rename to src/MongODM/Utility/IDBCache.cs index f7ff3ae9..533d4875 100644 --- a/src/MongoDM/Utility/IDBCache.cs +++ b/src/MongODM/Utility/IDBCache.cs @@ -1,7 +1,7 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using System.Collections.Generic; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public interface IDBCache { diff --git a/src/MongoDM/Utility/IDBMaintainer.cs b/src/MongODM/Utility/IDBMaintainer.cs similarity index 62% rename from src/MongoDM/Utility/IDBMaintainer.cs rename to src/MongODM/Utility/IDBMaintainer.cs index 01d52758..00d1b614 100644 --- a/src/MongoDM/Utility/IDBMaintainer.cs +++ b/src/MongODM/Utility/IDBMaintainer.cs @@ -1,6 +1,6 @@ -using Digicando.MongoDM.ProxyModels; +using Digicando.MongODM.ProxyModels; -namespace Digicando.MongoDM.Utility +namespace Digicando.MongODM.Utility { public interface IDBMaintainer { diff --git a/test/MongoDM.Tests/Comparers/FakeModelComparer.cs b/test/MongODM.Tests/Comparers/FakeModelComparer.cs similarity index 96% rename from test/MongoDM.Tests/Comparers/FakeModelComparer.cs rename to test/MongODM.Tests/Comparers/FakeModelComparer.cs index f10c7598..2690fa5c 100644 --- a/test/MongoDM.Tests/Comparers/FakeModelComparer.cs +++ b/test/MongODM.Tests/Comparers/FakeModelComparer.cs @@ -1,8 +1,8 @@ -using Digicando.MongoDM.Models; +using Digicando.MongODM.Models; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongoDM.Comparers +namespace Digicando.MongODM.Comparers { public class FakeModelComparer : EqualityComparer { diff --git a/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs similarity index 98% rename from test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs rename to test/MongODM.Tests/ExtendedClassMapSerializerTest.cs index 78989379..43077ddd 100644 --- a/test/MongoDM.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs @@ -1,9 +1,9 @@ using Digicando.DomainHelper; -using Digicando.MongoDM.Comparers; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.Serialization; -using Digicando.MongoDM.Serialization.Modifiers; -using Digicando.MongoDM.Serialization.Serializers; +using Digicando.MongODM.Comparers; +using Digicando.MongODM.Models; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; +using Digicando.MongODM.Serialization.Serializers; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -14,7 +14,7 @@ using System.Threading.Tasks; using Xunit; -namespace Digicando.MongoDM +namespace Digicando.MongODM { public class ExtendedClassMapSerializerTest { diff --git a/test/MongoDM.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Tests/MockHelpers/InterceptorMockHelper.cs similarity index 99% rename from test/MongoDM.Tests/MockHelpers/InterceptorMockHelper.cs rename to test/MongODM.Tests/MockHelpers/InterceptorMockHelper.cs index 776c16e2..40cf02ab 100644 --- a/test/MongoDM.Tests/MockHelpers/InterceptorMockHelper.cs +++ b/test/MongODM.Tests/MockHelpers/InterceptorMockHelper.cs @@ -3,7 +3,7 @@ using System; using System.Linq.Expressions; -namespace Digicando.MongoDM.MockHelpers +namespace Digicando.MongODM.MockHelpers { public static class InterceptorMockHelper { diff --git a/test/MongoDM.Tests/Models/FakeEntityModelBase.cs b/test/MongODM.Tests/Models/FakeEntityModelBase.cs similarity index 89% rename from test/MongoDM.Tests/Models/FakeEntityModelBase.cs rename to test/MongODM.Tests/Models/FakeEntityModelBase.cs index 6036eaec..758441b4 100644 --- a/test/MongoDM.Tests/Models/FakeEntityModelBase.cs +++ b/test/MongODM.Tests/Models/FakeEntityModelBase.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public abstract class FakeEntityModelBase : ModelBase, IEntityModel { diff --git a/test/MongoDM.Tests/Models/FakeModel.cs b/test/MongODM.Tests/Models/FakeModel.cs similarity index 90% rename from test/MongoDM.Tests/Models/FakeModel.cs rename to test/MongODM.Tests/Models/FakeModel.cs index aef82826..0166479c 100644 --- a/test/MongoDM.Tests/Models/FakeModel.cs +++ b/test/MongODM.Tests/Models/FakeModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public class FakeModel : FakeEntityModelBase { diff --git a/test/MongoDM.Tests/Models/FakeModelProxy.cs b/test/MongODM.Tests/Models/FakeModelProxy.cs similarity index 61% rename from test/MongoDM.Tests/Models/FakeModelProxy.cs rename to test/MongODM.Tests/Models/FakeModelProxy.cs index df86f700..890f7526 100644 --- a/test/MongoDM.Tests/Models/FakeModelProxy.cs +++ b/test/MongODM.Tests/Models/FakeModelProxy.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public class FakeModelProxy : FakeModel { diff --git a/test/MongoDM.Tests/Models/ModelBase.cs b/test/MongODM.Tests/Models/ModelBase.cs similarity index 84% rename from test/MongoDM.Tests/Models/ModelBase.cs rename to test/MongODM.Tests/Models/ModelBase.cs index 2d50e3a4..5162ae12 100644 --- a/test/MongoDM.Tests/Models/ModelBase.cs +++ b/test/MongODM.Tests/Models/ModelBase.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongoDM.Models +namespace Digicando.MongODM.Models { public abstract class ModelBase : IModel { diff --git a/test/MongoDM.Tests/MongoDM.Tests.csproj b/test/MongODM.Tests/MongODM.Tests.csproj similarity index 86% rename from test/MongoDM.Tests/MongoDM.Tests.csproj rename to test/MongODM.Tests/MongODM.Tests.csproj index 94e0fd60..368efd8e 100644 --- a/test/MongoDM.Tests/MongoDM.Tests.csproj +++ b/test/MongODM.Tests/MongODM.Tests.csproj @@ -4,11 +4,11 @@ netcoreapp3.1 true true - Digicando.MongoDM + Digicando.MongODM - + diff --git a/test/MongoDM.Tests/ReferenceableInterceptorTest.cs b/test/MongODM.Tests/ReferenceableInterceptorTest.cs similarity index 97% rename from test/MongoDM.Tests/ReferenceableInterceptorTest.cs rename to test/MongODM.Tests/ReferenceableInterceptorTest.cs index 96f478d9..02461a8b 100644 --- a/test/MongoDM.Tests/ReferenceableInterceptorTest.cs +++ b/test/MongODM.Tests/ReferenceableInterceptorTest.cs @@ -1,14 +1,14 @@ -using Digicando.MongoDM.MockHelpers; -using Digicando.MongoDM.Models; -using Digicando.MongoDM.ProxyModels; -using Digicando.MongoDM.Repositories; +using Digicando.MongODM.MockHelpers; +using Digicando.MongODM.Models; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; using Moq; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Xunit; -namespace Digicando.MongoDM +namespace Digicando.MongODM { public class ReferenceableInterceptorTest { From f749629efb1141c9474be48630cc698c3c799c4f Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 6 Jan 2020 03:33:35 +0100 Subject: [PATCH 05/46] Completed MODM-31-dbcontextoptions --- src/MongODM/DbContext.cs | 10 ++++------ src/MongODM/DbContextOptions.cs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 src/MongODM/DbContextOptions.cs diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index 82768b09..c96b132e 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -21,23 +21,21 @@ public abstract class DbContext : IDbContext // Constructor. public DbContext( - string connectionString, IDBCache dbCache, IDBMaintainer dbMaintainer, - string dbName, IDocumentSchemaRegister documentSchemaRegister, - DocumentVersion documentVersion, + DbContextOptions options, IProxyGenerator proxyGenerator) { DBCache = dbCache; DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; - DocumentVersion = documentVersion; + DocumentVersion = options.DocumentVersion; ProxyGenerator = proxyGenerator; // Initialize MongoDB driver. - Client = new MongoClient(connectionString); - Database = Client.GetDatabase(dbName); + Client = new MongoClient(options.ConnectionString); + Database = Client.GetDatabase(options.DBName); // Init IoC dependencies. documentSchemaRegister.Initialize(this); diff --git a/src/MongODM/DbContextOptions.cs b/src/MongODM/DbContextOptions.cs new file mode 100644 index 00000000..de86eb65 --- /dev/null +++ b/src/MongODM/DbContextOptions.cs @@ -0,0 +1,21 @@ +using Digicando.MongODM.Serialization; + +namespace Digicando.MongODM +{ + public class DbContextOptions + { + public DbContextOptions( + string connectionString, + string dbName, + DocumentVersion documentVersion = null) + { + ConnectionString = connectionString; + DBName = dbName; + DocumentVersion = documentVersion ?? "1.0.0"; + } + + public string ConnectionString { get; } + public string DBName { get; } + public DocumentVersion DocumentVersion { get; } + } +} From 9b0d99f1c8d40e45ac086654e0d400ce2d9f17d9 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 10 Jan 2020 00:17:44 +0100 Subject: [PATCH 06/46] Refactoring execution context provider --- MongODM.sln | 7 ++ .../AsyncLocal/AsyncLocalContext.cs | 29 ++++++++ .../AsyncLocal/AsyncLocalContextHandler.cs | 19 +++++ .../AsyncLocal/IAsyncLocalContext.cs | 7 ++ .../AsyncLocal/IAsyncLocalContextHandler.cs | 8 +++ .../AsyncLocal/IHandledAsyncLocalContext.cs | 9 +++ .../CurrentContextAccessor.cs | 44 ++++++++++++ src/ExecutionContext/ExecutionContext.csproj | 13 ++++ src/ExecutionContext/IContextAccessor.cs | 9 +++ .../ICurrentContextAccessor.cs} | 6 +- .../AsyncLocalContextHangfireFilter.cs | 39 +++++++++++ .../Filters/LocalContextFilter.cs | 39 ----------- .../ServiceCollectionExtensions.cs | 23 ------- .../{TaskRunner.cs => HangfireTaskRunner.cs} | 4 +- src/MongODM/DbContext.cs | 5 +- src/MongODM/DbContextOptions.cs | 38 ++++++++-- src/MongODM/MongODM.csproj | 4 ++ .../Modifiers/CacheSerializerModifier.cs | 6 +- .../Modifiers/ReferenceSerializerModifier.cs | 6 +- .../Modifiers/SerializerModifierAccessor.cs | 16 ++--- src/MongODM/ServiceCollectionExtensions.cs | 34 +++++---- src/MongODM/Utility/AsyncLocalContext.cs | 23 ------- .../Utility/AsyncLocalContextAccessor.cs | 31 --------- src/MongODM/Utility/ContextAccessorFacade.cs | 69 ------------------- src/MongODM/Utility/DBCache.cs | 7 +- src/MongODM/Utility/IAsyncLocalContext.cs | 11 --- .../Utility/IAsyncLocalContextAccessor.cs | 13 ---- 27 files changed, 265 insertions(+), 254 deletions(-) create mode 100644 src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs create mode 100644 src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs create mode 100644 src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs create mode 100644 src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs create mode 100644 src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs create mode 100644 src/ExecutionContext/CurrentContextAccessor.cs create mode 100644 src/ExecutionContext/ExecutionContext.csproj create mode 100644 src/ExecutionContext/IContextAccessor.cs rename src/{MongODM/Utility/IContextAccessorFacade.cs => ExecutionContext/ICurrentContextAccessor.cs} (60%) create mode 100644 src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs delete mode 100644 src/MongODM.Hangfire/Filters/LocalContextFilter.cs delete mode 100644 src/MongODM.Hangfire/ServiceCollectionExtensions.cs rename src/MongODM.Hangfire/Tasks/{TaskRunner.cs => HangfireTaskRunner.cs} (89%) delete mode 100644 src/MongODM/Utility/AsyncLocalContext.cs delete mode 100644 src/MongODM/Utility/AsyncLocalContextAccessor.cs delete mode 100644 src/MongODM/Utility/ContextAccessorFacade.cs delete mode 100644 src/MongODM/Utility/IAsyncLocalContext.cs delete mode 100644 src/MongODM/Utility/IAsyncLocalContextAccessor.cs diff --git a/MongODM.sln b/MongODM.sln index b1bd62ee..4835891f 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Tests", "test\MongO EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Hangfire", "src\MongODM.Hangfire\MongODM.Hangfire.csproj", "{10897D0D-4898-4A4D-8D1E-B2435E93D9A1}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext", "src\ExecutionContext\ExecutionContext.csproj", "{DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -31,6 +33,10 @@ Global {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Release|Any CPU.Build.0 = Release|Any CPU + {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -39,6 +45,7 @@ Global {AA9CE7D3-169F-4725-970E-4CE900E91EDD} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {82789357-2EC1-4159-B3F6-0F830ABAFF4C} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} {10897D0D-4898-4A4D-8D1E-B2435E93D9A1} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} + {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs new file mode 100644 index 00000000..0284d55b --- /dev/null +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Digicando.ExecContext.AsyncLocal +{ + public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext + { + // Fields. + private static readonly AsyncLocal> asyncLocalContext = new AsyncLocal>(); + + // Properties. + public IDictionary Items => asyncLocalContext.Value; + + // Methods. + public IAsyncLocalContextHandler StartNewAsyncLocalContext() => new AsyncLocalContextHandler(this); + + public void OnCreated(IAsyncLocalContextHandler context) + { + if (asyncLocalContext.Value != null) + throw new InvalidOperationException("Only one context at time is supported"); + + asyncLocalContext.Value = new Dictionary(); + } + + public void OnDisposed(IAsyncLocalContextHandler context) => + asyncLocalContext.Value = null; + } +} diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs new file mode 100644 index 00000000..7bc1c476 --- /dev/null +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs @@ -0,0 +1,19 @@ +namespace Digicando.ExecContext.AsyncLocal +{ + public class AsyncLocalContextHandler : IAsyncLocalContextHandler + { + // Fields. + private readonly IHandledAsyncLocalContext handledContext; + + // Constructors. + internal AsyncLocalContextHandler(IHandledAsyncLocalContext handledContext) + { + this.handledContext = handledContext; + handledContext.OnCreated(this); + } + + // Methods. + public void Dispose() => + handledContext.OnDisposed(this); + } +} diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs new file mode 100644 index 00000000..08f0a8d6 --- /dev/null +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs @@ -0,0 +1,7 @@ +namespace Digicando.ExecContext.AsyncLocal +{ + public interface IAsyncLocalContext : IContextAccessor + { + IAsyncLocalContextHandler StartNewAsyncLocalContext(); + } +} \ No newline at end of file diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs new file mode 100644 index 00000000..30d79ad2 --- /dev/null +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs @@ -0,0 +1,8 @@ +using System; + +namespace Digicando.ExecContext.AsyncLocal +{ + public interface IAsyncLocalContextHandler : IDisposable + { + } +} diff --git a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs new file mode 100644 index 00000000..30571507 --- /dev/null +++ b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs @@ -0,0 +1,9 @@ +namespace Digicando.ExecContext.AsyncLocal +{ + internal interface IHandledAsyncLocalContext + { + void OnCreated(IAsyncLocalContextHandler context); + + void OnDisposed(IAsyncLocalContextHandler context); + } +} diff --git a/src/ExecutionContext/CurrentContextAccessor.cs b/src/ExecutionContext/CurrentContextAccessor.cs new file mode 100644 index 00000000..eb867761 --- /dev/null +++ b/src/ExecutionContext/CurrentContextAccessor.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Digicando.ExecContext +{ + public class CurrentContextAccessor : ICurrentContextAccessor + { + // Fields. + private readonly IEnumerable contextAccessors; + + // Constructors. + public CurrentContextAccessor( + IEnumerable contextAccessors) + { + this.contextAccessors = contextAccessors; + } + + // Proeprties. + public IReadOnlyDictionary Items => + CurrentContextItems.ToDictionary(pair => pair.Key, pair => pair.Value); + + public object SyncRoot => CurrentContextItems; + + // Private properties. + private IDictionary CurrentContextItems + { + get + { + foreach (var accessor in contextAccessors) + if (accessor.Items != null) + return accessor.Items; + throw new InvalidOperationException(); + } + } + + // Methods. + public void AddItem(string key, object value) => + CurrentContextItems.Add(key, value); + + public bool RemoveItem(string key) => + CurrentContextItems.Remove(key); + } +} diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj new file mode 100644 index 00000000..fab104c4 --- /dev/null +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -0,0 +1,13 @@ + + + + netstandard2.0 + true + Digicando.ExecContext + true + 0.19.0-dev + Digicando Srl + Execution context provider + + + diff --git a/src/ExecutionContext/IContextAccessor.cs b/src/ExecutionContext/IContextAccessor.cs new file mode 100644 index 00000000..295dfe1b --- /dev/null +++ b/src/ExecutionContext/IContextAccessor.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Digicando.ExecContext +{ + public interface IContextAccessor + { + IDictionary Items { get; } + } +} diff --git a/src/MongODM/Utility/IContextAccessorFacade.cs b/src/ExecutionContext/ICurrentContextAccessor.cs similarity index 60% rename from src/MongODM/Utility/IContextAccessorFacade.cs rename to src/ExecutionContext/ICurrentContextAccessor.cs index d4ba8a28..bcc05a5d 100644 --- a/src/MongODM/Utility/IContextAccessorFacade.cs +++ b/src/ExecutionContext/ICurrentContextAccessor.cs @@ -1,11 +1,11 @@ using System.Collections.Generic; -namespace Digicando.MongODM.Utility +namespace Digicando.ExecContext { - public interface IContextAccessorFacade + public interface ICurrentContextAccessor { // Properties. - IReadOnlyDictionary Items { get; } + IReadOnlyDictionary Items { get; } object SyncRoot { get; } // Methods. diff --git a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs new file mode 100644 index 00000000..620fee7a --- /dev/null +++ b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs @@ -0,0 +1,39 @@ +using Digicando.ExecContext.AsyncLocal; +using Hangfire.Server; +using System.Collections.Generic; + +namespace Digicando.MongODM.HF.Filters +{ + public class AsyncLocalContextHangfireFilter : IServerFilter + { + // Fields. + private readonly Dictionary contextHandlers = new Dictionary(); + private readonly IAsyncLocalContext asyncLocalContext; + + // Constructors. + public AsyncLocalContextHangfireFilter(IAsyncLocalContext asyncLocalContext) + { + this.asyncLocalContext = asyncLocalContext; + } + + // Properties. + public void OnPerforming(PerformingContext filterContext) + { + lock (contextHandlers) + { + contextHandlers.Add(filterContext.BackgroundJob.Id, asyncLocalContext.StartNewAsyncLocalContext()); + } + } + + public void OnPerformed(PerformedContext filterContext) + { + lock (contextHandlers) + { + var jobId = filterContext.BackgroundJob.Id; + var context = contextHandlers[jobId]; + contextHandlers.Remove(jobId); + context.Dispose(); + } + } + } +} diff --git a/src/MongODM.Hangfire/Filters/LocalContextFilter.cs b/src/MongODM.Hangfire/Filters/LocalContextFilter.cs deleted file mode 100644 index faa3d4e0..00000000 --- a/src/MongODM.Hangfire/Filters/LocalContextFilter.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Digicando.MongODM.Utility; -using Hangfire.Server; -using System.Collections.Generic; - -namespace Digicando.MongODM.HF.Filters -{ - class LocalContextFilter : IServerFilter - { - // Fields. - private readonly IAsyncLocalContextAccessor localContextAccessor; - private readonly Dictionary contexts = new Dictionary(); - - // Constructors. - public LocalContextFilter(IAsyncLocalContextAccessor localContextAccessor) - { - this.localContextAccessor = localContextAccessor; - } - - // Properties. - public void OnPerforming(PerformingContext filterContext) - { - lock (contexts) - { - contexts.Add(filterContext.BackgroundJob.Id, localContextAccessor.GetNewLocalContext()); - } - } - - public void OnPerformed(PerformedContext filterContext) - { - lock (contexts) - { - var jobId = filterContext.BackgroundJob.Id; - var context = contexts[jobId]; - contexts.Remove(jobId); - context.Dispose(); - } - } - } -} diff --git a/src/MongODM.Hangfire/ServiceCollectionExtensions.cs b/src/MongODM.Hangfire/ServiceCollectionExtensions.cs deleted file mode 100644 index 67855318..00000000 --- a/src/MongODM.Hangfire/ServiceCollectionExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Digicando.MongODM.HF.Filters; -using Digicando.MongODM.HF.Tasks; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Utility; -using Hangfire; -using Microsoft.Extensions.DependencyInjection; - -namespace Digicando.MongODM.HF -{ - public static class ServiceCollectionExtensions - { - public static void AddMongoDMHangfire(this IServiceCollection services) - where TDbContext : class, IDbContext - where TDbContextImpl : class, TDbContext - where TProxyGenerator: class, IProxyGenerator - { - services.AddMongoDM(); - - // Add Hangfire filters. - GlobalJobFilters.Filters.Add(new LocalContextFilter(AsyncLocalContextAccessor.Instance)); - } - } -} diff --git a/src/MongODM.Hangfire/Tasks/TaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs similarity index 89% rename from src/MongODM.Hangfire/Tasks/TaskRunner.cs rename to src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index 48c16d5f..3bfb5de8 100644 --- a/src/MongODM.Hangfire/Tasks/TaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -5,13 +5,13 @@ namespace Digicando.MongODM.HF.Tasks { - public class TaskRunner : ITaskRunner + public class HangfireTaskRunner : ITaskRunner { // Fields. private readonly IBackgroundJobClient backgroundJobClient; // Constructors. - public TaskRunner( + public HangfireTaskRunner( IBackgroundJobClient backgroundJobClient) { this.backgroundJobClient = backgroundJobClient; diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index c96b132e..17ab904b 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -1,4 +1,5 @@ -using Digicando.MongODM.Models; +using Digicando.ExecContext; +using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; @@ -31,6 +32,7 @@ public DbContext( DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; + ExecContextAccessor = new CurrentContextAccessor(options.ExecContextAccessors); ProxyGenerator = proxyGenerator; // Initialize MongoDB driver. @@ -65,6 +67,7 @@ public DbContext( public IDBMaintainer DBMaintainer { get; } public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } + public ICurrentContextAccessor ExecContextAccessor { get; } public bool IsMigrating { get; private set; } public abstract IReadOnlyDictionary ModelCollectionRepositoryMap { get; } public abstract IReadOnlyDictionary ModelGridFSRepositoryMap { get; } diff --git a/src/MongODM/DbContextOptions.cs b/src/MongODM/DbContextOptions.cs index de86eb65..06aa7c77 100644 --- a/src/MongODM/DbContextOptions.cs +++ b/src/MongODM/DbContextOptions.cs @@ -1,21 +1,51 @@ -using Digicando.MongODM.Serialization; +using Digicando.ExecContext; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Tasks; +using System; +using System.Collections.Generic; namespace Digicando.MongODM { - public class DbContextOptions + public abstract class DbContextOptions { public DbContextOptions( string connectionString, string dbName, - DocumentVersion documentVersion = null) + DocumentVersion documentVersion, + IEnumerable execContextAccessors, + IProxyGenerator proxyGenerator, + ITaskRunner taskRunner) { ConnectionString = connectionString; DBName = dbName; - DocumentVersion = documentVersion ?? "1.0.0"; + DocumentVersion = documentVersion; + ExecContextAccessors = execContextAccessors ?? throw new ArgumentNullException(nameof(execContextAccessors)); + ProxyGenerator = proxyGenerator; + TaskRunner = taskRunner; } public string ConnectionString { get; } public string DBName { get; } public DocumentVersion DocumentVersion { get; } + public IEnumerable ExecContextAccessors { get; } + public IProxyGenerator ProxyGenerator { get; } + public ITaskRunner TaskRunner { get; } + } + + public class DbContextOptions : DbContextOptions + where TDbContext : DbContext + { + public DbContextOptions( + string connectionString, + string dbName, + DocumentVersion documentVersion, + IEnumerable execContextAccessors, + IProxyGenerator proxyGenerator, + ITaskRunner taskRunner) + : base(connectionString, dbName, documentVersion, execContextAccessors, proxyGenerator, taskRunner) + { } + + public Type DbContextType => typeof(TDbContext); } } diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index 8d6ddd78..cc623b5f 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -19,4 +19,8 @@ + + + + diff --git a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs index add7642d..62b484f0 100644 --- a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs @@ -1,4 +1,4 @@ -using Digicando.MongODM.Utility; +using Digicando.ExecContext; using System; using System.Collections; using System.Collections.Generic; @@ -15,7 +15,7 @@ class CacheSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public CacheSerializerModifier(IContextAccessorFacade contextAccessor) + public CacheSerializerModifier(ICurrentContextAccessor contextAccessor) { if (contextAccessor == null) throw new ArgumentNullException(nameof(contextAccessor)); @@ -39,7 +39,7 @@ public void Dispose() public bool NoCache { get; set; } // Static methods. - public static bool IsNoCacheEnabled(IReadOnlyDictionary contextItems) + public static bool IsNoCacheEnabled(IReadOnlyDictionary contextItems) { if (!contextItems.ContainsKey(ModifierKey)) return false; diff --git a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs index 00604979..d172eba2 100644 --- a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -1,4 +1,4 @@ -using Digicando.MongODM.Utility; +using Digicando.ExecContext; using System; using System.Collections; using System.Collections.Generic; @@ -15,7 +15,7 @@ class ReferenceSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public ReferenceSerializerModifier(IContextAccessorFacade contextAccessor) + public ReferenceSerializerModifier(ICurrentContextAccessor contextAccessor) { if (contextAccessor == null) throw new ArgumentNullException(nameof(contextAccessor)); @@ -39,7 +39,7 @@ public void Dispose() public bool ReadOnlyId { get; set; } // Static methods. - public static bool IsReadOnlyIdEnabled(IReadOnlyDictionary contextItems) + public static bool IsReadOnlyIdEnabled(IReadOnlyDictionary contextItems) { if (!contextItems.ContainsKey(ModifierKey)) return false; diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index fc73f355..6e6cf30e 100644 --- a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -1,4 +1,4 @@ -using Digicando.MongODM.Utility; +using Digicando.ExecContext; using System; namespace Digicando.MongODM.Serialization.Modifiers @@ -6,31 +6,31 @@ namespace Digicando.MongODM.Serialization.Modifiers class SerializerModifierAccessor : ISerializerModifierAccessor { // Fields. - private readonly IContextAccessorFacade contextAccessorFacade; + private readonly ICurrentContextAccessor contextAccessorAccessor; // Constructors. public SerializerModifierAccessor( - IContextAccessorFacade contextAccessorFacade) + ICurrentContextAccessor contextAccessorAccessor) { - this.contextAccessorFacade = contextAccessorFacade; + this.contextAccessorAccessor = contextAccessorAccessor; } // Properties. public bool IsReadOnlyReferencedIdEnabled => - ReferenceSerializerModifier.IsReadOnlyIdEnabled(contextAccessorFacade.Items); + ReferenceSerializerModifier.IsReadOnlyIdEnabled(contextAccessorAccessor.Items); public bool IsNoCacheEnabled => - CacheSerializerModifier.IsNoCacheEnabled(contextAccessorFacade.Items); + CacheSerializerModifier.IsNoCacheEnabled(contextAccessorAccessor.Items); // Methods. public IDisposable EnableCacheSerializerModifier(bool noCache) => - new CacheSerializerModifier(contextAccessorFacade) + new CacheSerializerModifier(contextAccessorAccessor) { NoCache = noCache }; public IDisposable EnableReferenceSerializerModifier(bool readOnlyId) => - new ReferenceSerializerModifier(contextAccessorFacade) + new ReferenceSerializerModifier(contextAccessorAccessor) { ReadOnlyId = readOnlyId }; diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index 676858e7..66eefde8 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -1,37 +1,35 @@ -using Digicando.MongODM.ProxyModels; +using Castle.DynamicProxy; using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Tasks; using Digicando.MongODM.Utility; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Digicando.MongODM { public static class ServiceCollectionExtensions { - public static void AddMongoDM(this IServiceCollection services) + public static void UseMongoDbContext( + this IServiceCollection services, + DbContextOptions options) where TDbContext : class, IDbContext - where TDbContextImpl : class, TDbContext - where TProxyGenerator: class, IProxyGenerator - where TTaskRunner: class, ITaskRunner + where TDbContextImpl : DbContext, TDbContext { services.AddSingleton(); - services.AddSingleton(provider => provider.GetService()); + services.AddSingleton(options); - services.AddSingleton(AsyncLocalContextAccessor.Instance); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); + // DbContext dependencies. + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); - // Proxy generator. - services.AddSingleton(); - services.AddSingleton(new Castle.DynamicProxy.ProxyGenerator()); + // Castle proxy generator. + services.TryAddSingleton(new ProxyGenerator()); - // Add tasks. - services.AddSingleton(); - services.AddScoped(); + // Tasks. + services.TryAddScoped(); } } } diff --git a/src/MongODM/Utility/AsyncLocalContext.cs b/src/MongODM/Utility/AsyncLocalContext.cs deleted file mode 100644 index d3a767d6..00000000 --- a/src/MongODM/Utility/AsyncLocalContext.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; - -namespace Digicando.MongODM.Utility -{ - public class AsyncLocalContext : IAsyncLocalContext - { - // Fields. - private readonly IAsyncLocalContextAccessor localContextAccessor; - - // Constructors. - internal AsyncLocalContext(IAsyncLocalContextAccessor localContextAccessor) - { - this.localContextAccessor = localContextAccessor; - localContextAccessor.OnCreated(this); - } - - // Properties. - public IDictionary Items { get; } = new Dictionary(); - - public void Dispose() => - localContextAccessor.OnDisposed(this); - } -} diff --git a/src/MongODM/Utility/AsyncLocalContextAccessor.cs b/src/MongODM/Utility/AsyncLocalContextAccessor.cs deleted file mode 100644 index 84d74914..00000000 --- a/src/MongODM/Utility/AsyncLocalContextAccessor.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Threading; - -namespace Digicando.MongODM.Utility -{ - public class AsyncLocalContextAccessor : IAsyncLocalContextAccessor - { - // Fields. - private static readonly AsyncLocal localContextCurrent = new AsyncLocal(); - - // Properties. - public AsyncLocalContext Context => localContextCurrent.Value; - - // Static properties. - public static IAsyncLocalContextAccessor Instance { get; } = new AsyncLocalContextAccessor(); - - // Methods. - public IAsyncLocalContext GetNewLocalContext() => new AsyncLocalContext(this); - - public void OnCreated(AsyncLocalContext context) - { - if (localContextCurrent.Value != null) - throw new InvalidOperationException("Only one context at time is supported"); - - localContextCurrent.Value = context; - } - - public void OnDisposed(AsyncLocalContext context) => - localContextCurrent.Value = null; - } -} diff --git a/src/MongODM/Utility/ContextAccessorFacade.cs b/src/MongODM/Utility/ContextAccessorFacade.cs deleted file mode 100644 index a69d266d..00000000 --- a/src/MongODM/Utility/ContextAccessorFacade.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Microsoft.AspNetCore.Http; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Digicando.MongODM.Utility -{ - class ContextAccessorFacade : IContextAccessorFacade - { - // Fields. - private readonly IHttpContextAccessor httpContextAccessor; - private readonly IAsyncLocalContextAccessor localContextAccessor; - - // Constructors. - public ContextAccessorFacade( - IHttpContextAccessor httpContextAccessor, - IAsyncLocalContextAccessor localContextAccessor) - { - this.httpContextAccessor = httpContextAccessor; // Provided by Asp.Net, available only on client call. - this.localContextAccessor = localContextAccessor; // Optional context created by user on local async method stack. - } - - // Proeprties. - public IReadOnlyDictionary Items - { - get - { - if (localContextAccessor.Context != null) - return localContextAccessor.Context.Items.ToDictionary(pair => pair.Key as object, pair => pair.Value); - if (httpContextAccessor.HttpContext != null) - return httpContextAccessor.HttpContext.Items.ToDictionary(pair => pair.Key, pair => pair.Value); - throw new InvalidOperationException(); - } - } - - public object SyncRoot - { - get - { - if (localContextAccessor.Context != null) - return localContextAccessor.Context.Items; - if (httpContextAccessor.HttpContext != null) - return httpContextAccessor.HttpContext.Items; - throw new InvalidOperationException(); - } - } - - // Methods. - public void AddItem(string key, object value) - { - if (localContextAccessor.Context != null) - localContextAccessor.Context.Items.Add(key, value); - else if (httpContextAccessor.HttpContext != null) - httpContextAccessor.HttpContext.Items.Add(key, value); - else - throw new InvalidOperationException(); - } - - public bool RemoveItem(string key) - { - if (localContextAccessor.Context != null) - return localContextAccessor.Context.Items.Remove(key); - else if (httpContextAccessor.HttpContext != null) - return httpContextAccessor.HttpContext.Items.Remove(key); - else - throw new InvalidOperationException(); - } - } -} diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs index 59353868..110f2dce 100644 --- a/src/MongODM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -1,4 +1,5 @@ -using Digicando.MongODM.Models; +using Digicando.ExecContext; +using Digicando.MongODM.Models; using System; using System.Collections.Generic; @@ -10,10 +11,10 @@ class DBCache : IDBCache private const string CacheKey = "DBCache"; // Fields. - private readonly IContextAccessorFacade contextAccessor; + private readonly ICurrentContextAccessor contextAccessor; // Constructors. - public DBCache(IContextAccessorFacade contextAccessor) + public DBCache(ICurrentContextAccessor contextAccessor) { this.contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor)); } diff --git a/src/MongODM/Utility/IAsyncLocalContext.cs b/src/MongODM/Utility/IAsyncLocalContext.cs deleted file mode 100644 index edf18577..00000000 --- a/src/MongODM/Utility/IAsyncLocalContext.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Digicando.MongODM.Utility -{ - public interface IAsyncLocalContext : IDisposable - { - // Properties. - IDictionary Items { get; } - } -} diff --git a/src/MongODM/Utility/IAsyncLocalContextAccessor.cs b/src/MongODM/Utility/IAsyncLocalContextAccessor.cs deleted file mode 100644 index 25040697..00000000 --- a/src/MongODM/Utility/IAsyncLocalContextAccessor.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Digicando.MongODM.Utility -{ - public interface IAsyncLocalContextAccessor - { - AsyncLocalContext Context { get; } - - IAsyncLocalContext GetNewLocalContext(); - - void OnCreated(AsyncLocalContext context); - - void OnDisposed(AsyncLocalContext context); - } -} \ No newline at end of file From b1ede17f7be906df73ea8f90ef8fbf1bfa2990fe Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sat, 11 Jan 2020 19:15:09 +0100 Subject: [PATCH 07/46] Cleaned and commented ExecutionContext project --- .../AsyncLocal/AsyncLocalContext.cs | 14 +++++- .../AsyncLocal/AsyncLocalContextHandler.cs | 4 ++ .../AsyncLocal/IAsyncLocalContext.cs | 8 +++- .../AsyncLocal/IAsyncLocalContextHandler.cs | 3 ++ .../AsyncLocal/IHandledAsyncLocalContext.cs | 4 ++ src/ExecutionContext/ContextSelector.cs | 33 ++++++++++++++ .../CurrentContextAccessor.cs | 44 ------------------- src/ExecutionContext/ExecutionContext.csproj | 4 +- src/ExecutionContext/IContext.cs | 13 ++++++ src/ExecutionContext/IContextAccessor.cs | 9 ---- .../ICurrentContextAccessor.cs | 16 ------- .../AsyncLocalContextHangfireFilter.cs | 2 +- src/MongODM/DbContext.cs | 4 +- src/MongODM/DbContextOptions.cs | 6 +-- .../Modifiers/CacheSerializerModifier.cs | 18 ++++---- .../Modifiers/ReferenceSerializerModifier.cs | 18 ++++---- .../Modifiers/SerializerModifierAccessor.cs | 14 +++--- src/MongODM/Utility/DBCache.cs | 20 ++++----- 18 files changed, 119 insertions(+), 115 deletions(-) create mode 100644 src/ExecutionContext/ContextSelector.cs delete mode 100644 src/ExecutionContext/CurrentContextAccessor.cs create mode 100644 src/ExecutionContext/IContext.cs delete mode 100644 src/ExecutionContext/IContextAccessor.cs delete mode 100644 src/ExecutionContext/ICurrentContextAccessor.cs diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index 0284d55b..1c9727a3 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -4,6 +4,18 @@ namespace Digicando.ExecContext.AsyncLocal { + /// + /// Async local context implementation. This can be used on a single or multiple instances. + /// The container permits to have an Item instance inside this + /// method calling tree. + /// + /// + /// + /// Before try to use an async local context, call the method + /// for initialize the container, and receive a context handler. + /// After have used, dispose the handler for destroy the current context dictionary. + /// + /// public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext { // Fields. @@ -13,7 +25,7 @@ public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext public IDictionary Items => asyncLocalContext.Value; // Methods. - public IAsyncLocalContextHandler StartNewAsyncLocalContext() => new AsyncLocalContextHandler(this); + public IAsyncLocalContextHandler InitAsyncLocalContext() => new AsyncLocalContextHandler(this); public void OnCreated(IAsyncLocalContextHandler context) { diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs index 7bc1c476..ca6b995b 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs @@ -1,5 +1,9 @@ namespace Digicando.ExecContext.AsyncLocal { + /// + /// The handler for an initialization. + /// Dispose this for release the context. + /// public class AsyncLocalContextHandler : IAsyncLocalContextHandler { // Fields. diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs index 08f0a8d6..49ac1b30 100644 --- a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs @@ -1,7 +1,11 @@ namespace Digicando.ExecContext.AsyncLocal { - public interface IAsyncLocalContext : IContextAccessor + /// + /// The interface. + /// Permits to create an async local context living with the method calling tree. + /// + public interface IAsyncLocalContext : IContext { - IAsyncLocalContextHandler StartNewAsyncLocalContext(); + IAsyncLocalContextHandler InitAsyncLocalContext(); } } \ No newline at end of file diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs index 30d79ad2..16302b81 100644 --- a/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs @@ -2,6 +2,9 @@ namespace Digicando.ExecContext.AsyncLocal { + /// + /// A disposable interface for + /// public interface IAsyncLocalContextHandler : IDisposable { } diff --git a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs index 30571507..1abe308a 100644 --- a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs @@ -1,5 +1,9 @@ namespace Digicando.ExecContext.AsyncLocal { + /// + /// Interface used by for comunicate with its + /// creator . + /// internal interface IHandledAsyncLocalContext { void OnCreated(IAsyncLocalContextHandler context); diff --git a/src/ExecutionContext/ContextSelector.cs b/src/ExecutionContext/ContextSelector.cs new file mode 100644 index 00000000..d230ade9 --- /dev/null +++ b/src/ExecutionContext/ContextSelector.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; + +namespace Digicando.ExecContext +{ + /// + /// A multi context selector that take different contexts, and select the first available. + /// + public class ContextSelector : IContext + { + // Fields. + private readonly IEnumerable contexts; + + // Constructors. + public ContextSelector( + IEnumerable contexts) + { + this.contexts = contexts; + } + + // Proeprties. + public IDictionary Items + { + get + { + foreach (var context in contexts) + if (context.Items != null) + return context.Items; + throw new InvalidOperationException(); + } + } + } +} diff --git a/src/ExecutionContext/CurrentContextAccessor.cs b/src/ExecutionContext/CurrentContextAccessor.cs deleted file mode 100644 index eb867761..00000000 --- a/src/ExecutionContext/CurrentContextAccessor.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Digicando.ExecContext -{ - public class CurrentContextAccessor : ICurrentContextAccessor - { - // Fields. - private readonly IEnumerable contextAccessors; - - // Constructors. - public CurrentContextAccessor( - IEnumerable contextAccessors) - { - this.contextAccessors = contextAccessors; - } - - // Proeprties. - public IReadOnlyDictionary Items => - CurrentContextItems.ToDictionary(pair => pair.Key, pair => pair.Value); - - public object SyncRoot => CurrentContextItems; - - // Private properties. - private IDictionary CurrentContextItems - { - get - { - foreach (var accessor in contextAccessors) - if (accessor.Items != null) - return accessor.Items; - throw new InvalidOperationException(); - } - } - - // Methods. - public void AddItem(string key, object value) => - CurrentContextItems.Add(key, value); - - public bool RemoveItem(string key) => - CurrentContextItems.Remove(key); - } -} diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index fab104c4..e0617e44 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -1,11 +1,11 @@ - + netstandard2.0 true Digicando.ExecContext true - 0.19.0-dev + 0.19.0 Digicando Srl Execution context provider diff --git a/src/ExecutionContext/IContext.cs b/src/ExecutionContext/IContext.cs new file mode 100644 index 00000000..480ddc20 --- /dev/null +++ b/src/ExecutionContext/IContext.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Digicando.ExecContext +{ + /// + /// Represents an execution context, where information can be put and retrieve alongside + /// the process with a key-value dictionary. + /// + public interface IContext + { + IDictionary Items { get; } + } +} diff --git a/src/ExecutionContext/IContextAccessor.cs b/src/ExecutionContext/IContextAccessor.cs deleted file mode 100644 index 295dfe1b..00000000 --- a/src/ExecutionContext/IContextAccessor.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace Digicando.ExecContext -{ - public interface IContextAccessor - { - IDictionary Items { get; } - } -} diff --git a/src/ExecutionContext/ICurrentContextAccessor.cs b/src/ExecutionContext/ICurrentContextAccessor.cs deleted file mode 100644 index bcc05a5d..00000000 --- a/src/ExecutionContext/ICurrentContextAccessor.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Collections.Generic; - -namespace Digicando.ExecContext -{ - public interface ICurrentContextAccessor - { - // Properties. - IReadOnlyDictionary Items { get; } - object SyncRoot { get; } - - // Methods. - void AddItem(string key, object value); - - bool RemoveItem(string key); - } -} \ No newline at end of file diff --git a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs index 620fee7a..02faef10 100644 --- a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs +++ b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs @@ -21,7 +21,7 @@ public void OnPerforming(PerformingContext filterContext) { lock (contextHandlers) { - contextHandlers.Add(filterContext.BackgroundJob.Id, asyncLocalContext.StartNewAsyncLocalContext()); + contextHandlers.Add(filterContext.BackgroundJob.Id, asyncLocalContext.InitAsyncLocalContext()); } } diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index 17ab904b..77725f6c 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -32,7 +32,7 @@ public DbContext( DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; - ExecContextAccessor = new CurrentContextAccessor(options.ExecContextAccessors); + ExecContextAccessor = new ContextSelector(options.ExecContextAccessors); ProxyGenerator = proxyGenerator; // Initialize MongoDB driver. @@ -67,7 +67,7 @@ public DbContext( public IDBMaintainer DBMaintainer { get; } public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } - public ICurrentContextAccessor ExecContextAccessor { get; } + public IContext ExecContextAccessor { get; } public bool IsMigrating { get; private set; } public abstract IReadOnlyDictionary ModelCollectionRepositoryMap { get; } public abstract IReadOnlyDictionary ModelGridFSRepositoryMap { get; } diff --git a/src/MongODM/DbContextOptions.cs b/src/MongODM/DbContextOptions.cs index 06aa7c77..fec55f3b 100644 --- a/src/MongODM/DbContextOptions.cs +++ b/src/MongODM/DbContextOptions.cs @@ -13,7 +13,7 @@ public DbContextOptions( string connectionString, string dbName, DocumentVersion documentVersion, - IEnumerable execContextAccessors, + IEnumerable execContextAccessors, IProxyGenerator proxyGenerator, ITaskRunner taskRunner) { @@ -28,7 +28,7 @@ public DbContextOptions( public string ConnectionString { get; } public string DBName { get; } public DocumentVersion DocumentVersion { get; } - public IEnumerable ExecContextAccessors { get; } + public IEnumerable ExecContextAccessors { get; } public IProxyGenerator ProxyGenerator { get; } public ITaskRunner TaskRunner { get; } } @@ -40,7 +40,7 @@ public DbContextOptions( string connectionString, string dbName, DocumentVersion documentVersion, - IEnumerable execContextAccessors, + IEnumerable execContextAccessors, IProxyGenerator proxyGenerator, ITaskRunner taskRunner) : base(connectionString, dbName, documentVersion, execContextAccessors, proxyGenerator, taskRunner) diff --git a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs index 62b484f0..fc474609 100644 --- a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs @@ -15,15 +15,15 @@ class CacheSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public CacheSerializerModifier(ICurrentContextAccessor contextAccessor) + public CacheSerializerModifier(IContext context) { - if (contextAccessor == null) - throw new ArgumentNullException(nameof(contextAccessor)); + if (context == null) + throw new ArgumentNullException(nameof(context)); - if (!contextAccessor.Items.ContainsKey(ModifierKey)) - contextAccessor.AddItem(ModifierKey, new List()); + if (!context.Items.ContainsKey(ModifierKey)) + context.Items.Add(ModifierKey, new List()); - requestes = contextAccessor.Items[ModifierKey] as ICollection; + requestes = context.Items[ModifierKey] as ICollection; lock (((ICollection)requestes).SyncRoot) requestes.Add(this); @@ -39,11 +39,11 @@ public void Dispose() public bool NoCache { get; set; } // Static methods. - public static bool IsNoCacheEnabled(IReadOnlyDictionary contextItems) + public static bool IsNoCacheEnabled(IContext context) { - if (!contextItems.ContainsKey(ModifierKey)) + if (!context.Items.ContainsKey(ModifierKey)) return false; - var requestes = contextItems[ModifierKey] as ICollection; + var requestes = context.Items[ModifierKey] as ICollection; lock (((ICollection)requestes).SyncRoot) return requestes.Any(r => r.NoCache); diff --git a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs index d172eba2..6a208353 100644 --- a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -15,15 +15,15 @@ class ReferenceSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public ReferenceSerializerModifier(ICurrentContextAccessor contextAccessor) + public ReferenceSerializerModifier(IContext context) { - if (contextAccessor == null) - throw new ArgumentNullException(nameof(contextAccessor)); + if (context == null) + throw new ArgumentNullException(nameof(context)); - if (!contextAccessor.Items.ContainsKey(ModifierKey)) - contextAccessor.AddItem(ModifierKey, new List()); + if (!context.Items.ContainsKey(ModifierKey)) + context.Items.Add(ModifierKey, new List()); - requestes = contextAccessor.Items[ModifierKey] as ICollection; + requestes = context.Items[ModifierKey] as ICollection; lock (((ICollection)requestes).SyncRoot) requestes.Add(this); @@ -39,11 +39,11 @@ public void Dispose() public bool ReadOnlyId { get; set; } // Static methods. - public static bool IsReadOnlyIdEnabled(IReadOnlyDictionary contextItems) + public static bool IsReadOnlyIdEnabled(IContext context) { - if (!contextItems.ContainsKey(ModifierKey)) + if (!context.Items.ContainsKey(ModifierKey)) return false; - var requestes = contextItems[ModifierKey] as ICollection; + var requestes = context.Items[ModifierKey] as ICollection; lock (((ICollection)requestes).SyncRoot) return requestes.Any(r => r.ReadOnlyId); diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index 6e6cf30e..c562dfbb 100644 --- a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -6,31 +6,31 @@ namespace Digicando.MongODM.Serialization.Modifiers class SerializerModifierAccessor : ISerializerModifierAccessor { // Fields. - private readonly ICurrentContextAccessor contextAccessorAccessor; + private readonly IContext context; // Constructors. public SerializerModifierAccessor( - ICurrentContextAccessor contextAccessorAccessor) + IContext context) { - this.contextAccessorAccessor = contextAccessorAccessor; + this.context = context; } // Properties. public bool IsReadOnlyReferencedIdEnabled => - ReferenceSerializerModifier.IsReadOnlyIdEnabled(contextAccessorAccessor.Items); + ReferenceSerializerModifier.IsReadOnlyIdEnabled(context); public bool IsNoCacheEnabled => - CacheSerializerModifier.IsNoCacheEnabled(contextAccessorAccessor.Items); + CacheSerializerModifier.IsNoCacheEnabled(context); // Methods. public IDisposable EnableCacheSerializerModifier(bool noCache) => - new CacheSerializerModifier(contextAccessorAccessor) + new CacheSerializerModifier(context) { NoCache = noCache }; public IDisposable EnableReferenceSerializerModifier(bool readOnlyId) => - new ReferenceSerializerModifier(contextAccessorAccessor) + new ReferenceSerializerModifier(context) { ReadOnlyId = readOnlyId }; diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs index 110f2dce..87fa82a6 100644 --- a/src/MongODM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -11,12 +11,12 @@ class DBCache : IDBCache private const string CacheKey = "DBCache"; // Fields. - private readonly ICurrentContextAccessor contextAccessor; + private readonly IContext context; // Constructors. - public DBCache(ICurrentContextAccessor contextAccessor) + public DBCache(IContext context) { - this.contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor)); + this.context = context ?? throw new ArgumentNullException(nameof(context)); } // Properties. @@ -24,7 +24,7 @@ public IReadOnlyDictionary LoadedModels { get { - lock (contextAccessor.SyncRoot) + lock (context.Items) return GetScopedCache(); } } @@ -32,7 +32,7 @@ public IReadOnlyDictionary LoadedModels // Methods. public void ClearCache() { - lock (contextAccessor.SyncRoot) + lock (context.Items) GetScopedCache().Clear(); } @@ -44,23 +44,23 @@ public void AddModel(object id, TModel model) if (model == null) throw new ArgumentNullException(nameof(model)); - lock (contextAccessor.SyncRoot) + lock (context.Items) GetScopedCache().Add(id, model); } public void RemoveModel(object id) { - lock (contextAccessor.SyncRoot) + lock (context.Items) GetScopedCache().Remove(id); } // Helpers. private Dictionary GetScopedCache() { - if (!contextAccessor.Items.ContainsKey(CacheKey)) - contextAccessor.AddItem(CacheKey, new Dictionary()); + if (!context.Items.ContainsKey(CacheKey)) + context.Items.Add(CacheKey, new Dictionary()); - return contextAccessor.Items[CacheKey] as Dictionary; + return context.Items[CacheKey] as Dictionary; } } } From 33a420559df72f5def22a2a12be2587267766a44 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 13 Jan 2020 18:42:15 +0100 Subject: [PATCH 08/46] Implementing tests Added roslyn analyzer Documenting --- MongODM.sln | 7 ++++ .../AsyncLocal/AsyncLocalContext.cs | 10 ++--- .../AsyncLocal/AsyncLocalContextHandler.cs | 2 +- src/ExecutionContext/ContextSelector.cs | 17 +++++--- src/ExecutionContext/ExecutionContext.csproj | 7 ++++ src/ExecutionContext/GlobalSuppressions.cs | 6 +++ src/ExecutionContext/IContext.cs | 3 ++ .../ContextSelectorTests.cs | 42 +++++++++++++++++++ .../ExecutionContext.Tests.csproj | 28 +++++++++++++ test/MongODM.Tests/MongODM.Tests.csproj | 10 +++-- 10 files changed, 117 insertions(+), 15 deletions(-) create mode 100644 src/ExecutionContext/GlobalSuppressions.cs create mode 100644 test/ExecutionContext.Tests/ContextSelectorTests.cs create mode 100644 test/ExecutionContext.Tests/ExecutionContext.Tests.csproj diff --git a/MongODM.sln b/MongODM.sln index 4835891f..e3c3a60d 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Hangfire", "src\Mon EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext", "src\ExecutionContext\ExecutionContext.csproj", "{DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecutionContext.Tests", "test\ExecutionContext.Tests\ExecutionContext.Tests.csproj", "{BF4F963A-DBCE-4C53-A209-502F4CAF12C5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,6 +39,10 @@ Global {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Debug|Any CPU.Build.0 = Debug|Any CPU {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Release|Any CPU.ActiveCfg = Release|Any CPU {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}.Release|Any CPU.Build.0 = Release|Any CPU + {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -46,6 +52,7 @@ Global {82789357-2EC1-4159-B3F6-0F830ABAFF4C} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} {10897D0D-4898-4A4D-8D1E-B2435E93D9A1} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} + {BF4F963A-DBCE-4C53-A209-502F4CAF12C5} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index 1c9727a3..86234e7c 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -5,16 +5,14 @@ namespace Digicando.ExecContext.AsyncLocal { /// - /// Async local context implementation. This can be used on a single or multiple instances. + /// Async local context implementation. This can be used as singleton or with multiple instances. /// The container permits to have an Item instance inside this /// method calling tree. /// /// - /// - /// Before try to use an async local context, call the method - /// for initialize the container, and receive a context handler. - /// After have used, dispose the handler for destroy the current context dictionary. - /// + /// Before try to use an async local context, call the method + /// for initialize the container, and receive a context handler. + /// After have used, dispose the handler for destroy the current context dictionary. /// public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext { diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs index ca6b995b..861a4995 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs @@ -4,7 +4,7 @@ /// The handler for an initialization. /// Dispose this for release the context. /// - public class AsyncLocalContextHandler : IAsyncLocalContextHandler + public sealed class AsyncLocalContextHandler : IAsyncLocalContextHandler { // Fields. private readonly IHandledAsyncLocalContext handledContext; diff --git a/src/ExecutionContext/ContextSelector.cs b/src/ExecutionContext/ContextSelector.cs index d230ade9..86fe22f9 100644 --- a/src/ExecutionContext/ContextSelector.cs +++ b/src/ExecutionContext/ContextSelector.cs @@ -4,18 +4,25 @@ namespace Digicando.ExecContext { /// - /// A multi context selector that take different contexts, and select the first available. + /// A multi context selector that take different contexts, and select the first available. /// + /// + /// This class is intended to have the same lifetime of it's consumer. For example, in case + /// of using with a DbContext, the same DbContext instance will use the same ContextSelector + /// instance. This mean that if a DbContext is running over different execution contexts, + /// every invoke on same context needs to return the same dictionary. + /// The simplest way to perform this, is to return the first not null available dictionary + /// on subscribed contexts. + /// public class ContextSelector : IContext { // Fields. private readonly IEnumerable contexts; // Constructors. - public ContextSelector( - IEnumerable contexts) + public ContextSelector(IEnumerable contexts) { - this.contexts = contexts; + this.contexts = contexts ?? throw new ArgumentNullException(nameof(contexts)); } // Proeprties. @@ -26,7 +33,7 @@ public IDictionary Items foreach (var context in contexts) if (context.Items != null) return context.Items; - throw new InvalidOperationException(); + return null; } } } diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index e0617e44..a0023bee 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -10,4 +10,11 @@ Execution context provider + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/ExecutionContext/GlobalSuppressions.cs b/src/ExecutionContext/GlobalSuppressions.cs new file mode 100644 index 00000000..7a3f9213 --- /dev/null +++ b/src/ExecutionContext/GlobalSuppressions.cs @@ -0,0 +1,6 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "This library will be not localized", Scope = "NamespaceAndDescendants", Target = "Digicando.ExecContext")] \ No newline at end of file diff --git a/src/ExecutionContext/IContext.cs b/src/ExecutionContext/IContext.cs index 480ddc20..6a41207e 100644 --- a/src/ExecutionContext/IContext.cs +++ b/src/ExecutionContext/IContext.cs @@ -8,6 +8,9 @@ namespace Digicando.ExecContext /// public interface IContext { + /// + /// The context dictionary. + /// IDictionary Items { get; } } } diff --git a/test/ExecutionContext.Tests/ContextSelectorTests.cs b/test/ExecutionContext.Tests/ContextSelectorTests.cs new file mode 100644 index 00000000..b071a49f --- /dev/null +++ b/test/ExecutionContext.Tests/ContextSelectorTests.cs @@ -0,0 +1,42 @@ +using Moq; +using System; +using System.Collections.Generic; +using Xunit; + +namespace Digicando.ExecContext +{ + public class ContextSelectorTests + { + [Fact] + public void NullContextsException() + { + Assert.Throws(() => new ContextSelector(null)); + } + + [Theory] + [InlineData(false, false, null)] + [InlineData(false, true, "1")] + [InlineData(true, false, "0")] + [InlineData(true, true, "0")] + public void ContextSelection( + bool enableContext1, + bool enableContext2, + string expectedResult) + { + // Setup. + Mock context0 = new Mock(); + Mock context1 = new Mock(); + context0.SetupGet(c => c.Items) + .Returns(enableContext1 ? new Dictionary { { "val", "0" } } : null); + context1.SetupGet(c => c.Items) + .Returns(enableContext2 ? new Dictionary { { "val", "1" } } : null); + var selector = new ContextSelector(new[] { context0.Object, context1.Object }); + + // Action. + var result = selector.Items?["val"] as string; + + // Assert. + Assert.Equal(expectedResult, result); + } + } +} diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj new file mode 100644 index 00000000..8b784722 --- /dev/null +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -0,0 +1,28 @@ + + + + netcoreapp3.1 + true + Digicando.ExecContext + false + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/test/MongODM.Tests/MongODM.Tests.csproj b/test/MongODM.Tests/MongODM.Tests.csproj index 368efd8e..89b7f44b 100644 --- a/test/MongODM.Tests/MongODM.Tests.csproj +++ b/test/MongODM.Tests/MongODM.Tests.csproj @@ -3,8 +3,8 @@ netcoreapp3.1 true - true Digicando.MongODM + false @@ -12,13 +12,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all runtime; build; native; contentfiles; analyzers - - From 8296005ed47796f6ccacd25acf17bed72fa4e1be Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 14 Jan 2020 02:17:32 +0100 Subject: [PATCH 09/46] Implemented tests --- .../AsyncLocal/AsyncLocalContext.cs | 6 +- .../AsyncLocal/AsyncLocalContextHandler.cs | 12 ++- .../AsyncLocal/IHandledAsyncLocalContext.cs | 2 - .../Properties/AssemblyInfo.cs | 4 + .../{ => Properties}/GlobalSuppressions.cs | 0 .../AsyncLocalContextHandlerTests.cs | 34 ++++++++ .../AsyncLocal/AsyncLocalContextTests.cs | 79 +++++++++++++++++++ 7 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 src/ExecutionContext/Properties/AssemblyInfo.cs rename src/ExecutionContext/{ => Properties}/GlobalSuppressions.cs (100%) create mode 100644 test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs create mode 100644 test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index 86234e7c..0af76391 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -23,14 +23,14 @@ public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext public IDictionary Items => asyncLocalContext.Value; // Methods. - public IAsyncLocalContextHandler InitAsyncLocalContext() => new AsyncLocalContextHandler(this); - - public void OnCreated(IAsyncLocalContextHandler context) + public IAsyncLocalContextHandler InitAsyncLocalContext() { if (asyncLocalContext.Value != null) throw new InvalidOperationException("Only one context at time is supported"); asyncLocalContext.Value = new Dictionary(); + + return new AsyncLocalContextHandler(this); } public void OnDisposed(IAsyncLocalContextHandler context) => diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs index 861a4995..f91bea0e 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs @@ -6,18 +6,16 @@ /// public sealed class AsyncLocalContextHandler : IAsyncLocalContextHandler { - // Fields. - private readonly IHandledAsyncLocalContext handledContext; - // Constructors. internal AsyncLocalContextHandler(IHandledAsyncLocalContext handledContext) { - this.handledContext = handledContext; - handledContext.OnCreated(this); + HandledContext = handledContext; } + // Properties. + internal IHandledAsyncLocalContext HandledContext { get; } + // Methods. - public void Dispose() => - handledContext.OnDisposed(this); + public void Dispose() => HandledContext.OnDisposed(this); } } diff --git a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs index 1abe308a..5026cee9 100644 --- a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs @@ -6,8 +6,6 @@ /// internal interface IHandledAsyncLocalContext { - void OnCreated(IAsyncLocalContextHandler context); - void OnDisposed(IAsyncLocalContextHandler context); } } diff --git a/src/ExecutionContext/Properties/AssemblyInfo.cs b/src/ExecutionContext/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..fbf5dc36 --- /dev/null +++ b/src/ExecutionContext/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] +[assembly: InternalsVisibleTo("ExecutionContext.Tests")] diff --git a/src/ExecutionContext/GlobalSuppressions.cs b/src/ExecutionContext/Properties/GlobalSuppressions.cs similarity index 100% rename from src/ExecutionContext/GlobalSuppressions.cs rename to src/ExecutionContext/Properties/GlobalSuppressions.cs diff --git a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs new file mode 100644 index 00000000..5d25f273 --- /dev/null +++ b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs @@ -0,0 +1,34 @@ +using Moq; +using Xunit; + +namespace Digicando.ExecContext.AsyncLocal +{ + public class AsyncLocalContextHandlerTests + { + private readonly Mock handledContext; + private readonly AsyncLocalContextHandler handler; + + public AsyncLocalContextHandlerTests() + { + handledContext = new Mock(); + handler = new AsyncLocalContextHandler(handledContext.Object); + } + + [Fact] + public void Initialization() + { + // Assert. + Assert.Equal(handledContext.Object, handler.HandledContext); + } + + [Fact] + public void HandlerDispose() + { + // Action. + handler.Dispose(); + + // Assert. + handledContext.Verify(c => c.OnDisposed(handler), Times.Once); + } + } +} diff --git a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs new file mode 100644 index 00000000..6ec4759f --- /dev/null +++ b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs @@ -0,0 +1,79 @@ +using System.Threading.Tasks; +using Xunit; + +namespace Digicando.ExecContext.AsyncLocal +{ + public class AsyncLocalContextTests + { + private readonly AsyncLocalContext asyncLocalContext; + + public AsyncLocalContextTests() + { + asyncLocalContext = new AsyncLocalContext(); + } + + [Fact] + public void ItemsNullAtCreation() + { + // Assert. + Assert.Null(asyncLocalContext.Items); + } + + [Fact] + public async Task AsyncLocalLifeCycle() + { + await Task.Run(async () => + { + // Action. + asyncLocalContext.InitAsyncLocalContext(); + + // Assert. + Assert.NotNull(asyncLocalContext.Items); + await Task.Run(() => + { + Assert.NotNull(asyncLocalContext.Items); + }); + }); + + // Assert. + Assert.Null(asyncLocalContext.Items); + } + + [Fact] + public void SyncLocalLifeCycle() + { + void localMethod() + { + // Action. + asyncLocalContext.InitAsyncLocalContext(); + + // Assert. + Assert.NotNull(asyncLocalContext.Items); + void subLocalMethod() + { + Assert.NotNull(asyncLocalContext.Items); + } + subLocalMethod(); + } + localMethod(); + + // Assert. + /* Outside of an async invoker, the container is not automatically disposed. */ + Assert.NotNull(asyncLocalContext.Items); + } + + [Fact] + public void ContextDispose() + { + // Action. + using (var handler = asyncLocalContext.InitAsyncLocalContext()) + { + // Assert. + Assert.NotNull(asyncLocalContext.Items); + } + + // Assert. + Assert.Null(asyncLocalContext.Items); + } + } +} From c33f9c4d20c3fbdc5de2a0ee47fdd73840a8548f Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 15 Jan 2020 01:37:09 +0100 Subject: [PATCH 10/46] Configuration updates --- .../AsyncLocal/IAsyncLocalContext.cs | 2 +- src/ExecutionContext/ContextSelector.cs | 6 +-- .../{IContext.cs => IExecutionContext.cs} | 2 +- src/MongODM/DbContext.cs | 5 +- src/MongODM/DbContextOptions.cs | 50 ++----------------- src/MongODM/MongODM.csproj | 1 + .../Modifiers/CacheSerializerModifier.cs | 4 +- .../Modifiers/ReferenceSerializerModifier.cs | 4 +- .../Modifiers/SerializerModifierAccessor.cs | 4 +- src/MongODM/ServiceCollectionExtensions.cs | 23 +++------ src/MongODM/Utility/DBCache.cs | 4 +- .../ContextSelectorTests.cs | 4 +- 12 files changed, 32 insertions(+), 77 deletions(-) rename src/ExecutionContext/{IContext.cs => IExecutionContext.cs} (91%) diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs index 49ac1b30..0ea26db9 100644 --- a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs @@ -4,7 +4,7 @@ /// The interface. /// Permits to create an async local context living with the method calling tree. /// - public interface IAsyncLocalContext : IContext + public interface IAsyncLocalContext : IExecutionContext { IAsyncLocalContextHandler InitAsyncLocalContext(); } diff --git a/src/ExecutionContext/ContextSelector.cs b/src/ExecutionContext/ContextSelector.cs index 86fe22f9..1a4eeebb 100644 --- a/src/ExecutionContext/ContextSelector.cs +++ b/src/ExecutionContext/ContextSelector.cs @@ -14,13 +14,13 @@ namespace Digicando.ExecContext /// The simplest way to perform this, is to return the first not null available dictionary /// on subscribed contexts. /// - public class ContextSelector : IContext + public class ContextSelector : IExecutionContext { // Fields. - private readonly IEnumerable contexts; + private readonly IEnumerable contexts; // Constructors. - public ContextSelector(IEnumerable contexts) + public ContextSelector(IEnumerable contexts) { this.contexts = contexts ?? throw new ArgumentNullException(nameof(contexts)); } diff --git a/src/ExecutionContext/IContext.cs b/src/ExecutionContext/IExecutionContext.cs similarity index 91% rename from src/ExecutionContext/IContext.cs rename to src/ExecutionContext/IExecutionContext.cs index 6a41207e..0349eef9 100644 --- a/src/ExecutionContext/IContext.cs +++ b/src/ExecutionContext/IExecutionContext.cs @@ -6,7 +6,7 @@ namespace Digicando.ExecContext /// Represents an execution context, where information can be put and retrieve alongside /// the process with a key-value dictionary. /// - public interface IContext + public interface IExecutionContext { /// /// The context dictionary. diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index 77725f6c..1abddbf2 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -25,6 +25,7 @@ public DbContext( IDBCache dbCache, IDBMaintainer dbMaintainer, IDocumentSchemaRegister documentSchemaRegister, + IExecutionContext executionContext, DbContextOptions options, IProxyGenerator proxyGenerator) { @@ -32,7 +33,7 @@ public DbContext( DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; - ExecContextAccessor = new ContextSelector(options.ExecContextAccessors); + ExecutionContext = executionContext; ProxyGenerator = proxyGenerator; // Initialize MongoDB driver. @@ -67,7 +68,7 @@ public DbContext( public IDBMaintainer DBMaintainer { get; } public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } - public IContext ExecContextAccessor { get; } + public IExecutionContext ExecutionContext { get; } public bool IsMigrating { get; private set; } public abstract IReadOnlyDictionary ModelCollectionRepositoryMap { get; } public abstract IReadOnlyDictionary ModelGridFSRepositoryMap { get; } diff --git a/src/MongODM/DbContextOptions.cs b/src/MongODM/DbContextOptions.cs index fec55f3b..f2f8a79a 100644 --- a/src/MongODM/DbContextOptions.cs +++ b/src/MongODM/DbContextOptions.cs @@ -1,51 +1,11 @@ -using Digicando.ExecContext; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Tasks; -using System; -using System.Collections.Generic; +using Digicando.MongODM.Serialization; namespace Digicando.MongODM { - public abstract class DbContextOptions + public class DbContextOptions { - public DbContextOptions( - string connectionString, - string dbName, - DocumentVersion documentVersion, - IEnumerable execContextAccessors, - IProxyGenerator proxyGenerator, - ITaskRunner taskRunner) - { - ConnectionString = connectionString; - DBName = dbName; - DocumentVersion = documentVersion; - ExecContextAccessors = execContextAccessors ?? throw new ArgumentNullException(nameof(execContextAccessors)); - ProxyGenerator = proxyGenerator; - TaskRunner = taskRunner; - } - - public string ConnectionString { get; } - public string DBName { get; } - public DocumentVersion DocumentVersion { get; } - public IEnumerable ExecContextAccessors { get; } - public IProxyGenerator ProxyGenerator { get; } - public ITaskRunner TaskRunner { get; } - } - - public class DbContextOptions : DbContextOptions - where TDbContext : DbContext - { - public DbContextOptions( - string connectionString, - string dbName, - DocumentVersion documentVersion, - IEnumerable execContextAccessors, - IProxyGenerator proxyGenerator, - ITaskRunner taskRunner) - : base(connectionString, dbName, documentVersion, execContextAccessors, proxyGenerator, taskRunner) - { } - - public Type DbContextType => typeof(TDbContext); + public string ConnectionString { get; set; } = "mongodb://localhost/localDb"; + public string DBName { get; set; } = "localDb"; + public DocumentVersion DocumentVersion { get; set; } = "1.0.0"; } } diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index cc623b5f..377c747d 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -14,6 +14,7 @@ + diff --git a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs index fc474609..78e888ff 100644 --- a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs @@ -15,7 +15,7 @@ class CacheSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public CacheSerializerModifier(IContext context) + public CacheSerializerModifier(IExecutionContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); @@ -39,7 +39,7 @@ public void Dispose() public bool NoCache { get; set; } // Static methods. - public static bool IsNoCacheEnabled(IContext context) + public static bool IsNoCacheEnabled(IExecutionContext context) { if (!context.Items.ContainsKey(ModifierKey)) return false; diff --git a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs index 6a208353..3510ff63 100644 --- a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -15,7 +15,7 @@ class ReferenceSerializerModifier : IDisposable private readonly ICollection requestes; // Constructors and dispose. - public ReferenceSerializerModifier(IContext context) + public ReferenceSerializerModifier(IExecutionContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); @@ -39,7 +39,7 @@ public void Dispose() public bool ReadOnlyId { get; set; } // Static methods. - public static bool IsReadOnlyIdEnabled(IContext context) + public static bool IsReadOnlyIdEnabled(IExecutionContext context) { if (!context.Items.ContainsKey(ModifierKey)) return false; diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index c562dfbb..831b0ec4 100644 --- a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -6,11 +6,11 @@ namespace Digicando.MongODM.Serialization.Modifiers class SerializerModifierAccessor : ISerializerModifierAccessor { // Fields. - private readonly IContext context; + private readonly IExecutionContext context; // Constructors. public SerializerModifierAccessor( - IContext context) + IExecutionContext context) { this.context = context; } diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index 66eefde8..17a9a6c8 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ -using Castle.DynamicProxy; -using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Tasks; using Digicando.MongODM.Utility; @@ -10,26 +9,20 @@ namespace Digicando.MongODM { public static class ServiceCollectionExtensions { - public static void UseMongoDbContext( - this IServiceCollection services, - DbContextOptions options) - where TDbContext : class, IDbContext - where TDbContextImpl : DbContext, TDbContext + public static void UseMongODM(this IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(options); - - // DbContext dependencies. + // DbContext internal. + //dependencies services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); - // Castle proxy generator. - services.TryAddSingleton(new ProxyGenerator()); + //tasks + services.TryAddTransient(); - // Tasks. - services.TryAddScoped(); + //castle proxy generator. + services.TryAddSingleton(new Castle.DynamicProxy.ProxyGenerator()); } } } diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs index 87fa82a6..f6a7dd3c 100644 --- a/src/MongODM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -11,10 +11,10 @@ class DBCache : IDBCache private const string CacheKey = "DBCache"; // Fields. - private readonly IContext context; + private readonly IExecutionContext context; // Constructors. - public DBCache(IContext context) + public DBCache(IExecutionContext context) { this.context = context ?? throw new ArgumentNullException(nameof(context)); } diff --git a/test/ExecutionContext.Tests/ContextSelectorTests.cs b/test/ExecutionContext.Tests/ContextSelectorTests.cs index b071a49f..b7a17f68 100644 --- a/test/ExecutionContext.Tests/ContextSelectorTests.cs +++ b/test/ExecutionContext.Tests/ContextSelectorTests.cs @@ -24,8 +24,8 @@ public void ContextSelection( string expectedResult) { // Setup. - Mock context0 = new Mock(); - Mock context1 = new Mock(); + Mock context0 = new Mock(); + Mock context1 = new Mock(); context0.SetupGet(c => c.Items) .Returns(enableContext1 ? new Dictionary { { "val", "0" } } : null); context1.SetupGet(c => c.Items) From 231590933ee1a44ac37adfcf596574d3c5c45c1b Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 16 Jan 2020 18:02:27 +0100 Subject: [PATCH 11/46] Completed MODM-29 --- .../AsyncLocal/AsyncLocalContext.cs | 6 ++-- ...elector.cs => ExecutionContextSelector.cs} | 6 ++-- src/ExecutionContext/IExecutionContext.cs | 2 +- .../AspNetCore/HttpContextExecutionContext.cs | 21 +++++++++++++ src/MongODM/DbContext.cs | 6 +--- src/MongODM/ServiceCollectionExtensions.cs | 31 +++++++++++++++++-- ...ts.cs => ExecutionContextSelectorTests.cs} | 10 +++--- 7 files changed, 63 insertions(+), 19 deletions(-) rename src/ExecutionContext/{ContextSelector.cs => ExecutionContextSelector.cs} (87%) create mode 100644 src/MongODM/AspNetCore/HttpContextExecutionContext.cs rename test/ExecutionContext.Tests/{ContextSelectorTests.cs => ExecutionContextSelectorTests.cs} (73%) diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index 0af76391..f3f141df 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -17,10 +17,10 @@ namespace Digicando.ExecContext.AsyncLocal public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext { // Fields. - private static readonly AsyncLocal> asyncLocalContext = new AsyncLocal>(); + private static readonly AsyncLocal> asyncLocalContext = new AsyncLocal>(); // Properties. - public IDictionary Items => asyncLocalContext.Value; + public IDictionary Items => asyncLocalContext.Value; // Methods. public IAsyncLocalContextHandler InitAsyncLocalContext() @@ -28,7 +28,7 @@ public IAsyncLocalContextHandler InitAsyncLocalContext() if (asyncLocalContext.Value != null) throw new InvalidOperationException("Only one context at time is supported"); - asyncLocalContext.Value = new Dictionary(); + asyncLocalContext.Value = new Dictionary(); return new AsyncLocalContextHandler(this); } diff --git a/src/ExecutionContext/ContextSelector.cs b/src/ExecutionContext/ExecutionContextSelector.cs similarity index 87% rename from src/ExecutionContext/ContextSelector.cs rename to src/ExecutionContext/ExecutionContextSelector.cs index 1a4eeebb..f6ff1869 100644 --- a/src/ExecutionContext/ContextSelector.cs +++ b/src/ExecutionContext/ExecutionContextSelector.cs @@ -14,19 +14,19 @@ namespace Digicando.ExecContext /// The simplest way to perform this, is to return the first not null available dictionary /// on subscribed contexts. /// - public class ContextSelector : IExecutionContext + public class ExecutionContextSelector : IExecutionContext { // Fields. private readonly IEnumerable contexts; // Constructors. - public ContextSelector(IEnumerable contexts) + public ExecutionContextSelector(IEnumerable contexts) { this.contexts = contexts ?? throw new ArgumentNullException(nameof(contexts)); } // Proeprties. - public IDictionary Items + public IDictionary Items { get { diff --git a/src/ExecutionContext/IExecutionContext.cs b/src/ExecutionContext/IExecutionContext.cs index 0349eef9..785c734c 100644 --- a/src/ExecutionContext/IExecutionContext.cs +++ b/src/ExecutionContext/IExecutionContext.cs @@ -11,6 +11,6 @@ public interface IExecutionContext /// /// The context dictionary. /// - IDictionary Items { get; } + IDictionary Items { get; } } } diff --git a/src/MongODM/AspNetCore/HttpContextExecutionContext.cs b/src/MongODM/AspNetCore/HttpContextExecutionContext.cs new file mode 100644 index 00000000..2971d6f7 --- /dev/null +++ b/src/MongODM/AspNetCore/HttpContextExecutionContext.cs @@ -0,0 +1,21 @@ +using Digicando.ExecContext; +using Microsoft.AspNetCore.Http; +using System.Collections.Generic; + +namespace Digicando.MongODM.AspNetCore +{ + public class HttpContextExecutionContext : IExecutionContext + { + // Fields. + private readonly IHttpContextAccessor httpContextAccessor; + + // Constructors. + public HttpContextExecutionContext(IHttpContextAccessor httpContextAccessor) + { + this.httpContextAccessor = httpContextAccessor; + } + + // Properties. + public IDictionary Items => httpContextAccessor.HttpContext.Items; + } +} diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index 1abddbf2..c96b132e 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -1,5 +1,4 @@ -using Digicando.ExecContext; -using Digicando.MongODM.Models; +using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; @@ -25,7 +24,6 @@ public DbContext( IDBCache dbCache, IDBMaintainer dbMaintainer, IDocumentSchemaRegister documentSchemaRegister, - IExecutionContext executionContext, DbContextOptions options, IProxyGenerator proxyGenerator) { @@ -33,7 +31,6 @@ public DbContext( DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; - ExecutionContext = executionContext; ProxyGenerator = proxyGenerator; // Initialize MongoDB driver. @@ -68,7 +65,6 @@ public DbContext( public IDBMaintainer DBMaintainer { get; } public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } - public IExecutionContext ExecutionContext { get; } public bool IsMigrating { get; private set; } public abstract IReadOnlyDictionary ModelCollectionRepositoryMap { get; } public abstract IReadOnlyDictionary ModelGridFSRepositoryMap { get; } diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index 17a9a6c8..0c82d15b 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -1,16 +1,43 @@ -using Digicando.MongODM.Serialization; +using Digicando.ExecContext; +using Digicando.ExecContext.AsyncLocal; +using Digicando.MongODM.AspNetCore; +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Tasks; using Digicando.MongODM.Utility; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using System.Collections.Generic; +using System.Linq; namespace Digicando.MongODM { public static class ServiceCollectionExtensions { - public static void UseMongODM(this IServiceCollection services) + public static void UseMongODM( + this IServiceCollection services, + IEnumerable executionContexts = null) + where TProxyGenerator: class, IProxyGenerator + where TTaskRunner: class, ITaskRunner { + services.TryAddSingleton(serviceProvider => + { + if (executionContexts is null || !executionContexts.Any()) + executionContexts = new IExecutionContext[] //default + { + new HttpContextExecutionContext(serviceProvider.GetService()), + new AsyncLocalContext() + }; + + return executionContexts.Count() == 1 ? + executionContexts.First() : + new ExecutionContextSelector(executionContexts); + }); + services.TryAddSingleton(); + services.TryAddSingleton(); + // DbContext internal. //dependencies services.TryAddTransient(); diff --git a/test/ExecutionContext.Tests/ContextSelectorTests.cs b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs similarity index 73% rename from test/ExecutionContext.Tests/ContextSelectorTests.cs rename to test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs index b7a17f68..d04666bc 100644 --- a/test/ExecutionContext.Tests/ContextSelectorTests.cs +++ b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs @@ -5,12 +5,12 @@ namespace Digicando.ExecContext { - public class ContextSelectorTests + public class ExecutionContextSelectorTests { [Fact] public void NullContextsException() { - Assert.Throws(() => new ContextSelector(null)); + Assert.Throws(() => new ExecutionContextSelector(null)); } [Theory] @@ -27,10 +27,10 @@ public void ContextSelection( Mock context0 = new Mock(); Mock context1 = new Mock(); context0.SetupGet(c => c.Items) - .Returns(enableContext1 ? new Dictionary { { "val", "0" } } : null); + .Returns(enableContext1 ? new Dictionary { { "val", "0" } } : null); context1.SetupGet(c => c.Items) - .Returns(enableContext2 ? new Dictionary { { "val", "1" } } : null); - var selector = new ContextSelector(new[] { context0.Object, context1.Object }); + .Returns(enableContext2 ? new Dictionary { { "val", "1" } } : null); + var selector = new ExecutionContextSelector(new[] { context0.Object, context1.Object }); // Action. var result = selector.Items?["val"] as string; From 58e899139aada69f3698653e070340b6fa48c0c1 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 20 Jan 2020 00:16:21 +0100 Subject: [PATCH 12/46] Fixes Added documentation --- doc/DbContextClassDiagram.drawio | 1 + .../AsyncLocal/AsyncLocalContext.cs | 3 + .../Tasks/HangfireTaskRunner.cs | 4 +- .../Tasks/UpdateDocDependenciesTaskFacade.cs | 4 +- .../AspNetCore/HttpContextExecutionContext.cs | 2 +- src/MongODM/DbContext.cs | 21 +++--- src/MongODM/IDbContext.cs | 67 ++++++++++++++++++- .../Migration/MongoCollectionMigration.cs | 5 +- .../Migration/MongoDocumentMigration.cs | 5 +- src/MongODM/Migration/MongoMigrationBase.cs | 5 +- .../Repositories/CollectionRepositoryBase.cs | 8 +-- .../Repositories/GridFSRepositoryBase.cs | 2 +- src/MongODM/Repositories/IRepository.cs | 4 +- src/MongODM/Repositories/RepositoryBase.cs | 2 +- .../Serialization/DocumentSchemaRegister.cs | 40 ++++++----- .../Serialization/IDocumentSchemaRegister.cs | 23 ++++--- .../IModelSerializerCollector.cs | 9 ++- .../Modifiers/SerializerModifierAccessor.cs | 14 ++-- .../Serializers/ExtendedClassMapSerializer.cs | 16 +++-- .../Serializers/ReferenceSerializer.cs | 21 +++--- src/MongODM/ServiceCollectionExtensions.cs | 9 ++- src/MongODM/Tasks/ITaskRunner.cs | 2 +- .../Tasks/IUpdateDocDependenciesTask.cs | 5 +- .../Tasks/UpdateDocDependenciesTask.cs | 15 +++-- src/MongODM/Utility/DBCache.cs | 20 +++--- src/MongODM/Utility/DBMaintainer.cs | 28 +++++--- src/MongODM/Utility/IDBCache.cs | 21 +++++- src/MongODM/Utility/IDBMaintainer.cs | 15 +++++ .../ExtendedClassMapSerializerTest.cs | 24 ++++--- 29 files changed, 274 insertions(+), 121 deletions(-) create mode 100644 doc/DbContextClassDiagram.drawio diff --git a/doc/DbContextClassDiagram.drawio b/doc/DbContextClassDiagram.drawio new file mode 100644 index 00000000..417627fa --- /dev/null +++ b/doc/DbContextClassDiagram.drawio @@ -0,0 +1 @@ +7V1tc6M4Ev41qcp8sAvE+8fYTmZSN9mdimd37z5dKUax2cHgAzKJ59evBBIGSdjyC8b22ZNJjAAhdbfU3U+3xI0xnH98TuBi9hT7KLwBmv9xY4xuANCB5uA/pGRJSyxgFyXTJPBp2apgHPxCtFCjpW+Bj9LahVkch1mwqBdO4ihCk6xWBpMkfq9f9hqH9acu4BQJBeMJDMXSvwI/m9FS3fZWJ76gYDqjj3YB7fELnPyYJvFbRJ93A4zX/FOcnkNWF+1oOoN+/F4pMu5vjGESx1nxbf4xRCEhLiNbcd9Dw9my3QmKMpUbzP/qg4X/5cufzl9///bg/zH+6/7PHu3LTxi+IdYNO8T1DRakydmSksn+3xtp52AOk2kQ3Rh3+Ky2+MC/cWHeW1Ley+JFcc6snMvQR9aDYTCl901wg1GyqhN/m9K/+ZODSgGc4woHoXj0SOp4hRNUFtdvqdWIiRLwT3lJhBJW8Dh6GcYRaXalghf+cly24MtmCaEak3HWQb25rztROUSvWYXMYt3k2mEY5IJBLnt8iqNpTEsk/SfXj2AGX2CKqneUZWu63F4PRoMhnMxYg9hRR015gkGU4f9YcFl7KkXdNCqevM0xR8eYLHP4jKZBmq2aJz/ZaUP/REkaxLQyvrCTlj2mT8E0gVkQTYtrX7Dq6aYpuWodxmGIlRymxzNaxGmQxcnyCdI5dRTkZ2Cy7LCFn7ESfxifautOsF3fkvhj+RnhWQLidtHRyRV20TCqJ6OXlPwZoyTAKvoXSjAdg9cAJXeTCUrTssVrLjgPfVgMdFRKSIDSu3QZTW4/Fbd+h+mPJt04hj/RcAaj6Zp7umReBpNsjJmB5bzavvpVtLFVG6qwCOidX2Dkh6I5JekcqHUL/ERJFmCD+q4w8Ua5ETigBt+o6M4gxle9hrn9+xpgu9QYvGITi7oDOqDHD3AehMST+ILCn4jUSoQom4fkovLZVZuXmsGkDeijUkRt4M8onqOMjH2NnrWZ40EdFoMevq+Mf2A5fasonVUsf+DSSyF1OaZl5SuzG3+hlvcWVriuS8xwjsrE1Vio9790mOALq0FbSxdDq9OlB0TC6JYmkkVvjyyaQJbCLB30R0nwk5gzHJGwj7UgX9/m4UMC5/jr4H0WZGi8IJ6CMXpPiFqoShTrnZ6PwrJfhiaVtvXc2yyD3dHSFSVsCG7uBkHpR+HDgZJ3JHEm6v4Rx5S143dfitKzoC68pkR2dQm9rbbI7R2Y3BJP7KQIrmtdU1w3Ns+hKPLvCGSEj+IFwtppgEsecnWUUwsfVTVShY5plsQ/EDbPiUGEq8KzJfngMzkChHxWgz9FY/pATPIgWz6jEBID9H51plmNpfFbMkGb8Rqs8KdoHUPpcCfNUZ6QbMqaJG/xzzo+JuMXre5bHJCZgEmG69RFg5/Sij7Sm6p4FVePpzcoJFZRQQSholx6yi7uIVDmVaCECa0TgbI5gbJ2Eyhd40wc67jyJFHBa+TpJYwnPzBrfZjOVtJQSpe2o3TtLiklOr9JVJzuRMUx3L5j1Zhs8BpHVVwss/QCWFXOkWcgiRXROTq/xlQpYfPfF2SCSteh52I1F+RjepwMllNWVeRNiXVkt2UdAdGVukBlpjxF6V2qM02co3a2kTjVCGxw1BkKiMBFJXbGO+fvwTyEEWLjkp4xKgOZTj7GQD7eJ7Mg9L/CZfxGmJtmcPKDHQ1mcRL8wtXClVASVKwQWGDXrhiTO6kSTRDBJ78xadO5oif4UbvwK0wzWjCJwxAu0qAAWPRy/h3EWRbP6UUHmE1sTe9btrf61GXHFj0v4Jh9wxGnF9dua3oBkuklj0LmqKmfg/Pp1yClyOYqzsFm/8f7fD6gCQYNWiAXK366ieJCpPBExRVx6kEuU+kCToJo+jW/ZmSuSp4p3cy6ZpkFvp9PjkmcQYauEU4vyCDLCWsN8A/mylDDBoSFGz7Ex/rqGP+Qy5MMjxTcFxJGJBMilqx3lGZSoVk/+jaLUjk/KMsKaE1WZFYNdt61VYiBikecpIW43Edvc5TktGbikguK5I6r7LQlOxY4AdlRAHnCIGdgwWiW0qPvxOU55lduklC2fidcH/V0gfWGyHpDwuYQvqDwG4k/5WHwUVJcy7G/Mw47rromaYnBhsxO7drlOYGEJCH5ZWuH6uQCsl9j6DPLQBKqr5A2fvmbZP7hqRBonJ0goe3ZEeLOL6hwy/pJhkje2e8s33JO/n5qCk8PQwSTXEBuP63r85HC0c9ojnWe0KWGpp21o28DwGFEPUOX2OMGkAWUdaMtd99QCCifv7uvil0zMnfi7XOBsp6+I3rtCXBkTz9yRMRQiIi0naYAPI6eliyBw5ZgayXgdvjRJlqlo2CK55vIj/v3H2jyRmS+ERbZPWfB3TlnoWTlVjkLQJqz0B5dZajC1RgUJeqCrMLHDM23MAfZ9/NKWtt3SMpzMCzJRChLweCjWAcbr44Y4RxjjzhEGcH5Gqa9KM4UpjwqnDoBVJRYV/fY7TbZUCZtLetsqfDBkbCBV96HS/VSifVUTDOKhdRttzjJZvEUj77wa5zTkBT+jbJsScUfvmWxqlm4Jek3G36uquUHFC2/Pa08YAl+gHnc7ALDEm0QBhqoxWH0m2scZu04twFvf0vSDPFYl7l6Xms2ki3wvZenGmqINxOKRP6N9ugVKJcPrL2CLHKpaA0nN2TrOq84+YEYLIuEyBncGkwuWlp01H9G2XgSL5DP8MCGRVBrMNVr/KwVqZHF6eVS0x4sKAu9NsOCJ5eCyIh+0oif5Wh9TTM1w3Id0zS8TaCdcj5iXi/wdNPxXKDZBlfvkZMTTRk4c3EQMzOvDudptCBxphCFsHcEmU0+597gMxLblipZgP8K+TVsNiDAfacO7qWPEbbp8pQdX1xw3zFYWf2tGutc9ed2tWvIQYKwaq3Rfo/+WPgwo4H028e7Nz8oLKMigvwvtJTGjU8dIt0+59rmpy5ZIBZIgLhyndrB7S1TDJjVR/IVmTkM712jb9XTYqXgjO6AvqtL3LQ1fjh96DOJURIse/VUxxafKglGSpdB8p4/DDFXIzyOB8S8SVtRrSJCSH1Gf5eNZOTu4aW7faZ6dKAZDWoSw9bgILMRLshg+uP5LVpta/R9VXBl8e540PFZLOLAAgOviN+uHJZkxjbqkrYYfObYDSP6Rk+auYCdrB81ydosg4dsdl1xXFSHW0c/Tr3eY69AtlQCw2cP2ahmBXYpZ67DJ/PpfIRQVcZcm5NWYBxZqmS5plfIZuMGjOedq9XNbo0PSfwLRV1v1bgBxjpuYwr5ki2Uq0h6XRhV0uVOEGt7SBD6daAlDoro2meUPaH5C0pGCCtD7KFMApTeFkWP0Wv8aRuyF7eRjSk3rlthzyaYXu3R35cLdMiHtk/DOg9Zp+6p4n9FWMFPUProF327oWtdVv2s33+Wna6BxCwpi6Cz3B64R5RsfsVOoZ2KRbU5CW/7/f4lrtjRLYYLrsOJy80Qazhxa0lcDECo4sRqsNsVMd5VDCynb5ha+dE5oZAtKrH6hsTh11kk/PBiIcvtw+zfZ6/e/09MrxxgeyXxNQhAa5ieLfOsmgzhK2N3T947OmPF+V4w5nN4Xtz4QrDk12brXdmvssfBsdlvKayjvGL1u3JY10xlFrcF1luNsdZhPF8EISqduiUz7VJqccsH8jXtdmdx0NUn/NYSby2FfPzjIu+r9V3HRd0ZWnzmqLvHbXLcE1IuG1B3zGK4rFxGh1Bjm3WTW/DOVhs27nLL39Bju6GvBLhoxGGjANvtbnuGEs7iRecm4q7XtxzspLrFbxaE3Hr3ZH65cU/Y2ftQIi/s8cBJ8PZ3tCTzZx6SZ8r0pCVa14A0Js/ravVdwMHaoPyx11EwMl3uxMmk56RX7ei653EzJ7C1vmmsZs5dJ07L0/uV3Vs9lxM4Cz/Ydss5mq0kOfSsamlcriqguxA3N5y/w9hge3h738D881bnbVvmf18zFhpfSncJmQrYw43q8dVhgmCGHrE7CqMJKrzv4q1ubCEs61n93m4yDnLmkKhoNQTcnHXAKZAzi9GVW9msS6tnJl8Nz2nLgbdFQOfCtLSqA88mz0vzbgSH3sDaH1TUstGOWjZMvkdH8FxsWbb4VQNWFz9clPY7rrbCJCwWJY7iSTV7iZC3lldy0QsS9RJOFlzL2tawR1VjF49Ds6VOm/WY3Z0e84iv2Zh84pF9HioqjQtSKas0wwV916rosLo0mh72Sqtup9mOggP8i2U3uZ3CDezlku2qRBmAfVWJSu/9Pm8VWThYv8V5YuZ9ROKbGzYnKO54RtD/PQpXWaz+o994+4n3v2h3TgGR47ekI5/YeuAgXcRpNSWFp0xRV0mVreu7JBvA44Mm0l0JpNsqtxaMtrcLW3RmBKCPIPs3uxp//w/+TtIJiqMRyyHND5Y3G1JDN6PUqjv9MEVxIg7wrhFsUTSFdzMfygJwuQ1yvfUN4y/vAbNmMbSj/5ltXt28WD2P9pqHvesEKThJsk1VZU5Sa+/KdMS02+uWqodM4iqH2l7Z2DKZaC1l0wGbdeY1ZXNX9spysqW4SFvc3e5V7yeXyMEoftK2C9A1gjjYQHNc29LteuR31zfg0lo9Tbddy/AMfo/uI78ix7n8OJFyNgfoUNY0oPVt3lIGu4qYXk/ZaMlOBiJgXAe+FO4w7SNAZa5oIJ30ez62t0oN3gWRbN583Hd6KNgfXb/TQ3UGcVVnEOVQ8765XwQpB87qH7e1Mo/EtKxDXHEP3EsfXiXk39n4UrEAz2V8qSaqK4fA9h5fnt63K5+OU25d0Uj7nsAoDVDU/Hq+cxpgjiu+llQCqxx3hClsUHg2I+zg7yHYd986z+zrlQHW9E7aYw0wMc3gsgaYrvMx6u7Hl8oCrHMZXwffw2/v1QHA7puOZrkuMCzgmfx2IYqr/w42wMQQ3oUNMEfYzr3rAcYCbRcxwDzFAaa88mvfAeY46wfYkTWYd+kQBzg9DcbwuUsYYJ4yxqGKyO+Nvuv1AdbW+MKHSUwyjFaX4yEwK9bUGPf/AA== \ No newline at end of file diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index f3f141df..ab6d52fa 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -22,6 +22,9 @@ public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext // Properties. public IDictionary Items => asyncLocalContext.Value; + // Static properties. + public static IAsyncLocalContext Instance { get; } = new AsyncLocalContext(); + // Methods. public IAsyncLocalContextHandler InitAsyncLocalContext() { diff --git a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index 3bfb5de8..c061359c 100644 --- a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -18,8 +18,8 @@ public HangfireTaskRunner( } // Methods. - public void RunUpdateDocDependenciesTask(Type modelType, Type keyType, IEnumerable idPaths, object modelId) => + public void RunUpdateDocDependenciesTask(Type dbContextType, Type modelType, Type keyType, IEnumerable idPaths, object modelId) => backgroundJobClient.Enqueue( - task => task.RunAsync(modelType, keyType, idPaths, modelId)); + task => task.RunAsync(dbContextType, modelType, keyType, idPaths, modelId)); } } diff --git a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs index 30128113..d32285dd 100644 --- a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs +++ b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -20,10 +20,10 @@ public UpdateDocDependenciesTaskFacade(IUpdateDocDependenciesTask task) // Methods. [Queue(Queues.DB_MAINTENANCE)] - public Task RunAsync(Type modelType, Type keyType, IEnumerable idPaths, object modelId) => + public Task RunAsync(Type dbContextType, Type modelType, Type keyType, IEnumerable idPaths, object modelId) => typeof(UpdateDocDependenciesTask).GetMethod( nameof(UpdateDocDependenciesTask.RunAsync), BindingFlags.Public | BindingFlags.Instance) - .MakeGenericMethod(modelType, keyType) + .MakeGenericMethod(dbContextType, modelType, keyType) .Invoke(task, new object[] { idPaths, modelId }) as Task; } } diff --git a/src/MongODM/AspNetCore/HttpContextExecutionContext.cs b/src/MongODM/AspNetCore/HttpContextExecutionContext.cs index 2971d6f7..f314a92a 100644 --- a/src/MongODM/AspNetCore/HttpContextExecutionContext.cs +++ b/src/MongODM/AspNetCore/HttpContextExecutionContext.cs @@ -16,6 +16,6 @@ public HttpContextExecutionContext(IHttpContextAccessor httpContextAccessor) } // Properties. - public IDictionary Items => httpContextAccessor.HttpContext.Items; + public IDictionary Items => httpContextAccessor?.HttpContext?.Items; } } diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index c96b132e..ffa19f2e 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -2,6 +2,7 @@ using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization.Conventions; @@ -19,26 +20,29 @@ public abstract class DbContext : IDbContext // Consts. public const string DocumentVersionElementName = "v"; - // Constructor. + // Constructors and initialization. public DbContext( IDBCache dbCache, IDBMaintainer dbMaintainer, IDocumentSchemaRegister documentSchemaRegister, DbContextOptions options, - IProxyGenerator proxyGenerator) + IProxyGenerator proxyGenerator, + ISerializerModifierAccessor serializerModifierAccessor) { DBCache = dbCache; DBMaintainer = dbMaintainer; DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; ProxyGenerator = proxyGenerator; + SerializerModifierAccessor = serializerModifierAccessor; // Initialize MongoDB driver. Client = new MongoClient(options.ConnectionString); Database = Client.GetDatabase(options.DBName); // Init IoC dependencies. - documentSchemaRegister.Initialize(this); + DocumentSchemaRegister.Initialize(this); + DBMaintainer.Initialize(this); // Customize conventions. ConventionRegistry.Register("Enum string", new ConventionPack @@ -48,10 +52,10 @@ public DbContext( // Register serializers. foreach (var serializerCollector in SerializerCollectors) - serializerCollector.Register(this); + serializerCollector.Register(DBCache, DocumentSchemaRegister, ProxyGenerator); // Build and freeze document schema register. - documentSchemaRegister.Freeze(); + DocumentSchemaRegister.Freeze(); } // Public properties. @@ -74,12 +78,13 @@ public DbContext( ModelGridFSRepositoryMap.Select(pair => (pair.Key, pair.Value as IRepository))) .ToDictionary(pair => pair.ModelType, pair => pair.Repository); public IProxyGenerator ProxyGenerator { get; } + public ISerializerModifierAccessor SerializerModifierAccessor { get; } // Protected properties. protected abstract IEnumerable SerializerCollectors { get; } // Methods. - public async Task MigrateRepositoriesAsync() + public async Task MigrateRepositoriesAsync(CancellationToken cancellationToken = default) { // Migrate documents. IsMigrating = true; @@ -87,11 +92,11 @@ public async Task MigrateRepositoriesAsync() where repository.MigrationInfo != null orderby repository.MigrationInfo.PriorityIndex select repository.MigrationInfo) - await migration.MigrateAsync(); + await migration.MigrateAsync(cancellationToken); // Build indexes. foreach (var repository in ModelCollectionRepositoryMap.Values) - await repository.BuildIndexesAsync(DocumentSchemaRegister); + await repository.BuildIndexesAsync(DocumentSchemaRegister, cancellationToken); IsMigrating = false; } diff --git a/src/MongODM/IDbContext.cs b/src/MongODM/IDbContext.cs index 4d8e019a..a7f11240 100644 --- a/src/MongODM/IDbContext.cs +++ b/src/MongODM/IDbContext.cs @@ -1,6 +1,7 @@ using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Utility; using MongoDB.Driver; using System; @@ -10,26 +11,90 @@ namespace Digicando.MongODM { + /// + /// Interface of implementation. + /// public interface IDbContext { // Properties. + /// + /// Current MongoDB client. + /// IMongoClient Client { get; } + + /// + /// Current MongoDB database. + /// IMongoDatabase Database { get; } + + /// + /// Database cache container. + /// IDBCache DBCache { get; } + + /// + /// Database operator interested into maintenance tasks. + /// IDBMaintainer DBMaintainer { get; } + + /// + /// Container for model serialization and document schema information. + /// IDocumentSchemaRegister DocumentSchemaRegister { get; } + + /// + /// Current operating document version. + /// DocumentVersion DocumentVersion { get; } + + /// + /// Flag reporting eventual current migration operation. + /// bool IsMigrating { get; } + + /// + /// Model-Repository map for collection types. + /// IReadOnlyDictionary ModelCollectionRepositoryMap { get; } + + /// + /// Model-Repository map for gridfs types. + /// IReadOnlyDictionary ModelGridFSRepositoryMap { get; } + + /// + /// Model-Repository map for both collection and gridfs types. + /// IReadOnlyDictionary ModelRepositoryMap { get; } + + /// + /// Current model proxy generator. + /// IProxyGenerator ProxyGenerator { get; } + /// + /// Serializer modifier accessor. + /// + ISerializerModifierAccessor SerializerModifierAccessor { get; } + // Methods. - Task MigrateRepositoriesAsync(); + /// + /// Start a database migration process. + /// + /// Cancellation token + Task MigrateRepositoriesAsync(CancellationToken cancellationToken = default); + /// + /// Save current model changes on db. + /// + /// Cancellation token Task SaveChangesAsync(CancellationToken cancellationToken = default); + /// + /// Start a new database transaction session. + /// + /// Cancellation token + /// The session handler Task StartSessionAsync(CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/MongODM/Migration/MongoCollectionMigration.cs b/src/MongODM/Migration/MongoCollectionMigration.cs index 18735863..6b5d756c 100644 --- a/src/MongODM/Migration/MongoCollectionMigration.cs +++ b/src/MongODM/Migration/MongoCollectionMigration.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using System; +using System.Threading; using System.Threading.Tasks; namespace Digicando.MongODM.Migration @@ -30,7 +31,7 @@ public MongoCollectionMigration( this.sourceCollection = sourceCollection; } - public override async Task MigrateAsync() + public override async Task MigrateAsync(CancellationToken cancellationToken = default) { // Migrate documents. await sourceCollection.Find(Builders.Filter.Empty, new FindOptions { NoCursorTimeout = true }) @@ -38,7 +39,7 @@ public override async Task MigrateAsync() { if (discriminator(obj)) destinationCollection.InsertOneAsync(converter(obj)); - }); + }, cancellationToken); } } } diff --git a/src/MongODM/Migration/MongoDocumentMigration.cs b/src/MongODM/Migration/MongoDocumentMigration.cs index a2113e75..2e4e0f1f 100644 --- a/src/MongODM/Migration/MongoDocumentMigration.cs +++ b/src/MongODM/Migration/MongoDocumentMigration.cs @@ -2,6 +2,7 @@ using Digicando.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; +using System.Threading; using System.Threading.Tasks; namespace Digicando.MongODM.Migration @@ -31,7 +32,7 @@ public MongoDocumentMigration( /// /// Fix all documents prev of MinimumDocumentVersion /// - public override async Task MigrateAsync() + public override async Task MigrateAsync(CancellationToken cancellationToken = default) { var filterBuilder = Builders.Filter; var filter = filterBuilder.Or( @@ -59,7 +60,7 @@ public override async Task MigrateAsync() // Replace documents. await collection.Find(filter, new FindOptions { NoCursorTimeout = true }) - .ForEachAsync(obj => collection.ReplaceOneAsync(Builders.Filter.Eq(m => m.Id, obj.Id), obj)); + .ForEachAsync(obj => collection.ReplaceOneAsync(Builders.Filter.Eq(m => m.Id, obj.Id), obj), cancellationToken); } } } diff --git a/src/MongODM/Migration/MongoMigrationBase.cs b/src/MongODM/Migration/MongoMigrationBase.cs index 1b2656ef..29062c56 100644 --- a/src/MongODM/Migration/MongoMigrationBase.cs +++ b/src/MongODM/Migration/MongoMigrationBase.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Threading; +using System.Threading.Tasks; namespace Digicando.MongODM.Migration { @@ -11,6 +12,6 @@ public MongoMigrationBase(int priorityIndex) public int PriorityIndex { get; } - public abstract Task MigrateAsync(); + public abstract Task MigrateAsync(CancellationToken cancellationToken = default); } } diff --git a/src/MongODM/Repositories/CollectionRepositoryBase.cs b/src/MongODM/Repositories/CollectionRepositoryBase.cs index c6645231..91aa5dfe 100644 --- a/src/MongODM/Repositories/CollectionRepositoryBase.cs +++ b/src/MongODM/Repositories/CollectionRepositoryBase.cs @@ -39,7 +39,7 @@ public CollectionRepositoryBase( new (IndexKeysDefinition keys, CreateIndexOptions options)[0]; // Public methods. - public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister) + public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) { var newIndexes = new List<(string name, CreateIndexModel createIndex)>(); @@ -83,7 +83,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis // Get current indexes. var currentIndexes = new List(); - using (var indexList = await Collection.Indexes.ListAsync()) + using (var indexList = await Collection.Indexes.ListAsync(cancellationToken)) while (indexList.MoveNext()) currentIndexes.AddRange(indexList.Current); @@ -94,11 +94,11 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis where !newIndexes.Any(newIndex => newIndex.name == indexName) select index) { - await Collection.Indexes.DropOneAsync(oldIndex.GetElement("name").Value.ToString()); + await Collection.Indexes.DropOneAsync(oldIndex.GetElement("name").Value.ToString(), cancellationToken); } // Build new indexes. - await Collection.Indexes.CreateManyAsync(newIndexes.Select(i => i.createIndex)); + await Collection.Indexes.CreateManyAsync(newIndexes.Select(i => i.createIndex), cancellationToken); } public virtual Task> FindAsync( diff --git a/src/MongODM/Repositories/GridFSRepositoryBase.cs b/src/MongODM/Repositories/GridFSRepositoryBase.cs index cd871f9e..541aee98 100644 --- a/src/MongODM/Repositories/GridFSRepositoryBase.cs +++ b/src/MongODM/Repositories/GridFSRepositoryBase.cs @@ -41,7 +41,7 @@ public GridFSRepositoryBase( protected IProxyGenerator ProxyGenerator { get; } // Methods. - public override Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister) => Task.CompletedTask; + public override Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) => Task.CompletedTask; public virtual Task DownloadAsBytesAsync(string id, CancellationToken cancellationToken = default) => GridFSBucket.DownloadAsBytesAsync(ObjectId.Parse(id), null, cancellationToken); diff --git a/src/MongODM/Repositories/IRepository.cs b/src/MongODM/Repositories/IRepository.cs index d2d6bb2e..d56e8427 100644 --- a/src/MongODM/Repositories/IRepository.cs +++ b/src/MongODM/Repositories/IRepository.cs @@ -11,7 +11,9 @@ public interface IRepository { MongoMigrationBase MigrationInfo { get; } - Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister); + Task BuildIndexesAsync( + IDocumentSchemaRegister schemaRegister, + CancellationToken cancellationToken = default); Task DeleteAsync( IEntityModel model, diff --git a/src/MongODM/Repositories/RepositoryBase.cs b/src/MongODM/Repositories/RepositoryBase.cs index 11fbbd7a..b46621b5 100644 --- a/src/MongODM/Repositories/RepositoryBase.cs +++ b/src/MongODM/Repositories/RepositoryBase.cs @@ -30,7 +30,7 @@ public RepositoryBase(IDbContext dbContext) public virtual MongoMigrationBase MigrationInfo { get; } // Public methods. - public abstract Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister); + public abstract Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default); public virtual async Task CreateAsync(IEnumerable models, CancellationToken cancellationToken = default) { diff --git a/src/MongODM/Serialization/DocumentSchemaRegister.cs b/src/MongODM/Serialization/DocumentSchemaRegister.cs index 690b6fd7..dce5b813 100644 --- a/src/MongODM/Serialization/DocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/DocumentSchemaRegister.cs @@ -16,6 +16,7 @@ namespace Digicando.MongODM.Serialization class DocumentSchemaRegister : IDocumentSchemaRegister { // Fields. + private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly Dictionary> memberDependenciesMap = new Dictionary>(); private readonly Dictionary> modelDependenciesMap = @@ -34,36 +35,34 @@ public DocumentSchemaRegister( this.serializerModifierAccessor = serializerModifierAccessor; } - //here for circular dependency injection with DBContext public void Initialize(IDbContext dbContext) { - if (this.dbContext != null) + if (IsInitialized) throw new InvalidOperationException("Instance already initialized"); this.dbContext = dbContext; + + IsInitialized = true; } // Properties. - public ReaderWriterLockSlim ConfigLock { get; } = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); public bool IsFrozen { get; private set; } + public bool IsInitialized { get; private set; } public IEnumerable Schemas => schemas; // Methods. - public IDocumentSchemaRegister Freeze() + public void Freeze() { - ConfigLock.EnterReadLock(); + configLock.EnterReadLock(); try { - if (IsFrozen) - { - return this; - } + if (IsFrozen) return; } finally { - ConfigLock.ExitReadLock(); + configLock.ExitReadLock(); } - ConfigLock.EnterWriteLock(); + configLock.EnterWriteLock(); try { if (!IsFrozen) @@ -98,10 +97,8 @@ public IDocumentSchemaRegister Freeze() } finally { - ConfigLock.ExitWriteLock(); + configLock.ExitWriteLock(); } - - return this; } public IEnumerable GetMemberDependencies(MemberInfo memberInfo) @@ -134,7 +131,7 @@ public IEnumerable GetModelEntityReferencesIds(Type mod public void RegisterModelSchema( DocumentVersion fromVersion, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, @@ -146,7 +143,7 @@ public void RegisterModelSchema( DocumentVersion fromVersion, Action, ISerializerModifierAccessor> classMapInitializer, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, @@ -158,10 +155,10 @@ public void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class { - ConfigLock.EnterWriteLock(); + configLock.EnterWriteLock(); try { if (IsFrozen) @@ -176,10 +173,11 @@ public void RegisterModelSchema( else if (!typeof(TModel).IsAbstract) //else if can deserialize, set default serializer serializer = new ExtendedClassMapSerializer( - dbContext, + dbContext.DBCache, + dbContext.DocumentVersion, serializerModifierAccessor, (m, v) => modelMigrationAsync?.Invoke( - m, v, serializerModifierAccessor) ?? Task.FromResult(m)) + m, v, dbContext) ?? Task.FromResult(m)) { AddVersion = typeof(IEntityModel).IsAssignableFrom(typeof(TModel)) }; //true only for entity models // Register schema. @@ -187,7 +185,7 @@ public void RegisterModelSchema( } finally { - ConfigLock.ExitWriteLock(); + configLock.ExitWriteLock(); } } diff --git a/src/MongODM/Serialization/IDocumentSchemaRegister.cs b/src/MongODM/Serialization/IDocumentSchemaRegister.cs index 5b8c7757..e31157be 100644 --- a/src/MongODM/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/IDocumentSchemaRegister.cs @@ -7,13 +7,20 @@ namespace Digicando.MongODM.Serialization { + /// + /// Interface for implementation. + /// public interface IDocumentSchemaRegister { + bool IsFrozen { get; } + bool IsInitialized { get; } + IEnumerable Schemas { get; } + + // Methods. /// - /// Build and freeze the register + /// Build and freeze the register. /// - /// This instance - IDocumentSchemaRegister Freeze(); + void Freeze(); IEnumerable GetMemberDependencies(MemberInfo memberInfo); @@ -22,9 +29,9 @@ public interface IDocumentSchemaRegister IEnumerable GetModelEntityReferencesIds(Type modelType); /// - /// Call before everything else. Used for avoid circular dependency injection with MongoStorage + /// Call before everything else. /// - /// Current instance of IDBContext + /// Instance of void Initialize(IDbContext dbContext); /// @@ -37,7 +44,7 @@ public interface IDocumentSchemaRegister void RegisterModelSchema( DocumentVersion fromVersion, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; /// @@ -52,7 +59,7 @@ void RegisterModelSchema( DocumentVersion fromVersion, Action, ISerializerModifierAccessor> classMapInitializer, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; /// @@ -67,7 +74,7 @@ void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; } } \ No newline at end of file diff --git a/src/MongODM/Serialization/IModelSerializerCollector.cs b/src/MongODM/Serialization/IModelSerializerCollector.cs index da289e88..d1f642e4 100644 --- a/src/MongODM/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM/Serialization/IModelSerializerCollector.cs @@ -1,8 +1,13 @@ -namespace Digicando.MongODM.Serialization +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Utility; + +namespace Digicando.MongODM.Serialization { public interface IModelSerializerCollector { // Methods. - void Register(IDbContext dbContext); + void Register(IDBCache dbCache, + IDocumentSchemaRegister documentSchemaRegister, + IProxyGenerator proxyGenerator); } } diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index 831b0ec4..1de74542 100644 --- a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -6,31 +6,31 @@ namespace Digicando.MongODM.Serialization.Modifiers class SerializerModifierAccessor : ISerializerModifierAccessor { // Fields. - private readonly IExecutionContext context; + private readonly IExecutionContext executionContext; // Constructors. public SerializerModifierAccessor( - IExecutionContext context) + IExecutionContext executionContext) { - this.context = context; + this.executionContext = executionContext; } // Properties. public bool IsReadOnlyReferencedIdEnabled => - ReferenceSerializerModifier.IsReadOnlyIdEnabled(context); + ReferenceSerializerModifier.IsReadOnlyIdEnabled(executionContext); public bool IsNoCacheEnabled => - CacheSerializerModifier.IsNoCacheEnabled(context); + CacheSerializerModifier.IsNoCacheEnabled(executionContext); // Methods. public IDisposable EnableCacheSerializerModifier(bool noCache) => - new CacheSerializerModifier(context) + new CacheSerializerModifier(executionContext) { NoCache = noCache }; public IDisposable EnableReferenceSerializerModifier(bool readOnlyId) => - new ReferenceSerializerModifier(context) + new ReferenceSerializerModifier(executionContext) { ReadOnlyId = readOnlyId }; diff --git a/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs index 98471045..dcdb747b 100644 --- a/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -2,6 +2,7 @@ using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization.Modifiers; +using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; @@ -35,24 +36,25 @@ public Func Condition // Fields. private BsonClassMapSerializer _serializer; - private readonly IDbContext dbContext; + private readonly IDBCache dbCache; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly ICollection extraElements; private readonly Func> fixDeserializedModelAsync; // Constructor. public ExtendedClassMapSerializer( - IDbContext dbContext, + IDBCache dbCache, + DocumentVersion documentVersion, ISerializerModifierAccessor serializerModifierAccessor, Func> fixDeserializedModelAsync = null) { - this.dbContext = dbContext; + this.dbCache = dbCache; this.serializerModifierAccessor = serializerModifierAccessor; extraElements = new List(); this.fixDeserializedModelAsync = fixDeserializedModelAsync ?? ((m, _) => Task.FromResult(m)); documentVersionElement = new BsonElement( DbContext.DocumentVersionElementName, - DocumentVersionToBsonArray(dbContext.DocumentVersion)); + DocumentVersionToBsonArray(documentVersion)); } // Properties. @@ -109,17 +111,17 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser if (!serializerModifierAccessor.IsNoCacheEnabled && GetDocumentId(model, out var id, out _, out _) && id != null) { - if (dbContext.DBCache.LoadedModels.ContainsKey(id)) + if (dbCache.LoadedModels.ContainsKey(id)) { var fullModel = model; - model = dbContext.DBCache.LoadedModels[id] as TModel; + model = dbCache.LoadedModels[id] as TModel; if ((model as IReferenceable).IsSummary) (model as IReferenceable).MergeFullModel(fullModel); } else { - dbContext.DBCache.AddModel(id, model as IEntityModel); + dbCache.AddModel(id, model as IEntityModel); } } diff --git a/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs index 39d614d9..e7dc380e 100644 --- a/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs @@ -2,6 +2,7 @@ using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization.Modifiers; +using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Conventions; @@ -24,8 +25,8 @@ public class ReferenceSerializer : private readonly ReaderWriterLockSlim configLockAdapters = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockClassMaps = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockSerializers = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - - private readonly IDbContext dbContext; + private readonly IDBCache dbCache; + private readonly IProxyGenerator proxyGenerator; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly IDictionary registeredAdapters = new Dictionary(); @@ -34,11 +35,13 @@ public class ReferenceSerializer : // Constructors. public ReferenceSerializer( - IDbContext dbContext, + IDBCache dbCache, + IProxyGenerator proxyGenerator, ISerializerModifierAccessor serializerModifierAccessor, bool useCascadeDelete) { - this.dbContext = dbContext; + this.dbCache = dbCache; + this.proxyGenerator = proxyGenerator; this.serializerModifierAccessor = serializerModifierAccessor; UseCascadeDelete = useCascadeDelete; } @@ -81,10 +84,10 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD return null; // Check if model as been loaded in cache. - if (dbContext.DBCache.LoadedModels.ContainsKey(id) && + if (dbCache.LoadedModels.ContainsKey(id) && !serializerModifierAccessor.IsNoCacheEnabled) { - var cachedModel = dbContext.DBCache.LoadedModels[id] as TModelBase; + var cachedModel = dbCache.LoadedModels[id] as TModelBase; if ((cachedModel as IReferenceable).IsSummary) { @@ -130,7 +133,7 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD // Add in cache. if (!serializerModifierAccessor.IsNoCacheEnabled) - dbContext.DBCache.AddModel(model.Id, model); + dbCache.AddModel(model.Id, model); } } @@ -183,7 +186,7 @@ public ReferenceSerializer RegisterType(Action dbContext.ProxyGenerator.CreateInstance()); + classMap.SetCreator(() => proxyGenerator.CreateInstance()); // Add info to dictionary of registered types. configLockClassMaps.EnterWriteLock(); @@ -354,7 +357,7 @@ private IBsonSerializer GetSerializer(Type actualType) private bool IsProxyClassType(TModel value, out Type modelType) { modelType = value.GetType(); - if (dbContext.ProxyGenerator.IsProxyType(modelType)) + if (proxyGenerator.IsProxyType(modelType)) { modelType = modelType.BaseType; return true; diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index 0c82d15b..7208d352 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -28,7 +28,7 @@ public static void UseMongODM( executionContexts = new IExecutionContext[] //default { new HttpContextExecutionContext(serviceProvider.GetService()), - new AsyncLocalContext() + AsyncLocalContext.Instance }; return executionContexts.Count() == 1 ? @@ -40,10 +40,15 @@ public static void UseMongODM( // DbContext internal. //dependencies + /***** + * Transient dependencies have to be injected only into DbContext instance, + * and passed to other with Initialize() method. This because otherwise inside + * the same dbContext different components could have different instances of the same component. + */ services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); - services.TryAddTransient(); + services.TryAddSingleton(); //tasks services.TryAddTransient(); diff --git a/src/MongODM/Tasks/ITaskRunner.cs b/src/MongODM/Tasks/ITaskRunner.cs index 8aab6903..5ed06fac 100644 --- a/src/MongODM/Tasks/ITaskRunner.cs +++ b/src/MongODM/Tasks/ITaskRunner.cs @@ -5,6 +5,6 @@ namespace Digicando.MongODM.Tasks { public interface ITaskRunner { - void RunUpdateDocDependenciesTask(Type modelType, Type keyType, IEnumerable idPaths, object modelId); + void RunUpdateDocDependenciesTask(Type dbContextType, Type modelType, Type keyType, IEnumerable idPaths, object modelId); } } diff --git a/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs b/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs index 58325af7..b70e6993 100644 --- a/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs +++ b/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs @@ -6,9 +6,10 @@ namespace Digicando.MongODM.Tasks { public interface IUpdateDocDependenciesTask { - Task RunAsync( + Task RunAsync( IEnumerable idPaths, TKey modelId) - where TModel : class, IEntityModel; + where TModel : class, IEntityModel + where TDbContext : class, IDbContext; } } \ No newline at end of file diff --git a/src/MongODM/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs index 6ed214d5..d4872d30 100644 --- a/src/MongODM/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs @@ -1,7 +1,9 @@ using Digicando.MongODM.Models; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization.Modifiers; +using Microsoft.Extensions.DependencyInjection; using MongoDB.Driver; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -10,24 +12,27 @@ namespace Digicando.MongODM.Tasks public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask { // Fields. - private readonly IDbContext dbContext; private readonly ISerializerModifierAccessor serializerModifierAccessor; + private readonly IServiceProvider serviceProvider; // Constructors. public UpdateDocDependenciesTask( - IDbContext dbContext, - ISerializerModifierAccessor serializerModifierAccessor) + ISerializerModifierAccessor serializerModifierAccessor, + IServiceProvider serviceProvider) { - this.dbContext = dbContext; this.serializerModifierAccessor = serializerModifierAccessor; + this.serviceProvider = serviceProvider; } // Methods. - public async Task RunAsync( + public async Task RunAsync( IEnumerable idPaths, TKey modelId) where TModel : class, IEntityModel + where TDbContext : class, IDbContext { + var dbContext = serviceProvider.GetService(); + // Get repository. if (!dbContext.ModelCollectionRepositoryMap.ContainsKey(typeof(TModel))) return; diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs index f6a7dd3c..6d3faa5b 100644 --- a/src/MongODM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -11,12 +11,12 @@ class DBCache : IDBCache private const string CacheKey = "DBCache"; // Fields. - private readonly IExecutionContext context; + private readonly IExecutionContext executionContext; // Constructors. - public DBCache(IExecutionContext context) + public DBCache(IExecutionContext executionContext) { - this.context = context ?? throw new ArgumentNullException(nameof(context)); + this.executionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext)); } // Properties. @@ -24,7 +24,7 @@ public IReadOnlyDictionary LoadedModels { get { - lock (context.Items) + lock (executionContext.Items) return GetScopedCache(); } } @@ -32,7 +32,7 @@ public IReadOnlyDictionary LoadedModels // Methods. public void ClearCache() { - lock (context.Items) + lock (executionContext.Items) GetScopedCache().Clear(); } @@ -44,23 +44,23 @@ public void AddModel(object id, TModel model) if (model == null) throw new ArgumentNullException(nameof(model)); - lock (context.Items) + lock (executionContext.Items) GetScopedCache().Add(id, model); } public void RemoveModel(object id) { - lock (context.Items) + lock (executionContext.Items) GetScopedCache().Remove(id); } // Helpers. private Dictionary GetScopedCache() { - if (!context.Items.ContainsKey(CacheKey)) - context.Items.Add(CacheKey, new Dictionary()); + if (!executionContext.Items.ContainsKey(CacheKey)) + executionContext.Items.Add(CacheKey, new Dictionary()); - return context.Items[CacheKey] as Dictionary; + return executionContext.Items[CacheKey] as Dictionary; } } } diff --git a/src/MongODM/Utility/DBMaintainer.cs b/src/MongODM/Utility/DBMaintainer.cs index 13dbf2c8..4d07e78e 100644 --- a/src/MongODM/Utility/DBMaintainer.cs +++ b/src/MongODM/Utility/DBMaintainer.cs @@ -1,7 +1,7 @@ using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization; using Digicando.MongODM.Tasks; using MongoDB.Driver; +using System; using System.Linq; namespace Digicando.MongODM.Utility @@ -9,23 +9,33 @@ namespace Digicando.MongODM.Utility public class DBMaintainer : IDBMaintainer { // Fields. - private readonly IDocumentSchemaRegister documentSchemaRegister; + private IDbContext dbContext; private readonly ITaskRunner taskRunner; - // Constructors. - public DBMaintainer( - IDocumentSchemaRegister documentSchemaRegister, - ITaskRunner taskRunner) + // Constructors and initialization. + public DBMaintainer(ITaskRunner taskRunner) { - this.documentSchemaRegister = documentSchemaRegister; this.taskRunner = taskRunner; } + public void Initialize(IDbContext dbContext) + { + if (IsInitialized) + throw new InvalidOperationException("Instance already initialized"); + + this.dbContext = dbContext; + + IsInitialized = true; + } + + // Properties. + public bool IsInitialized { get; private set; } + // Methods. public void OnUpdatedModel(IAuditable updatedModel, TKey modelId) { var updatedMembers = updatedModel.ChangedMembers; - var dependencies = updatedMembers.SelectMany(member => documentSchemaRegister.GetMemberDependencies(member)) + var dependencies = updatedMembers.SelectMany(member => dbContext.DocumentSchemaRegister.GetMemberDependencies(member)) .Where(d => d.IsEntityReferenceMember); foreach (var dependencyGroup in dependencies.GroupBy(d => d.RootModelType)) @@ -35,7 +45,7 @@ public void OnUpdatedModel(IAuditable updatedModel, TKey modelId) .Distinct(); // Enqueue call for background job. - taskRunner.RunUpdateDocDependenciesTask(dependencyGroup.Key, typeof(TKey), idPaths, modelId); + taskRunner.RunUpdateDocDependenciesTask(dbContext.GetType(), dependencyGroup.Key, typeof(TKey), idPaths, modelId); } } } diff --git a/src/MongODM/Utility/IDBCache.cs b/src/MongODM/Utility/IDBCache.cs index 533d4875..ab415a8d 100644 --- a/src/MongODM/Utility/IDBCache.cs +++ b/src/MongODM/Utility/IDBCache.cs @@ -3,17 +3,36 @@ namespace Digicando.MongODM.Utility { + /// + /// Interface for implementation. + /// public interface IDBCache { // Properties. - IReadOnlyDictionary LoadedModels { get; } + /// + /// List of current cached models, indexed by Id. + /// + IReadOnlyDictionary LoadedModels { get; } // (id -> model) // Methods. + /// + /// Add a new model in cache. + /// + /// The model type + /// The model Id + /// The model void AddModel(object id, TModel model) where TModel : class, IEntityModel; + /// + /// Clear current cache archive. + /// void ClearCache(); + /// + /// Remove a model from cache. + /// + /// The model Id void RemoveModel(object id); } } \ No newline at end of file diff --git a/src/MongODM/Utility/IDBMaintainer.cs b/src/MongODM/Utility/IDBMaintainer.cs index 00d1b614..fb9168b3 100644 --- a/src/MongODM/Utility/IDBMaintainer.cs +++ b/src/MongODM/Utility/IDBMaintainer.cs @@ -2,8 +2,23 @@ namespace Digicando.MongODM.Utility { + /// + /// Interface for implementation. + /// public interface IDBMaintainer { + // Properties. + bool IsInitialized { get; } + + // Methods. + void Initialize(IDbContext dbContext); + + /// + /// Method to invoke when an auditable model is changed. + /// + /// The model type + /// The changed model + /// The model id void OnUpdatedModel(IAuditable auditableModel, TKey modelId); } } \ No newline at end of file diff --git a/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs index 43077ddd..288361fd 100644 --- a/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs @@ -4,6 +4,7 @@ using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Serialization.Serializers; +using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -56,17 +57,15 @@ public SerializationTestElement( } // Fields. - private readonly Mock dbContextMock; + private readonly Mock dbCacheMock; private readonly DocumentVersion documentVersion = new DocumentVersion("1.0.0"); private readonly Mock serializerModifierAccessorMock; // Constructor. public ExtendedClassMapSerializerTest() { - dbContextMock = new Mock(); - dbContextMock.Setup(c => c.DocumentVersion) - .Returns(() => documentVersion); - dbContextMock.Setup(c => c.DBCache.LoadedModels.ContainsKey(It.IsAny())) + dbCacheMock = new Mock(); + dbCacheMock.Setup(c => c.LoadedModels.ContainsKey(It.IsAny())) .Returns(() => false); serializerModifierAccessorMock = new Mock(); @@ -137,7 +136,8 @@ public void Deserialize(DeserializationTestElement test) // Setup var bsonReader = new BsonDocumentReader(test.Document); var serializer = new ExtendedClassMapSerializer( - dbContextMock.Object, + dbCacheMock.Object, + documentVersion, serializerModifierAccessorMock.Object, (m, _) => { @@ -166,7 +166,8 @@ public void GetDocumentId() var model = new FakeModel { Id = "idVal" }; var comparisonSerializer = CreateBsonClassMapSerializer(); var serializer = new ExtendedClassMapSerializer( - dbContextMock.Object, + dbCacheMock.Object, + documentVersion, serializerModifierAccessorMock.Object); // Action @@ -196,7 +197,8 @@ public void GetMemberSerializationInfo() var memberName = nameof(FakeModel.StringProp); var comparisonSerializer = CreateBsonClassMapSerializer(); var serializer = new ExtendedClassMapSerializer( - dbContextMock.Object, + dbCacheMock.Object, + documentVersion, serializerModifierAccessorMock.Object); // Action @@ -354,7 +356,8 @@ public void Serialize(SerializationTestElement test) { // Setup var serializer = new ExtendedClassMapSerializer( - dbContextMock.Object, + dbCacheMock.Object, + documentVersion, serializerModifierAccessorMock.Object) .AddExtraElement(new BsonElement("ExtraElement", new BsonString("extraValue")), test.Condition); @@ -378,7 +381,8 @@ public void SetDocumentId() var model = new FakeModel(); var comparisonSerializer = CreateBsonClassMapSerializer(); var serializer = new ExtendedClassMapSerializer( - dbContextMock.Object, + dbCacheMock.Object, + documentVersion, serializerModifierAccessorMock.Object); // Action From 300ffb9bd3aac7364e2cdcfe74fc89853c0a6ec1 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 7 Feb 2020 17:12:48 +0100 Subject: [PATCH 13/46] Updated dependencies --- src/MongODM/MongODM.csproj | 6 +++--- test/MongODM.Tests/MongODM.Tests.csproj | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index 8d6ddd78..e0a40cb9 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -14,9 +14,9 @@ - - - + + + diff --git a/test/MongODM.Tests/MongODM.Tests.csproj b/test/MongODM.Tests/MongODM.Tests.csproj index 368efd8e..dc15455b 100644 --- a/test/MongODM.Tests/MongODM.Tests.csproj +++ b/test/MongODM.Tests/MongODM.Tests.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers From c5faa376e12b5e714d3f4b51e5a8dfdba8aeea0a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 7 Feb 2020 22:37:28 +0100 Subject: [PATCH 14/46] Completed MODM-30 --- MongODM.sln | 9 ++++++- .../HttpContextExecutionContext.cs | 0 .../MongODM.AspNetCore.csproj | 27 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 3 +-- src/MongODM/MongODM.csproj | 3 --- .../Serialization/DocumentSchemaRegister.cs | 2 +- .../Modifiers/SerializerModifierAccessor.cs | 2 +- .../Tasks/UpdateDocDependenciesTask.cs | 3 +-- src/MongODM/Utility/DBCache.cs | 2 +- 9 files changed, 40 insertions(+), 11 deletions(-) rename src/{MongODM/AspNetCore => MongODM.AspNetCore}/HttpContextExecutionContext.cs (100%) create mode 100644 src/MongODM.AspNetCore/MongODM.AspNetCore.csproj rename src/{MongODM => MongODM.AspNetCore}/ServiceCollectionExtensions.cs (97%) diff --git a/MongODM.sln b/MongODM.sln index e3c3a60d..b586991a 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Hangfire", "src\Mon EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext", "src\ExecutionContext\ExecutionContext.csproj", "{DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExecutionContext.Tests", "test\ExecutionContext.Tests\ExecutionContext.Tests.csproj", "{BF4F963A-DBCE-4C53-A209-502F4CAF12C5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext.Tests", "test\ExecutionContext.Tests\ExecutionContext.Tests.csproj", "{BF4F963A-DBCE-4C53-A209-502F4CAF12C5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongODM.AspNetCore", "src\MongODM.AspNetCore\MongODM.AspNetCore.csproj", "{6374F645-5D17-494E-9529-7F83426900B3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,6 +45,10 @@ Global {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Debug|Any CPU.Build.0 = Debug|Any CPU {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Release|Any CPU.ActiveCfg = Release|Any CPU {BF4F963A-DBCE-4C53-A209-502F4CAF12C5}.Release|Any CPU.Build.0 = Release|Any CPU + {6374F645-5D17-494E-9529-7F83426900B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6374F645-5D17-494E-9529-7F83426900B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6374F645-5D17-494E-9529-7F83426900B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6374F645-5D17-494E-9529-7F83426900B3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -53,6 +59,7 @@ Global {10897D0D-4898-4A4D-8D1E-B2435E93D9A1} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {BF4F963A-DBCE-4C53-A209-502F4CAF12C5} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} + {6374F645-5D17-494E-9529-7F83426900B3} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/src/MongODM/AspNetCore/HttpContextExecutionContext.cs b/src/MongODM.AspNetCore/HttpContextExecutionContext.cs similarity index 100% rename from src/MongODM/AspNetCore/HttpContextExecutionContext.cs rename to src/MongODM.AspNetCore/HttpContextExecutionContext.cs diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj new file mode 100644 index 00000000..5297b911 --- /dev/null +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -0,0 +1,27 @@ + + + + netstandard2.0;netcoreapp3.0 + true + Digicando.MongODM.AspNetCore + true + 0.19.0-dev + Digicando Srl + Asp.Net Core adapter for MongODM + + + + + + + + + + + + + + + + + diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs similarity index 97% rename from src/MongODM/ServiceCollectionExtensions.cs rename to src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 7208d352..4fa05d10 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -7,12 +7,11 @@ using Digicando.MongODM.Tasks; using Digicando.MongODM.Utility; using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongODM +namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index 3c5d5b5f..6a612cca 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -12,9 +12,6 @@ - - - diff --git a/src/MongODM/Serialization/DocumentSchemaRegister.cs b/src/MongODM/Serialization/DocumentSchemaRegister.cs index dce5b813..ccf5921f 100644 --- a/src/MongODM/Serialization/DocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/DocumentSchemaRegister.cs @@ -13,7 +13,7 @@ namespace Digicando.MongODM.Serialization { - class DocumentSchemaRegister : IDocumentSchemaRegister + public class DocumentSchemaRegister : IDocumentSchemaRegister { // Fields. private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs index 1de74542..61477823 100644 --- a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -3,7 +3,7 @@ namespace Digicando.MongODM.Serialization.Modifiers { - class SerializerModifierAccessor : ISerializerModifierAccessor + public class SerializerModifierAccessor : ISerializerModifierAccessor { // Fields. private readonly IExecutionContext executionContext; diff --git a/src/MongODM/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs index d4872d30..39f87475 100644 --- a/src/MongODM/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM/Tasks/UpdateDocDependenciesTask.cs @@ -1,7 +1,6 @@ using Digicando.MongODM.Models; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization.Modifiers; -using Microsoft.Extensions.DependencyInjection; using MongoDB.Driver; using System; using System.Collections.Generic; @@ -31,7 +30,7 @@ public async Task RunAsync( where TModel : class, IEntityModel where TDbContext : class, IDbContext { - var dbContext = serviceProvider.GetService(); + var dbContext = serviceProvider.GetService(typeof(TDbContext)) as TDbContext; // Get repository. if (!dbContext.ModelCollectionRepositoryMap.ContainsKey(typeof(TModel))) diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM/Utility/DBCache.cs index 6d3faa5b..c52da4b4 100644 --- a/src/MongODM/Utility/DBCache.cs +++ b/src/MongODM/Utility/DBCache.cs @@ -5,7 +5,7 @@ namespace Digicando.MongODM.Utility { - class DBCache : IDBCache + public class DBCache : IDBCache { // Consts. private const string CacheKey = "DBCache"; From 3d4dd055be5c7293ea476cbb0dbfc5b3a1a097fb Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 12 Feb 2020 03:16:44 +0100 Subject: [PATCH 15/46] Imported ProxyGenerator class Added IDbContext instance to ProxyGenerator.CreateInstance arguments --- doc/DbContextClassDiagram.drawio | 2 +- .../MongODM.AspNetCore.csproj | 2 +- .../ServiceCollectionExtensions.cs | 6 + src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- src/MongODM/DbContext.cs | 2 +- src/MongODM/MongODM.csproj | 2 +- src/MongODM/ProxyModels/IProxyGenerator.cs | 4 +- src/MongODM/ProxyModels/ProxyGenerator.cs | 187 ++++++++++++++++++ .../Repositories/CollectionRepositoryBase.cs | 2 +- .../Repositories/GridFSRepositoryBase.cs | 2 +- src/MongODM/Repositories/RepositoryBase.cs | 26 +-- .../Serialization/DocumentSchemaRegister.cs | 13 +- .../Serialization/IDocumentSchemaRegister.cs | 11 +- .../IModelSerializerCollector.cs | 1 + .../Serializers/GeoPointSerializer.cs | 13 +- .../Serializers/ReferenceSerializer.cs | 14 +- 16 files changed, 243 insertions(+), 46 deletions(-) create mode 100644 src/MongODM/ProxyModels/ProxyGenerator.cs diff --git a/doc/DbContextClassDiagram.drawio b/doc/DbContextClassDiagram.drawio index 417627fa..3828422c 100644 --- a/doc/DbContextClassDiagram.drawio +++ b/doc/DbContextClassDiagram.drawio @@ -1 +1 @@ -7V1tc6M4Ev41qcp8sAvE+8fYTmZSN9mdimd37z5dKUax2cHgAzKJ59evBBIGSdjyC8b22ZNJjAAhdbfU3U+3xI0xnH98TuBi9hT7KLwBmv9xY4xuANCB5uA/pGRJSyxgFyXTJPBp2apgHPxCtFCjpW+Bj9LahVkch1mwqBdO4ihCk6xWBpMkfq9f9hqH9acu4BQJBeMJDMXSvwI/m9FS3fZWJ76gYDqjj3YB7fELnPyYJvFbRJ93A4zX/FOcnkNWF+1oOoN+/F4pMu5vjGESx1nxbf4xRCEhLiNbcd9Dw9my3QmKMpUbzP/qg4X/5cufzl9///bg/zH+6/7PHu3LTxi+IdYNO8T1DRakydmSksn+3xtp52AOk2kQ3Rh3+Ky2+MC/cWHeW1Ley+JFcc6snMvQR9aDYTCl901wg1GyqhN/m9K/+ZODSgGc4woHoXj0SOp4hRNUFtdvqdWIiRLwT3lJhBJW8Dh6GcYRaXalghf+cly24MtmCaEak3HWQb25rztROUSvWYXMYt3k2mEY5IJBLnt8iqNpTEsk/SfXj2AGX2CKqneUZWu63F4PRoMhnMxYg9hRR015gkGU4f9YcFl7KkXdNCqevM0xR8eYLHP4jKZBmq2aJz/ZaUP/REkaxLQyvrCTlj2mT8E0gVkQTYtrX7Dq6aYpuWodxmGIlRymxzNaxGmQxcnyCdI5dRTkZ2Cy7LCFn7ESfxifautOsF3fkvhj+RnhWQLidtHRyRV20TCqJ6OXlPwZoyTAKvoXSjAdg9cAJXeTCUrTssVrLjgPfVgMdFRKSIDSu3QZTW4/Fbd+h+mPJt04hj/RcAaj6Zp7umReBpNsjJmB5bzavvpVtLFVG6qwCOidX2Dkh6I5JekcqHUL/ERJFmCD+q4w8Ua5ETigBt+o6M4gxle9hrn9+xpgu9QYvGITi7oDOqDHD3AehMST+ILCn4jUSoQom4fkovLZVZuXmsGkDeijUkRt4M8onqOMjH2NnrWZ40EdFoMevq+Mf2A5fasonVUsf+DSSyF1OaZl5SuzG3+hlvcWVriuS8xwjsrE1Vio9790mOALq0FbSxdDq9OlB0TC6JYmkkVvjyyaQJbCLB30R0nwk5gzHJGwj7UgX9/m4UMC5/jr4H0WZGi8IJ6CMXpPiFqoShTrnZ6PwrJfhiaVtvXc2yyD3dHSFSVsCG7uBkHpR+HDgZJ3JHEm6v4Rx5S143dfitKzoC68pkR2dQm9rbbI7R2Y3BJP7KQIrmtdU1w3Ns+hKPLvCGSEj+IFwtppgEsecnWUUwsfVTVShY5plsQ/EDbPiUGEq8KzJfngMzkChHxWgz9FY/pATPIgWz6jEBID9H51plmNpfFbMkGb8Rqs8KdoHUPpcCfNUZ6QbMqaJG/xzzo+JuMXre5bHJCZgEmG69RFg5/Sij7Sm6p4FVePpzcoJFZRQQSholx6yi7uIVDmVaCECa0TgbI5gbJ2Eyhd40wc67jyJFHBa+TpJYwnPzBrfZjOVtJQSpe2o3TtLiklOr9JVJzuRMUx3L5j1Zhs8BpHVVwss/QCWFXOkWcgiRXROTq/xlQpYfPfF2SCSteh52I1F+RjepwMllNWVeRNiXVkt2UdAdGVukBlpjxF6V2qM02co3a2kTjVCGxw1BkKiMBFJXbGO+fvwTyEEWLjkp4xKgOZTj7GQD7eJ7Mg9L/CZfxGmJtmcPKDHQ1mcRL8wtXClVASVKwQWGDXrhiTO6kSTRDBJ78xadO5oif4UbvwK0wzWjCJwxAu0qAAWPRy/h3EWRbP6UUHmE1sTe9btrf61GXHFj0v4Jh9wxGnF9dua3oBkuklj0LmqKmfg/Pp1yClyOYqzsFm/8f7fD6gCQYNWiAXK366ieJCpPBExRVx6kEuU+kCToJo+jW/ZmSuSp4p3cy6ZpkFvp9PjkmcQYauEU4vyCDLCWsN8A/mylDDBoSFGz7Ex/rqGP+Qy5MMjxTcFxJGJBMilqx3lGZSoVk/+jaLUjk/KMsKaE1WZFYNdt61VYiBikecpIW43Edvc5TktGbikguK5I6r7LQlOxY4AdlRAHnCIGdgwWiW0qPvxOU55lduklC2fidcH/V0gfWGyHpDwuYQvqDwG4k/5WHwUVJcy7G/Mw47rromaYnBhsxO7drlOYGEJCH5ZWuH6uQCsl9j6DPLQBKqr5A2fvmbZP7hqRBonJ0goe3ZEeLOL6hwy/pJhkje2e8s33JO/n5qCk8PQwSTXEBuP63r85HC0c9ojnWe0KWGpp21o28DwGFEPUOX2OMGkAWUdaMtd99QCCifv7uvil0zMnfi7XOBsp6+I3rtCXBkTz9yRMRQiIi0naYAPI6eliyBw5ZgayXgdvjRJlqlo2CK55vIj/v3H2jyRmS+ERbZPWfB3TlnoWTlVjkLQJqz0B5dZajC1RgUJeqCrMLHDM23MAfZ9/NKWtt3SMpzMCzJRChLweCjWAcbr44Y4RxjjzhEGcH5Gqa9KM4UpjwqnDoBVJRYV/fY7TbZUCZtLetsqfDBkbCBV96HS/VSifVUTDOKhdRttzjJZvEUj77wa5zTkBT+jbJsScUfvmWxqlm4Jek3G36uquUHFC2/Pa08YAl+gHnc7ALDEm0QBhqoxWH0m2scZu04twFvf0vSDPFYl7l6Xms2ki3wvZenGmqINxOKRP6N9ugVKJcPrL2CLHKpaA0nN2TrOq84+YEYLIuEyBncGkwuWlp01H9G2XgSL5DP8MCGRVBrMNVr/KwVqZHF6eVS0x4sKAu9NsOCJ5eCyIh+0oif5Wh9TTM1w3Id0zS8TaCdcj5iXi/wdNPxXKDZBlfvkZMTTRk4c3EQMzOvDudptCBxphCFsHcEmU0+597gMxLblipZgP8K+TVsNiDAfacO7qWPEbbp8pQdX1xw3zFYWf2tGutc9ed2tWvIQYKwaq3Rfo/+WPgwo4H028e7Nz8oLKMigvwvtJTGjU8dIt0+59rmpy5ZIBZIgLhyndrB7S1TDJjVR/IVmTkM712jb9XTYqXgjO6AvqtL3LQ1fjh96DOJURIse/VUxxafKglGSpdB8p4/DDFXIzyOB8S8SVtRrSJCSH1Gf5eNZOTu4aW7faZ6dKAZDWoSw9bgILMRLshg+uP5LVpta/R9VXBl8e540PFZLOLAAgOviN+uHJZkxjbqkrYYfObYDSP6Rk+auYCdrB81ydosg4dsdl1xXFSHW0c/Tr3eY69AtlQCw2cP2ahmBXYpZ67DJ/PpfIRQVcZcm5NWYBxZqmS5plfIZuMGjOedq9XNbo0PSfwLRV1v1bgBxjpuYwr5ki2Uq0h6XRhV0uVOEGt7SBD6daAlDoro2meUPaH5C0pGCCtD7KFMApTeFkWP0Wv8aRuyF7eRjSk3rlthzyaYXu3R35cLdMiHtk/DOg9Zp+6p4n9FWMFPUProF327oWtdVv2s33+Wna6BxCwpi6Cz3B64R5RsfsVOoZ2KRbU5CW/7/f4lrtjRLYYLrsOJy80Qazhxa0lcDECo4sRqsNsVMd5VDCynb5ha+dE5oZAtKrH6hsTh11kk/PBiIcvtw+zfZ6/e/09MrxxgeyXxNQhAa5ieLfOsmgzhK2N3T947OmPF+V4w5nN4Xtz4QrDk12brXdmvssfBsdlvKayjvGL1u3JY10xlFrcF1luNsdZhPF8EISqduiUz7VJqccsH8jXtdmdx0NUn/NYSby2FfPzjIu+r9V3HRd0ZWnzmqLvHbXLcE1IuG1B3zGK4rFxGh1Bjm3WTW/DOVhs27nLL39Bju6GvBLhoxGGjANvtbnuGEs7iRecm4q7XtxzspLrFbxaE3Hr3ZH65cU/Y2ftQIi/s8cBJ8PZ3tCTzZx6SZ8r0pCVa14A0Js/ravVdwMHaoPyx11EwMl3uxMmk56RX7ei653EzJ7C1vmmsZs5dJ07L0/uV3Vs9lxM4Cz/Ydss5mq0kOfSsamlcriqguxA3N5y/w9hge3h738D881bnbVvmf18zFhpfSncJmQrYw43q8dVhgmCGHrE7CqMJKrzv4q1ubCEs61n93m4yDnLmkKhoNQTcnHXAKZAzi9GVW9msS6tnJl8Nz2nLgbdFQOfCtLSqA88mz0vzbgSH3sDaH1TUstGOWjZMvkdH8FxsWbb4VQNWFz9clPY7rrbCJCwWJY7iSTV7iZC3lldy0QsS9RJOFlzL2tawR1VjF49Ds6VOm/WY3Z0e84iv2Zh84pF9HioqjQtSKas0wwV916rosLo0mh72Sqtup9mOggP8i2U3uZ3CDezlku2qRBmAfVWJSu/9Pm8VWThYv8V5YuZ9ROKbGzYnKO54RtD/PQpXWaz+o994+4n3v2h3TgGR47ekI5/YeuAgXcRpNSWFp0xRV0mVreu7JBvA44Mm0l0JpNsqtxaMtrcLW3RmBKCPIPs3uxp//w/+TtIJiqMRyyHND5Y3G1JDN6PUqjv9MEVxIg7wrhFsUTSFdzMfygJwuQ1yvfUN4y/vAbNmMbSj/5ltXt28WD2P9pqHvesEKThJsk1VZU5Sa+/KdMS02+uWqodM4iqH2l7Z2DKZaC1l0wGbdeY1ZXNX9spysqW4SFvc3e5V7yeXyMEoftK2C9A1gjjYQHNc29LteuR31zfg0lo9Tbddy/AMfo/uI78ix7n8OJFyNgfoUNY0oPVt3lIGu4qYXk/ZaMlOBiJgXAe+FO4w7SNAZa5oIJ30ez62t0oN3gWRbN583Hd6KNgfXb/TQ3UGcVVnEOVQ8765XwQpB87qH7e1Mo/EtKxDXHEP3EsfXiXk39n4UrEAz2V8qSaqK4fA9h5fnt63K5+OU25d0Uj7nsAoDVDU/Hq+cxpgjiu+llQCqxx3hClsUHg2I+zg7yHYd986z+zrlQHW9E7aYw0wMc3gsgaYrvMx6u7Hl8oCrHMZXwffw2/v1QHA7puOZrkuMCzgmfx2IYqr/w42wMQQ3oUNMEfYzr3rAcYCbRcxwDzFAaa88mvfAeY46wfYkTWYd+kQBzg9DcbwuUsYYJ4yxqGKyO+Nvuv1AdbW+MKHSUwyjFaX4yEwK9bUGPf/AA== \ No newline at end of file +7V1bc6M6Ev41qco8xAXi/hjbSSa1yZnZZM5ln7aIUWwmGLyAk3h+/UogYZCELV8A2+OcOYktblJf1N1ft8SFNph+3sXubPIYeTC4AIr3eaENLwBQddNEf3DLgrSYFmkZx75H2pYNz/4vSBoV0jr3PZhUTkyjKEj9WbVxFIUhHKWVNjeOo4/qaa9RUH3qzB1DruF55AZ869++l05Iq2o6ywNfoT+ekEfbwMoPvLijt3EczUPyvAugvWY/+eGpS+9FBppMXC/6KDVpNxfaII6iNP80/RzAABOXki2/7rbmaNHvGIapzAX6f9X+zPv69S/r759/3Hp/Pv9989cVGcu7G8whHYYZoPv1Z7jL6YKQyfzfHPezP3XjsR9eaNfoqDL7RL9RYzZa3H6VRrP8mF46lsLP9MoN/DG5boQ6DOPlPdGnMfmbPdkvNbhTdMN+wH+7x/d4dUewaK5eUrkjIorPPuUl5lpow/3wZRCFuNulG7ywp6O2Gds2iTHVqIzTAar1Y92KygF8TUtk5u+Nzx0EfiYY+LT7xygcR6RFMH58/tBN3Rc3geUrirYVQ25uBMP+wB1NaIfot4668uj6YYr+R4JL+1Nq6qZT0Wg+RRx9RmSZuk9w7Cfpsnvig5129C8YJ35EbsY2dtKz++TRH8du6ofj/NwXZHq66UpmWgdRECAjh+jxBGdR4qdRvHh0yZw69LMjbrzosId3yIjfPh9q7w6wX9/j6HNxB9Es4aJ+Ee1kGrvoGLGT4UuC/zzD2Ecm+heMER39Vx/G16MRTJKixytOOA57mCs6LCTEh8l1sghHl1/yS3+4yVudbXx23+Fg4objFdd0ybzUjdNnxAwk5+X+Vc8inS37ULlHQK786oZewLtTgsGByrDAO4xTHznU17mLN8ycwD5x+Ib5cPoROus1yPzfVx/5pVr/FblYJBxQAfl+6079AEcSX2HwDvFdsRCl0wCfVDy77PMSNxj3AX6WmogPfAejKUyx7ivkqEkDDxKwaOTrx9L5B4bVM/LWScnzBzY51SUhx7i4+dLtRh+I572BF66qAjecoTIONWby4y8CJveF3kFZSRdNqdLlCvCEUQ2FJ4vaHFkUjiy5W9rvDWP/HbszDJFQjDXDH+fT4DZ2p+hj/2Pip/B5hiMFbfgRY7NQlig6OjXTwmJcmiKUttXcWy+D3dHS5iVsAC6u+34RR6GvfanoSBBMVOMjhikr9XdXipKjoCq8ukB2VQG9jabI7eyZ3IJI7KAIripdU1zV1s+hMPSuMWSEvkUziKxTH7XcZuYooxb6VrZIJTomaRy9QeSeY4cI3QrNlvgHHckQIOjRO3hj+EweiEjup4snGLjYAb1ZHqk3Y0k0j0dwPV6DDP4YrmIoUXfcHekJySSsibMev1fxMRG/yO2+Rz6eCahk2FZVNNgpLR8juaiMVzH3cdQag0RvlBOBu1EmPcUQdxAo/SxQ3ITWiUCZjEAZ2wmUqjAujtGuPAlM8Ap5egmi0Rtirecmk6U0FNKlbCld20tKgc6vExWrO1GxNLtnGRUma6zFkRUXQy+iAHorq+UZSOBFdI7Or3BVCtj82wxPUMkq9Jy/zQnFmA4jg8WUVRZ5XeAdmU15R4APpU7QmElPUWqX5kzh56itfSTGNAITtDpDAR64KOXO2OD8w58GbgipXpIjWkmRyeSj9cX6Ppr4gffgLqI5Zm6SuqM3+q0/iWL/F7qtuxRKjIrlAgvMyhnP+EpiRGOI8cnvVNpUpunR/ayc+OAmKWkYRUHgzhI/B1jUYv7tR2kaTclJe5hNTEXtGaaz/KnKjslHXsDSe5rFTy+22dT0AgTTS5aFzFBTLwPnkwc/IcjmMs9BZ//7m2w+IAUGNVYgEyt2ugmjXKTQRMU0MeZBLFPJzB354fghO2eoL1ueCN30qmWZ+J6XTY5xlLoUXcOcnmElywhr9NE/xJWBghwIA3V8gL6ry+/oHz49TpGmoLHgNCKeEJFkfcAkFQrNau1bL0rF/CAtK6AxWRF5NSh4V5YpBiIeUZzk4nITzqcwzmhNxSUTFMEVZ9lpSnYMcACyIwHyBH7GwJzRtKRH3YrLU8SvzCUhbP2BuT68UjnWazzrNQGbA/cFBt9x/ilLgw/j/FyG/Z1x2LLlLUlDDNZEfmrXIc8BFCRxxS8bB1QHl5B9iFyPegaCVH2JtNHLT1z5h6ZCoDB+goC2R0eIay+nwiUdJ1aRbLA/aL3lFP/9UpeeHgTQjTMBufyyaswtpaOf4BTZPG5INV076kDfBIDBiK40VeCPa0CUUFa1psJ9TSKhfPzhvix2TcncSbTPJMqu1C3Ra4eDI6/UljMimkRGpOkyBeAw9DREBRymAFsrALf9axvvlQ79MZpvQi/q3XzC0RzLfC0ssn3Ngr11zULByo1qFoCwZqE5uopQhbMzyEvUCXmF9ymcbuAO0s/HVbS2q0qKazAMwUQoKsFgs1h701eLz3A+o4g4gCnG+WqmvTBKJaY8IpwqBlSkWFeN2M0m2VAUbS2qbCnxwRKwgTXe+yv1ksn1lFwzgoVUfbcoTifRGGlf8BBlNMSNP2GaLoj4u/M0knULNyT9esfPlvX8gKTnt6OXBwwuDtDbrS7QDN4HoaCBXB5GvTjnYVbquQlY/1tQZoh0XRTqOY35SCbH96us1FCBrJuQF/Kv9UfPQLlYsXZKsoilojGcXBOt6zzj5HtisCgTImZwYzA572kRrb+D6fMomkGP4oE1i6BWYKrn/FkjUiPK04ulpjlYUJR6rYcFD64EkRL9oBE/w1J6iqIrmmFbuq4560A76XrE7L7AUXXLsYFiasx9Wy5O1EXgzMlBzNS92l+k0YDE6VwWwtwSZNbZmnuNrUhsWqpECf4z5Fez2QAH9x06uJfch8iny0p2PH7BfcdgZfm3bK5zOZ7L5a4he0nCyvVG+Rb+OfPclCTSL++v556fe0Z5BvlfcCHMGx86RLp5zbXJTl2iRCwQAHHFOrW9+1s6nzCravIZmdkP722tZ1TLYoXgjGqBnq0KwrQVcTh56BPOUWIse/lUy+SfKkhGCpdBspG/GyCuhkiP+9i9SRoxrTxCSGJGb5uNZMTh4amHfbp8dqAeDaoTw8bgIL0WLkjd5O1pHi63NfqxbDizeHs8qH0W8zgwx8Az4rcthwWVsbW2pCkGHzl2Q4m+NpKmIWAn60d1vDZLYyGbbVcc57dDvSM/VvW+ba9ANmQSw0cP2chWBXYpZ7bFFvOpbIZQVsZsk5FWoLUsVaJa0zNks3YDxuOu1epmt8bbOPoFw663alwDY7XbmVy+RAvlSpJeFUaZcrkDxNpuYwh/7WmJgyS6dgfTRzh9gfEQImOIIpSRD5PLvOk+fI2+bEL2/DK8MeXadSv02RjTqzz6x2IG9/nQ5mlY5SEd1A0x/K8QGfgRTO69fGwXZK3LcpzV649y0BWQmBZlYXSW2QO3RclmV+zk1ilfVJuR8LLX653iih3VoLjgKpy42AyxghM3VsRFAYQyTiwHu50R423FwLB6mq4UPyojFKJFJUZPEwT8Ks2E718sRLV9iP277NX7e2J6hYLtVMRXIwCNYXqmKLKqc4TPjN2+eK91xvLzPefMZ/A8v/EF58mvrNY7s19mj4O22W9IrKM8Y/XbclhVdGkWNwXWG7W51kE0nfkBLIK6BXXtEuJxixX5XHa7tTio8hN+Y4W3hkQ9frvI+3J9V7uoO0WLjxx1d5hNjq+4kssa1B2x2F2UTiMqVNtnVWcWvNPVhrW73LIXXNHd0JcCnHdiv1mAzXa3PUIJp/miYxNx2+kZFgpS7fw3TUJuvHsyu9z4itvZe18iz+3xwEjw5lc0JPNHnpKnxvSgJVpVgDAnz9pq+V3AwcqkfNvrKCiZTnfipNJz0Kt2VNVxmJkTmEpP15Yz57YTp+GovdLurY7NCJyBHmzaxRxNV5Lse1Y1FKZWFZBdiOs7zl6hrfE9nJ0voPF5o/O2KYq/zxULtS+lO4VKBRThhtX86iCGbgrvUTjqhiOYR9/5W93oQlg6suq13VQcZMzBWdFyCri+6oAxIEeWo3OMqjsn2NmmiNwqcE5T8bvJ4zknZqRl43c6d55YcMPH8w3FNuzLbNaG8+wFTguBjSkqJj8byPLaiJMyju0aM0TCfM3iMBqVi5sweStlJye9XlEt0GYu8qzsHNuqmTt5mJquhFpv58zu7JyDQ9Ha2hQHbwNRMnlMDkva5Gk26NlGcR+DwUF0BwWt5ahUb8YgAva9s+uiUu4C+u7JZk2iCN8+m0Sp14Ift4nM468/oqxu8ybE6c81exfkVzxB1/sWBssiV+/eq738wMef9zujAM/xSzyQL3S5sJ/MoqRcscJSJr9XQZWN73dKPoDD5lSEmxYId11uLFdtbpbV6MwJgJ9++g89G33+D/qMqw3yb0NaYpp9WVysqRxdD2LLbgREDcWBBMjbJrh50eRe3by3kJjZP9dZExDbbMf0isfQjP2nvnl5b2P5Mttzmfa2EyQXJIn2XBUFSY29StPiq3LPO67us8arULWdirVFMtFYRacF1tvMc0XntuwVlWwLcZGmuLvZm+APrs6DUvygfRegKhhxMIFi2aahmtXE8LYvyCV3dRTVtA3N0dgtvFt+g451+nkk6WIP0KGsKUDpmaynDLYVMbVa0dGQnwx4wLgKfElcoZstQGU27yAd9GtANvZKgXJob/yQcD+6fuOH7ARiy04g0pnonScLG/SAtfxvG2Xfm27x++Oelm6pGhfeK11rl4z7dyzaJVvELp3/2rXuUnPUnln66bgc1+Y9tB+xGyY+DOtf3XdMCmbZ/CtLBZhKuxomsXnh0WjY3t9RsOuedo7eU0sKVve+2rYUjK8xOC0FU1U2Qd29fskszjoW/dr7/n47rxwAZk+3FMO2gWYAR2e3EpFcGbg3BePzdyemYBa31XvXCkazbCehYI6kgkmvCttVwSxrtYK1bMGck8c3Ds+CUXDuFBTMkUY4ZOH4naF3tapgrenX3b/fjYWizL89vH37x3DDz7vbwRWvXuzKJFbHft/sOqfUApFclV1Xe1Y10aLSZFflrSlIPH6D12YIpfG8V4+krNXr8kZ5+zpZ292wCDsosisocMC7rw7mCVK3a8/LiOcGRSEt3YaVlYPT2HRr/2zW5KeUfVRoCHsoeq+cVrwyNWd1xuARnCEbQ5eFxit3ZTpzWuYdpy1zWgZLbKlYg3Hc1rqLq9whyXxYK/lzVbF1XKuhmYplOLqjMaC9YfaU0g/Qqg+Q36oje4wNsmeYyEGtPga7raues7Wfir7GES6DX56OQrVJvi5cu/k/ \ No newline at end of file diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 5297b911..669f6660 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -5,7 +5,7 @@ true Digicando.MongODM.AspNetCore true - 0.19.0-dev + 0.19.0-dev1 Digicando Srl Asp.Net Core adapter for MongODM diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 4fa05d10..045258b3 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -15,6 +15,12 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { + public static void UseMongODM( + this IServiceCollection services, + IEnumerable executionContexts = null) + where TTaskRunner : class, ITaskRunner => + UseMongODM(services, executionContexts); + public static void UseMongODM( this IServiceCollection services, IEnumerable executionContexts = null) diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 808e567f..42c92976 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -5,7 +5,7 @@ true Digicando.MongODM.HF true - 0.19.0-dev + 0.19.0-dev1 Digicando Srl Linker for use MongoDB with Hangfire diff --git a/src/MongODM/DbContext.cs b/src/MongODM/DbContext.cs index ffa19f2e..de74fcc2 100644 --- a/src/MongODM/DbContext.cs +++ b/src/MongODM/DbContext.cs @@ -52,7 +52,7 @@ public DbContext( // Register serializers. foreach (var serializerCollector in SerializerCollectors) - serializerCollector.Register(DBCache, DocumentSchemaRegister, ProxyGenerator); + serializerCollector.Register(DBCache, this, DocumentSchemaRegister, ProxyGenerator); // Build and freeze document schema register. DocumentSchemaRegister.Freeze(); diff --git a/src/MongODM/MongODM.csproj b/src/MongODM/MongODM.csproj index 6a612cca..a358fb46 100644 --- a/src/MongODM/MongODM.csproj +++ b/src/MongODM/MongODM.csproj @@ -5,7 +5,7 @@ true Digicando.MongODM true - 0.19.0-dev + 0.19.0-dev1 Digicando Srl ODM framework for MongoDB diff --git a/src/MongODM/ProxyModels/IProxyGenerator.cs b/src/MongODM/ProxyModels/IProxyGenerator.cs index da6ce880..219f26dd 100644 --- a/src/MongODM/ProxyModels/IProxyGenerator.cs +++ b/src/MongODM/ProxyModels/IProxyGenerator.cs @@ -4,8 +4,8 @@ namespace Digicando.MongODM.ProxyModels { public interface IProxyGenerator { - object CreateInstance(Type type, params object[] constructorArguments); - TModel CreateInstance(params object[] constructorArguments); + object CreateInstance(IDbContext dbContext, Type type, params object[] constructorArguments); + TModel CreateInstance(IDbContext dbContext, params object[] constructorArguments); bool IsProxyType(Type type); } } \ No newline at end of file diff --git a/src/MongODM/ProxyModels/ProxyGenerator.cs b/src/MongODM/ProxyModels/ProxyGenerator.cs new file mode 100644 index 00000000..53e4ad29 --- /dev/null +++ b/src/MongODM/ProxyModels/ProxyGenerator.cs @@ -0,0 +1,187 @@ +using Castle.DynamicProxy; +using Digicando.MongODM.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; + +namespace Digicando.MongODM.ProxyModels +{ + public class ProxyGenerator : IProxyGenerator + { + // Fields. + private readonly Castle.DynamicProxy.IProxyGenerator proxyGeneratorCore; + + private readonly Dictionary InterceptorInstancerSelector)> modelConfigurationDictionary = + new Dictionary InterceptorInstancerSelector)>(); + private readonly ReaderWriterLockSlim modelConfigurationDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + + private readonly Dictionary proxyTypeDictionary = new Dictionary(); + private readonly ReaderWriterLockSlim proxyTypeDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); + + // Constructors. + public ProxyGenerator( + Castle.DynamicProxy.IProxyGenerator proxyGeneratorCore) + { + this.proxyGeneratorCore = proxyGeneratorCore; + } + + // Methods. + public object CreateInstance( + IDbContext dbContext, + Type type, + params object[] constructorArguments) + { + // Get configuration. + (Type[] AdditionalInterfaces, Func InterceptorInstancerSelector) configuration = (null, null); + modelConfigurationDictionaryLock.EnterReadLock(); + bool configurationFound = false; + try + { + if (modelConfigurationDictionary.ContainsKey(type)) + { + configuration = modelConfigurationDictionary[type]; + configurationFound = true; + } + } + finally + { + modelConfigurationDictionaryLock.ExitReadLock(); + } + + if (!configurationFound) + { + modelConfigurationDictionaryLock.EnterWriteLock(); + try + { + if (modelConfigurationDictionary.ContainsKey(type)) + { + configuration = modelConfigurationDictionary[type]; + } + else + { + var additionalInterfaces = GetAdditionalInterfaces(type); + configuration = (additionalInterfaces, GetInterceptorInstancer(type, additionalInterfaces)); + modelConfigurationDictionary.Add(type, configuration); + } + } + finally + { + modelConfigurationDictionaryLock.ExitWriteLock(); + } + } + + // Generate model. + var proxyModel = proxyGeneratorCore.CreateClassProxy( + type, + configuration.AdditionalInterfaces, + ProxyGenerationOptions.Default, + constructorArguments, + configuration.InterceptorInstancerSelector(dbContext)); + + // Add to proxy type dictionary. + var addToproxyTypeDictionary = false; + proxyTypeDictionaryLock.EnterReadLock(); + try + { + addToproxyTypeDictionary = !proxyTypeDictionary.ContainsKey(type); + } + finally + { + proxyTypeDictionaryLock.ExitReadLock(); + } + + if (addToproxyTypeDictionary) + { + proxyTypeDictionaryLock.EnterWriteLock(); + try + { + if (!proxyTypeDictionary.ContainsKey(type)) + { + proxyTypeDictionary.Add(type, proxyModel.GetType()); + } + } + finally + { + proxyTypeDictionaryLock.ExitWriteLock(); + } + } + + return proxyModel; + } + + public TModel CreateInstance(IDbContext dbContext, params object[] constructorArguments) => + (TModel)CreateInstance(dbContext, typeof(TModel), constructorArguments); + + public bool IsProxyType(Type type) + { + proxyTypeDictionaryLock.EnterReadLock(); + try + { + return proxyTypeDictionary.ContainsValue(type); + } + finally + { + proxyTypeDictionaryLock.ExitReadLock(); + } + } + + // Protected virtual methods. + protected virtual IEnumerable GetCustomAdditionalInterfaces(Type modelType) => + Array.Empty(); + + protected virtual IEnumerable> GetCustomInterceptorInstancer(Type modelType, IEnumerable additionalInterfaces) => + Array.Empty>(); + + // Helpers. + private Type[] GetAdditionalInterfaces(Type modelType) + { + var interfaces = new List(); + + // Add custom additional interfaces. + interfaces.AddRange(GetCustomAdditionalInterfaces(modelType)); + + // Add internal additional interfaces. + if (modelType.GetInterfaces().Contains(typeof(IEntityModel))) //only if is IEntityModel. + { + interfaces.Add(typeof(IAuditable)); + interfaces.Add(typeof(IReferenceable)); + } + + return interfaces.ToArray(); + } + + private Func GetInterceptorInstancer( + Type modelType, + IEnumerable additionalInterfaces) + { + var interceptorInstancers = new List>(); + + // Add custom interceptor instancers. + interceptorInstancers.AddRange(GetCustomInterceptorInstancer(modelType, additionalInterfaces)); + + // Add internal interceptor instances. + if (modelType.GetInterfaces().Contains(typeof(IEntityModel))) //only if is IEntityModel. + { + var entityModelType = modelType.GetInterfaces().First( + i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityModel<>)); + var entityModelKeyType = entityModelType.GetGenericArguments().Single(); + + //auditableInterceptor + interceptorInstancers.Add(dbContext => Activator.CreateInstance( + typeof(AuditableInterceptor<>).MakeGenericType(modelType), + additionalInterfaces) as IInterceptor); + + //summarizableInterceptor + interceptorInstancers.Add(dbContext => Activator.CreateInstance( + typeof(ReferenceableInterceptor<,>).MakeGenericType(modelType, entityModelKeyType), + additionalInterfaces, + dbContext) as IInterceptor); + } + + return dbContext => (from instancer in interceptorInstancers + select instancer(dbContext)).ToArray(); + } + } +} diff --git a/src/MongODM/Repositories/CollectionRepositoryBase.cs b/src/MongODM/Repositories/CollectionRepositoryBase.cs index 91aa5dfe..6c1feac6 100644 --- a/src/MongODM/Repositories/CollectionRepositoryBase.cs +++ b/src/MongODM/Repositories/CollectionRepositoryBase.cs @@ -61,7 +61,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis newIndexes.Add( ("ver", new CreateIndexModel( - Builders.IndexKeys.Ascending(DbContext.DocumentVersionElementName), + Builders.IndexKeys.Ascending(MongODM.DbContext.DocumentVersionElementName), new CreateIndexOptions { Name = "ver" }))); //referenced documents diff --git a/src/MongODM/Repositories/GridFSRepositoryBase.cs b/src/MongODM/Repositories/GridFSRepositoryBase.cs index 541aee98..222a14cd 100644 --- a/src/MongODM/Repositories/GridFSRepositoryBase.cs +++ b/src/MongODM/Repositories/GridFSRepositoryBase.cs @@ -82,7 +82,7 @@ protected override async Task FindOneOnDBAsync(string id, CancellationTo if (mongoFile == null) throw new KeyNotFoundException($"Can't find key {id}"); - var file = ProxyGenerator.CreateInstance(); + var file = ProxyGenerator.CreateInstance(DbContext); ReflectionHelper.SetValue(file, m => m.Id, mongoFile.Id.ToString()); ReflectionHelper.SetValue(file, m => m.Length, mongoFile.Length); ReflectionHelper.SetValue(file, m => m.Name, mongoFile.Filename); diff --git a/src/MongODM/Repositories/RepositoryBase.cs b/src/MongODM/Repositories/RepositoryBase.cs index b46621b5..fb924180 100644 --- a/src/MongODM/Repositories/RepositoryBase.cs +++ b/src/MongODM/Repositories/RepositoryBase.cs @@ -17,31 +17,31 @@ public abstract class RepositoryBase : IRepository where TModel : class, IEntityModel { - // Fields. - private readonly IDbContext dbContext; - // Constructors. public RepositoryBase(IDbContext dbContext) { - this.dbContext = dbContext; + this.DbContext = dbContext; } // Properties. public virtual MongoMigrationBase MigrationInfo { get; } + // Protected properties. + protected IDbContext DbContext { get; } + // Public methods. public abstract Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default); public virtual async Task CreateAsync(IEnumerable models, CancellationToken cancellationToken = default) { await CreateOnDBAsync(models, cancellationToken); - await dbContext.SaveChangesAsync(); + await DbContext.SaveChangesAsync(); } public virtual async Task CreateAsync(TModel model, CancellationToken cancellationToken = default) { await CreateOnDBAsync(model, cancellationToken); - await dbContext.SaveChangesAsync(); + await DbContext.SaveChangesAsync(); } public async Task DeleteAsync(TKey id, CancellationToken cancellationToken = default) @@ -53,7 +53,7 @@ public async Task DeleteAsync(TKey id, CancellationToken cancellationToken = def public virtual async Task DeleteAsync(TModel model, CancellationToken cancellationToken = default) { // Process cascade delete. - var referencesIdsPaths = dbContext.DocumentSchemaRegister.GetModelEntityReferencesIds(typeof(TModel)) + var referencesIdsPaths = DbContext.DocumentSchemaRegister.GetModelEntityReferencesIds(typeof(TModel)) .Where(d => d.UseCascadeDelete == true) .Where(d => d.EntityClassMapPath.Count() == 2) //ignore references of references .DistinctBy(d => d.FullPathToString()) @@ -64,14 +64,14 @@ public virtual async Task DeleteAsync(TModel model, CancellationToken cancellati // Unlink dependent models. model.DisposeForDelete(); - await dbContext.SaveChangesAsync(); + await DbContext.SaveChangesAsync(); // Delete model. await DeleteOnDBAsync(model, cancellationToken); // Remove from cache. - if (dbContext.DBCache.LoadedModels.ContainsKey(model.Id)) - dbContext.DBCache.RemoveModel(model.Id); + if (DbContext.DBCache.LoadedModels.ContainsKey(model.Id)) + DbContext.DBCache.RemoveModel(model.Id); } public async Task DeleteAsync(IEntityModel model, CancellationToken cancellationToken = default) @@ -85,9 +85,9 @@ public virtual async Task FindOneAsync( TKey id, CancellationToken cancellationToken = default) { - if (dbContext.DBCache.LoadedModels.ContainsKey(id)) + if (DbContext.DBCache.LoadedModels.ContainsKey(id)) { - var cachedModel = dbContext.DBCache.LoadedModels[id] as TModel; + var cachedModel = DbContext.DBCache.LoadedModels[id] as TModel; if ((cachedModel as IReferenceable)?.IsSummary == false) return cachedModel; } @@ -135,7 +135,7 @@ private async Task CascadeDeleteMembersAsync(object currentModel, IEnumerable GetModelEntityReferencesIds(Type mod public void RegisterModelSchema( DocumentVersion fromVersion, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, @@ -141,13 +141,13 @@ public void RegisterModelSchema( public void RegisterModelSchema( DocumentVersion fromVersion, - Action, ISerializerModifierAccessor> classMapInitializer, + Action> classMapInitializer, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, - new BsonClassMap(cm => classMapInitializer(cm, serializerModifierAccessor)), + new BsonClassMap(classMapInitializer), initCustomSerializer, modelMigrationAsync); @@ -155,7 +155,7 @@ public void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class { configLock.EnterWriteLock(); @@ -176,8 +176,7 @@ public void RegisterModelSchema( dbContext.DBCache, dbContext.DocumentVersion, serializerModifierAccessor, - (m, v) => modelMigrationAsync?.Invoke( - m, v, dbContext) ?? Task.FromResult(m)) + (m, v) => modelMigrationAsync?.Invoke(m, v) ?? Task.FromResult(m)) { AddVersion = typeof(IEntityModel).IsAssignableFrom(typeof(TModel)) }; //true only for entity models // Register schema. diff --git a/src/MongODM/Serialization/IDocumentSchemaRegister.cs b/src/MongODM/Serialization/IDocumentSchemaRegister.cs index e31157be..02e41b0a 100644 --- a/src/MongODM/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongODM/Serialization/IDocumentSchemaRegister.cs @@ -1,5 +1,4 @@ -using Digicando.MongODM.Serialization.Modifiers; -using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization; using System; using System.Collections.Generic; using System.Reflection; @@ -44,7 +43,7 @@ public interface IDocumentSchemaRegister void RegisterModelSchema( DocumentVersion fromVersion, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; /// @@ -57,9 +56,9 @@ void RegisterModelSchema( /// Model migration method void RegisterModelSchema( DocumentVersion fromVersion, - Action, ISerializerModifierAccessor> classMapInitializer, + Action> classMapInitializer, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; /// @@ -74,7 +73,7 @@ void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func> modelMigrationAsync = null) where TModel : class; } } \ No newline at end of file diff --git a/src/MongODM/Serialization/IModelSerializerCollector.cs b/src/MongODM/Serialization/IModelSerializerCollector.cs index d1f642e4..1296876b 100644 --- a/src/MongODM/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM/Serialization/IModelSerializerCollector.cs @@ -7,6 +7,7 @@ public interface IModelSerializerCollector { // Methods. void Register(IDBCache dbCache, + IDbContext dbContext, IDocumentSchemaRegister documentSchemaRegister, IProxyGenerator proxyGenerator); } diff --git a/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs index f681fe36..42820e28 100644 --- a/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs +++ b/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs @@ -13,22 +13,27 @@ namespace Digicando.MongODM.Serialization.Serializers public class GeoPointSerializer : SerializerBase where TInModel : class { + // Fields. private readonly MemberInfo latitudeMemberInfo; private readonly MemberInfo longitudeMemberInfo; private readonly GeoJsonPointSerializer pointSerializer; + private readonly IDbContext dbContext; private readonly IProxyGenerator proxyGenerator; + // Constructors. public GeoPointSerializer( + IDbContext dbContext, Expression> longitudeMember, - Expression> latitudeMember, - IProxyGenerator proxyGenerator) + Expression> latitudeMember) { longitudeMemberInfo = ReflectionHelper.GetMemberInfoFromLambda(longitudeMember); latitudeMemberInfo = ReflectionHelper.GetMemberInfoFromLambda(latitudeMember); pointSerializer = new GeoJsonPointSerializer(); - this.proxyGenerator = proxyGenerator; + this.dbContext = dbContext; + proxyGenerator = dbContext.ProxyGenerator; } + // Methods. public override TInModel Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { // Deserialize point. @@ -39,7 +44,7 @@ public override TInModel Deserialize(BsonDeserializationContext context, BsonDes } // Create model instance. - var model = proxyGenerator.CreateInstance(); + var model = proxyGenerator.CreateInstance(dbContext); // Copy data. ReflectionHelper.SetValue(model, longitudeMemberInfo, point.Coordinates.Values[0]); diff --git a/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs index e7dc380e..793181a7 100644 --- a/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs @@ -26,6 +26,7 @@ public class ReferenceSerializer : private readonly ReaderWriterLockSlim configLockClassMaps = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockSerializers = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly IDBCache dbCache; + private readonly IDbContext dbContext; private readonly IProxyGenerator proxyGenerator; private readonly ISerializerModifierAccessor serializerModifierAccessor; @@ -35,14 +36,13 @@ public class ReferenceSerializer : // Constructors. public ReferenceSerializer( - IDBCache dbCache, - IProxyGenerator proxyGenerator, - ISerializerModifierAccessor serializerModifierAccessor, + IDbContext dbContext, bool useCascadeDelete) { - this.dbCache = dbCache; - this.proxyGenerator = proxyGenerator; - this.serializerModifierAccessor = serializerModifierAccessor; + this.dbCache = dbContext.DBCache; + this.proxyGenerator = dbContext.ProxyGenerator; + this.serializerModifierAccessor = dbContext.SerializerModifierAccessor; + this.dbContext = dbContext; UseCascadeDelete = useCascadeDelete; } @@ -186,7 +186,7 @@ public ReferenceSerializer RegisterType(Action proxyGenerator.CreateInstance()); + classMap.SetCreator(() => proxyGenerator.CreateInstance(dbContext)); // Add info to dictionary of registered types. configLockClassMaps.EnterWriteLock(); From 65554719344bcc1102f39ad81f3ff0fba876204a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 12 Feb 2020 03:20:25 +0100 Subject: [PATCH 16/46] Fixed test dependency --- test/ExecutionContext.Tests/ExecutionContext.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index 8b784722..cf45c3a5 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -16,7 +16,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + From 05e38bc0317da2f9fb119fb12e606646e5e99a55 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 12 Feb 2020 18:41:41 +0100 Subject: [PATCH 17/46] Renamed MongODM project to MongODM.Core --- MongODM.sln | 30 +++++++++---------- .../MongODM.AspNetCore.csproj | 8 ++--- src/{MongODM => MongODM.Core}/DbContext.cs | 0 .../DbContextOptions.cs | 0 .../Extensions/ClassMapExtensions.cs | 0 .../Extensions/EnumerableExtensions.cs | 0 .../GlobalSuppressions.cs | 0 src/{MongODM => MongODM.Core}/IDbContext.cs | 0 .../Migration/MongoCollectionMigration.cs | 0 .../Migration/MongoDocumentMigration.cs | 0 .../Migration/MongoMigrationBase.cs | 0 .../Models/IEntityModel.cs | 0 .../Models/IFileModel.cs | 0 .../Models/IModel.cs | 0 .../MongODM.Core.csproj} | 0 .../ProxyModels/AuditableInterceptor.cs | 0 .../ProxyModels/IAuditable.cs | 0 .../ProxyModels/IProxyGenerator.cs | 0 .../ProxyModels/IReferenceable.cs | 0 .../ProxyModels/ProxyGenerator.cs | 0 .../ProxyModels/ReferenceableInterceptor.cs | 0 .../Repositories/CollectionRepositoryBase.cs | 0 .../Repositories/GridFSRepositoryBase.cs | 0 .../Repositories/ICollectionRepository.cs | 0 .../Repositories/IGridFSRepository.cs | 0 .../Repositories/IRepository.cs | 0 .../Repositories/RepositoryBase.cs | 0 .../Serialization/DocumentSchema.cs | 0 .../Serialization/DocumentSchemaMemberMap.cs | 0 .../Serialization/DocumentSchemaRegister.cs | 0 .../Serialization/DocumentVersion.cs | 0 .../Serialization/EntityMember.cs | 0 .../ExtendedBsonDocumentReader.cs | 0 .../ExtendedBsonDocumentWriter.cs | 0 .../Serialization/IDocumentSchemaRegister.cs | 0 .../IModelSerializerCollector.cs | 0 .../Modifiers/CacheSerializerModifier.cs | 0 .../Modifiers/ISerializerModifierAccessor.cs | 0 .../Modifiers/ReferenceSerializerModifier.cs | 0 .../Modifiers/SerializerModifierAccessor.cs | 0 .../Serializers/DictionarySerializer.cs | 0 .../Serializers/EnumerableSerializer.cs | 0 .../Serializers/ExtendedClassMapSerializer.cs | 0 .../Serializers/ExtraElementsSerializer.cs | 0 .../Serializers/GeoPointSerializer.cs | 0 .../Serializers/HexToBinaryDataSerializer.cs | 0 .../IClassMapContainerSerializer.cs | 0 .../IReferenceContainerSerializer.cs | 0 .../ReadOnlyDictionarySerializer.cs | 0 .../Serializers/ReferenceSerializer.cs | 0 .../Serializers/ReferenceSerializerAdapter.cs | 0 .../Serializers/ReferenceSerializerSwitch.cs | 0 .../Tasks/ITaskRunner.cs | 0 .../Tasks/IUpdateDocDependenciesTask.cs | 0 src/{MongODM => MongODM.Core}/Tasks/Queues.cs | 0 .../Tasks/UpdateDocDependenciesTask.cs | 0 .../Utility/DBCache.cs | 0 .../Utility/DBMaintainer.cs | 0 .../Utility/IDBCache.cs | 0 .../Utility/IDBMaintainer.cs | 0 src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- .../Comparers/FakeModelComparer.cs | 0 .../ExtendedClassMapSerializerTest.cs | 0 .../MockHelpers/InterceptorMockHelper.cs | 0 .../Models/FakeEntityModelBase.cs | 0 .../Models/FakeModel.cs | 0 .../Models/FakeModelProxy.cs | 0 .../Models/ModelBase.cs | 0 .../MongODM.Core.Tests.csproj} | 8 ++--- .../ReferenceableInterceptorTest.cs | 0 70 files changed, 24 insertions(+), 24 deletions(-) rename src/{MongODM => MongODM.Core}/DbContext.cs (100%) rename src/{MongODM => MongODM.Core}/DbContextOptions.cs (100%) rename src/{MongODM => MongODM.Core}/Extensions/ClassMapExtensions.cs (100%) rename src/{MongODM => MongODM.Core}/Extensions/EnumerableExtensions.cs (100%) rename src/{MongODM => MongODM.Core}/GlobalSuppressions.cs (100%) rename src/{MongODM => MongODM.Core}/IDbContext.cs (100%) rename src/{MongODM => MongODM.Core}/Migration/MongoCollectionMigration.cs (100%) rename src/{MongODM => MongODM.Core}/Migration/MongoDocumentMigration.cs (100%) rename src/{MongODM => MongODM.Core}/Migration/MongoMigrationBase.cs (100%) rename src/{MongODM => MongODM.Core}/Models/IEntityModel.cs (100%) rename src/{MongODM => MongODM.Core}/Models/IFileModel.cs (100%) rename src/{MongODM => MongODM.Core}/Models/IModel.cs (100%) rename src/{MongODM/MongODM.csproj => MongODM.Core/MongODM.Core.csproj} (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/AuditableInterceptor.cs (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/IAuditable.cs (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/IProxyGenerator.cs (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/IReferenceable.cs (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/ProxyGenerator.cs (100%) rename src/{MongODM => MongODM.Core}/ProxyModels/ReferenceableInterceptor.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/CollectionRepositoryBase.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/GridFSRepositoryBase.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/ICollectionRepository.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/IGridFSRepository.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/IRepository.cs (100%) rename src/{MongODM => MongODM.Core}/Repositories/RepositoryBase.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/DocumentSchema.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/DocumentSchemaMemberMap.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/DocumentSchemaRegister.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/DocumentVersion.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/EntityMember.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/ExtendedBsonDocumentReader.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/ExtendedBsonDocumentWriter.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/IDocumentSchemaRegister.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/IModelSerializerCollector.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Modifiers/CacheSerializerModifier.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Modifiers/ISerializerModifierAccessor.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Modifiers/ReferenceSerializerModifier.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Modifiers/SerializerModifierAccessor.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/DictionarySerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/EnumerableSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ExtendedClassMapSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ExtraElementsSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/GeoPointSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/HexToBinaryDataSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/IClassMapContainerSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/IReferenceContainerSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ReadOnlyDictionarySerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ReferenceSerializer.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ReferenceSerializerAdapter.cs (100%) rename src/{MongODM => MongODM.Core}/Serialization/Serializers/ReferenceSerializerSwitch.cs (100%) rename src/{MongODM => MongODM.Core}/Tasks/ITaskRunner.cs (100%) rename src/{MongODM => MongODM.Core}/Tasks/IUpdateDocDependenciesTask.cs (100%) rename src/{MongODM => MongODM.Core}/Tasks/Queues.cs (100%) rename src/{MongODM => MongODM.Core}/Tasks/UpdateDocDependenciesTask.cs (100%) rename src/{MongODM => MongODM.Core}/Utility/DBCache.cs (100%) rename src/{MongODM => MongODM.Core}/Utility/DBMaintainer.cs (100%) rename src/{MongODM => MongODM.Core}/Utility/IDBCache.cs (100%) rename src/{MongODM => MongODM.Core}/Utility/IDBMaintainer.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/Comparers/FakeModelComparer.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/ExtendedClassMapSerializerTest.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/MockHelpers/InterceptorMockHelper.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/Models/FakeEntityModelBase.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/Models/FakeModel.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/Models/FakeModelProxy.cs (100%) rename test/{MongODM.Tests => MongODM.Core.Tests}/Models/ModelBase.cs (100%) rename test/{MongODM.Tests/MongODM.Tests.csproj => MongODM.Core.Tests/MongODM.Core.Tests.csproj} (92%) rename test/{MongODM.Tests => MongODM.Core.Tests}/ReferenceableInterceptorTest.cs (100%) diff --git a/MongODM.sln b/MongODM.sln index b586991a..167d7cb2 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -7,17 +7,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{490DAED7-DAD EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CF1ABDEA-794F-4474-858D-BCB61F367D72}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM", "src\MongODM\MongODM.csproj", "{AA9CE7D3-169F-4725-970E-4CE900E91EDD}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Tests", "test\MongODM.Tests\MongODM.Tests.csproj", "{82789357-2EC1-4159-B3F6-0F830ABAFF4C}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Hangfire", "src\MongODM.Hangfire\MongODM.Hangfire.csproj", "{10897D0D-4898-4A4D-8D1E-B2435E93D9A1}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext", "src\ExecutionContext\ExecutionContext.csproj", "{DB6C020D-1C93-4456-8FB5-EF7CF505DF7B}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecutionContext.Tests", "test\ExecutionContext.Tests\ExecutionContext.Tests.csproj", "{BF4F963A-DBCE-4C53-A209-502F4CAF12C5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongODM.AspNetCore", "src\MongODM.AspNetCore\MongODM.AspNetCore.csproj", "{6374F645-5D17-494E-9529-7F83426900B3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.AspNetCore", "src\MongODM.AspNetCore\MongODM.AspNetCore.csproj", "{6374F645-5D17-494E-9529-7F83426900B3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Core", "src\MongODM.Core\MongODM.Core.csproj", "{4F2498A9-D60D-4D49-95B9-BC78EE2917B5}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Core.Tests", "test\MongODM.Core.Tests\MongODM.Core.Tests.csproj", "{50D6BEE5-54B5-43F3-BA92-6107AB6E311E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -25,14 +25,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {AA9CE7D3-169F-4725-970E-4CE900E91EDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AA9CE7D3-169F-4725-970E-4CE900E91EDD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AA9CE7D3-169F-4725-970E-4CE900E91EDD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AA9CE7D3-169F-4725-970E-4CE900E91EDD}.Release|Any CPU.Build.0 = Release|Any CPU - {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {82789357-2EC1-4159-B3F6-0F830ABAFF4C}.Release|Any CPU.Build.0 = Release|Any CPU {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {10897D0D-4898-4A4D-8D1E-B2435E93D9A1}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -49,17 +41,25 @@ Global {6374F645-5D17-494E-9529-7F83426900B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {6374F645-5D17-494E-9529-7F83426900B3}.Release|Any CPU.ActiveCfg = Release|Any CPU {6374F645-5D17-494E-9529-7F83426900B3}.Release|Any CPU.Build.0 = Release|Any CPU + {4F2498A9-D60D-4D49-95B9-BC78EE2917B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F2498A9-D60D-4D49-95B9-BC78EE2917B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F2498A9-D60D-4D49-95B9-BC78EE2917B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F2498A9-D60D-4D49-95B9-BC78EE2917B5}.Release|Any CPU.Build.0 = Release|Any CPU + {50D6BEE5-54B5-43F3-BA92-6107AB6E311E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {50D6BEE5-54B5-43F3-BA92-6107AB6E311E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {50D6BEE5-54B5-43F3-BA92-6107AB6E311E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {50D6BEE5-54B5-43F3-BA92-6107AB6E311E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {AA9CE7D3-169F-4725-970E-4CE900E91EDD} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} - {82789357-2EC1-4159-B3F6-0F830ABAFF4C} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} {10897D0D-4898-4A4D-8D1E-B2435E93D9A1} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {DB6C020D-1C93-4456-8FB5-EF7CF505DF7B} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {BF4F963A-DBCE-4C53-A209-502F4CAF12C5} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} {6374F645-5D17-494E-9529-7F83426900B3} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} + {4F2498A9-D60D-4D49-95B9-BC78EE2917B5} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} + {50D6BEE5-54B5-43F3-BA92-6107AB6E311E} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 669f6660..73bbc5a0 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -10,10 +10,6 @@ Asp.Net Core adapter for MongODM - - - - @@ -24,4 +20,8 @@ + + + + diff --git a/src/MongODM/DbContext.cs b/src/MongODM.Core/DbContext.cs similarity index 100% rename from src/MongODM/DbContext.cs rename to src/MongODM.Core/DbContext.cs diff --git a/src/MongODM/DbContextOptions.cs b/src/MongODM.Core/DbContextOptions.cs similarity index 100% rename from src/MongODM/DbContextOptions.cs rename to src/MongODM.Core/DbContextOptions.cs diff --git a/src/MongODM/Extensions/ClassMapExtensions.cs b/src/MongODM.Core/Extensions/ClassMapExtensions.cs similarity index 100% rename from src/MongODM/Extensions/ClassMapExtensions.cs rename to src/MongODM.Core/Extensions/ClassMapExtensions.cs diff --git a/src/MongODM/Extensions/EnumerableExtensions.cs b/src/MongODM.Core/Extensions/EnumerableExtensions.cs similarity index 100% rename from src/MongODM/Extensions/EnumerableExtensions.cs rename to src/MongODM.Core/Extensions/EnumerableExtensions.cs diff --git a/src/MongODM/GlobalSuppressions.cs b/src/MongODM.Core/GlobalSuppressions.cs similarity index 100% rename from src/MongODM/GlobalSuppressions.cs rename to src/MongODM.Core/GlobalSuppressions.cs diff --git a/src/MongODM/IDbContext.cs b/src/MongODM.Core/IDbContext.cs similarity index 100% rename from src/MongODM/IDbContext.cs rename to src/MongODM.Core/IDbContext.cs diff --git a/src/MongODM/Migration/MongoCollectionMigration.cs b/src/MongODM.Core/Migration/MongoCollectionMigration.cs similarity index 100% rename from src/MongODM/Migration/MongoCollectionMigration.cs rename to src/MongODM.Core/Migration/MongoCollectionMigration.cs diff --git a/src/MongODM/Migration/MongoDocumentMigration.cs b/src/MongODM.Core/Migration/MongoDocumentMigration.cs similarity index 100% rename from src/MongODM/Migration/MongoDocumentMigration.cs rename to src/MongODM.Core/Migration/MongoDocumentMigration.cs diff --git a/src/MongODM/Migration/MongoMigrationBase.cs b/src/MongODM.Core/Migration/MongoMigrationBase.cs similarity index 100% rename from src/MongODM/Migration/MongoMigrationBase.cs rename to src/MongODM.Core/Migration/MongoMigrationBase.cs diff --git a/src/MongODM/Models/IEntityModel.cs b/src/MongODM.Core/Models/IEntityModel.cs similarity index 100% rename from src/MongODM/Models/IEntityModel.cs rename to src/MongODM.Core/Models/IEntityModel.cs diff --git a/src/MongODM/Models/IFileModel.cs b/src/MongODM.Core/Models/IFileModel.cs similarity index 100% rename from src/MongODM/Models/IFileModel.cs rename to src/MongODM.Core/Models/IFileModel.cs diff --git a/src/MongODM/Models/IModel.cs b/src/MongODM.Core/Models/IModel.cs similarity index 100% rename from src/MongODM/Models/IModel.cs rename to src/MongODM.Core/Models/IModel.cs diff --git a/src/MongODM/MongODM.csproj b/src/MongODM.Core/MongODM.Core.csproj similarity index 100% rename from src/MongODM/MongODM.csproj rename to src/MongODM.Core/MongODM.Core.csproj diff --git a/src/MongODM/ProxyModels/AuditableInterceptor.cs b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs similarity index 100% rename from src/MongODM/ProxyModels/AuditableInterceptor.cs rename to src/MongODM.Core/ProxyModels/AuditableInterceptor.cs diff --git a/src/MongODM/ProxyModels/IAuditable.cs b/src/MongODM.Core/ProxyModels/IAuditable.cs similarity index 100% rename from src/MongODM/ProxyModels/IAuditable.cs rename to src/MongODM.Core/ProxyModels/IAuditable.cs diff --git a/src/MongODM/ProxyModels/IProxyGenerator.cs b/src/MongODM.Core/ProxyModels/IProxyGenerator.cs similarity index 100% rename from src/MongODM/ProxyModels/IProxyGenerator.cs rename to src/MongODM.Core/ProxyModels/IProxyGenerator.cs diff --git a/src/MongODM/ProxyModels/IReferenceable.cs b/src/MongODM.Core/ProxyModels/IReferenceable.cs similarity index 100% rename from src/MongODM/ProxyModels/IReferenceable.cs rename to src/MongODM.Core/ProxyModels/IReferenceable.cs diff --git a/src/MongODM/ProxyModels/ProxyGenerator.cs b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs similarity index 100% rename from src/MongODM/ProxyModels/ProxyGenerator.cs rename to src/MongODM.Core/ProxyModels/ProxyGenerator.cs diff --git a/src/MongODM/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs similarity index 100% rename from src/MongODM/ProxyModels/ReferenceableInterceptor.cs rename to src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs diff --git a/src/MongODM/Repositories/CollectionRepositoryBase.cs b/src/MongODM.Core/Repositories/CollectionRepositoryBase.cs similarity index 100% rename from src/MongODM/Repositories/CollectionRepositoryBase.cs rename to src/MongODM.Core/Repositories/CollectionRepositoryBase.cs diff --git a/src/MongODM/Repositories/GridFSRepositoryBase.cs b/src/MongODM.Core/Repositories/GridFSRepositoryBase.cs similarity index 100% rename from src/MongODM/Repositories/GridFSRepositoryBase.cs rename to src/MongODM.Core/Repositories/GridFSRepositoryBase.cs diff --git a/src/MongODM/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs similarity index 100% rename from src/MongODM/Repositories/ICollectionRepository.cs rename to src/MongODM.Core/Repositories/ICollectionRepository.cs diff --git a/src/MongODM/Repositories/IGridFSRepository.cs b/src/MongODM.Core/Repositories/IGridFSRepository.cs similarity index 100% rename from src/MongODM/Repositories/IGridFSRepository.cs rename to src/MongODM.Core/Repositories/IGridFSRepository.cs diff --git a/src/MongODM/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs similarity index 100% rename from src/MongODM/Repositories/IRepository.cs rename to src/MongODM.Core/Repositories/IRepository.cs diff --git a/src/MongODM/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs similarity index 100% rename from src/MongODM/Repositories/RepositoryBase.cs rename to src/MongODM.Core/Repositories/RepositoryBase.cs diff --git a/src/MongODM/Serialization/DocumentSchema.cs b/src/MongODM.Core/Serialization/DocumentSchema.cs similarity index 100% rename from src/MongODM/Serialization/DocumentSchema.cs rename to src/MongODM.Core/Serialization/DocumentSchema.cs diff --git a/src/MongODM/Serialization/DocumentSchemaMemberMap.cs b/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs similarity index 100% rename from src/MongODM/Serialization/DocumentSchemaMemberMap.cs rename to src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs diff --git a/src/MongODM/Serialization/DocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs similarity index 100% rename from src/MongODM/Serialization/DocumentSchemaRegister.cs rename to src/MongODM.Core/Serialization/DocumentSchemaRegister.cs diff --git a/src/MongODM/Serialization/DocumentVersion.cs b/src/MongODM.Core/Serialization/DocumentVersion.cs similarity index 100% rename from src/MongODM/Serialization/DocumentVersion.cs rename to src/MongODM.Core/Serialization/DocumentVersion.cs diff --git a/src/MongODM/Serialization/EntityMember.cs b/src/MongODM.Core/Serialization/EntityMember.cs similarity index 100% rename from src/MongODM/Serialization/EntityMember.cs rename to src/MongODM.Core/Serialization/EntityMember.cs diff --git a/src/MongODM/Serialization/ExtendedBsonDocumentReader.cs b/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs similarity index 100% rename from src/MongODM/Serialization/ExtendedBsonDocumentReader.cs rename to src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs diff --git a/src/MongODM/Serialization/ExtendedBsonDocumentWriter.cs b/src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs similarity index 100% rename from src/MongODM/Serialization/ExtendedBsonDocumentWriter.cs rename to src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs diff --git a/src/MongODM/Serialization/IDocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs similarity index 100% rename from src/MongODM/Serialization/IDocumentSchemaRegister.cs rename to src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs diff --git a/src/MongODM/Serialization/IModelSerializerCollector.cs b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs similarity index 100% rename from src/MongODM/Serialization/IModelSerializerCollector.cs rename to src/MongODM.Core/Serialization/IModelSerializerCollector.cs diff --git a/src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs similarity index 100% rename from src/MongODM/Serialization/Modifiers/CacheSerializerModifier.cs rename to src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs diff --git a/src/MongODM/Serialization/Modifiers/ISerializerModifierAccessor.cs b/src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs similarity index 100% rename from src/MongODM/Serialization/Modifiers/ISerializerModifierAccessor.cs rename to src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs diff --git a/src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs similarity index 100% rename from src/MongODM/Serialization/Modifiers/ReferenceSerializerModifier.cs rename to src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs diff --git a/src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs similarity index 100% rename from src/MongODM/Serialization/Modifiers/SerializerModifierAccessor.cs rename to src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs diff --git a/src/MongODM/Serialization/Serializers/DictionarySerializer.cs b/src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/DictionarySerializer.cs rename to src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs diff --git a/src/MongODM/Serialization/Serializers/EnumerableSerializer.cs b/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/EnumerableSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ExtendedClassMapSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/ExtraElementsSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ExtraElementsSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/GeoPointSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/HexToBinaryDataSerializer.cs b/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/HexToBinaryDataSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/IClassMapContainerSerializer.cs b/src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/IClassMapContainerSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/IReferenceContainerSerializer.cs b/src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/IReferenceContainerSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ReadOnlyDictionarySerializer.cs rename to src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs diff --git a/src/MongODM/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ReferenceSerializer.cs rename to src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs diff --git a/src/MongODM/Serialization/Serializers/ReferenceSerializerAdapter.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ReferenceSerializerAdapter.cs rename to src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs diff --git a/src/MongODM/Serialization/Serializers/ReferenceSerializerSwitch.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs similarity index 100% rename from src/MongODM/Serialization/Serializers/ReferenceSerializerSwitch.cs rename to src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs diff --git a/src/MongODM/Tasks/ITaskRunner.cs b/src/MongODM.Core/Tasks/ITaskRunner.cs similarity index 100% rename from src/MongODM/Tasks/ITaskRunner.cs rename to src/MongODM.Core/Tasks/ITaskRunner.cs diff --git a/src/MongODM/Tasks/IUpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs similarity index 100% rename from src/MongODM/Tasks/IUpdateDocDependenciesTask.cs rename to src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs diff --git a/src/MongODM/Tasks/Queues.cs b/src/MongODM.Core/Tasks/Queues.cs similarity index 100% rename from src/MongODM/Tasks/Queues.cs rename to src/MongODM.Core/Tasks/Queues.cs diff --git a/src/MongODM/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs similarity index 100% rename from src/MongODM/Tasks/UpdateDocDependenciesTask.cs rename to src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs diff --git a/src/MongODM/Utility/DBCache.cs b/src/MongODM.Core/Utility/DBCache.cs similarity index 100% rename from src/MongODM/Utility/DBCache.cs rename to src/MongODM.Core/Utility/DBCache.cs diff --git a/src/MongODM/Utility/DBMaintainer.cs b/src/MongODM.Core/Utility/DBMaintainer.cs similarity index 100% rename from src/MongODM/Utility/DBMaintainer.cs rename to src/MongODM.Core/Utility/DBMaintainer.cs diff --git a/src/MongODM/Utility/IDBCache.cs b/src/MongODM.Core/Utility/IDBCache.cs similarity index 100% rename from src/MongODM/Utility/IDBCache.cs rename to src/MongODM.Core/Utility/IDBCache.cs diff --git a/src/MongODM/Utility/IDBMaintainer.cs b/src/MongODM.Core/Utility/IDBMaintainer.cs similarity index 100% rename from src/MongODM/Utility/IDBMaintainer.cs rename to src/MongODM.Core/Utility/IDBMaintainer.cs diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 42c92976..849f5685 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -15,7 +15,7 @@ - + diff --git a/test/MongODM.Tests/Comparers/FakeModelComparer.cs b/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs similarity index 100% rename from test/MongODM.Tests/Comparers/FakeModelComparer.cs rename to test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs diff --git a/test/MongODM.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs similarity index 100% rename from test/MongODM.Tests/ExtendedClassMapSerializerTest.cs rename to test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs diff --git a/test/MongODM.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs similarity index 100% rename from test/MongODM.Tests/MockHelpers/InterceptorMockHelper.cs rename to test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs diff --git a/test/MongODM.Tests/Models/FakeEntityModelBase.cs b/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs similarity index 100% rename from test/MongODM.Tests/Models/FakeEntityModelBase.cs rename to test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs diff --git a/test/MongODM.Tests/Models/FakeModel.cs b/test/MongODM.Core.Tests/Models/FakeModel.cs similarity index 100% rename from test/MongODM.Tests/Models/FakeModel.cs rename to test/MongODM.Core.Tests/Models/FakeModel.cs diff --git a/test/MongODM.Tests/Models/FakeModelProxy.cs b/test/MongODM.Core.Tests/Models/FakeModelProxy.cs similarity index 100% rename from test/MongODM.Tests/Models/FakeModelProxy.cs rename to test/MongODM.Core.Tests/Models/FakeModelProxy.cs diff --git a/test/MongODM.Tests/Models/ModelBase.cs b/test/MongODM.Core.Tests/Models/ModelBase.cs similarity index 100% rename from test/MongODM.Tests/Models/ModelBase.cs rename to test/MongODM.Core.Tests/Models/ModelBase.cs diff --git a/test/MongODM.Tests/MongODM.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj similarity index 92% rename from test/MongODM.Tests/MongODM.Tests.csproj rename to test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index d4883211..af497f9f 100644 --- a/test/MongODM.Tests/MongODM.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -7,10 +7,6 @@ false - - - - all @@ -25,4 +21,8 @@ + + + + diff --git a/test/MongODM.Tests/ReferenceableInterceptorTest.cs b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs similarity index 100% rename from test/MongODM.Tests/ReferenceableInterceptorTest.cs rename to test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs From d0f6dabd680931815826ead7d8fb97e75b708cb7 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 18 Feb 2020 15:47:55 +0100 Subject: [PATCH 18/46] Updated dependencies --- src/MongODM.Core/MongODM.Core.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index a358fb46..c93dc358 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -12,8 +12,8 @@ - - + + From 7ffc7bddf6fd721d6d9ebc04dd56962c2917e542 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 20 Feb 2020 03:07:21 +0100 Subject: [PATCH 19/46] Fixed issue with IHttpContextAccessor injection --- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 2 +- src/MongODM.AspNetCore/ServiceCollectionExtensions.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 73bbc5a0..fce8bdcb 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 045258b3..d120eb8f 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -27,6 +27,8 @@ public static void UseMongODM( where TProxyGenerator: class, IProxyGenerator where TTaskRunner: class, ITaskRunner { + services.TryAddSingleton(); + services.TryAddSingleton(serviceProvider => { if (executionContexts is null || !executionContexts.Any()) From 990c7ce1c8596a0366ea3cc0abb39a6a2a8535b1 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sat, 22 Feb 2020 19:14:44 +0100 Subject: [PATCH 20/46] Updated ServiceCollectionExtensions --- .../ServiceCollectionExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index d120eb8f..213ccfe0 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using Digicando.ExecContext; using Digicando.ExecContext.AsyncLocal; +using Digicando.MongODM; using Digicando.MongODM.AspNetCore; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; @@ -63,5 +64,21 @@ public static void UseMongODM( //castle proxy generator. services.TryAddSingleton(new Castle.DynamicProxy.ProxyGenerator()); } + + public static void UseMongODMDbContext( + this IServiceCollection services) + where TDbContext : class, IDbContext + { + services.AddSingleton(); + } + + public static void UseMongODMDbContext( + this IServiceCollection services) + where TDbContext : class, IDbContext + where TDbContextImpl : class, TDbContext + { + services.AddSingleton(); + services.AddSingleton(sp => sp.GetService() as TDbContextImpl); + } } } From 05f370a47e1bf14f6840cb4799158ff1a38b568c Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 25 Feb 2020 01:27:14 +0100 Subject: [PATCH 21/46] Updated versioning system --- src/ExecutionContext/ExecutionContext.csproj | 5 ++++- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 8 +++++++- src/MongODM.Core/MongODM.Core.csproj | 5 ++++- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 5 ++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index a0023bee..7787f2ad 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -5,12 +5,15 @@ true Digicando.ExecContext true - 0.19.0 Digicando Srl Execution context provider + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index fce8bdcb..698aed9a 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -5,7 +5,6 @@ true Digicando.MongODM.AspNetCore true - 0.19.0-dev1 Digicando Srl Asp.Net Core adapter for MongODM @@ -20,6 +19,13 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index c93dc358..235f1572 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -5,13 +5,16 @@ true Digicando.MongODM true - 0.19.0-dev1 Digicando Srl ODM framework for MongoDB + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 849f5685..e85743dd 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -5,12 +5,15 @@ true Digicando.MongODM.HF true - 0.19.0-dev1 Digicando Srl Linker for use MongoDB with Hangfire + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + From 58389946c60db08a1f47fa4be51a9361122a992b Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 1 Mar 2020 19:24:57 +0100 Subject: [PATCH 22/46] Updated DocumentVersion parsing --- src/MongODM.Core/Serialization/DocumentVersion.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MongODM.Core/Serialization/DocumentVersion.cs b/src/MongODM.Core/Serialization/DocumentVersion.cs index 153a27d8..cc03c76b 100644 --- a/src/MongODM.Core/Serialization/DocumentVersion.cs +++ b/src/MongODM.Core/Serialization/DocumentVersion.cs @@ -18,9 +18,10 @@ public DocumentVersion(string version) // * 3.1 // * 3.1.4 // * 3.1.4-alpha1 + // * 3.1.4-beta.2 // * 3.1-DEV var match = Regex.Match(version, - @"^(?\d+)(\.(?\d+))?(\.(?\d+))?(-(? /// Type of source model /// Type of destination model - public class MongoCollectionMigration : MongoMigrationBase + public class MongoCollectionMigration : MongoMigrationBase { private readonly Func converter; private readonly IMongoCollection destinationCollection; private readonly Func discriminator; - private readonly IMongoCollection sourceCollection; public MongoCollectionMigration( Func discriminator, Func converter, - IMongoCollection sourceCollection, IMongoCollection destinationCollection, int priorityIndex) : base(priorityIndex) @@ -28,10 +26,9 @@ public MongoCollectionMigration( this.converter = converter; this.destinationCollection = destinationCollection; this.discriminator = discriminator; - this.sourceCollection = sourceCollection; } - public override async Task MigrateAsync(CancellationToken cancellationToken = default) + public override async Task MigrateAsync(IMongoCollection sourceCollection, CancellationToken cancellationToken = default) { // Migrate documents. await sourceCollection.Find(Builders.Filter.Empty, new FindOptions { NoCursorTimeout = true }) diff --git a/src/MongODM.Core/Migration/MongoDocumentMigration.cs b/src/MongODM.Core/Migration/MongoDocumentMigration.cs index 2e4e0f1f..1c55ff28 100644 --- a/src/MongODM.Core/Migration/MongoDocumentMigration.cs +++ b/src/MongODM.Core/Migration/MongoDocumentMigration.cs @@ -12,18 +12,14 @@ namespace Digicando.MongODM.Migration /// /// The model type /// The model's key type - public class MongoDocumentMigration : MongoMigrationBase + public class MongoDocumentMigration : MongoMigrationBase where TModel : class, IEntityModel { - private readonly IMongoCollection collection; - public MongoDocumentMigration( - IMongoCollection collection, DocumentVersion minimumDocumentVersion, int priorityIndex = 0) : base(priorityIndex) { - this.collection = collection; MinimumDocumentVersion = minimumDocumentVersion; } @@ -32,7 +28,7 @@ public MongoDocumentMigration( /// /// Fix all documents prev of MinimumDocumentVersion /// - public override async Task MigrateAsync(CancellationToken cancellationToken = default) + public override async Task MigrateAsync(IMongoCollection sourceCollection, CancellationToken cancellationToken = default) { var filterBuilder = Builders.Filter; var filter = filterBuilder.Or( @@ -59,8 +55,8 @@ public override async Task MigrateAsync(CancellationToken cancellationToken = de filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.2", MinimumDocumentVersion.PatchRelease))); // Replace documents. - await collection.Find(filter, new FindOptions { NoCursorTimeout = true }) - .ForEachAsync(obj => collection.ReplaceOneAsync(Builders.Filter.Eq(m => m.Id, obj.Id), obj), cancellationToken); + await sourceCollection.Find(filter, new FindOptions { NoCursorTimeout = true }) + .ForEachAsync(obj => sourceCollection.ReplaceOneAsync(Builders.Filter.Eq(m => m.Id, obj.Id), obj), cancellationToken); } } } diff --git a/src/MongODM.Core/Migration/MongoMigrationBase.cs b/src/MongODM.Core/Migration/MongoMigrationBase.cs index 29062c56..a01cea36 100644 --- a/src/MongODM.Core/Migration/MongoMigrationBase.cs +++ b/src/MongODM.Core/Migration/MongoMigrationBase.cs @@ -1,9 +1,10 @@ -using System.Threading; +using MongoDB.Driver; +using System.Threading; using System.Threading.Tasks; namespace Digicando.MongODM.Migration { - public abstract class MongoMigrationBase + public abstract class MongoMigrationBase { public MongoMigrationBase(int priorityIndex) { @@ -12,6 +13,8 @@ public MongoMigrationBase(int priorityIndex) public int PriorityIndex { get; } - public abstract Task MigrateAsync(CancellationToken cancellationToken = default); + public abstract Task MigrateAsync( + IMongoCollection sourceCollection, + CancellationToken cancellationToken = default); } } diff --git a/src/MongODM.Core/Repositories/CollectionRepositoryBase.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs similarity index 92% rename from src/MongODM.Core/Repositories/CollectionRepositoryBase.cs rename to src/MongODM.Core/Repositories/CollectionRepository.cs index 9fcae227..d232bcff 100644 --- a/src/MongODM.Core/Repositories/CollectionRepositoryBase.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -1,4 +1,5 @@ using Digicando.MongODM.Exceptions; +using Digicando.MongODM.Migration; using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; @@ -15,7 +16,7 @@ namespace Digicando.MongODM.Repositories { - public abstract class CollectionRepositoryBase : + public class CollectionRepository : RepositoryBase, ICollectionRepository where TModel : class, IEntityModel @@ -23,22 +24,31 @@ public abstract class CollectionRepositoryBase : // Fields. private readonly IDBMaintainer dbMaintainer; private readonly IDocumentSchemaRegister documentSchemaRegister; + private readonly CollectionRepositoryOptions options; // Constructors. - public CollectionRepositoryBase( - string collectionName, - IDbContext dbContext) + public CollectionRepository( + IDbContext dbContext, + string name) + : this(dbContext, new CollectionRepositoryOptions(name)) + { } + + public CollectionRepository( + IDbContext dbContext, + CollectionRepositoryOptions options) : base(dbContext) { - Collection = dbContext.Database.GetCollection(collectionName); + this.options = options ?? throw new ArgumentNullException(nameof(options)); + + Collection = dbContext.Database.GetCollection(options.Name); dbMaintainer = dbContext.DBMaintainer; documentSchemaRegister = dbContext.DocumentSchemaRegister; + MigrationInfo = options.MigrationInfo; } // Properties. public IMongoCollection Collection { get; } - protected virtual IEnumerable<(IndexKeysDefinition keys, CreateIndexOptions options)> IndexBuilders => - new (IndexKeysDefinition keys, CreateIndexOptions options)[0]; + public MongoMigrationBase MigrationInfo { get; } // Public methods. public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) @@ -47,7 +57,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis // Define new indexes. //repository defined - newIndexes.AddRange(IndexBuilders.Select(pair => + newIndexes.AddRange(options.IndexBuilders.Select(pair => { var (keys, options) = pair; if (options.Name == null) diff --git a/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs new file mode 100644 index 00000000..f8b464c4 --- /dev/null +++ b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs @@ -0,0 +1,19 @@ +using Digicando.MongODM.Migration; +using MongoDB.Driver; +using System; +using System.Collections.Generic; + +namespace Digicando.MongODM.Repositories +{ + public class CollectionRepositoryOptions : RepositoryOptionsBase + { + public CollectionRepositoryOptions(string name) + : base(name) + { + IndexBuilders = Array.Empty<(IndexKeysDefinition keys, CreateIndexOptions options)>(); + } + + public IEnumerable<(IndexKeysDefinition keys, CreateIndexOptions options)> IndexBuilders { get; set; } + public MongoMigrationBase MigrationInfo { get; set; } + } +} diff --git a/src/MongODM.Core/Repositories/GridFSRepositoryBase.cs b/src/MongODM.Core/Repositories/GridFSRepository.cs similarity index 72% rename from src/MongODM.Core/Repositories/GridFSRepositoryBase.cs rename to src/MongODM.Core/Repositories/GridFSRepository.cs index c7a691ba..84f24295 100644 --- a/src/MongODM.Core/Repositories/GridFSRepositoryBase.cs +++ b/src/MongODM.Core/Repositories/GridFSRepository.cs @@ -4,8 +4,6 @@ using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; using MongoDB.Bson; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; using MongoDB.Driver; using MongoDB.Driver.GridFS; using System; @@ -16,24 +14,32 @@ namespace Digicando.MongODM.Repositories { - public abstract class GridFSRepositoryBase : + public class GridFSRepository : RepositoryBase, IGridFSRepository where TModel : class, IFileModel { + // Fields. + private readonly GridFSRepositoryOptions options; + // Constructors. - public GridFSRepositoryBase( - string bucketName, - IDbContext dbContext) + public GridFSRepository( + IDbContext dbContext, + string name) + : this(dbContext, new GridFSRepositoryOptions(name)) + { } + + public GridFSRepository( + IDbContext dbContext, + GridFSRepositoryOptions options) : base(dbContext) { - var bucketOptions = new GridFSBucketOptions(); - if (bucketName != null) - { - bucketOptions.BucketName = bucketName; - } + this.options = options ?? throw new ArgumentNullException(nameof(options)); - GridFSBucket = new GridFSBucket(dbContext.Database, bucketOptions); + GridFSBucket = new GridFSBucket(dbContext.Database, new GridFSBucketOptions + { + BucketName = options.Name + }); ProxyGenerator = dbContext.ProxyGenerator; } @@ -51,9 +57,6 @@ public virtual async Task DownloadAsStreamAsync(string id, CancellationT await GridFSBucket.OpenDownloadStreamAsync(ObjectId.Parse(id), null, cancellationToken); // Protected methods. - protected virtual void CloneMetadataData(TModel src, TModel dest) - { } - protected override async Task CreateOnDBAsync(IEnumerable models, CancellationToken cancellationToken) { foreach (var model in models) @@ -62,10 +65,14 @@ protected override async Task CreateOnDBAsync(IEnumerable models, Cancel protected override async Task CreateOnDBAsync(TModel model, CancellationToken cancellationToken) { + if (model is null) + throw new ArgumentNullException(nameof(model)); + + // Upload. model.Stream.Position = 0; var id = await GridFSBucket.UploadFromStreamAsync(model.Name, model.Stream, new GridFSUploadOptions { - Metadata = SerializeMetadata(model) + Metadata = options.MetadataSerializer?.Invoke(model) }); ReflectionHelper.SetValue(model, m => m.Id, id.ToString()); } @@ -89,31 +96,9 @@ protected override async Task FindOneOnDBAsync(string id, CancellationTo ReflectionHelper.SetValue(file, m => m.Name, mongoFile.Filename); // Deserialize metadata. - DeserializeMetadata(file, mongoFile.Metadata); + options.MetadataDeserializer?.Invoke(mongoFile.Metadata, file); return file; } - - // Private helpers. - private void DeserializeMetadata(TModel obj, BsonDocument metadata) - { - if (metadata != null) - { - var metadataObject = BsonSerializer.Deserialize(metadata); - CloneMetadataData(metadataObject, obj); - } - } - - private BsonDocument SerializeMetadata(TModel obj) - { - if (obj == null) - { - return null; - } - - var document = new BsonDocument(); - BsonSerializer.Serialize(new BsonDocumentWriter(document), obj); - return document; - } } } diff --git a/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs new file mode 100644 index 00000000..ff935cec --- /dev/null +++ b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs @@ -0,0 +1,15 @@ +using MongoDB.Bson; +using System; + +namespace Digicando.MongODM.Repositories +{ + public class GridFSRepositoryOptions : RepositoryOptionsBase + { + public GridFSRepositoryOptions(string name) + : base(name) + { } + + public Action MetadataDeserializer { get; set; } + public Func MetadataSerializer { get; set; } + } +} diff --git a/src/MongODM.Core/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs index 78be0e4d..fecb7dcd 100644 --- a/src/MongODM.Core/Repositories/ICollectionRepository.cs +++ b/src/MongODM.Core/Repositories/ICollectionRepository.cs @@ -1,4 +1,5 @@ -using Digicando.MongODM.Models; +using Digicando.MongODM.Migration; +using Digicando.MongODM.Models; using MongoDB.Driver; using MongoDB.Driver.Linq; using System; @@ -26,6 +27,7 @@ public interface ICollectionRepository : IRepository where TModel : class, IEntityModel { IMongoCollection Collection { get; } + MongoMigrationBase MigrationInfo { get; } Task> FindAsync( FilterDefinition filter, diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index d56e8427..ba1f0300 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -1,5 +1,4 @@ -using Digicando.MongODM.Migration; -using Digicando.MongODM.Models; +using Digicando.MongODM.Models; using Digicando.MongODM.Serialization; using System.Collections.Generic; using System.Threading; @@ -9,8 +8,6 @@ namespace Digicando.MongODM.Repositories { public interface IRepository { - MongoMigrationBase MigrationInfo { get; } - Task BuildIndexesAsync( IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default); diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index 42d8bd60..f5e8d877 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -1,6 +1,5 @@ using Digicando.DomainHelper; using Digicando.MongODM.Exceptions; -using Digicando.MongODM.Migration; using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; @@ -24,9 +23,6 @@ public RepositoryBase(IDbContext dbContext) this.DbContext = dbContext; } - // Properties. - public virtual MongoMigrationBase MigrationInfo { get; } - // Protected properties. protected IDbContext DbContext { get; } diff --git a/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs b/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs new file mode 100644 index 00000000..212aa5b6 --- /dev/null +++ b/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs @@ -0,0 +1,12 @@ +namespace Digicando.MongODM.Repositories +{ + public abstract class RepositoryOptionsBase + { + public RepositoryOptionsBase(string name) + { + Name = name ?? throw new System.ArgumentNullException(nameof(name)); + } + + public string Name { get; } + } +} diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index 67ccc42a..9ee3e993 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -8,8 +8,8 @@ - - + + all diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index d489415a..0fccce9a 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -8,8 +8,8 @@ - - + + all From 24a0e113e002b959b0f61491950c9c1485c2311a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 27 Apr 2020 16:46:00 +0200 Subject: [PATCH 29/46] Updated migrations --- src/MongODM.Core/DbContext.cs | 12 +++--- .../Migration/MongoCollectionMigration.cs | 40 ++++++++++--------- .../Migration/MongoDocumentMigration.cs | 30 +++++++------- .../Migration/MongoMigrationBase.cs | 16 ++------ .../Repositories/CollectionRepository.cs | 2 - .../CollectionRepositoryOptions.cs | 4 +- .../Repositories/ICollectionRepository.cs | 4 +- 7 files changed, 49 insertions(+), 59 deletions(-) diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index de74fcc2..76d5999d 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -1,4 +1,5 @@ -using Digicando.MongODM.Models; +using Digicando.MongODM.Migration; +using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; @@ -81,17 +82,16 @@ public DbContext( public ISerializerModifierAccessor SerializerModifierAccessor { get; } // Protected properties. + protected virtual IEnumerable MigrationTaskList { get; } = Array.Empty(); protected abstract IEnumerable SerializerCollectors { get; } // Methods. public async Task MigrateRepositoriesAsync(CancellationToken cancellationToken = default) { - // Migrate documents. IsMigrating = true; - foreach (var migration in from repository in ModelCollectionRepositoryMap.Values - where repository.MigrationInfo != null - orderby repository.MigrationInfo.PriorityIndex - select repository.MigrationInfo) + + // Migrate collections. + foreach (var migration in MigrationTaskList) await migration.MigrateAsync(cancellationToken); // Build indexes. diff --git a/src/MongODM.Core/Migration/MongoCollectionMigration.cs b/src/MongODM.Core/Migration/MongoCollectionMigration.cs index a2f15a10..ac4dec5e 100644 --- a/src/MongODM.Core/Migration/MongoCollectionMigration.cs +++ b/src/MongODM.Core/Migration/MongoCollectionMigration.cs @@ -1,4 +1,6 @@ -using MongoDB.Driver; +using Digicando.MongODM.Models; +using Digicando.MongODM.Repositories; +using MongoDB.Driver; using System; using System.Threading; using System.Threading.Tasks; @@ -8,35 +10,37 @@ namespace Digicando.MongODM.Migration /// /// Migrate a collection to another /// - /// Type of source model - /// Type of destination model - public class MongoCollectionMigration : MongoMigrationBase + /// Type of source model + /// Type of source key + /// Type of destination model + /// Type of destination key + public class MongoCollectionMigration : MongoMigrationBase + where TModelSource : class, IEntityModel + where TModelDest : class, IEntityModel { - private readonly Func converter; - private readonly IMongoCollection destinationCollection; - private readonly Func discriminator; + private readonly Func converter; + private readonly Func discriminator; + private readonly IMongoCollection destinationCollection; + private readonly IMongoCollection sourceCollection; public MongoCollectionMigration( - Func discriminator, - Func converter, - IMongoCollection destinationCollection, - int priorityIndex) - : base(priorityIndex) + ICollectionRepository sourceCollection, + ICollectionRepository destinationCollection, + Func converter, + Func discriminator) { + this.sourceCollection = sourceCollection.Collection; + this.destinationCollection = destinationCollection.Collection; this.converter = converter; - this.destinationCollection = destinationCollection; this.discriminator = discriminator; } - public override async Task MigrateAsync(IMongoCollection sourceCollection, CancellationToken cancellationToken = default) - { - // Migrate documents. - await sourceCollection.Find(Builders.Filter.Empty, new FindOptions { NoCursorTimeout = true }) + public override Task MigrateAsync(CancellationToken cancellationToken = default) => + sourceCollection.Find(Builders.Filter.Empty, new FindOptions { NoCursorTimeout = true }) .ForEachAsync(obj => { if (discriminator(obj)) destinationCollection.InsertOneAsync(converter(obj)); }, cancellationToken); - } } } diff --git a/src/MongODM.Core/Migration/MongoDocumentMigration.cs b/src/MongODM.Core/Migration/MongoDocumentMigration.cs index 1c55ff28..0f52439b 100644 --- a/src/MongODM.Core/Migration/MongoDocumentMigration.cs +++ b/src/MongODM.Core/Migration/MongoDocumentMigration.cs @@ -1,4 +1,5 @@ using Digicando.MongODM.Models; +using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; @@ -12,23 +13,24 @@ namespace Digicando.MongODM.Migration /// /// The model type /// The model's key type - public class MongoDocumentMigration : MongoMigrationBase + public class MongoDocumentMigration : MongoMigrationBase where TModel : class, IEntityModel { + private readonly DocumentVersion minimumDocumentVersion; + private readonly IMongoCollection sourceCollection; + public MongoDocumentMigration( - DocumentVersion minimumDocumentVersion, - int priorityIndex = 0) - : base(priorityIndex) + ICollectionRepository sourceCollection, + DocumentVersion minimumDocumentVersion) { - MinimumDocumentVersion = minimumDocumentVersion; + this.sourceCollection = sourceCollection.Collection; + this.minimumDocumentVersion = minimumDocumentVersion; } - public DocumentVersion MinimumDocumentVersion { get; } - /// /// Fix all documents prev of MinimumDocumentVersion /// - public override async Task MigrateAsync(IMongoCollection sourceCollection, CancellationToken cancellationToken = default) + public override async Task MigrateAsync(CancellationToken cancellationToken = default) { var filterBuilder = Builders.Filter; var filter = filterBuilder.Or( @@ -41,18 +43,18 @@ public override async Task MigrateAsync(IMongoCollection sourceCollectio // Version is an array with values ("0.12.0" <= doc.Version). //doc.Major < min.Major - filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.0", minimumDocumentVersion.MajorRelease), //doc.Major == min.Major && doc.Minor < min.Minor filterBuilder.And( - filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), - filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease)), + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", minimumDocumentVersion.MajorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.1", minimumDocumentVersion.MinorRelease)), //doc.Major == min.Major && doc.Minor == min.Minor && doc.Patch < min.Patch filterBuilder.And( - filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", MinimumDocumentVersion.MajorRelease), - filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.1", MinimumDocumentVersion.MinorRelease), - filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.2", MinimumDocumentVersion.PatchRelease))); + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.0", minimumDocumentVersion.MajorRelease), + filterBuilder.Eq($"{DbContext.DocumentVersionElementName}.1", minimumDocumentVersion.MinorRelease), + filterBuilder.Lt($"{DbContext.DocumentVersionElementName}.2", minimumDocumentVersion.PatchRelease))); // Replace documents. await sourceCollection.Find(filter, new FindOptions { NoCursorTimeout = true }) diff --git a/src/MongODM.Core/Migration/MongoMigrationBase.cs b/src/MongODM.Core/Migration/MongoMigrationBase.cs index a01cea36..6a2f1591 100644 --- a/src/MongODM.Core/Migration/MongoMigrationBase.cs +++ b/src/MongODM.Core/Migration/MongoMigrationBase.cs @@ -1,20 +1,10 @@ -using MongoDB.Driver; -using System.Threading; +using System.Threading; using System.Threading.Tasks; namespace Digicando.MongODM.Migration { - public abstract class MongoMigrationBase + public abstract class MongoMigrationBase { - public MongoMigrationBase(int priorityIndex) - { - PriorityIndex = priorityIndex; - } - - public int PriorityIndex { get; } - - public abstract Task MigrateAsync( - IMongoCollection sourceCollection, - CancellationToken cancellationToken = default); + public abstract Task MigrateAsync(CancellationToken cancellationToken = default); } } diff --git a/src/MongODM.Core/Repositories/CollectionRepository.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs index d232bcff..c6a0cde7 100644 --- a/src/MongODM.Core/Repositories/CollectionRepository.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -43,12 +43,10 @@ public CollectionRepository( Collection = dbContext.Database.GetCollection(options.Name); dbMaintainer = dbContext.DBMaintainer; documentSchemaRegister = dbContext.DocumentSchemaRegister; - MigrationInfo = options.MigrationInfo; } // Properties. public IMongoCollection Collection { get; } - public MongoMigrationBase MigrationInfo { get; } // Public methods. public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) diff --git a/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs index f8b464c4..13ac3f6a 100644 --- a/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs +++ b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs @@ -1,5 +1,4 @@ -using Digicando.MongODM.Migration; -using MongoDB.Driver; +using MongoDB.Driver; using System; using System.Collections.Generic; @@ -14,6 +13,5 @@ public CollectionRepositoryOptions(string name) } public IEnumerable<(IndexKeysDefinition keys, CreateIndexOptions options)> IndexBuilders { get; set; } - public MongoMigrationBase MigrationInfo { get; set; } } } diff --git a/src/MongODM.Core/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs index fecb7dcd..78be0e4d 100644 --- a/src/MongODM.Core/Repositories/ICollectionRepository.cs +++ b/src/MongODM.Core/Repositories/ICollectionRepository.cs @@ -1,5 +1,4 @@ -using Digicando.MongODM.Migration; -using Digicando.MongODM.Models; +using Digicando.MongODM.Models; using MongoDB.Driver; using MongoDB.Driver.Linq; using System; @@ -27,7 +26,6 @@ public interface ICollectionRepository : IRepository where TModel : class, IEntityModel { IMongoCollection Collection { get; } - MongoMigrationBase MigrationInfo { get; } Task> FindAsync( FilterDefinition filter, From 2e4061b725afdd0bf27ac8a8fd214c4fc705bed3 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 27 Apr 2020 19:39:38 +0200 Subject: [PATCH 30/46] Enabled nullable check on minor projects --- .../AsyncLocal/AsyncLocalContext.cs | 4 +- src/ExecutionContext/ExecutionContext.csproj | 2 + .../ExecutionContextSelector.cs | 2 +- src/ExecutionContext/IExecutionContext.cs | 2 +- .../HttpContextExecutionContext.cs | 2 +- .../MongODM.AspNetCore.csproj | 2 + .../ServiceCollectionExtensions.cs | 4 +- src/MongODM.Core/MongODM.Core.csproj | 1 + .../Serializers/ExtendedClassMapSerializer.cs | 27 ++- .../Tasks/UpdateDocDependenciesTask.cs | 31 +-- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 + .../Tasks/UpdateDocDependenciesTaskFacade.cs | 11 +- .../ExecutionContext.Tests.csproj | 1 + .../ExecutionContextSelectorTests.cs | 6 - .../Comparers/FakeModelComparer.cs | 6 +- .../ExtendedClassMapSerializerTest.cs | 182 +++++++++--------- .../MockHelpers/InterceptorMockHelper.cs | 22 +-- .../Models/FakeEntityModelBase.cs | 2 +- test/MongODM.Core.Tests/Models/FakeModel.cs | 6 +- test/MongODM.Core.Tests/Models/ModelBase.cs | 2 +- .../MongODM.Core.Tests.csproj | 1 + 21 files changed, 155 insertions(+), 163 deletions(-) diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index ab6d52fa..28fb2758 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -17,10 +17,10 @@ namespace Digicando.ExecContext.AsyncLocal public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext { // Fields. - private static readonly AsyncLocal> asyncLocalContext = new AsyncLocal>(); + private static readonly AsyncLocal?> asyncLocalContext = new AsyncLocal?>(); // Properties. - public IDictionary Items => asyncLocalContext.Value; + public IDictionary? Items => asyncLocalContext.Value; // Static properties. public static IAsyncLocalContext Instance { get; } = new AsyncLocalContext(); diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index c21b91d2..cd6b6664 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -7,6 +7,8 @@ true Digicando Srl Execution context provider + 8.0 + enable diff --git a/src/ExecutionContext/ExecutionContextSelector.cs b/src/ExecutionContext/ExecutionContextSelector.cs index f6ff1869..7c3df66b 100644 --- a/src/ExecutionContext/ExecutionContextSelector.cs +++ b/src/ExecutionContext/ExecutionContextSelector.cs @@ -26,7 +26,7 @@ public ExecutionContextSelector(IEnumerable contexts) } // Proeprties. - public IDictionary Items + public IDictionary? Items { get { diff --git a/src/ExecutionContext/IExecutionContext.cs b/src/ExecutionContext/IExecutionContext.cs index 785c734c..16eadbac 100644 --- a/src/ExecutionContext/IExecutionContext.cs +++ b/src/ExecutionContext/IExecutionContext.cs @@ -11,6 +11,6 @@ public interface IExecutionContext /// /// The context dictionary. /// - IDictionary Items { get; } + IDictionary? Items { get; } } } diff --git a/src/MongODM.AspNetCore/HttpContextExecutionContext.cs b/src/MongODM.AspNetCore/HttpContextExecutionContext.cs index f314a92a..ad2d32db 100644 --- a/src/MongODM.AspNetCore/HttpContextExecutionContext.cs +++ b/src/MongODM.AspNetCore/HttpContextExecutionContext.cs @@ -16,6 +16,6 @@ public HttpContextExecutionContext(IHttpContextAccessor httpContextAccessor) } // Properties. - public IDictionary Items => httpContextAccessor?.HttpContext?.Items; + public IDictionary? Items => httpContextAccessor?.HttpContext?.Items; } } diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index f0886f62..94a5a639 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -7,6 +7,8 @@ true Digicando Srl Asp.Net Core adapter for MongODM + 8.0 + enable diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 213ccfe0..950b2fb8 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -18,13 +18,13 @@ public static class ServiceCollectionExtensions { public static void UseMongODM( this IServiceCollection services, - IEnumerable executionContexts = null) + IEnumerable? executionContexts = null) where TTaskRunner : class, ITaskRunner => UseMongODM(services, executionContexts); public static void UseMongODM( this IServiceCollection services, - IEnumerable executionContexts = null) + IEnumerable? executionContexts = null) where TProxyGenerator: class, IProxyGenerator where TTaskRunner: class, ITaskRunner { diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 2f682af7..c1fade0b 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -7,6 +7,7 @@ true Digicando Srl ODM framework for MongoDB + 8.0 diff --git a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs index dcdb747b..51fb7dcb 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -210,23 +210,18 @@ public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializati } // Helpers. - private static DocumentVersion BsonValueToDocumentVersion(BsonValue bsonValue) - { - switch (bsonValue) + private static DocumentVersion BsonValueToDocumentVersion(BsonValue bsonValue) => + bsonValue switch { - case BsonNull _: - return null; - case BsonString bsonString: // v < 0.12.0 - return new DocumentVersion(bsonString.AsString); - case BsonArray bsonArray: // 0.12.0 <= v - return new DocumentVersion( - bsonArray[0].AsInt32, - bsonArray[1].AsInt32, - bsonArray[2].AsInt32, - bsonArray.Count >= 4 ? bsonArray[3].AsString : null); - default: throw new NotSupportedException(); - } - } + BsonNull _ => null, + BsonString bsonString => new DocumentVersion(bsonString.AsString), + BsonArray bsonArray => new DocumentVersion( + bsonArray[0].AsInt32, + bsonArray[1].AsInt32, + bsonArray[2].AsInt32, + bsonArray.Count >= 4 ? bsonArray[3].AsString : null), + _ => throw new NotSupportedException(), + }; private static BsonArray DocumentVersionToBsonArray(DocumentVersion documentVersion) { diff --git a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs index 39f87475..c7a0a2bb 100644 --- a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs @@ -43,27 +43,28 @@ public async Task RunAsync( { foreach (var idPath in idPaths) { - using (var cursor = await repository.FindAsync( + using var cursor = await repository.FindAsync( Builders.Filter.Eq(idPath, modelId), - new FindOptions { NoCursorTimeout = true })) + new FindOptions { NoCursorTimeout = true }); + + // Load and replace. + while (await cursor.MoveNextAsync()) { - // Load and replace. - while (await cursor.MoveNextAsync()) - foreach (var model in cursor.Current) + foreach (var model in cursor.Current) + { + if (!upgradedDocumentsId.Contains(model.Id)) { - if (!upgradedDocumentsId.Contains(model.Id)) + try { - try - { - // Replace on db. - await repository.ReplaceAsync(model, false); - } - catch { } - - // Add id to upgraded list. - upgradedDocumentsId.Add(model.Id); + // Replace on db. + await repository.ReplaceAsync(model, false); } + catch { } + + // Add id to upgraded list. + upgradedDocumentsId.Add(model.Id); } + } } } } diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index a1c1340b..bda1b569 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -7,6 +7,8 @@ true Digicando Srl Linker for use MongoDB with Hangfire + 8.0 + enable diff --git a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs index d32285dd..82d91b0d 100644 --- a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs +++ b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -20,10 +20,13 @@ public UpdateDocDependenciesTaskFacade(IUpdateDocDependenciesTask task) // Methods. [Queue(Queues.DB_MAINTENANCE)] - public Task RunAsync(Type dbContextType, Type modelType, Type keyType, IEnumerable idPaths, object modelId) => - typeof(UpdateDocDependenciesTask).GetMethod( + public Task RunAsync(Type dbContextType, Type modelType, Type keyType, IEnumerable idPaths, object modelId) + { + var method = typeof(UpdateDocDependenciesTask).GetMethod( nameof(UpdateDocDependenciesTask.RunAsync), BindingFlags.Public | BindingFlags.Instance) - .MakeGenericMethod(dbContextType, modelType, keyType) - .Invoke(task, new object[] { idPaths, modelId }) as Task; + .MakeGenericMethod(dbContextType, modelType, keyType); + + return (Task)method.Invoke(task, new object[] { idPaths, modelId }); + } } } diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index 9ee3e993..280b4767 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -5,6 +5,7 @@ true Digicando.ExecContext false + enable diff --git a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs index d04666bc..7c8ea11d 100644 --- a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs +++ b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs @@ -7,12 +7,6 @@ namespace Digicando.ExecContext { public class ExecutionContextSelectorTests { - [Fact] - public void NullContextsException() - { - Assert.Throws(() => new ExecutionContextSelector(null)); - } - [Theory] [InlineData(false, false, null)] [InlineData(false, true, "1")] diff --git a/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs b/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs index 2690fa5c..eed1f128 100644 --- a/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs +++ b/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs @@ -4,9 +4,9 @@ namespace Digicando.MongODM.Comparers { - public class FakeModelComparer : EqualityComparer + public class FakeModelComparer : EqualityComparer { - public override bool Equals(FakeModel x, FakeModel y) + public override bool Equals(FakeModel? x, FakeModel? y) { if (ReferenceEquals(x, y)) return true; if (x is null || y is null) return false; @@ -50,7 +50,7 @@ public override bool Equals(FakeModel x, FakeModel y) return true; } - public override int GetHashCode(FakeModel obj) + public override int GetHashCode(FakeModel? obj) { if (obj is null) return -1; return obj.GetHashCode(); diff --git a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs index 288361fd..966a661d 100644 --- a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs @@ -23,37 +23,46 @@ public class ExtendedClassMapSerializerTest public class DeserializationTestElement { public DeserializationTestElement( - Action bsonReaderPreAction = null, - Action bsonReaderPostAction = null) + BsonDocument document, + FakeModel? expectedModel, + Action? preAction = null, + Action? postAction = null) { - BsonReaderPreAction = bsonReaderPreAction ?? (rd => { }); - BsonReaderPostAction = bsonReaderPostAction ?? (rd => { }); + Document = document; + ExpectedModel = expectedModel; + PreAction = preAction ?? (rd => { }); + PostAction = postAction ?? (rd => { }); } - public Action BsonReaderPostAction { get; } - public Action BsonReaderPreAction { get; } - public BsonDocument Document { get; set; } - public FakeModel ExpectedModel { get; set; } + public BsonDocument Document { get; } + public FakeModel? ExpectedModel { get; } + public Action PreAction { get; } + public Action PostAction { get; } } public class SerializationTestElement { public SerializationTestElement( - Action bsonWriterPreAction = null, - Action bsonWriterPostAction = null) + FakeModel? model, + BsonDocument expectedDocument, + Func? condition = null, + Action? preAction = null, + Action? postAction = null) { - SerializedDocument = new BsonDocument(); BsonWriter = new BsonDocumentWriter(SerializedDocument); - BsonWriterPreAction = bsonWriterPreAction ?? (wr => { }); - BsonWriterPostAction = bsonWriterPostAction ?? (wr => { }); + Condition = condition; + ExpectedDocument = expectedDocument; + Model = model; + PreAction = preAction ?? (wr => { }); + PostAction = postAction ?? (wr => { }); } public BsonWriter BsonWriter { get; } - public Action BsonWriterPostAction { get; } - public Action BsonWriterPreAction { get; } - public Func Condition { get; set; } - public BsonDocument ExpectedDocument { get; set; } - public FakeModel Model { get; set; } - public BsonDocument SerializedDocument { get; } + public Func? Condition { get; } + public BsonDocument ExpectedDocument { get; } + public FakeModel? Model { get; } + public Action PreAction { get; } + public Action PostAction { get; } + public BsonDocument SerializedDocument { get; } = new BsonDocument(); } // Fields. @@ -80,51 +89,45 @@ public static IEnumerable DeserializationTests { // Null model new DeserializationTestElement( - rd => + new BsonDocument(new BsonElement("elem", BsonNull.Value)), + null, + preAction: rd => { rd.ReadStartDocument(); rd.ReadName(); }, - rs => rs.ReadEndDocument()) - { - ExpectedModel = null, - Document = new BsonDocument(new BsonElement("elem", BsonNull.Value)) - }, + postAction: rd => rd.ReadEndDocument()), // Model without extra members - new DeserializationTestElement - { - ExpectedModel = new FakeModel - { - Id = "idVal", - IntegerProp = 8, - StringProp = "ok" - }, - Document = new BsonDocument(new BsonElement[] + new DeserializationTestElement( + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("IntegerProp", new BsonInt32(8)), new BsonElement("StringProp", new BsonString("ok")) - } as IEnumerable) - }, - - // Model with extra members - new DeserializationTestElement - { - ExpectedModel = new FakeModel + } as IEnumerable), + new FakeModel { Id = "idVal", IntegerProp = 8, - StringProp = "rightValue" - }, - Document = new BsonDocument(new BsonElement[] + StringProp = "ok" + }), + + // Model with extra members + new DeserializationTestElement( + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("IntegerProp", new BsonInt32(8)), new BsonElement("StringProp", new BsonString("wrongValue")), new BsonElement("ExtraElement", new BsonString("rightValue")) - } as IEnumerable) - } + } as IEnumerable), + new FakeModel + { + Id = "idVal", + IntegerProp = 8, + StringProp = "rightValue" + }) }; return tests.Select(t => new object[] { t }); } @@ -149,11 +152,11 @@ public void Deserialize(DeserializationTestElement test) }); // Action - test.BsonReaderPreAction(bsonReader); + test.PreAction(bsonReader); var result = serializer.Deserialize( BsonDeserializationContext.CreateRoot(bsonReader), new BsonDeserializationArgs { NominalType = typeof(FakeModel) }); - test.BsonReaderPostAction(bsonReader); + test.PostAction(bsonReader); // Assert Assert.Equal(test.ExpectedModel, result as FakeModel, new FakeModelComparer()); @@ -220,21 +223,18 @@ public static IEnumerable SerializationTests { // Null model new SerializationTestElement( - wr => + null, + new BsonDocument(new BsonElement("elem", BsonNull.Value)), + preAction: wr => { wr.WriteStartDocument(); wr.WriteName("elem"); }, - wr => wr.WriteEndDocument()) - { - Model = null, - ExpectedDocument = new BsonDocument(new BsonElement("elem", BsonNull.Value)) - }, + postAction: wr => wr.WriteEndDocument()), // Complex model - new SerializationTestElement - { - Model = new FakeModel() + new SerializationTestElement( + new FakeModel() { EnumerableProp = new[] { new FakeModel(), null }, Id = "idVal", @@ -242,7 +242,7 @@ public static IEnumerable SerializationTests ObjectProp = new FakeModel(), StringProp = "yes" }, - ExpectedDocument = new BsonDocument(new BsonElement[] + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("CreationDateTime", new BsonDateTime(new DateTime())), @@ -271,18 +271,15 @@ public static IEnumerable SerializationTests } as IEnumerable)), new BsonElement("StringProp", new BsonString("yes")), new BsonElement("ExtraElement", new BsonString("extraValue")) - } as IEnumerable) - }, + } as IEnumerable)), // True condition. - new SerializationTestElement - { - Condition = _ => true, - Model = new FakeModel() + new SerializationTestElement( + new FakeModel() { Id = "idVal", }, - ExpectedDocument = new BsonDocument(new BsonElement[] + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("CreationDateTime", new BsonDateTime(new DateTime())), @@ -291,18 +288,16 @@ public static IEnumerable SerializationTests new BsonElement("ObjectProp", BsonNull.Value), new BsonElement("StringProp", BsonNull.Value), new BsonElement("ExtraElement", new BsonString("extraValue")) - } as IEnumerable) - }, + } as IEnumerable), + condition: _ => true), // False condition. - new SerializationTestElement - { - Condition = _ => false, - Model = new FakeModel() + new SerializationTestElement( + new FakeModel() { Id = "idVal", }, - ExpectedDocument = new BsonDocument(new BsonElement[] + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("CreationDateTime", new BsonDateTime(new DateTime())), @@ -310,24 +305,19 @@ public static IEnumerable SerializationTests new BsonElement("IntegerProp", new BsonInt32(0)), new BsonElement("ObjectProp", BsonNull.Value), new BsonElement("StringProp", BsonNull.Value) - } as IEnumerable) - } - }; - - // With a proxy class. - { - var model = new FakeModelProxy() - { - Id = "idVal", - IntegerProp = 42, - ObjectProp = new FakeModel(), - StringProp = "yes" - }; - tests.Add(new SerializationTestElement - { - Condition = _ => false, - Model = model, - ExpectedDocument = new BsonDocument(new BsonElement[] + } as IEnumerable), + condition: _ => false), + + // With a proxy class. + new SerializationTestElement( + new FakeModelProxy() + { + Id = "idVal", + IntegerProp = 42, + ObjectProp = new FakeModel(), + StringProp = "yes" + }, + new BsonDocument(new BsonElement[] { new BsonElement("_id", new BsonString("idVal")), new BsonElement("CreationDateTime", new BsonDateTime(new DateTime())), @@ -343,9 +333,9 @@ public static IEnumerable SerializationTests new BsonElement("StringProp", BsonNull.Value) } as IEnumerable)), new BsonElement("StringProp", new BsonString("yes")) - } as IEnumerable) - }); - } + } as IEnumerable), + condition: _ => false) + }; return tests.Select(t => new object[] { t }); } @@ -362,12 +352,12 @@ public void Serialize(SerializationTestElement test) .AddExtraElement(new BsonElement("ExtraElement", new BsonString("extraValue")), test.Condition); // Action - test.BsonWriterPreAction(test.BsonWriter); + test.PreAction(test.BsonWriter); serializer.Serialize( BsonSerializationContext.CreateRoot(test.BsonWriter), new BsonSerializationArgs { NominalType = typeof(FakeModel) }, - test.Model); - test.BsonWriterPostAction(test.BsonWriter); + test.Model!); + test.PostAction(test.BsonWriter); // Assert Assert.Equal(0, test.SerializedDocument.CompareTo(test.ExpectedDocument)); diff --git a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs index 40cf02ab..3d075bd2 100644 --- a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs +++ b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs @@ -9,8 +9,8 @@ public static class InterceptorMockHelper { public static Mock GetExternalMethodInvocationMock( string methodName, - object[] arguments = null, - TProxy proxyModel = null) + object[]? arguments = null, + TProxy? proxyModel = null) where TProxy : class { var invocation = GetInvocationMock(proxyModel); @@ -31,7 +31,7 @@ public static class InterceptorMockHelper public static Mock GetExternalPropertyGetInvocationMock( Expression> memberLambda, - TProxy proxyModel = null, + TProxy? proxyModel = null, TMember returnValue = default) where TProxy : class { @@ -52,28 +52,28 @@ public static class InterceptorMockHelper public static Mock GetExternalPropertySetInvocationMock( Expression> memberLambda, TMember value, - TProxy proxyModel = null) + TProxy? proxyModel = null) where TProxy : class { var invocation = GetInvocationMock(proxyModel); var memberInfo = ReflectionHelper.GetMemberInfoFromLambda(memberLambda); invocation.Setup(i => i.Arguments) - .Returns(new object[] { value }); + .Returns(new object[] { value! }); invocation.Setup(i => i.Method.Name) .Returns($"set_{memberInfo.Name}"); invocation.Setup(i => i.Method.ReturnType) .Returns(typeof(TMember)); invocation.Setup(i => i.GetArgumentValue(0)) - .Returns(value); + .Returns(value!); return invocation; } public static Mock GetMethodInvocationMock( string methodName, - object[] arguments = null, - TProxy proxyModel = null) + object[]? arguments = null, + TProxy? proxyModel = null) where TProxy : class { return GetExternalMethodInvocationMock(methodName, arguments, proxyModel); @@ -81,7 +81,7 @@ public static class InterceptorMockHelper public static Mock GetPropertyGetInvocationMock( Expression> memberLambda, - TProxy proxyModel = null, + TProxy? proxyModel = null, TMember returnValue = default) where TProxy : class { @@ -91,7 +91,7 @@ public static class InterceptorMockHelper public static Mock GetPropertySetInvocationMock( Expression> memberLambda, TMember value, - TProxy proxyModel = null) + TProxy? proxyModel = null) where TProxy : class { return GetExternalPropertySetInvocationMock(memberLambda, value, proxyModel); @@ -100,7 +100,7 @@ public static class InterceptorMockHelper // Helpers. private static Mock GetInvocationMock( - TProxy proxyModel) + TProxy? proxyModel) where TProxy : class { var invocation = new Mock(); diff --git a/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs b/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs index 758441b4..b479c34c 100644 --- a/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs +++ b/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs @@ -4,7 +4,7 @@ namespace Digicando.MongODM.Models { public abstract class FakeEntityModelBase : ModelBase, IEntityModel { - public virtual TKey Id { get; set; } + public virtual TKey Id { get; set; } = default!; public virtual DateTime CreationDateTime { get; private set; } public virtual void DisposeForDelete() { } diff --git a/test/MongODM.Core.Tests/Models/FakeModel.cs b/test/MongODM.Core.Tests/Models/FakeModel.cs index 0166479c..0ee03ee2 100644 --- a/test/MongODM.Core.Tests/Models/FakeModel.cs +++ b/test/MongODM.Core.Tests/Models/FakeModel.cs @@ -4,9 +4,9 @@ namespace Digicando.MongODM.Models { public class FakeModel : FakeEntityModelBase { - public virtual IEnumerable EnumerableProp { get; set; } + public virtual IEnumerable? EnumerableProp { get; set; } public virtual int IntegerProp { get; set; } - public virtual FakeModel ObjectProp { get; set; } - public virtual string StringProp { get; set; } + public virtual FakeModel? ObjectProp { get; set; } + public virtual string? StringProp { get; set; } } } diff --git a/test/MongODM.Core.Tests/Models/ModelBase.cs b/test/MongODM.Core.Tests/Models/ModelBase.cs index 5162ae12..71691237 100644 --- a/test/MongODM.Core.Tests/Models/ModelBase.cs +++ b/test/MongODM.Core.Tests/Models/ModelBase.cs @@ -4,6 +4,6 @@ namespace Digicando.MongODM.Models { public abstract class ModelBase : IModel { - public virtual IDictionary ExtraElements { get; protected set; } + public virtual IDictionary? ExtraElements { get; protected set; } } } \ No newline at end of file diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index 0fccce9a..38bf966c 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -5,6 +5,7 @@ true Digicando.MongODM false + enable From 830b57e15f4a668cd90414adb10c1f2107bb057d Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 28 Apr 2020 03:32:53 +0200 Subject: [PATCH 31/46] Updating repository and dbcontext Implemeting nullable in core --- src/MongODM.Core/DbContext.cs | 12 +++++- src/MongODM.Core/IDbContextInitializable.cs | 10 +++++ .../Repositories/CollectionRepository.cs | 40 +++++++------------ .../Repositories/GridFSRepository.cs | 28 +++++-------- .../Repositories/IGridFSRepository.cs | 4 ++ src/MongODM.Core/Repositories/IRepository.cs | 7 +++- .../Repositories/RepositoryBase.cs | 28 ++++++++----- .../Serialization/IDocumentSchemaRegister.cs | 22 ++++------ src/MongODM.Core/Utility/IDBMaintainer.cs | 8 +--- .../ReferenceableInterceptorTest.cs | 2 +- 10 files changed, 81 insertions(+), 80 deletions(-) create mode 100644 src/MongODM.Core/IDbContextInitializable.cs diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index 76d5999d..c9231378 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -14,6 +14,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM { public abstract class DbContext : IDbContext @@ -41,10 +42,17 @@ public DbContext( Client = new MongoClient(options.ConnectionString); Database = Client.GetDatabase(options.DBName); - // Init IoC dependencies. + // Initialize internal dependencies. DocumentSchemaRegister.Initialize(this); DBMaintainer.Initialize(this); + // Find repositories. + //todo + + // Initialize repositories. + foreach (var repository in ModelRepositoryMap.Values) + repository.Initialize(this); + // Customize conventions. ConventionRegistry.Register("Enum string", new ConventionPack { @@ -62,7 +70,7 @@ public DbContext( // Public properties. public IReadOnlyCollection ChangedModelsList => DBCache.LoadedModels.Values - .Where(model => (model as IAuditable).IsChanged) + .Where(model => (model as IAuditable)?.IsChanged == true) .ToList(); public IMongoClient Client { get; } public IMongoDatabase Database { get; } diff --git a/src/MongODM.Core/IDbContextInitializable.cs b/src/MongODM.Core/IDbContextInitializable.cs new file mode 100644 index 00000000..37188b6c --- /dev/null +++ b/src/MongODM.Core/IDbContextInitializable.cs @@ -0,0 +1,10 @@ +#nullable enable +namespace Digicando.MongODM +{ + public interface IDbContextInitializable + { + bool IsInitialized { get; } + + void Initialize(IDbContext dbContext); + } +} \ No newline at end of file diff --git a/src/MongODM.Core/Repositories/CollectionRepository.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs index 1f394283..ef4a8d03 100644 --- a/src/MongODM.Core/Repositories/CollectionRepository.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -2,7 +2,6 @@ using Digicando.MongODM.Models; using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; -using Digicando.MongODM.Utility; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Linq; @@ -13,6 +12,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Repositories { public class CollectionRepository : @@ -21,31 +21,21 @@ public class CollectionRepository : where TModel : class, IEntityModel { // Fields. - private readonly IDBMaintainer dbMaintainer; - private readonly IDocumentSchemaRegister documentSchemaRegister; private readonly CollectionRepositoryOptions options; + private IMongoCollection _collection = default!; // Constructors. - public CollectionRepository( - IDbContext dbContext, - string name) - : this(dbContext, new CollectionRepositoryOptions(name)) + public CollectionRepository(string name) + : this(new CollectionRepositoryOptions(name)) { } - public CollectionRepository( - IDbContext dbContext, - CollectionRepositoryOptions options) - : base(dbContext) + public CollectionRepository(CollectionRepositoryOptions options) { this.options = options ?? throw new ArgumentNullException(nameof(options)); - - Collection = dbContext.Database.GetCollection(options.Name); - dbMaintainer = dbContext.DBMaintainer; - documentSchemaRegister = dbContext.DocumentSchemaRegister; } // Properties. - public IMongoCollection Collection { get; } + public IMongoCollection Collection => _collection ?? (_collection = DbContext.Database.GetCollection(options.Name)); // Public methods. public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) @@ -74,7 +64,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis new CreateIndexOptions { Name = "ver" }))); //referenced documents - var dependencies = documentSchemaRegister.GetModelEntityReferencesIds(typeof(TModel)); + var dependencies = DbContext.DocumentSchemaRegister.GetModelEntityReferencesIds(typeof(TModel)); var idPaths = dependencies .Select(dependency => dependency.MemberPathToString()) @@ -112,7 +102,7 @@ public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegis public virtual Task> FindAsync( FilterDefinition filter, - FindOptions options = null, + FindOptions? options = null, CancellationToken cancellationToken = default) => Collection.FindAsync(filter, options, cancellationToken); @@ -123,21 +113,21 @@ public Task FindOneAsync( public virtual Task QueryElementsAsync( Func, Task> query, - AggregateOptions aggregateOptions = null) => + AggregateOptions? aggregateOptions = null) => query(Collection.AsQueryable(aggregateOptions)); public virtual Task ReplaceAsync( object model, bool updateDependentDocuments = true, CancellationToken cancellationToken = default) => - ReplaceAsync(model as TModel, updateDependentDocuments, cancellationToken); + ReplaceAsync((TModel)model, updateDependentDocuments, cancellationToken); public virtual Task ReplaceAsync( object model, IClientSessionHandle session, bool updateDependentDocuments = true, CancellationToken cancellationToken = default) => - ReplaceAsync(model as TModel, session, updateDependentDocuments, cancellationToken); + ReplaceAsync((TModel)model, session, updateDependentDocuments, cancellationToken); public virtual Task ReplaceAsync( TModel model, @@ -171,7 +161,7 @@ protected override async Task FindOneOnDBAsync(TKey id, CancellationToke try { - return await FindOneOnDBAsync(m => m.Id.Equals(id), cancellationToken: cancellationToken); + return await FindOneOnDBAsync(m => m.Id!.Equals(id), cancellationToken: cancellationToken); } catch (EntityNotFoundException) { @@ -198,7 +188,7 @@ private async Task FindOneOnDBAsync( private async Task ReplaceHelperAsync( TModel model, - IClientSessionHandle session, + IClientSessionHandle? session, bool updateDependentDocuments, CancellationToken cancellationToken) { @@ -224,10 +214,10 @@ await Collection.ReplaceOneAsync( // Update dependent documents. if (updateDependentDocuments) - dbMaintainer.OnUpdatedModel(model as IAuditable, model.Id); + DbContext.DBMaintainer.OnUpdatedModel((IAuditable)model, model.Id); // Reset changed members. - (model as IAuditable).ResetChangedMembers(); + (model as IAuditable)?.ResetChangedMembers(); } } } \ No newline at end of file diff --git a/src/MongODM.Core/Repositories/GridFSRepository.cs b/src/MongODM.Core/Repositories/GridFSRepository.cs index 84f24295..6ccb3de9 100644 --- a/src/MongODM.Core/Repositories/GridFSRepository.cs +++ b/src/MongODM.Core/Repositories/GridFSRepository.cs @@ -1,7 +1,6 @@ using Digicando.DomainHelper; using Digicando.MongODM.Exceptions; using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; @@ -12,6 +11,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Repositories { public class GridFSRepository : @@ -21,31 +21,21 @@ public class GridFSRepository : { // Fields. private readonly GridFSRepositoryOptions options; + private GridFSBucket _gridFSBucket = default!; // Constructors. - public GridFSRepository( - IDbContext dbContext, - string name) - : this(dbContext, new GridFSRepositoryOptions(name)) + public GridFSRepository(string name) + : this(new GridFSRepositoryOptions(name)) { } - public GridFSRepository( - IDbContext dbContext, - GridFSRepositoryOptions options) - : base(dbContext) + public GridFSRepository(GridFSRepositoryOptions options) { this.options = options ?? throw new ArgumentNullException(nameof(options)); - - GridFSBucket = new GridFSBucket(dbContext.Database, new GridFSBucketOptions - { - BucketName = options.Name - }); - ProxyGenerator = dbContext.ProxyGenerator; } // Properties. - protected IGridFSBucket GridFSBucket { get; } - protected IProxyGenerator ProxyGenerator { get; } + public IGridFSBucket GridFSBucket => _gridFSBucket ?? + (_gridFSBucket = new GridFSBucket(DbContext.Database, new GridFSBucketOptions { BucketName = options.Name })); // Methods. public override Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) => Task.CompletedTask; @@ -90,7 +80,7 @@ protected override async Task FindOneOnDBAsync(string id, CancellationTo if (mongoFile == null) throw new EntityNotFoundException($"Can't find key {id}"); - var file = ProxyGenerator.CreateInstance(DbContext); + var file = DbContext.ProxyGenerator.CreateInstance(DbContext); ReflectionHelper.SetValue(file, m => m.Id, mongoFile.Id.ToString()); ReflectionHelper.SetValue(file, m => m.Length, mongoFile.Length); ReflectionHelper.SetValue(file, m => m.Name, mongoFile.Filename); @@ -101,4 +91,4 @@ protected override async Task FindOneOnDBAsync(string id, CancellationTo return file; } } -} +} \ No newline at end of file diff --git a/src/MongODM.Core/Repositories/IGridFSRepository.cs b/src/MongODM.Core/Repositories/IGridFSRepository.cs index 4959eb8a..da66acff 100644 --- a/src/MongODM.Core/Repositories/IGridFSRepository.cs +++ b/src/MongODM.Core/Repositories/IGridFSRepository.cs @@ -1,12 +1,16 @@ using Digicando.MongODM.Models; +using MongoDB.Driver.GridFS; using System.IO; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Repositories { public interface IGridFSRepository : IRepository { + IGridFSBucket GridFSBucket { get; } + Task DownloadAsBytesAsync(string id, CancellationToken cancellationToken = default); Task DownloadAsStreamAsync(string id, CancellationToken cancellationToken = default); diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index ba1f0300..93230240 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -4,10 +4,13 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Repositories { - public interface IRepository + public interface IRepository : IDbContextInitializable { + IDbContext DbContext { get; } + Task BuildIndexesAsync( IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default); @@ -46,7 +49,7 @@ Task DeleteAsync( /// Model's Id /// The cancellation token /// The model, null if it doesn't exist - Task TryFindOneAsync( + Task TryFindOneAsync( TKey id, CancellationToken cancellationToken = default); } diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index f5e8d877..03d08236 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -11,22 +11,28 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Repositories { public abstract class RepositoryBase : IRepository where TModel : class, IEntityModel { - // Constructors. - public RepositoryBase(IDbContext dbContext) + // Initializer. + public virtual void Initialize(IDbContext dbContext) { - this.DbContext = dbContext; + if (IsInitialized) + throw new InvalidOperationException("Instance already initialized"); + DbContext = dbContext; + + IsInitialized = true; } - // Protected properties. - protected IDbContext DbContext { get; } + // Properties. + public IDbContext DbContext { get; private set; } = default!; + public bool IsInitialized { get; private set; } - // Public methods. + // Methods. public abstract Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default); public virtual async Task CreateAsync(IEnumerable models, CancellationToken cancellationToken = default) @@ -86,13 +92,13 @@ public virtual async Task FindOneAsync( { var cachedModel = DbContext.DBCache.LoadedModels[id] as TModel; if ((cachedModel as IReferenceable)?.IsSummary == false) - return cachedModel; + return cachedModel!; } return await FindOneOnDBAsync(id, cancellationToken); } - public async Task TryFindOneAsync( + public async Task TryFindOneAsync( TKey id, CancellationToken cancellationToken = default) { @@ -111,7 +117,7 @@ public async Task TryFindOneAsync( } } - // Protected methods. + // Protected abstract methods. protected abstract Task CreateOnDBAsync(IEnumerable models, CancellationToken cancellationToken); protected abstract Task CreateOnDBAsync(TModel model, CancellationToken cancellationToken); @@ -133,7 +139,7 @@ private async Task CascadeDeleteMembersAsync(object currentModel, IEnumerable /// Interface for implementation. /// - public interface IDocumentSchemaRegister + public interface IDocumentSchemaRegister : IDbContextInitializable { bool IsFrozen { get; } - bool IsInitialized { get; } IEnumerable Schemas { get; } // Methods. @@ -27,12 +27,6 @@ public interface IDocumentSchemaRegister IEnumerable GetModelEntityReferencesIds(Type modelType); - /// - /// Call before everything else. - /// - /// Instance of - void Initialize(IDbContext dbContext); - /// /// Register a new model schema /// @@ -42,8 +36,8 @@ public interface IDocumentSchemaRegister /// Model migration method void RegisterModelSchema( DocumentVersion fromVersion, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class; /// @@ -57,8 +51,8 @@ void RegisterModelSchema( void RegisterModelSchema( DocumentVersion fromVersion, Action> classMapInitializer, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class; /// @@ -72,8 +66,8 @@ void RegisterModelSchema( void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class; } } \ No newline at end of file diff --git a/src/MongODM.Core/Utility/IDBMaintainer.cs b/src/MongODM.Core/Utility/IDBMaintainer.cs index fb9168b3..f387f2cc 100644 --- a/src/MongODM.Core/Utility/IDBMaintainer.cs +++ b/src/MongODM.Core/Utility/IDBMaintainer.cs @@ -1,18 +1,14 @@ using Digicando.MongODM.ProxyModels; +#nullable enable namespace Digicando.MongODM.Utility { /// /// Interface for implementation. /// - public interface IDBMaintainer + public interface IDBMaintainer : IDbContextInitializable { - // Properties. - bool IsInitialized { get; } - // Methods. - void Initialize(IDbContext dbContext); - /// /// Method to invoke when an auditable model is changed. /// diff --git a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs index 02461a8b..bd69c3d3 100644 --- a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs +++ b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs @@ -123,7 +123,7 @@ public void GetNotLoadedMember() }; repositoryMock.Setup(r => r.TryFindOneAsync(modelId, It.IsAny())) - .Returns(Task.FromResult(new FakeModel + .Returns(Task.FromResult(new FakeModel { Id = modelId, IntegerProp = 7, From 0a32097ce0f3e8b43d18592caab2515becc3c7f5 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 29 Apr 2020 20:30:28 +0200 Subject: [PATCH 32/46] Implemented repository discovery in dbcontext --- .../ServiceCollectionExtensions.cs | 2 + src/MongODM.Core/DbContext.cs | 22 ++-- src/MongODM.Core/IDbContext.cs | 21 +-- .../ProxyModels/ReferenceableInterceptor.cs | 14 +- src/MongODM.Core/Repositories/IRepository.cs | 3 + .../Repositories/IRepositoryRegister.cs | 24 ++++ .../Repositories/RepositoryBase.cs | 4 +- .../Repositories/RepositoryRegister.cs | 123 ++++++++++++++++++ .../Tasks/UpdateDocDependenciesTask.cs | 7 +- .../ReferenceableInterceptorTest.cs | 2 +- 10 files changed, 179 insertions(+), 43 deletions(-) create mode 100644 src/MongODM.Core/Repositories/IRepositoryRegister.cs create mode 100644 src/MongODM.Core/Repositories/RepositoryRegister.cs diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 950b2fb8..06172427 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using Digicando.MongODM; using Digicando.MongODM.AspNetCore; using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Tasks; @@ -56,6 +57,7 @@ public static void UseMongODM( services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); + services.TryAddTransient(); services.TryAddSingleton(); //tasks diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index c9231378..a8d4609e 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -29,6 +29,7 @@ public DbContext( IDocumentSchemaRegister documentSchemaRegister, DbContextOptions options, IProxyGenerator proxyGenerator, + IRepositoryRegister repositoryRegister, ISerializerModifierAccessor serializerModifierAccessor) { DBCache = dbCache; @@ -36,6 +37,7 @@ public DbContext( DocumentSchemaRegister = documentSchemaRegister; DocumentVersion = options.DocumentVersion; ProxyGenerator = proxyGenerator; + RepositoryRegister = repositoryRegister; SerializerModifierAccessor = serializerModifierAccessor; // Initialize MongoDB driver. @@ -45,12 +47,10 @@ public DbContext( // Initialize internal dependencies. DocumentSchemaRegister.Initialize(this); DBMaintainer.Initialize(this); - - // Find repositories. - //todo + RepositoryRegister.Initialize(this); // Initialize repositories. - foreach (var repository in ModelRepositoryMap.Values) + foreach (var repository in RepositoryRegister.ModelRepositoryMap.Values) repository.Initialize(this); // Customize conventions. @@ -79,14 +79,8 @@ public DbContext( public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } public bool IsMigrating { get; private set; } - public abstract IReadOnlyDictionary ModelCollectionRepositoryMap { get; } - public abstract IReadOnlyDictionary ModelGridFSRepositoryMap { get; } - public IReadOnlyDictionary ModelRepositoryMap => - Enumerable.Union<(Type ModelType, IRepository Repository)>( - ModelCollectionRepositoryMap.Select(pair => (pair.Key, pair.Value as IRepository)), - ModelGridFSRepositoryMap.Select(pair => (pair.Key, pair.Value as IRepository))) - .ToDictionary(pair => pair.ModelType, pair => pair.Repository); public IProxyGenerator ProxyGenerator { get; } + public IRepositoryRegister RepositoryRegister { get; } public ISerializerModifierAccessor SerializerModifierAccessor { get; } // Protected properties. @@ -103,7 +97,7 @@ public async Task MigrateRepositoriesAsync(CancellationToken cancellationToken = await migration.MigrateAsync(cancellationToken); // Build indexes. - foreach (var repository in ModelCollectionRepositoryMap.Values) + foreach (var repository in RepositoryRegister.ModelCollectionRepositoryMap.Values) await repository.BuildIndexesAsync(DocumentSchemaRegister, cancellationToken); IsMigrating = false; @@ -145,9 +139,9 @@ public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = foreach (var model in ChangedModelsList) { var modelType = model.GetType().BaseType; - if (ModelCollectionRepositoryMap.ContainsKey(modelType)) //can't replace if is a file + if (RepositoryRegister.ModelCollectionRepositoryMap.ContainsKey(modelType)) //can't replace if is a file { - var repository = ModelCollectionRepositoryMap[modelType]; + var repository = RepositoryRegister.ModelCollectionRepositoryMap[modelType]; await repository.ReplaceAsync(model); } } diff --git a/src/MongODM.Core/IDbContext.cs b/src/MongODM.Core/IDbContext.cs index a7f11240..8b47962d 100644 --- a/src/MongODM.Core/IDbContext.cs +++ b/src/MongODM.Core/IDbContext.cs @@ -4,11 +4,10 @@ using Digicando.MongODM.Serialization.Modifiers; using Digicando.MongODM.Utility; using MongoDB.Driver; -using System; -using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM { /// @@ -53,24 +52,14 @@ public interface IDbContext bool IsMigrating { get; } /// - /// Model-Repository map for collection types. - /// - IReadOnlyDictionary ModelCollectionRepositoryMap { get; } - - /// - /// Model-Repository map for gridfs types. + /// Current model proxy generator. /// - IReadOnlyDictionary ModelGridFSRepositoryMap { get; } + IProxyGenerator ProxyGenerator { get; } /// - /// Model-Repository map for both collection and gridfs types. + /// Register of available repositories. /// - IReadOnlyDictionary ModelRepositoryMap { get; } - - /// - /// Current model proxy generator. - /// - IProxyGenerator ProxyGenerator { get; } + IRepositoryRegister RepositoryRegister { get; } /// /// Serializer modifier accessor. diff --git a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs index 2d68c264..cbf0101d 100644 --- a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs @@ -10,6 +10,7 @@ using System.Reflection; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.ProxyModels { public class ReferenceableInterceptor : ModelInterceptorBase @@ -26,10 +27,7 @@ public ReferenceableInterceptor( IDbContext dbContext) : base(additionalInterfaces) { - if (typeof(TModel).GetInterfaces().Contains(typeof(IFileModel))) - repository = dbContext.ModelGridFSRepositoryMap[typeof(TModel)] as IRepository; - else - repository = dbContext.ModelCollectionRepositoryMap[typeof(TModel)] as IRepository; + repository = (IRepository)dbContext.RepositoryRegister.ModelRepositoryMap[typeof(TModel)]; } // Protected methods. @@ -52,7 +50,7 @@ protected override bool InterceptInterface(IInvocation invocation) } else if (invocation.Method.Name == nameof(IReferenceable.MergeFullModel)) { - MergeFullModel(invocation.Proxy as TModel, invocation.GetArgumentValue(0) as TModel); + MergeFullModel((TModel)invocation.Proxy, invocation.GetArgumentValue(0) as TModel); } else if (invocation.Method.Name == nameof(IReferenceable.SetAsSummary)) { @@ -82,7 +80,7 @@ protected override void InterceptModel(IInvocation invocation) // If member is not loaded, load the full object. if (!settedMemberNames.ContainsKey(propertyName)) { - var task = FullLoadAsync(invocation.Proxy as TModel); + var task = FullLoadAsync((TModel)invocation.Proxy); task.Wait(); } } @@ -108,7 +106,7 @@ protected override void InterceptModel(IInvocation invocation) // If member is not setted and is summary, load the full object. if (!settedMemberNames.ContainsKey(propertyName)) { - var task = FullLoadAsync(invocation.Proxy as TModel); + var task = FullLoadAsync((TModel)invocation.Proxy); task.Wait(); break; @@ -136,7 +134,7 @@ private async Task FullLoadAsync(TModel model) } } - private void MergeFullModel(TModel model, TModel fullModel) + private void MergeFullModel(TModel model, TModel? fullModel) { if (fullModel != null) { diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index 93230240..8a0b44f0 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -1,5 +1,6 @@ using Digicando.MongODM.Models; using Digicando.MongODM.Serialization; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -10,6 +11,8 @@ namespace Digicando.MongODM.Repositories public interface IRepository : IDbContextInitializable { IDbContext DbContext { get; } + Type GetKeyType { get; } + Type GetModelType { get; } Task BuildIndexesAsync( IDocumentSchemaRegister schemaRegister, diff --git a/src/MongODM.Core/Repositories/IRepositoryRegister.cs b/src/MongODM.Core/Repositories/IRepositoryRegister.cs new file mode 100644 index 00000000..9826369f --- /dev/null +++ b/src/MongODM.Core/Repositories/IRepositoryRegister.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; + +#nullable enable +namespace Digicando.MongODM.Repositories +{ + public interface IRepositoryRegister : IDbContextInitializable + { + /// + /// Model-Repository map for collection types. + /// + IReadOnlyDictionary ModelCollectionRepositoryMap { get; } + + /// + /// Model-Repository map for gridfs types. + /// + IReadOnlyDictionary ModelGridFSRepositoryMap { get; } + + /// + /// Model-Repository map for both collection and gridfs types. + /// + IReadOnlyDictionary ModelRepositoryMap { get; } + } +} \ No newline at end of file diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index 03d08236..0f96acbe 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -30,6 +30,8 @@ public virtual void Initialize(IDbContext dbContext) // Properties. public IDbContext DbContext { get; private set; } = default!; + public Type GetKeyType => typeof(TKey); + public Type GetModelType => typeof(TModel); public bool IsInitialized { get; private set; } // Methods. @@ -138,7 +140,7 @@ private async Task CascadeDeleteMembersAsync(object currentModel, IEnumerable _modelCollectionRepositoryMap = default!; + private IReadOnlyDictionary _modelGridFSRepositoryMap = default!; + private IReadOnlyDictionary _modelRepositoryMap = default!; + + // Initializer. + public void Initialize(IDbContext dbContext) + { + if (IsInitialized) + throw new InvalidOperationException("Instance already initialized"); + this.dbContext = dbContext; + + IsInitialized = true; + } + + // Properties. + public bool IsInitialized { get; private set; } + + public IReadOnlyDictionary ModelCollectionRepositoryMap + { + get + { + if (_modelCollectionRepositoryMap is null) + { + var dbContextType = dbContext.GetType(); + + //select ICollectionRepository<,> implementing properties + var repos = dbContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(prop => + { + var propType = prop.PropertyType; + + if (propType.IsGenericType && + propType.GetGenericTypeDefinition() == typeof(ICollectionRepository<,>)) + return true; + + if (propType.GetInterfaces() + .Where(@interface => @interface.IsGenericType) + .Select(@interface => @interface.GetGenericTypeDefinition()) + .Contains(typeof(ICollectionRepository<,>))) + return true; + + return false; + }); + + //construct register + _modelCollectionRepositoryMap = repos.ToDictionary( + prop => ((ICollectionRepository)prop.GetValue(dbContext)).GetModelType, + prop => (ICollectionRepository)prop.GetValue(dbContext)); + } + + return _modelCollectionRepositoryMap; + } + } + public IReadOnlyDictionary ModelGridFSRepositoryMap + { + get + { + if (_modelGridFSRepositoryMap is null) + { + var dbContextType = dbContext.GetType(); + + //select ICollectionRepository<,> implementing properties + var repos = dbContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(prop => + { + var propType = prop.PropertyType; + + if (propType.IsGenericType && + propType.GetGenericTypeDefinition() == typeof(IGridFSRepository<>)) + return true; + + if (propType.GetInterfaces() + .Where(@interface => @interface.IsGenericType) + .Select(@interface => @interface.GetGenericTypeDefinition()) + .Contains(typeof(IGridFSRepository<>))) + return true; + + return false; + }); + + //construct register + _modelGridFSRepositoryMap = repos.ToDictionary( + prop => ((IGridFSRepository)prop.GetValue(dbContext)).GetModelType, + prop => (IGridFSRepository)prop.GetValue(dbContext)); + } + + return _modelGridFSRepositoryMap; + } + } + public IReadOnlyDictionary ModelRepositoryMap + { + get + { + if (_modelRepositoryMap is null) + { + var repoMap = new Dictionary(); + + foreach (var pair in ModelCollectionRepositoryMap) + repoMap.Add(pair.Key, pair.Value); + + foreach (var pair in ModelGridFSRepositoryMap) + repoMap.Add(pair.Key, pair.Value); + + _modelRepositoryMap = repoMap; + } + + return _modelRepositoryMap; + } + } + } +} diff --git a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs index c7a0a2bb..fd1bed6f 100644 --- a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Tasks { public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask @@ -30,12 +31,12 @@ public async Task RunAsync( where TModel : class, IEntityModel where TDbContext : class, IDbContext { - var dbContext = serviceProvider.GetService(typeof(TDbContext)) as TDbContext; + var dbContext = (TDbContext)serviceProvider.GetService(typeof(TDbContext)); // Get repository. - if (!dbContext.ModelCollectionRepositoryMap.ContainsKey(typeof(TModel))) + if (!dbContext.RepositoryRegister.ModelCollectionRepositoryMap.ContainsKey(typeof(TModel))) return; - var repository = dbContext.ModelCollectionRepositoryMap[typeof(TModel)] as ICollectionRepository; + var repository = (ICollectionRepository)dbContext.RepositoryRegister.ModelCollectionRepositoryMap[typeof(TModel)]; HashSet upgradedDocumentsId = new HashSet(); using (serializerModifierAccessor.EnableReferenceSerializerModifier(true)) diff --git a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs index bd69c3d3..bb125b1a 100644 --- a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs +++ b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs @@ -24,7 +24,7 @@ public ReferenceableInterceptorTest() repositoryMock = new Mock>(); dbContextMock = new Mock(); - dbContextMock.Setup(r => r.ModelCollectionRepositoryMap[typeof(FakeModel)]) + dbContextMock.Setup(c => c.RepositoryRegister.ModelRepositoryMap[typeof(FakeModel)]) .Returns(() => repositoryMock.Object); interceptor = new ReferenceableInterceptor( From 3d23e2f5ec2ff4f3c97e6aa12ebee7f195c45706 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 30 Apr 2020 02:26:10 +0200 Subject: [PATCH 33/46] Grouped DbContext dependencies injection into IDbContextDependencies --- .../ServiceCollectionExtensions.cs | 5 +- src/MongODM.Core/DbContext.cs | 25 +++--- src/MongODM.Core/IDbContext.cs | 4 +- src/MongODM.Core/Models/IEntityModel.cs | 1 + .../Repositories/RepositoryBase.cs | 8 +- .../IModelSerializerCollector.cs | 3 +- .../Serializers/ExtendedClassMapSerializer.cs | 86 ++++++++----------- .../Serializers/ReferenceSerializer.cs | 70 +++++++-------- src/MongODM.Core/Utility/DBCache.cs | 21 ++++- src/MongODM.Core/Utility/DBMaintainer.cs | 7 +- .../Utility/DbContextDependencies.cs | 34 ++++++++ src/MongODM.Core/Utility/IDBCache.cs | 5 +- src/MongODM.Core/Utility/IDBMaintainer.cs | 4 +- .../Utility/IDbContextDependencies.cs | 18 ++++ .../ExtendedClassMapSerializerTest.cs | 4 +- 15 files changed, 175 insertions(+), 120 deletions(-) create mode 100644 src/MongODM.Core/Utility/DbContextDependencies.cs create mode 100644 src/MongODM.Core/Utility/IDbContextDependencies.cs diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 06172427..a9859c73 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -54,8 +54,9 @@ public static void UseMongODM( * and passed to other with Initialize() method. This because otherwise inside * the same dbContext different components could have different instances of the same component. */ - services.TryAddTransient(); - services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); + services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddSingleton(); diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index a8d4609e..b2a25981 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -24,21 +24,16 @@ public abstract class DbContext : IDbContext // Constructors and initialization. public DbContext( - IDBCache dbCache, - IDBMaintainer dbMaintainer, - IDocumentSchemaRegister documentSchemaRegister, - DbContextOptions options, - IProxyGenerator proxyGenerator, - IRepositoryRegister repositoryRegister, - ISerializerModifierAccessor serializerModifierAccessor) + IDbContextDependencies dependencies, + DbContextOptions options) { - DBCache = dbCache; - DBMaintainer = dbMaintainer; - DocumentSchemaRegister = documentSchemaRegister; + DBCache = dependencies.DbCache; + DBMaintainer = dependencies.DbMaintainer; + DocumentSchemaRegister = dependencies.DocumentSchemaRegister; DocumentVersion = options.DocumentVersion; - ProxyGenerator = proxyGenerator; - RepositoryRegister = repositoryRegister; - SerializerModifierAccessor = serializerModifierAccessor; + ProxyGenerator = dependencies.ProxyGenerator; + RepositoryRegister = dependencies.RepositoryRegister; + SerializerModifierAccessor = dependencies.SerializerModifierAccessor; // Initialize MongoDB driver. Client = new MongoClient(options.ConnectionString); @@ -74,8 +69,8 @@ public DbContext( .ToList(); public IMongoClient Client { get; } public IMongoDatabase Database { get; } - public IDBCache DBCache { get; } - public IDBMaintainer DBMaintainer { get; } + public IDbCache DBCache { get; } + public IDbMaintainer DBMaintainer { get; } public IDocumentSchemaRegister DocumentSchemaRegister { get; } public DocumentVersion DocumentVersion { get; } public bool IsMigrating { get; private set; } diff --git a/src/MongODM.Core/IDbContext.cs b/src/MongODM.Core/IDbContext.cs index 8b47962d..c818043f 100644 --- a/src/MongODM.Core/IDbContext.cs +++ b/src/MongODM.Core/IDbContext.cs @@ -29,12 +29,12 @@ public interface IDbContext /// /// Database cache container. /// - IDBCache DBCache { get; } + IDbCache DBCache { get; } /// /// Database operator interested into maintenance tasks. /// - IDBMaintainer DBMaintainer { get; } + IDbMaintainer DBMaintainer { get; } /// /// Container for model serialization and document schema information. diff --git a/src/MongODM.Core/Models/IEntityModel.cs b/src/MongODM.Core/Models/IEntityModel.cs index d61c9c09..1781c357 100644 --- a/src/MongODM.Core/Models/IEntityModel.cs +++ b/src/MongODM.Core/Models/IEntityModel.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Digicando.MongODM.Models { public interface IEntityModel : IModel diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index 0f96acbe..45e31cca 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -75,8 +75,8 @@ public virtual async Task DeleteAsync(TModel model, CancellationToken cancellati await DeleteOnDBAsync(model, cancellationToken); // Remove from cache. - if (DbContext.DBCache.LoadedModels.ContainsKey(model.Id)) - DbContext.DBCache.RemoveModel(model.Id); + if (DbContext.DBCache.LoadedModels.ContainsKey(model.Id!)) + DbContext.DBCache.RemoveModel(model.Id!); } public async Task DeleteAsync(IEntityModel model, CancellationToken cancellationToken = default) @@ -90,9 +90,9 @@ public virtual async Task FindOneAsync( TKey id, CancellationToken cancellationToken = default) { - if (DbContext.DBCache.LoadedModels.ContainsKey(id)) + if (DbContext.DBCache.LoadedModels.ContainsKey(id!)) { - var cachedModel = DbContext.DBCache.LoadedModels[id] as TModel; + var cachedModel = DbContext.DBCache.LoadedModels[id!] as TModel; if ((cachedModel as IReferenceable)?.IsSummary == false) return cachedModel!; } diff --git a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs index 1296876b..482b7ea7 100644 --- a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs @@ -1,12 +1,13 @@ using Digicando.MongODM.ProxyModels; using Digicando.MongODM.Utility; +#nullable enable namespace Digicando.MongODM.Serialization { public interface IModelSerializerCollector { // Methods. - void Register(IDBCache dbCache, + void Register(IDbCache dbCache, IDbContext dbContext, IDocumentSchemaRegister documentSchemaRegister, IProxyGenerator proxyGenerator); diff --git a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs index 51fb7dcb..73440320 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -12,6 +12,7 @@ using System.Reflection; using System.Threading.Tasks; +#nullable enable namespace Digicando.MongODM.Serialization.Serializers { public class ExtendedClassMapSerializer : @@ -21,32 +22,26 @@ public class ExtendedClassMapSerializer : // Nested struct. private struct ExtraElementCondition { - Func _condition; - public BsonElement Element { get; set; } - public Func Condition - { - get { return _condition ?? (_ => true); } - set { _condition = value; } - } + public Func Condition { get; set; } } // Static readonly fields. private readonly BsonElement documentVersionElement; // Fields. - private BsonClassMapSerializer _serializer; - private readonly IDBCache dbCache; + private readonly IDbCache dbCache; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly ICollection extraElements; - private readonly Func> fixDeserializedModelAsync; + private readonly Func> fixDeserializedModelAsync; + private BsonClassMapSerializer _serializer = default!; // Constructor. public ExtendedClassMapSerializer( - IDBCache dbCache, + IDbCache dbCache, DocumentVersion documentVersion, ISerializerModifierAccessor serializerModifierAccessor, - Func> fixDeserializedModelAsync = null) + Func>? fixDeserializedModelAsync = null) { this.dbCache = dbCache; this.serializerModifierAccessor = serializerModifierAccessor; @@ -60,16 +55,28 @@ public ExtendedClassMapSerializer( // Properties. public bool AddVersion { get; set; } public IEnumerable ContainedClassMaps => new[] { BsonClassMap.LookupClassMap(typeof(TModel)) }; + public BsonClassMapSerializer Serializer + { + get + { + if (_serializer == null) + { + var classMap = BsonClassMap.LookupClassMap(typeof(TModel)); + _serializer = new BsonClassMapSerializer(classMap); + } + return _serializer; + } + } // Methods. public ExtendedClassMapSerializer AddExtraElement( BsonElement element, - Func condition = null) + Func? condition = null) { extraElements.Add(new ExtraElementCondition { Element = element, - Condition = condition + Condition = condition ?? (_ => true) }); return this; } @@ -80,14 +87,14 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser if (context.Reader.CurrentBsonType == BsonType.Null) { context.Reader.ReadNull(); - return null; + return null!; } // Deserialize on document. var bsonDocument = BsonDocumentSerializer.Instance.Deserialize(context, args); // Get version. - DocumentVersion documentVersion = null; + DocumentVersion? documentVersion = null; if (bsonDocument.TryGetElement(DbContext.DocumentVersionElementName, out BsonElement versionElement)) documentVersion = BsonValueToDocumentVersion(versionElement.Value); @@ -104,8 +111,7 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser }); // Deserialize. - var serializer = GetSerializer(); - var model = serializer.Deserialize(localContext, args); + var model = Serializer.Deserialize(localContext, args); // Add model to cache. if (!serializerModifierAccessor.IsNoCacheEnabled && @@ -114,14 +120,14 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser if (dbCache.LoadedModels.ContainsKey(id)) { var fullModel = model; - model = dbCache.LoadedModels[id] as TModel; + model = (TModel)dbCache.LoadedModels[id]; - if ((model as IReferenceable).IsSummary) - (model as IReferenceable).MergeFullModel(fullModel); + if (((IReferenceable)model).IsSummary) + ((IReferenceable)model).MergeFullModel(fullModel); } else { - dbCache.AddModel(id, model as IEntityModel); + dbCache.AddModel(id, (IEntityModel)model); } } @@ -136,11 +142,8 @@ public override TModel Deserialize(BsonDeserializationContext context, BsonDeser return model; } - public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) - { - var serializer = GetSerializer(); - return serializer.GetDocumentId(document, out id, out idNominalType, out idGenerator); - } + public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) => + Serializer.GetDocumentId(document, out id, out idNominalType, out idGenerator); public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TModel value) { @@ -172,8 +175,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati } // Serialize. - var serializer = GetSerializer(); - serializer.Serialize(localContext, args, value); + Serializer.Serialize(localContext, args, value); // Add version. if (AddVersion && bsonWriter.IsRootDocument) @@ -197,20 +199,14 @@ where elementCondition.Condition(localContext) BsonDocumentSerializer.Instance.Serialize(context, args, bsonDocument); } - public void SetDocumentId(object document, object id) - { - var serializer = GetSerializer(); - serializer.SetDocumentId(document, id); - } + public void SetDocumentId(object document, object id) => + Serializer.SetDocumentId(document, id); - public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo) - { - var serializer = GetSerializer(); - return serializer.TryGetMemberSerializationInfo(memberName, out serializationInfo); - } + public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo) => + Serializer.TryGetMemberSerializationInfo(memberName, out serializationInfo); // Helpers. - private static DocumentVersion BsonValueToDocumentVersion(BsonValue bsonValue) => + private static DocumentVersion? BsonValueToDocumentVersion(BsonValue bsonValue) => bsonValue switch { BsonNull _ => null, @@ -238,15 +234,5 @@ private static BsonArray DocumentVersionToBsonArray(DocumentVersion documentVers return bsonArray; } - - private BsonClassMapSerializer GetSerializer() - { - if (_serializer == null) - { - var classMap = BsonClassMap.LookupClassMap(typeof(TModel)); - _serializer = new BsonClassMapSerializer(classMap); - } - return _serializer; - } } } diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs index 793181a7..f74dfb9a 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs @@ -13,6 +13,7 @@ using System.Reflection; using System.Threading; +#nullable enable namespace Digicando.MongODM.Serialization.Serializers { public class ReferenceSerializer : @@ -20,12 +21,12 @@ public class ReferenceSerializer : where TModelBase : class, IEntityModel { // Fields. - private IDiscriminatorConvention _discriminatorConvention; + private IDiscriminatorConvention _discriminatorConvention = default!; private readonly ReaderWriterLockSlim configLockAdapters = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockClassMaps = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); private readonly ReaderWriterLockSlim configLockSerializers = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly IDBCache dbCache; + private readonly IDbCache dbCache; private readonly IDbContext dbContext; private readonly IProxyGenerator proxyGenerator; private readonly ISerializerModifierAccessor serializerModifierAccessor; @@ -48,6 +49,15 @@ public ReferenceSerializer( // Properties. public IEnumerable ContainedClassMaps => registeredClassMaps.Values; + public IDiscriminatorConvention DiscriminatorConvention + { + get + { + if (_discriminatorConvention == null) + _discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(TModelBase)); + return _discriminatorConvention; + } + } public bool? UseCascadeDelete { get; } // Methods. @@ -62,15 +72,14 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD break; case BsonType.Null: bsonReader.ReadNull(); - return null; + return null!; default: var message = $"Expected a nested document representing the serialized form of a {nameof(TModelBase)} value, but found a value of type {bsonType} instead."; throw new InvalidOperationException(message); } // Get actual type. - var discriminatorConvention = GetDiscriminatorConvention(); - var actualType = discriminatorConvention.GetActualType(bsonReader, args.NominalType); + var actualType = DiscriminatorConvention.GetActualType(bsonReader, args.NominalType); // Deserialize object. var serializer = GetSerializer(actualType); @@ -81,24 +90,24 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD { var id = model.Id; if (id == null) //ignore refered instances without id - return null; + return null!; // Check if model as been loaded in cache. if (dbCache.LoadedModels.ContainsKey(id) && !serializerModifierAccessor.IsNoCacheEnabled) { - var cachedModel = dbCache.LoadedModels[id] as TModelBase; + var cachedModel = (TModelBase)dbCache.LoadedModels[id]; - if ((cachedModel as IReferenceable).IsSummary) + if (((IReferenceable)cachedModel).IsSummary) { // Execute merging between summary models. - var sourceMembers = (model as IReferenceable).SettedMemberNames - .Except((cachedModel as IReferenceable).SettedMemberNames) + var sourceMembers = ((IReferenceable)model).SettedMemberNames + .Except(((IReferenceable)cachedModel).SettedMemberNames) .Select(memberName => cachedModel.GetType().GetMember(memberName).Single()) .ToArray(); //temporary disable auditing - (cachedModel as IAuditable).DisableAuditing(); + ((IAuditable)cachedModel).DisableAuditing(); foreach (var member in sourceMembers) { @@ -107,9 +116,9 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD } //reenable auditing - (cachedModel as IAuditable).EnableAuditing(); + ((IAuditable)cachedModel).EnableAuditing(); - (cachedModel as IReferenceable).SetAsSummary(sourceMembers.Select(m => m.Name)); + ((IReferenceable)cachedModel).SetAsSummary(sourceMembers.Select(m => m.Name)); } // Return the cached model. @@ -120,24 +129,24 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD // Set model as summarizable. if (serializerModifierAccessor.IsReadOnlyReferencedIdEnabled) { - (model as IReferenceable).ClearSettedMembers(); - (model as IReferenceable).SetAsSummary(new[] { nameof(model.Id) }); + ((IReferenceable)model).ClearSettedMembers(); + ((IReferenceable)model).SetAsSummary(new[] { nameof(model.Id) }); } else { - (model as IReferenceable).SetAsSummary((model as IReferenceable).SettedMemberNames); + ((IReferenceable)model).SetAsSummary(((IReferenceable)model).SettedMemberNames); } // Enable auditing. - (model as IAuditable).EnableAuditing(); + ((IAuditable)model).EnableAuditing(); // Add in cache. if (!serializerModifierAccessor.IsNoCacheEnabled) - dbCache.AddModel(model.Id, model); + dbCache.AddModel(model.Id!, model); } } - return model; + return model!; } public IBsonSerializer GetAdapter() @@ -148,7 +157,7 @@ public IBsonSerializer GetAdapter() { if (registeredAdapters.ContainsKey(typeof(TModel))) { - return registeredAdapters[typeof(TModel)] as IBsonSerializer; + return (IBsonSerializer)registeredAdapters[typeof(TModel)]; } } finally @@ -163,7 +172,7 @@ public IBsonSerializer GetAdapter() { registeredAdapters.Add(typeof(TModel), new ReferenceSerializerAdapter(this)); } - return registeredAdapters[typeof(TModel)] as IBsonSerializer; + return (IBsonSerializer)registeredAdapters[typeof(TModel)]; } finally { @@ -174,11 +183,11 @@ public IBsonSerializer GetAdapter() public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) { IsProxyClassType(document, out Type documentType); - var serializer = GetSerializer(documentType) as IBsonIdProvider; + var serializer = (IBsonIdProvider)GetSerializer(documentType); return serializer.GetDocumentId(document, out id, out idNominalType, out idGenerator); } - public ReferenceSerializer RegisterType(Action> classMapInitializer = null) + public ReferenceSerializer RegisterType(Action>? classMapInitializer = null) where TModel : class { // Initialize class map. @@ -254,7 +263,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati public void SetDocumentId(object document, object id) { IsProxyClassType(document, out Type documentType); - var serializer = GetSerializer(documentType) as IBsonIdProvider; + var serializer = (IBsonIdProvider)GetSerializer(documentType); serializer.SetDocumentId(document, id); } @@ -267,7 +276,7 @@ public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializati var modelType = (from pair in registeredClassMaps where pair.Value.GetMemberMap(memberName) != null select pair.Key).FirstOrDefault(); - var serializer = GetSerializer(modelType) as IBsonDocumentSerializer; + var serializer = (IBsonDocumentSerializer)GetSerializer(modelType); return serializer.TryGetMemberSerializationInfo(memberName, out serializationInfo); } finally @@ -300,13 +309,6 @@ private BsonClassMap CreateBsonClassMap(Action); var classMapSerializerType = classMapSerializerDefinition.MakeGenericType(actualType); - var serializer = Activator.CreateInstance(classMapSerializerType, classMap) as IBsonSerializer; + var serializer = (IBsonSerializer)Activator.CreateInstance(classMapSerializerType, classMap); registeredSerializers.Add(actualType, serializer); } return registeredSerializers[actualType]; @@ -356,7 +358,7 @@ private IBsonSerializer GetSerializer(Type actualType) private bool IsProxyClassType(TModel value, out Type modelType) { - modelType = value.GetType(); + modelType = value!.GetType(); if (proxyGenerator.IsProxyType(modelType)) { modelType = modelType.BaseType; diff --git a/src/MongODM.Core/Utility/DBCache.cs b/src/MongODM.Core/Utility/DBCache.cs index c52da4b4..067cf915 100644 --- a/src/MongODM.Core/Utility/DBCache.cs +++ b/src/MongODM.Core/Utility/DBCache.cs @@ -3,9 +3,10 @@ using System; using System.Collections.Generic; +#nullable enable namespace Digicando.MongODM.Utility { - public class DBCache : IDBCache + public class DbCache : IDbCache { // Consts. private const string CacheKey = "DBCache"; @@ -14,7 +15,7 @@ public class DBCache : IDBCache private readonly IExecutionContext executionContext; // Constructors. - public DBCache(IExecutionContext executionContext) + public DbCache(IExecutionContext executionContext) { this.executionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext)); } @@ -24,6 +25,9 @@ public IReadOnlyDictionary LoadedModels { get { + if (executionContext.Items is null) + throw new InvalidOperationException("Execution context can't have null Items here"); + lock (executionContext.Items) return GetScopedCache(); } @@ -32,6 +36,9 @@ public IReadOnlyDictionary LoadedModels // Methods. public void ClearCache() { + if (executionContext.Items is null) + throw new InvalidOperationException("Execution context can't have null Items here"); + lock (executionContext.Items) GetScopedCache().Clear(); } @@ -43,6 +50,8 @@ public void AddModel(object id, TModel model) throw new ArgumentNullException(nameof(id)); if (model == null) throw new ArgumentNullException(nameof(model)); + if (executionContext.Items is null) + throw new InvalidOperationException("Execution context can't have null Items here"); lock (executionContext.Items) GetScopedCache().Add(id, model); @@ -50,6 +59,9 @@ public void AddModel(object id, TModel model) public void RemoveModel(object id) { + if (executionContext.Items is null) + throw new InvalidOperationException("Execution context can't have null Items here"); + lock (executionContext.Items) GetScopedCache().Remove(id); } @@ -57,10 +69,13 @@ public void RemoveModel(object id) // Helpers. private Dictionary GetScopedCache() { + if (executionContext.Items is null) + throw new InvalidOperationException("Execution context can't have null Items here"); + if (!executionContext.Items.ContainsKey(CacheKey)) executionContext.Items.Add(CacheKey, new Dictionary()); - return executionContext.Items[CacheKey] as Dictionary; + return (Dictionary)executionContext.Items[CacheKey]; } } } diff --git a/src/MongODM.Core/Utility/DBMaintainer.cs b/src/MongODM.Core/Utility/DBMaintainer.cs index 4d07e78e..c39edadd 100644 --- a/src/MongODM.Core/Utility/DBMaintainer.cs +++ b/src/MongODM.Core/Utility/DBMaintainer.cs @@ -4,16 +4,17 @@ using System; using System.Linq; +#nullable enable namespace Digicando.MongODM.Utility { - public class DBMaintainer : IDBMaintainer + public class DbMaintainer : IDbMaintainer { // Fields. - private IDbContext dbContext; + private IDbContext dbContext = default!; private readonly ITaskRunner taskRunner; // Constructors and initialization. - public DBMaintainer(ITaskRunner taskRunner) + public DbMaintainer(ITaskRunner taskRunner) { this.taskRunner = taskRunner; } diff --git a/src/MongODM.Core/Utility/DbContextDependencies.cs b/src/MongODM.Core/Utility/DbContextDependencies.cs new file mode 100644 index 00000000..48354b39 --- /dev/null +++ b/src/MongODM.Core/Utility/DbContextDependencies.cs @@ -0,0 +1,34 @@ +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; + +#nullable enable +namespace Digicando.MongODM.Utility +{ + public class DbContextDependencies : IDbContextDependencies + { + public DbContextDependencies( + IDbCache dbCache, + IDbMaintainer dbMaintainer, + IDocumentSchemaRegister documentSchemaRegister, + IProxyGenerator proxyGenerator, + IRepositoryRegister repositoryRegister, + ISerializerModifierAccessor serializerModifierAccessor) + { + DbCache = dbCache; + DbMaintainer = dbMaintainer; + DocumentSchemaRegister = documentSchemaRegister; + ProxyGenerator = proxyGenerator; + RepositoryRegister = repositoryRegister; + SerializerModifierAccessor = serializerModifierAccessor; + } + + public IDbCache DbCache { get; } + public IDbMaintainer DbMaintainer { get; } + public IDocumentSchemaRegister DocumentSchemaRegister { get; } + public IProxyGenerator ProxyGenerator { get; } + public IRepositoryRegister RepositoryRegister { get; } + public ISerializerModifierAccessor SerializerModifierAccessor { get; } + } +} diff --git a/src/MongODM.Core/Utility/IDBCache.cs b/src/MongODM.Core/Utility/IDBCache.cs index ab415a8d..b108cf78 100644 --- a/src/MongODM.Core/Utility/IDBCache.cs +++ b/src/MongODM.Core/Utility/IDBCache.cs @@ -1,12 +1,13 @@ using Digicando.MongODM.Models; using System.Collections.Generic; +#nullable enable namespace Digicando.MongODM.Utility { /// - /// Interface for implementation. + /// Interface for implementation. /// - public interface IDBCache + public interface IDbCache { // Properties. /// diff --git a/src/MongODM.Core/Utility/IDBMaintainer.cs b/src/MongODM.Core/Utility/IDBMaintainer.cs index f387f2cc..1a3909ad 100644 --- a/src/MongODM.Core/Utility/IDBMaintainer.cs +++ b/src/MongODM.Core/Utility/IDBMaintainer.cs @@ -4,9 +4,9 @@ namespace Digicando.MongODM.Utility { /// - /// Interface for implementation. + /// Interface for implementation. /// - public interface IDBMaintainer : IDbContextInitializable + public interface IDbMaintainer : IDbContextInitializable { // Methods. /// diff --git a/src/MongODM.Core/Utility/IDbContextDependencies.cs b/src/MongODM.Core/Utility/IDbContextDependencies.cs new file mode 100644 index 00000000..638de911 --- /dev/null +++ b/src/MongODM.Core/Utility/IDbContextDependencies.cs @@ -0,0 +1,18 @@ +using Digicando.MongODM.ProxyModels; +using Digicando.MongODM.Repositories; +using Digicando.MongODM.Serialization; +using Digicando.MongODM.Serialization.Modifiers; + +#nullable enable +namespace Digicando.MongODM.Utility +{ + public interface IDbContextDependencies + { + IDbCache DbCache { get; } + IDbMaintainer DbMaintainer { get; } + IDocumentSchemaRegister DocumentSchemaRegister { get; } + IProxyGenerator ProxyGenerator { get; } + IRepositoryRegister RepositoryRegister { get; } + ISerializerModifierAccessor SerializerModifierAccessor { get; } + } +} \ No newline at end of file diff --git a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs index 966a661d..d60e7a70 100644 --- a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs @@ -66,14 +66,14 @@ public SerializationTestElement( } // Fields. - private readonly Mock dbCacheMock; + private readonly Mock dbCacheMock; private readonly DocumentVersion documentVersion = new DocumentVersion("1.0.0"); private readonly Mock serializerModifierAccessorMock; // Constructor. public ExtendedClassMapSerializerTest() { - dbCacheMock = new Mock(); + dbCacheMock = new Mock(); dbCacheMock.Setup(c => c.LoadedModels.ContainsKey(It.IsAny())) .Returns(() => false); From b1bc14b19652674d38d010001840be8ade094bb9 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 30 Apr 2020 02:42:35 +0200 Subject: [PATCH 34/46] Removed redundant parameters from IModelSerializerCollector.Register() --- src/MongODM.Core/DbContext.cs | 2 +- .../Serialization/IModelSerializerCollector.cs | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index b2a25981..4c225020 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -56,7 +56,7 @@ public DbContext( // Register serializers. foreach (var serializerCollector in SerializerCollectors) - serializerCollector.Register(DBCache, this, DocumentSchemaRegister, ProxyGenerator); + serializerCollector.Register(this); // Build and freeze document schema register. DocumentSchemaRegister.Freeze(); diff --git a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs index 482b7ea7..10bf3aba 100644 --- a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs @@ -1,15 +1,9 @@ -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Utility; - -#nullable enable +#nullable enable namespace Digicando.MongODM.Serialization { public interface IModelSerializerCollector { // Methods. - void Register(IDbCache dbCache, - IDbContext dbContext, - IDocumentSchemaRegister documentSchemaRegister, - IProxyGenerator proxyGenerator); + void Register(IDbContext dbContext); } } From a3ec11dab3361ed3a22014456cfdcb0bdf09219a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 30 Apr 2020 19:30:47 +0200 Subject: [PATCH 35/46] Enabled nullable check on core project --- .../ExecutionContextNotFoundException.cs | 16 ++++++++++++++++ src/MongODM.Core/DbContext.cs | 1 - .../Extensions/ClassMapExtensions.cs | 2 +- src/MongODM.Core/IDbContext.cs | 1 - src/MongODM.Core/IDbContextInitializable.cs | 3 +-- src/MongODM.Core/Models/IEntityModel.cs | 1 - src/MongODM.Core/Models/IModel.cs | 2 +- src/MongODM.Core/MongODM.Core.csproj | 1 + src/MongODM.Core/ProxyModels/ProxyGenerator.cs | 10 +++++----- .../ProxyModels/ReferenceableInterceptor.cs | 1 - .../Repositories/CollectionRepository.cs | 1 - .../Repositories/GridFSRepository.cs | 1 - .../Repositories/GridFSRepositoryOptions.cs | 4 ++-- .../Repositories/ICollectionRepository.cs | 4 ++-- .../Repositories/IGridFSRepository.cs | 1 - src/MongODM.Core/Repositories/IRepository.cs | 1 - .../Repositories/IRepositoryRegister.cs | 1 - src/MongODM.Core/Repositories/RepositoryBase.cs | 1 - .../Repositories/RepositoryRegister.cs | 1 - src/MongODM.Core/Serialization/DocumentSchema.cs | 4 ++-- .../Serialization/DocumentSchemaMemberMap.cs | 7 +++++-- .../Serialization/DocumentSchemaRegister.cs | 16 ++++++++-------- .../Serialization/DocumentVersion.cs | 14 +++++++------- src/MongODM.Core/Serialization/EntityMember.cs | 4 ++-- .../Serialization/ExtendedBsonDocumentReader.cs | 2 +- .../Serialization/IDocumentSchemaRegister.cs | 7 +++---- .../Serialization/IModelSerializerCollector.cs | 3 +-- .../Modifiers/CacheSerializerModifier.cs | 12 +++++++++--- .../Modifiers/ReferenceSerializerModifier.cs | 14 +++++++++++--- .../Serializers/EnumerableSerializer.cs | 2 +- .../Serializers/ExtendedClassMapSerializer.cs | 1 - .../Serializers/ExtraElementsSerializer.cs | 6 +++--- .../Serializers/GeoPointSerializer.cs | 2 +- .../Serializers/HexToBinaryDataSerializer.cs | 2 +- .../Serializers/ReferenceSerializer.cs | 6 ++---- .../Serializers/ReferenceSerializerAdapter.cs | 2 +- .../Serializers/ReferenceSerializerSwitch.cs | 2 +- .../Tasks/UpdateDocDependenciesTask.cs | 1 - src/MongODM.Core/Utility/DBCache.cs | 1 - src/MongODM.Core/Utility/DBMaintainer.cs | 6 +++++- .../Utility/DbContextDependencies.cs | 1 - src/MongODM.Core/Utility/IDBCache.cs | 1 - src/MongODM.Core/Utility/IDBMaintainer.cs | 1 - .../Utility/IDbContextDependencies.cs | 1 - 44 files changed, 93 insertions(+), 78 deletions(-) create mode 100644 src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs diff --git a/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs b/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs new file mode 100644 index 00000000..2a179d7f --- /dev/null +++ b/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs @@ -0,0 +1,16 @@ +using System; + +namespace Digicando.ExecContext.Exceptions +{ + public class ExecutionContextNotFoundException : Exception + { + public ExecutionContextNotFoundException(string message) : base(message) + { } + + public ExecutionContextNotFoundException(string message, Exception innerException) : base(message, innerException) + { } + + public ExecutionContextNotFoundException() + { } + } +} diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index 4c225020..0d998adc 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -14,7 +14,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM { public abstract class DbContext : IDbContext diff --git a/src/MongODM.Core/Extensions/ClassMapExtensions.cs b/src/MongODM.Core/Extensions/ClassMapExtensions.cs index 2ae89332..fe334c82 100644 --- a/src/MongODM.Core/Extensions/ClassMapExtensions.cs +++ b/src/MongODM.Core/Extensions/ClassMapExtensions.cs @@ -28,7 +28,7 @@ public static BsonMemberMap SetMemberSerializer { if (typeof(TMember) == typeof(TSerializer)) - return classMap.SetMemberSerializer(memberLambda, serializer as IBsonSerializer); + return classMap.SetMemberSerializer(memberLambda, (IBsonSerializer)serializer); else return classMap.SetMemberSerializer(memberLambda, serializer.GetAdapter()); } diff --git a/src/MongODM.Core/IDbContext.cs b/src/MongODM.Core/IDbContext.cs index c818043f..ce077c56 100644 --- a/src/MongODM.Core/IDbContext.cs +++ b/src/MongODM.Core/IDbContext.cs @@ -7,7 +7,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM { /// diff --git a/src/MongODM.Core/IDbContextInitializable.cs b/src/MongODM.Core/IDbContextInitializable.cs index 37188b6c..d89a90fc 100644 --- a/src/MongODM.Core/IDbContextInitializable.cs +++ b/src/MongODM.Core/IDbContextInitializable.cs @@ -1,5 +1,4 @@ -#nullable enable -namespace Digicando.MongODM +namespace Digicando.MongODM { public interface IDbContextInitializable { diff --git a/src/MongODM.Core/Models/IEntityModel.cs b/src/MongODM.Core/Models/IEntityModel.cs index 1781c357..d61c9c09 100644 --- a/src/MongODM.Core/Models/IEntityModel.cs +++ b/src/MongODM.Core/Models/IEntityModel.cs @@ -1,6 +1,5 @@ using System; -#nullable enable namespace Digicando.MongODM.Models { public interface IEntityModel : IModel diff --git a/src/MongODM.Core/Models/IModel.cs b/src/MongODM.Core/Models/IModel.cs index 1997fec3..b2cd357a 100644 --- a/src/MongODM.Core/Models/IModel.cs +++ b/src/MongODM.Core/Models/IModel.cs @@ -4,6 +4,6 @@ namespace Digicando.MongODM.Models { public interface IModel { - IDictionary ExtraElements { get; } + IDictionary? ExtraElements { get; } } } diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index c1fade0b..2c3f1282 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -8,6 +8,7 @@ Digicando Srl ODM framework for MongoDB 8.0 + enable diff --git a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs index 53e4ad29..8c2f895c 100644 --- a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs +++ b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs @@ -34,7 +34,7 @@ public object CreateInstance( params object[] constructorArguments) { // Get configuration. - (Type[] AdditionalInterfaces, Func InterceptorInstancerSelector) configuration = (null, null); + (Type[] AdditionalInterfaces, Func InterceptorInstancerSelector) configuration = (null!, null!); modelConfigurationDictionaryLock.EnterReadLock(); bool configurationFound = false; try @@ -169,15 +169,15 @@ private Func GetInterceptorInstancer( var entityModelKeyType = entityModelType.GetGenericArguments().Single(); //auditableInterceptor - interceptorInstancers.Add(dbContext => Activator.CreateInstance( + interceptorInstancers.Add(dbContext => (IInterceptor)Activator.CreateInstance( typeof(AuditableInterceptor<>).MakeGenericType(modelType), - additionalInterfaces) as IInterceptor); + additionalInterfaces)); //summarizableInterceptor - interceptorInstancers.Add(dbContext => Activator.CreateInstance( + interceptorInstancers.Add(dbContext => (IInterceptor)Activator.CreateInstance( typeof(ReferenceableInterceptor<,>).MakeGenericType(modelType, entityModelKeyType), additionalInterfaces, - dbContext) as IInterceptor); + dbContext)); } return dbContext => (from instancer in interceptorInstancers diff --git a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs index cbf0101d..08d50fab 100644 --- a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs @@ -10,7 +10,6 @@ using System.Reflection; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.ProxyModels { public class ReferenceableInterceptor : ModelInterceptorBase diff --git a/src/MongODM.Core/Repositories/CollectionRepository.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs index ef4a8d03..bd141490 100644 --- a/src/MongODM.Core/Repositories/CollectionRepository.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -12,7 +12,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Repositories { public class CollectionRepository : diff --git a/src/MongODM.Core/Repositories/GridFSRepository.cs b/src/MongODM.Core/Repositories/GridFSRepository.cs index 6ccb3de9..d5669dc8 100644 --- a/src/MongODM.Core/Repositories/GridFSRepository.cs +++ b/src/MongODM.Core/Repositories/GridFSRepository.cs @@ -11,7 +11,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Repositories { public class GridFSRepository : diff --git a/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs index ff935cec..6ae2f7f8 100644 --- a/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs +++ b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs @@ -9,7 +9,7 @@ public GridFSRepositoryOptions(string name) : base(name) { } - public Action MetadataDeserializer { get; set; } - public Func MetadataSerializer { get; set; } + public Action? MetadataDeserializer { get; set; } + public Func? MetadataSerializer { get; set; } } } diff --git a/src/MongODM.Core/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs index 78be0e4d..f2ae91ef 100644 --- a/src/MongODM.Core/Repositories/ICollectionRepository.cs +++ b/src/MongODM.Core/Repositories/ICollectionRepository.cs @@ -29,7 +29,7 @@ public interface ICollectionRepository : IRepository Task> FindAsync( FilterDefinition filter, - FindOptions options = null, + FindOptions? options = null, CancellationToken cancellationToken = default); Task FindOneAsync( @@ -38,7 +38,7 @@ Task FindOneAsync( Task QueryElementsAsync( Func, Task> query, - AggregateOptions aggregateOptions = null); + AggregateOptions? aggregateOptions = null); Task ReplaceAsync( TModel model, diff --git a/src/MongODM.Core/Repositories/IGridFSRepository.cs b/src/MongODM.Core/Repositories/IGridFSRepository.cs index da66acff..fd49187a 100644 --- a/src/MongODM.Core/Repositories/IGridFSRepository.cs +++ b/src/MongODM.Core/Repositories/IGridFSRepository.cs @@ -4,7 +4,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Repositories { public interface IGridFSRepository : IRepository diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index 8a0b44f0..a34723ed 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -5,7 +5,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Repositories { public interface IRepository : IDbContextInitializable diff --git a/src/MongODM.Core/Repositories/IRepositoryRegister.cs b/src/MongODM.Core/Repositories/IRepositoryRegister.cs index 9826369f..4272561f 100644 --- a/src/MongODM.Core/Repositories/IRepositoryRegister.cs +++ b/src/MongODM.Core/Repositories/IRepositoryRegister.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; -#nullable enable namespace Digicando.MongODM.Repositories { public interface IRepositoryRegister : IDbContextInitializable diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index 45e31cca..c2178644 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -11,7 +11,6 @@ using System.Threading; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Repositories { public abstract class RepositoryBase : diff --git a/src/MongODM.Core/Repositories/RepositoryRegister.cs b/src/MongODM.Core/Repositories/RepositoryRegister.cs index bee087b9..57d8402d 100644 --- a/src/MongODM.Core/Repositories/RepositoryRegister.cs +++ b/src/MongODM.Core/Repositories/RepositoryRegister.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Reflection; -#nullable enable namespace Digicando.MongODM.Repositories { public class RepositoryRegister : IRepositoryRegister diff --git a/src/MongODM.Core/Serialization/DocumentSchema.cs b/src/MongODM.Core/Serialization/DocumentSchema.cs index e0c23a8c..0f433dc5 100644 --- a/src/MongODM.Core/Serialization/DocumentSchema.cs +++ b/src/MongODM.Core/Serialization/DocumentSchema.cs @@ -6,7 +6,7 @@ namespace Digicando.MongODM.Serialization public class DocumentSchema { // Constructors. - public DocumentSchema(BsonClassMap classMap, Type modelType, IBsonSerializer serializer, DocumentVersion version) + public DocumentSchema(BsonClassMap classMap, Type modelType, IBsonSerializer? serializer, DocumentVersion version) { ClassMap = classMap; ModelType = modelType; @@ -17,7 +17,7 @@ public DocumentSchema(BsonClassMap classMap, Type modelType, IBsonSerializer ser // Properties. public BsonClassMap ClassMap { get; } public Type ModelType { get; } - public IBsonSerializer Serializer { get; } + public IBsonSerializer? Serializer { get; } public DocumentVersion Version { get; } } } diff --git a/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs b/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs index 4c10e6f9..be41c91c 100644 --- a/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs +++ b/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs @@ -22,7 +22,7 @@ public DocumentSchemaMemberMap( } // Properties. - public IEnumerable EntityClassMapPath => MemberPath.Select(m => m.EntityClassMap) + public IEnumerable EntityClassMapPath => MemberPath.Select(m => m.EntityClassMap!) .Where(cm => cm != null) .Distinct(); public bool IsIdMember => MemberPath.Last().IsId; @@ -32,7 +32,7 @@ public IEnumerable MemberPathToEntity { get { - var lastEntityNestedMembers = MemberPath.Aggregate( + var lastEntityNestedMembers = MemberPath.Aggregate( (1, null), (acc, member) => member.EntityClassMap == acc.lastEntityClassMap ? (acc.counter++, acc.lastEntityClassMap) : @@ -51,6 +51,9 @@ public IEnumerable MemberPathToId return MemberPath; var lastEntityClassMap = MemberPath.Last().EntityClassMap; + if (lastEntityClassMap is null) + throw new InvalidOperationException("This model is not related to a an entity model with an Id"); + return MemberPathToEntity.Append(new EntityMember(lastEntityClassMap, lastEntityClassMap.IdMemberMap)); } } diff --git a/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs index 4edee6c8..f90dd901 100644 --- a/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs +++ b/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs @@ -24,7 +24,7 @@ public class DocumentSchemaRegister : IDocumentSchemaRegister private readonly Dictionary> modelEntityReferencesIdsMap = new Dictionary>(); - private IDbContext dbContext; + private IDbContext dbContext = default!; private readonly ISerializerModifierAccessor serializerModifierAccessor; private readonly List schemas = new List(); @@ -130,8 +130,8 @@ public IEnumerable GetModelEntityReferencesIds(Type mod public void RegisterModelSchema( DocumentVersion fromVersion, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, @@ -142,8 +142,8 @@ public void RegisterModelSchema( public void RegisterModelSchema( DocumentVersion fromVersion, Action> classMapInitializer, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class => RegisterModelSchema( fromVersion, @@ -154,8 +154,8 @@ public void RegisterModelSchema( public void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, - Func> initCustomSerializer = null, - Func> modelMigrationAsync = null) + Func>? initCustomSerializer = null, + Func>? modelMigrationAsync = null) where TModel : class { configLock.EnterWriteLock(); @@ -165,7 +165,7 @@ public void RegisterModelSchema( throw new InvalidOperationException("Register is frozen"); // Generate model serializer. - IBsonSerializer serializer = null; + IBsonSerializer? serializer = null; if (initCustomSerializer != null) //if custom is setted, keep it serializer = initCustomSerializer(); diff --git a/src/MongODM.Core/Serialization/DocumentVersion.cs b/src/MongODM.Core/Serialization/DocumentVersion.cs index d6403964..5b754353 100644 --- a/src/MongODM.Core/Serialization/DocumentVersion.cs +++ b/src/MongODM.Core/Serialization/DocumentVersion.cs @@ -48,7 +48,7 @@ public DocumentVersion( int major, int minor, int patch, - string label) + string? label) { MajorRelease = major; MinorRelease = minor; @@ -57,16 +57,16 @@ public DocumentVersion( } // Properties. - public string LabelRelease { get; private set; } public int MajorRelease { get; private set; } public int MinorRelease { get; private set; } public int PatchRelease { get; private set; } + public string? LabelRelease { get; private set; } // Overrides. - public int CompareTo(DocumentVersion other) + public int CompareTo(DocumentVersion? other) { // If other is not a valid object reference, this instance is greater. - if (other == null) return 1; + if (other is null) return 1; if (this > other) return 1; if (this == other) return 0; @@ -98,7 +98,7 @@ public override string ToString() } // Operators. - public static bool operator < (DocumentVersion x, DocumentVersion y) + public static bool operator < (DocumentVersion? x, DocumentVersion? y) { // Check if null. if (y == null) @@ -121,9 +121,9 @@ public override string ToString() return false; } - public static bool operator > (DocumentVersion x, DocumentVersion y) => y < x; + public static bool operator > (DocumentVersion? x, DocumentVersion? y) => y < x; - public static bool operator == (DocumentVersion x, DocumentVersion y) + public static bool operator == (DocumentVersion? x, DocumentVersion? y) { if (ReferenceEquals(x, y)) return true; if (x is null || y is null) return false; diff --git a/src/MongODM.Core/Serialization/EntityMember.cs b/src/MongODM.Core/Serialization/EntityMember.cs index f96492a3..b2937d2b 100644 --- a/src/MongODM.Core/Serialization/EntityMember.cs +++ b/src/MongODM.Core/Serialization/EntityMember.cs @@ -6,7 +6,7 @@ public class EntityMember { // Constructors. public EntityMember( - BsonClassMap entityClassMap, + BsonClassMap? entityClassMap, BsonMemberMap memberMap) { EntityClassMap = entityClassMap; @@ -14,7 +14,7 @@ public EntityMember( } // Properties. - public BsonClassMap EntityClassMap { get; } + public BsonClassMap? EntityClassMap { get; } public bool IsId => MemberMap == EntityClassMap?.IdMemberMap; public BsonMemberMap MemberMap { get; } } diff --git a/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs b/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs index 44102b07..5770a138 100644 --- a/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs +++ b/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs @@ -9,6 +9,6 @@ public ExtendedBsonDocumentReader(BsonDocument document) : base(document) { } - public DocumentVersion DocumentVersion { get; set; } + public DocumentVersion? DocumentVersion { get; set; } } } diff --git a/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs index 898bc38b..9a1b388a 100644 --- a/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs @@ -4,7 +4,6 @@ using System.Reflection; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Serialization { /// @@ -37,7 +36,7 @@ public interface IDocumentSchemaRegister : IDbContextInitializable void RegisterModelSchema( DocumentVersion fromVersion, Func>? initCustomSerializer = null, - Func>? modelMigrationAsync = null) + Func>? modelMigrationAsync = null) where TModel : class; /// @@ -52,7 +51,7 @@ void RegisterModelSchema( DocumentVersion fromVersion, Action> classMapInitializer, Func>? initCustomSerializer = null, - Func>? modelMigrationAsync = null) + Func>? modelMigrationAsync = null) where TModel : class; /// @@ -67,7 +66,7 @@ void RegisterModelSchema( DocumentVersion fromVersion, BsonClassMap classMap, Func>? initCustomSerializer = null, - Func>? modelMigrationAsync = null) + Func>? modelMigrationAsync = null) where TModel : class; } } \ No newline at end of file diff --git a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs index 10bf3aba..da289e88 100644 --- a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs @@ -1,5 +1,4 @@ -#nullable enable -namespace Digicando.MongODM.Serialization +namespace Digicando.MongODM.Serialization { public interface IModelSerializerCollector { diff --git a/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs index 78e888ff..a68ec2d9 100644 --- a/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs @@ -1,4 +1,5 @@ using Digicando.ExecContext; +using Digicando.ExecContext.Exceptions; using System; using System.Collections; using System.Collections.Generic; @@ -17,13 +18,15 @@ class CacheSerializerModifier : IDisposable // Constructors and dispose. public CacheSerializerModifier(IExecutionContext context) { - if (context == null) + if (context is null) throw new ArgumentNullException(nameof(context)); + if (context.Items is null) + throw new ExecutionContextNotFoundException(); if (!context.Items.ContainsKey(ModifierKey)) context.Items.Add(ModifierKey, new List()); - requestes = context.Items[ModifierKey] as ICollection; + requestes = (ICollection)context.Items[ModifierKey]; lock (((ICollection)requestes).SyncRoot) requestes.Add(this); @@ -41,9 +44,12 @@ public void Dispose() // Static methods. public static bool IsNoCacheEnabled(IExecutionContext context) { + if (context.Items is null) + throw new ExecutionContextNotFoundException(); + if (!context.Items.ContainsKey(ModifierKey)) return false; - var requestes = context.Items[ModifierKey] as ICollection; + var requestes = (ICollection)context.Items[ModifierKey]; lock (((ICollection)requestes).SyncRoot) return requestes.Any(r => r.NoCache); diff --git a/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs index 3510ff63..e49488ce 100644 --- a/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -1,4 +1,5 @@ using Digicando.ExecContext; +using Digicando.ExecContext.Exceptions; using System; using System.Collections; using System.Collections.Generic; @@ -17,13 +18,15 @@ class ReferenceSerializerModifier : IDisposable // Constructors and dispose. public ReferenceSerializerModifier(IExecutionContext context) { - if (context == null) + if (context is null) throw new ArgumentNullException(nameof(context)); + if (context.Items is null) + throw new ExecutionContextNotFoundException(); if (!context.Items.ContainsKey(ModifierKey)) context.Items.Add(ModifierKey, new List()); - requestes = context.Items[ModifierKey] as ICollection; + requestes = (ICollection)context.Items[ModifierKey]; lock (((ICollection)requestes).SyncRoot) requestes.Add(this); @@ -41,9 +44,14 @@ public void Dispose() // Static methods. public static bool IsReadOnlyIdEnabled(IExecutionContext context) { + if (context is null) + throw new ArgumentNullException(nameof(context)); + if (context.Items is null) + throw new ExecutionContextNotFoundException(); + if (!context.Items.ContainsKey(ModifierKey)) return false; - var requestes = context.Items[ModifierKey] as ICollection; + var requestes = (ICollection)context.Items[ModifierKey]; lock (((ICollection)requestes).SyncRoot) return requestes.Any(r => r.ReadOnlyId); diff --git a/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs b/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs index 7b7cc845..59dfd60d 100644 --- a/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs @@ -61,7 +61,7 @@ public EnumerableSerializer WithItemSerializer(IBsonSerializer ite // Protected methods. protected override void AddItem(object accumulator, TItem item) => - (accumulator as List).Add(item); + ((List)accumulator).Add(item); protected override object CreateAccumulator() => new List(); diff --git a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs index 73440320..6e2aad66 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -12,7 +12,6 @@ using System.Reflection; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Serialization.Serializers { public class ExtendedClassMapSerializer : diff --git a/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs index 21c1a684..aecefe8b 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs @@ -15,7 +15,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati if (value is IDictionary) { context.Writer.WriteStartDocument(); - foreach (var pair in value as IDictionary) + foreach (var pair in (IDictionary)value) { context.Writer.WriteName(pair.Key); Serialize(context, args, pair.Value); @@ -25,7 +25,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati else if (value is IList) { context.Writer.WriteStartArray(); - foreach (var element in value as IList) + foreach (var element in (IList)value) { Serialize(context, args, element); } @@ -41,7 +41,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati public static TValue DeserializeValue( object extraElements, - IBsonSerializer serializer = null) + IBsonSerializer? serializer = null) { /* * Must create a context container because arrays diff --git a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs index 42820e28..1ca9da97 100644 --- a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs @@ -40,7 +40,7 @@ public override TInModel Deserialize(BsonDeserializationContext context, BsonDes var point = pointSerializer.Deserialize(context, args); if (point == null) { - return null; + return null!; } // Create model instance. diff --git a/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs b/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs index 190fc427..db7a7d99 100644 --- a/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs @@ -17,7 +17,7 @@ public override string Deserialize(BsonDeserializationContext context, BsonDeser break; case BsonType.Null: bsonReader.ReadNull(); - return null; + return null!; default: var message = $"Expected a value of type Binary, but found a value of type {bsonType} instead."; throw new InvalidOperationException(message); diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs index f74dfb9a..6fe80dc0 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs @@ -13,7 +13,6 @@ using System.Reflection; using System.Threading; -#nullable enable namespace Digicando.MongODM.Serialization.Serializers { public class ReferenceSerializer : @@ -251,9 +250,8 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati value = newModel; } - // Remove extra elements. - if ((value as IModel)?.ExtraElements != null) - (value as IModel).ExtraElements.Clear(); + // Clear extra elements. + (value as IModel)?.ExtraElements?.Clear(); // Serialize object. var serializer = GetSerializer(valueType); diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs index d77f71f8..6b7af37a 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs @@ -27,7 +27,7 @@ public ReferenceSerializerAdapter(ReferenceSerializer serializer // Methods. public override TOutModel Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { - return serializerBase.Deserialize(context, args) as TOutModel; + return (TOutModel)serializerBase.Deserialize(context, args); } public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs index 1d5c9f5a..bb0a2100 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs @@ -13,7 +13,7 @@ public class ReferenceSerializerSwitch : // Nested classes. public class CaseContext { - public DocumentVersion DocumentVersion { get; set; } + public DocumentVersion? DocumentVersion { get; set; } } // Fields. diff --git a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs index fd1bed6f..9f2f27af 100644 --- a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Threading.Tasks; -#nullable enable namespace Digicando.MongODM.Tasks { public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask diff --git a/src/MongODM.Core/Utility/DBCache.cs b/src/MongODM.Core/Utility/DBCache.cs index 067cf915..d6481099 100644 --- a/src/MongODM.Core/Utility/DBCache.cs +++ b/src/MongODM.Core/Utility/DBCache.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -#nullable enable namespace Digicando.MongODM.Utility { public class DbCache : IDbCache diff --git a/src/MongODM.Core/Utility/DBMaintainer.cs b/src/MongODM.Core/Utility/DBMaintainer.cs index c39edadd..77485a9a 100644 --- a/src/MongODM.Core/Utility/DBMaintainer.cs +++ b/src/MongODM.Core/Utility/DBMaintainer.cs @@ -4,7 +4,6 @@ using System; using System.Linq; -#nullable enable namespace Digicando.MongODM.Utility { public class DbMaintainer : IDbMaintainer @@ -35,6 +34,11 @@ public void Initialize(IDbContext dbContext) // Methods. public void OnUpdatedModel(IAuditable updatedModel, TKey modelId) { + if (updatedModel is null) + throw new ArgumentNullException(nameof(updatedModel)); + if (modelId is null) + throw new ArgumentNullException(nameof(modelId)); + var updatedMembers = updatedModel.ChangedMembers; var dependencies = updatedMembers.SelectMany(member => dbContext.DocumentSchemaRegister.GetMemberDependencies(member)) .Where(d => d.IsEntityReferenceMember); diff --git a/src/MongODM.Core/Utility/DbContextDependencies.cs b/src/MongODM.Core/Utility/DbContextDependencies.cs index 48354b39..708abc52 100644 --- a/src/MongODM.Core/Utility/DbContextDependencies.cs +++ b/src/MongODM.Core/Utility/DbContextDependencies.cs @@ -3,7 +3,6 @@ using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; -#nullable enable namespace Digicando.MongODM.Utility { public class DbContextDependencies : IDbContextDependencies diff --git a/src/MongODM.Core/Utility/IDBCache.cs b/src/MongODM.Core/Utility/IDBCache.cs index b108cf78..1c8ca9fd 100644 --- a/src/MongODM.Core/Utility/IDBCache.cs +++ b/src/MongODM.Core/Utility/IDBCache.cs @@ -1,7 +1,6 @@ using Digicando.MongODM.Models; using System.Collections.Generic; -#nullable enable namespace Digicando.MongODM.Utility { /// diff --git a/src/MongODM.Core/Utility/IDBMaintainer.cs b/src/MongODM.Core/Utility/IDBMaintainer.cs index 1a3909ad..032b0186 100644 --- a/src/MongODM.Core/Utility/IDBMaintainer.cs +++ b/src/MongODM.Core/Utility/IDBMaintainer.cs @@ -1,6 +1,5 @@ using Digicando.MongODM.ProxyModels; -#nullable enable namespace Digicando.MongODM.Utility { /// diff --git a/src/MongODM.Core/Utility/IDbContextDependencies.cs b/src/MongODM.Core/Utility/IDbContextDependencies.cs index 638de911..1dbd0f02 100644 --- a/src/MongODM.Core/Utility/IDbContextDependencies.cs +++ b/src/MongODM.Core/Utility/IDbContextDependencies.cs @@ -3,7 +3,6 @@ using Digicando.MongODM.Serialization; using Digicando.MongODM.Serialization.Modifiers; -#nullable enable namespace Digicando.MongODM.Utility { public interface IDbContextDependencies From 3375e499dcaea2e7ddb9fb94b8e9011fb22a213e Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 30 Apr 2020 19:50:17 +0200 Subject: [PATCH 36/46] Updated dependencies Added Microsoft.CodeAnalysis.FxCopAnalyzers to other secondary projects --- src/ExecutionContext/ExecutionContext.csproj | 2 +- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 4 ++++ .../Filters/AsyncLocalContextHangfireFilter.cs | 7 +++++++ src/MongODM.Hangfire/MongODM.Hangfire.csproj | 4 ++++ .../Tasks/UpdateDocDependenciesTaskFacade.cs | 2 ++ test/ExecutionContext.Tests/ExecutionContext.Tests.csproj | 2 +- test/MongODM.Core.Tests/MongODM.Core.Tests.csproj | 2 +- 7 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index cd6b6664..18181514 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -20,7 +20,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 94a5a639..0619d9cc 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -30,6 +30,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs index 02faef10..db708889 100644 --- a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs +++ b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs @@ -1,5 +1,6 @@ using Digicando.ExecContext.AsyncLocal; using Hangfire.Server; +using System; using System.Collections.Generic; namespace Digicando.MongODM.HF.Filters @@ -19,6 +20,9 @@ public AsyncLocalContextHangfireFilter(IAsyncLocalContext asyncLocalContext) // Properties. public void OnPerforming(PerformingContext filterContext) { + if (filterContext is null) + throw new ArgumentNullException(nameof(filterContext)); + lock (contextHandlers) { contextHandlers.Add(filterContext.BackgroundJob.Id, asyncLocalContext.InitAsyncLocalContext()); @@ -27,6 +31,9 @@ public void OnPerforming(PerformingContext filterContext) public void OnPerformed(PerformedContext filterContext) { + if (filterContext is null) + throw new ArgumentNullException(nameof(filterContext)); + lock (contextHandlers) { var jobId = filterContext.BackgroundJob.Id; diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index bda1b569..1d34001f 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -21,6 +21,10 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs index 82d91b0d..a461d575 100644 --- a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs +++ b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -2,11 +2,13 @@ using Hangfire; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Threading.Tasks; namespace Digicando.MongODM.HF.Tasks { + [SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "It is instantiated by Hangfire")] class UpdateDocDependenciesTaskFacade { // Fields. diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index 280b4767..ec7e2c23 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -10,7 +10,7 @@ - + all diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index 38bf966c..bcafb14d 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -10,7 +10,7 @@ - + all From e9e5376e2cb360cb30ea22f8f0a8d5df12e8f318 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 4 May 2020 18:58:18 +0200 Subject: [PATCH 37/46] Updated asp.net configuration --- src/ExecutionContext/ExecutionContext.csproj | 2 +- .../MongODM.AspNetCore.csproj | 2 +- .../MongODMConfiguration.cs | 14 ++++++ .../ServiceCollectionExtensions.cs | 45 +++++++++++++++---- src/MongODM.Core/DbContextOptions.cs | 4 ++ src/MongODM.Core/MongODM.Core.csproj | 2 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- .../Tasks/HangfireTaskRunner.cs | 8 +++- 8 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 src/MongODM.AspNetCore/MongODMConfiguration.cs diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 18181514..46c6a5dc 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 0619d9cc..1f005bee 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -26,7 +26,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODMConfiguration.cs b/src/MongODM.AspNetCore/MongODMConfiguration.cs new file mode 100644 index 00000000..ad877bae --- /dev/null +++ b/src/MongODM.AspNetCore/MongODMConfiguration.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace Digicando.MongODM.AspNetCore +{ + public class MongODMConfiguration + { + public MongODMConfiguration(IServiceCollection services) + { + Services = services; + } + + public IServiceCollection Services { get; } + } +} diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index a9859c73..7fb3593f 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -10,6 +10,7 @@ using Digicando.MongODM.Utility; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection.Extensions; +using System; using System.Collections.Generic; using System.Linq; @@ -17,13 +18,13 @@ namespace Microsoft.Extensions.DependencyInjection { public static class ServiceCollectionExtensions { - public static void UseMongODM( + public static MongODMConfiguration UseMongODM( this IServiceCollection services, IEnumerable? executionContexts = null) where TTaskRunner : class, ITaskRunner => UseMongODM(services, executionContexts); - public static void UseMongODM( + public static MongODMConfiguration UseMongODM( this IServiceCollection services, IEnumerable? executionContexts = null) where TProxyGenerator: class, IProxyGenerator @@ -66,22 +67,48 @@ public static void UseMongODM( //castle proxy generator. services.TryAddSingleton(new Castle.DynamicProxy.ProxyGenerator()); + + return new MongODMConfiguration(services); } - public static void UseMongODMDbContext( - this IServiceCollection services) + public static MongODMConfiguration AddDbContext( + this MongODMConfiguration config, + Action>? dbContextConfig = null) where TDbContext : class, IDbContext { - services.AddSingleton(); + if (config is null) + throw new ArgumentNullException(nameof(config)); + + // Register dbContext. + config.Services.AddSingleton(); + + // Register options. + var contextOptions = new DbContextOptions(); + dbContextConfig?.Invoke(contextOptions); + config.Services.AddSingleton(contextOptions); + + return config; } - public static void UseMongODMDbContext( - this IServiceCollection services) + public static MongODMConfiguration AddDbContext( + this MongODMConfiguration config, + Action>? dbContextConfig = null) where TDbContext : class, IDbContext where TDbContextImpl : class, TDbContext { - services.AddSingleton(); - services.AddSingleton(sp => sp.GetService() as TDbContextImpl); + if (config is null) + throw new ArgumentNullException(nameof(config)); + + // Register dbContext. + config.Services.AddSingleton(); + config.Services.AddSingleton(sp => sp.GetService() as TDbContextImpl); + + // Register options. + var contextOptions = new DbContextOptions(); + dbContextConfig?.Invoke(contextOptions); + config.Services.AddSingleton(contextOptions); + + return config; } } } diff --git a/src/MongODM.Core/DbContextOptions.cs b/src/MongODM.Core/DbContextOptions.cs index f2f8a79a..c0c8af5e 100644 --- a/src/MongODM.Core/DbContextOptions.cs +++ b/src/MongODM.Core/DbContextOptions.cs @@ -8,4 +8,8 @@ public class DbContextOptions public string DBName { get; set; } = "localDb"; public DocumentVersion DocumentVersion { get; set; } = "1.0.0"; } + + public class DbContextOptions : DbContextOptions + where TDbContext : class, IDbContext + { } } diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 2c3f1282..09ecc73a 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 1d34001f..60b7fdcf 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index c061359c..b24b500b 100644 --- a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -1,4 +1,6 @@ -using Digicando.MongODM.Tasks; +using Digicando.ExecContext.AsyncLocal; +using Digicando.MongODM.HF.Filters; +using Digicando.MongODM.Tasks; using Hangfire; using System; using System.Collections.Generic; @@ -15,6 +17,10 @@ public HangfireTaskRunner( IBackgroundJobClient backgroundJobClient) { this.backgroundJobClient = backgroundJobClient; + + // Add a default execution context running with any Hangfire task. + // Added because with asyncronous task, unrelated to requestes, there is no an alternative context to use with MongODM. + GlobalJobFilters.Filters.Add(new AsyncLocalContextHangfireFilter(AsyncLocalContext.Instance)); } // Methods. From b4027995de81519c050c361ed39b2666be27969f Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 4 May 2020 19:04:01 +0200 Subject: [PATCH 38/46] Config fix --- src/MongODM.AspNetCore/ServiceCollectionExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 7fb3593f..29a1454d 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -92,7 +92,7 @@ public static MongODMConfiguration AddDbContext( public static MongODMConfiguration AddDbContext( this MongODMConfiguration config, - Action>? dbContextConfig = null) + Action>? dbContextConfig = null) where TDbContext : class, IDbContext where TDbContextImpl : class, TDbContext { @@ -104,7 +104,7 @@ public static MongODMConfiguration AddDbContext( config.Services.AddSingleton(sp => sp.GetService() as TDbContextImpl); // Register options. - var contextOptions = new DbContextOptions(); + var contextOptions = new DbContextOptions(); dbContextConfig?.Invoke(contextOptions); config.Services.AddSingleton(contextOptions); From 2fc0feb5a82e98209dd76dcffde1d0d7b80c08cf Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sat, 9 May 2020 20:36:11 +0200 Subject: [PATCH 39/46] Simplified DbContextOptions configuration --- src/MongODM.Core/DbContextOptions.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MongODM.Core/DbContextOptions.cs b/src/MongODM.Core/DbContextOptions.cs index c0c8af5e..176aafd1 100644 --- a/src/MongODM.Core/DbContextOptions.cs +++ b/src/MongODM.Core/DbContextOptions.cs @@ -1,11 +1,13 @@ using Digicando.MongODM.Serialization; +using System.Linq; namespace Digicando.MongODM { public class DbContextOptions { public string ConnectionString { get; set; } = "mongodb://localhost/localDb"; - public string DBName { get; set; } = "localDb"; + public string DBName => ConnectionString.Split('?')[0] + .Split('/').Last(); public DocumentVersion DocumentVersion { get; set; } = "1.0.0"; } From 2008f396401554e77b4da00888531e4fe3793c0e Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Mon, 25 May 2020 17:34:13 +0200 Subject: [PATCH 40/46] Renamed from Digicando to Etherna --- .../AsyncLocal/AsyncLocalContext.cs | 2 +- .../AsyncLocal/AsyncLocalContextHandler.cs | 2 +- .../AsyncLocal/IAsyncLocalContext.cs | 2 +- .../AsyncLocal/IAsyncLocalContextHandler.cs | 2 +- .../AsyncLocal/IHandledAsyncLocalContext.cs | 2 +- .../ExecutionContextNotFoundException.cs | 2 +- src/ExecutionContext/ExecutionContext.csproj | 4 ++-- .../ExecutionContextSelector.cs | 2 +- src/ExecutionContext/IExecutionContext.cs | 2 +- .../Properties/GlobalSuppressions.cs | 2 +- .../HttpContextExecutionContext.cs | 4 ++-- .../MongODM.AspNetCore.csproj | 4 ++-- .../MongODMConfiguration.cs | 2 +- .../ServiceCollectionExtensions.cs | 20 +++++++++---------- src/MongODM.Core/DbContext.cs | 16 +++++++-------- src/MongODM.Core/DbContextOptions.cs | 4 ++-- .../Exceptions/EntityNotFoundException.cs | 2 +- .../Exceptions/InvalidEntityTypeException.cs | 2 +- .../Extensions/ClassMapExtensions.cs | 6 +++--- .../Extensions/EnumerableExtensions.cs | 2 +- src/MongODM.Core/GlobalSuppressions.cs | 4 ++-- src/MongODM.Core/IDbContext.cs | 12 +++++------ src/MongODM.Core/IDbContextInitializable.cs | 2 +- .../Migration/MongoCollectionMigration.cs | 6 +++--- .../Migration/MongoDocumentMigration.cs | 8 ++++---- .../Migration/MongoMigrationBase.cs | 2 +- src/MongODM.Core/Models/IEntityModel.cs | 2 +- src/MongODM.Core/Models/IFileModel.cs | 2 +- src/MongODM.Core/Models/IModel.cs | 2 +- src/MongODM.Core/MongODM.Core.csproj | 4 ++-- .../ProxyModels/AuditableInterceptor.cs | 2 +- src/MongODM.Core/ProxyModels/IAuditable.cs | 2 +- .../ProxyModels/IProxyGenerator.cs | 2 +- .../ProxyModels/IReferenceable.cs | 2 +- .../ProxyModels/ProxyGenerator.cs | 4 ++-- .../ProxyModels/ReferenceableInterceptor.cs | 6 +++--- .../Repositories/CollectionRepository.cs | 12 +++++------ .../CollectionRepositoryOptions.cs | 2 +- .../Repositories/GridFSRepository.cs | 12 +++++------ .../Repositories/GridFSRepositoryOptions.cs | 2 +- .../Repositories/ICollectionRepository.cs | 4 ++-- .../Repositories/IGridFSRepository.cs | 4 ++-- src/MongODM.Core/Repositories/IRepository.cs | 6 +++--- .../Repositories/IRepositoryRegister.cs | 2 +- .../Repositories/RepositoryBase.cs | 10 +++++----- .../Repositories/RepositoryOptionsBase.cs | 2 +- .../Repositories/RepositoryRegister.cs | 2 +- .../Serialization/DocumentSchema.cs | 2 +- .../Serialization/DocumentSchemaMemberMap.cs | 2 +- .../Serialization/DocumentSchemaRegister.cs | 8 ++++---- .../Serialization/DocumentVersion.cs | 2 +- .../Serialization/EntityMember.cs | 2 +- .../ExtendedBsonDocumentReader.cs | 2 +- .../ExtendedBsonDocumentWriter.cs | 2 +- .../Serialization/IDocumentSchemaRegister.cs | 2 +- .../IModelSerializerCollector.cs | 2 +- .../Modifiers/CacheSerializerModifier.cs | 6 +++--- .../Modifiers/ISerializerModifierAccessor.cs | 2 +- .../Modifiers/ReferenceSerializerModifier.cs | 6 +++--- .../Modifiers/SerializerModifierAccessor.cs | 4 ++-- .../Serializers/DictionarySerializer.cs | 2 +- .../Serializers/EnumerableSerializer.cs | 2 +- .../Serializers/ExtendedClassMapSerializer.cs | 10 +++++----- .../Serializers/ExtraElementsSerializer.cs | 10 +++++----- .../Serializers/GeoPointSerializer.cs | 4 ++-- .../Serializers/HexToBinaryDataSerializer.cs | 2 +- .../IClassMapContainerSerializer.cs | 4 +--- .../IReferenceContainerSerializer.cs | 2 +- .../ReadOnlyDictionarySerializer.cs | 2 +- .../Serializers/ReferenceSerializer.cs | 10 +++++----- .../Serializers/ReferenceSerializerAdapter.cs | 4 ++-- .../Serializers/ReferenceSerializerSwitch.cs | 4 ++-- src/MongODM.Core/Tasks/ITaskRunner.cs | 2 +- .../Tasks/IUpdateDocDependenciesTask.cs | 4 ++-- src/MongODM.Core/Tasks/Queues.cs | 2 +- .../Tasks/UpdateDocDependenciesTask.cs | 8 ++++---- src/MongODM.Core/Utility/DBCache.cs | 6 +++--- src/MongODM.Core/Utility/DBMaintainer.cs | 6 +++--- .../Utility/DbContextDependencies.cs | 10 +++++----- src/MongODM.Core/Utility/IDBCache.cs | 4 ++-- src/MongODM.Core/Utility/IDBMaintainer.cs | 4 ++-- .../Utility/IDbContextDependencies.cs | 10 +++++----- .../AsyncLocalContextHangfireFilter.cs | 4 ++-- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 4 ++-- .../Tasks/HangfireTaskRunner.cs | 8 ++++---- .../Tasks/UpdateDocDependenciesTaskFacade.cs | 4 ++-- .../AsyncLocalContextHandlerTests.cs | 2 +- .../AsyncLocal/AsyncLocalContextTests.cs | 2 +- .../ExecutionContext.Tests.csproj | 2 +- .../ExecutionContextSelectorTests.cs | 2 +- .../Comparers/FakeModelComparer.cs | 4 ++-- .../ExtendedClassMapSerializerTest.cs | 14 ++++++------- .../MockHelpers/InterceptorMockHelper.cs | 2 +- .../Models/FakeEntityModelBase.cs | 2 +- test/MongODM.Core.Tests/Models/FakeModel.cs | 2 +- .../Models/FakeModelProxy.cs | 2 +- test/MongODM.Core.Tests/Models/ModelBase.cs | 2 +- .../MongODM.Core.Tests.csproj | 2 +- .../ReferenceableInterceptorTest.cs | 10 +++++----- 99 files changed, 212 insertions(+), 214 deletions(-) diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs index 28fb2758..9f58fbb9 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Threading; -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { /// /// Async local context implementation. This can be used as singleton or with multiple instances. diff --git a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs index f91bea0e..79f45266 100644 --- a/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/AsyncLocalContextHandler.cs @@ -1,4 +1,4 @@ -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { /// /// The handler for an initialization. diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs index 4a8413ac..f7b12877 100644 --- a/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContext.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { /// /// The interface. diff --git a/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs index 16302b81..8e9fb773 100644 --- a/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs +++ b/src/ExecutionContext/AsyncLocal/IAsyncLocalContextHandler.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { /// /// A disposable interface for diff --git a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs index 5026cee9..f3564b19 100644 --- a/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs +++ b/src/ExecutionContext/AsyncLocal/IHandledAsyncLocalContext.cs @@ -1,4 +1,4 @@ -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { /// /// Interface used by for comunicate with its diff --git a/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs b/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs index 2a179d7f..60caf978 100644 --- a/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs +++ b/src/ExecutionContext/Exceptions/ExecutionContextNotFoundException.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.ExecContext.Exceptions +namespace Etherna.ExecContext.Exceptions { public class ExecutionContextNotFoundException : Exception { diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 46c6a5dc..843691ab 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -3,9 +3,9 @@ netstandard2.0 true - Digicando.ExecContext + Etherna.ExecContext true - Digicando Srl + Etherna Sagl Execution context provider 8.0 enable diff --git a/src/ExecutionContext/ExecutionContextSelector.cs b/src/ExecutionContext/ExecutionContextSelector.cs index 7c3df66b..db04f1b7 100644 --- a/src/ExecutionContext/ExecutionContextSelector.cs +++ b/src/ExecutionContext/ExecutionContextSelector.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Digicando.ExecContext +namespace Etherna.ExecContext { /// /// A multi context selector that take different contexts, and select the first available. diff --git a/src/ExecutionContext/IExecutionContext.cs b/src/ExecutionContext/IExecutionContext.cs index 16eadbac..10a6d294 100644 --- a/src/ExecutionContext/IExecutionContext.cs +++ b/src/ExecutionContext/IExecutionContext.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.ExecContext +namespace Etherna.ExecContext { /// /// Represents an execution context, where information can be put and retrieve alongside diff --git a/src/ExecutionContext/Properties/GlobalSuppressions.cs b/src/ExecutionContext/Properties/GlobalSuppressions.cs index 0638dd5f..8035245b 100644 --- a/src/ExecutionContext/Properties/GlobalSuppressions.cs +++ b/src/ExecutionContext/Properties/GlobalSuppressions.cs @@ -5,4 +5,4 @@ using System.Diagnostics.CodeAnalysis; -[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "This library will be not localized", Scope = "NamespaceAndDescendants", Target = "Digicando.ExecContext")] \ No newline at end of file +[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "This library will be not localized", Scope = "NamespaceAndDescendants", Target = "Etherna.ExecContext")] diff --git a/src/MongODM.AspNetCore/HttpContextExecutionContext.cs b/src/MongODM.AspNetCore/HttpContextExecutionContext.cs index ad2d32db..2b7a8262 100644 --- a/src/MongODM.AspNetCore/HttpContextExecutionContext.cs +++ b/src/MongODM.AspNetCore/HttpContextExecutionContext.cs @@ -1,8 +1,8 @@ -using Digicando.ExecContext; +using Etherna.ExecContext; using Microsoft.AspNetCore.Http; using System.Collections.Generic; -namespace Digicando.MongODM.AspNetCore +namespace Etherna.MongODM.AspNetCore { public class HttpContextExecutionContext : IExecutionContext { diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 1f005bee..16bb295c 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -3,9 +3,9 @@ netstandard2.0;netcoreapp3.0 true - Digicando.MongODM.AspNetCore + Etherna.MongODM.AspNetCore true - Digicando Srl + Etherna Sagl Asp.Net Core adapter for MongODM 8.0 enable diff --git a/src/MongODM.AspNetCore/MongODMConfiguration.cs b/src/MongODM.AspNetCore/MongODMConfiguration.cs index ad877bae..7b73a847 100644 --- a/src/MongODM.AspNetCore/MongODMConfiguration.cs +++ b/src/MongODM.AspNetCore/MongODMConfiguration.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.DependencyInjection; -namespace Digicando.MongODM.AspNetCore +namespace Etherna.MongODM.AspNetCore { public class MongODMConfiguration { diff --git a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs index 29a1454d..910067e1 100644 --- a/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs +++ b/src/MongODM.AspNetCore/ServiceCollectionExtensions.cs @@ -1,13 +1,13 @@ -using Digicando.ExecContext; -using Digicando.ExecContext.AsyncLocal; -using Digicando.MongODM; -using Digicando.MongODM.AspNetCore; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Tasks; -using Digicando.MongODM.Utility; +using Etherna.ExecContext; +using Etherna.ExecContext.AsyncLocal; +using Etherna.MongODM; +using Etherna.MongODM.AspNetCore; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Tasks; +using Etherna.MongODM.Utility; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection.Extensions; using System; diff --git a/src/MongODM.Core/DbContext.cs b/src/MongODM.Core/DbContext.cs index 0d998adc..770c1bb9 100644 --- a/src/MongODM.Core/DbContext.cs +++ b/src/MongODM.Core/DbContext.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.Migration; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Utility; +using Etherna.MongODM.Migration; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization.Conventions; using MongoDB.Driver; @@ -14,7 +14,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM +namespace Etherna.MongODM { public abstract class DbContext : IDbContext { diff --git a/src/MongODM.Core/DbContextOptions.cs b/src/MongODM.Core/DbContextOptions.cs index 176aafd1..eaa49bf8 100644 --- a/src/MongODM.Core/DbContextOptions.cs +++ b/src/MongODM.Core/DbContextOptions.cs @@ -1,7 +1,7 @@ -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Serialization; using System.Linq; -namespace Digicando.MongODM +namespace Etherna.MongODM { public class DbContextOptions { diff --git a/src/MongODM.Core/Exceptions/EntityNotFoundException.cs b/src/MongODM.Core/Exceptions/EntityNotFoundException.cs index 2d3d1326..fbcd8754 100644 --- a/src/MongODM.Core/Exceptions/EntityNotFoundException.cs +++ b/src/MongODM.Core/Exceptions/EntityNotFoundException.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.Exceptions +namespace Etherna.MongODM.Exceptions { public class EntityNotFoundException : Exception { diff --git a/src/MongODM.Core/Exceptions/InvalidEntityTypeException.cs b/src/MongODM.Core/Exceptions/InvalidEntityTypeException.cs index b6070138..bf1fb4d6 100644 --- a/src/MongODM.Core/Exceptions/InvalidEntityTypeException.cs +++ b/src/MongODM.Core/Exceptions/InvalidEntityTypeException.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.Exceptions +namespace Etherna.MongODM.Exceptions { public class InvalidEntityTypeException : Exception { diff --git a/src/MongODM.Core/Extensions/ClassMapExtensions.cs b/src/MongODM.Core/Extensions/ClassMapExtensions.cs index fe334c82..aea66a6e 100644 --- a/src/MongODM.Core/Extensions/ClassMapExtensions.cs +++ b/src/MongODM.Core/Extensions/ClassMapExtensions.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Serialization.Serializers; +using Etherna.MongODM.Models; +using Etherna.MongODM.Serialization.Serializers; using MongoDB.Bson.Serialization; using System; using System.Linq.Expressions; -namespace Digicando.MongODM.Extensions +namespace Etherna.MongODM.Extensions { public static class ClassMapExtensions { diff --git a/src/MongODM.Core/Extensions/EnumerableExtensions.cs b/src/MongODM.Core/Extensions/EnumerableExtensions.cs index ac4ecdaa..def59d78 100644 --- a/src/MongODM.Core/Extensions/EnumerableExtensions.cs +++ b/src/MongODM.Core/Extensions/EnumerableExtensions.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Linq.Expressions; -namespace Digicando.MongODM.Extensions +namespace Etherna.MongODM.Extensions { public static class EnumerableExtensions { diff --git a/src/MongODM.Core/GlobalSuppressions.cs b/src/MongODM.Core/GlobalSuppressions.cs index 8537d114..174ec059 100644 --- a/src/MongODM.Core/GlobalSuppressions.cs +++ b/src/MongODM.Core/GlobalSuppressions.cs @@ -4,6 +4,6 @@ // Project-level suppressions either have no target or are given // a specific target and scoped to a namespace, type, member, etc. -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongODM.Serialization.DocumentSchemaRegister.MembersDependenciesToString~System.String")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Digicando.MongODM.Serialization.DocumentSchemaRegister.ModelDependenciesToString~System.String")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Etherna.MongODM.Serialization.DocumentSchemaRegister.MembersDependenciesToString~System.String")] +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Code Quality", "IDE0051:Remove unused private members", Justification = "", Scope = "member", Target = "~M:Etherna.MongODM.Serialization.DocumentSchemaRegister.ModelDependenciesToString~System.String")] diff --git a/src/MongODM.Core/IDbContext.cs b/src/MongODM.Core/IDbContext.cs index ce077c56..2ffc85df 100644 --- a/src/MongODM.Core/IDbContext.cs +++ b/src/MongODM.Core/IDbContext.cs @@ -1,13 +1,13 @@ -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Utility; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Utility; using MongoDB.Driver; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM +namespace Etherna.MongODM { /// /// Interface of implementation. diff --git a/src/MongODM.Core/IDbContextInitializable.cs b/src/MongODM.Core/IDbContextInitializable.cs index d89a90fc..89e61e46 100644 --- a/src/MongODM.Core/IDbContextInitializable.cs +++ b/src/MongODM.Core/IDbContextInitializable.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM +namespace Etherna.MongODM { public interface IDbContextInitializable { diff --git a/src/MongODM.Core/Migration/MongoCollectionMigration.cs b/src/MongODM.Core/Migration/MongoCollectionMigration.cs index ac4dec5e..de10a709 100644 --- a/src/MongODM.Core/Migration/MongoCollectionMigration.cs +++ b/src/MongODM.Core/Migration/MongoCollectionMigration.cs @@ -1,11 +1,11 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Repositories; +using Etherna.MongODM.Models; +using Etherna.MongODM.Repositories; using MongoDB.Driver; using System; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Migration +namespace Etherna.MongODM.Migration { /// /// Migrate a collection to another diff --git a/src/MongODM.Core/Migration/MongoDocumentMigration.cs b/src/MongODM.Core/Migration/MongoDocumentMigration.cs index 0f52439b..5ddbb4da 100644 --- a/src/MongODM.Core/Migration/MongoDocumentMigration.cs +++ b/src/MongODM.Core/Migration/MongoDocumentMigration.cs @@ -1,12 +1,12 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Models; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Migration +namespace Etherna.MongODM.Migration { /// /// Migrate documents of a collection from an older version to a newer diff --git a/src/MongODM.Core/Migration/MongoMigrationBase.cs b/src/MongODM.Core/Migration/MongoMigrationBase.cs index 6a2f1591..53a67fdc 100644 --- a/src/MongODM.Core/Migration/MongoMigrationBase.cs +++ b/src/MongODM.Core/Migration/MongoMigrationBase.cs @@ -1,7 +1,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Migration +namespace Etherna.MongODM.Migration { public abstract class MongoMigrationBase { diff --git a/src/MongODM.Core/Models/IEntityModel.cs b/src/MongODM.Core/Models/IEntityModel.cs index d61c9c09..ffd76256 100644 --- a/src/MongODM.Core/Models/IEntityModel.cs +++ b/src/MongODM.Core/Models/IEntityModel.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public interface IEntityModel : IModel { diff --git a/src/MongODM.Core/Models/IFileModel.cs b/src/MongODM.Core/Models/IFileModel.cs index 5db30ac9..bddcf289 100644 --- a/src/MongODM.Core/Models/IFileModel.cs +++ b/src/MongODM.Core/Models/IFileModel.cs @@ -1,6 +1,6 @@ using System.IO; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public interface IFileModel : IEntityModel { diff --git a/src/MongODM.Core/Models/IModel.cs b/src/MongODM.Core/Models/IModel.cs index b2cd357a..5fec005e 100644 --- a/src/MongODM.Core/Models/IModel.cs +++ b/src/MongODM.Core/Models/IModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public interface IModel { diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 09ecc73a..7580eb39 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -3,9 +3,9 @@ netstandard2.0 true - Digicando.MongODM + Etherna.MongODM true - Digicando Srl + Etherna Sagl ODM framework for MongoDB 8.0 enable diff --git a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs index e3431644..358676c8 100644 --- a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Reflection; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public class AuditableInterceptor : ModelInterceptorBase { diff --git a/src/MongODM.Core/ProxyModels/IAuditable.cs b/src/MongODM.Core/ProxyModels/IAuditable.cs index 04036f7b..dd06ab5c 100644 --- a/src/MongODM.Core/ProxyModels/IAuditable.cs +++ b/src/MongODM.Core/ProxyModels/IAuditable.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Reflection; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public interface IAuditable { diff --git a/src/MongODM.Core/ProxyModels/IProxyGenerator.cs b/src/MongODM.Core/ProxyModels/IProxyGenerator.cs index 219f26dd..10902b19 100644 --- a/src/MongODM.Core/ProxyModels/IProxyGenerator.cs +++ b/src/MongODM.Core/ProxyModels/IProxyGenerator.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public interface IProxyGenerator { diff --git a/src/MongODM.Core/ProxyModels/IReferenceable.cs b/src/MongODM.Core/ProxyModels/IReferenceable.cs index 9a3ae0d3..b842230d 100644 --- a/src/MongODM.Core/ProxyModels/IReferenceable.cs +++ b/src/MongODM.Core/ProxyModels/IReferenceable.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public interface IReferenceable { diff --git a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs index 8c2f895c..21190269 100644 --- a/src/MongODM.Core/ProxyModels/ProxyGenerator.cs +++ b/src/MongODM.Core/ProxyModels/ProxyGenerator.cs @@ -1,11 +1,11 @@ using Castle.DynamicProxy; -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public class ProxyGenerator : IProxyGenerator { diff --git a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs index 08d50fab..951afa28 100644 --- a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs @@ -2,15 +2,15 @@ using Digicando.DomainHelper; using Digicando.DomainHelper.Attributes; using Digicando.DomainHelper.ProxyModel; -using Digicando.MongODM.Models; -using Digicando.MongODM.Repositories; +using Etherna.MongODM.Models; +using Etherna.MongODM.Repositories; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongODM.ProxyModels +namespace Etherna.MongODM.ProxyModels { public class ReferenceableInterceptor : ModelInterceptorBase where TModel : class, IEntityModel diff --git a/src/MongODM.Core/Repositories/CollectionRepository.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs index bd141490..615ae935 100644 --- a/src/MongODM.Core/Repositories/CollectionRepository.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -1,7 +1,7 @@ -using Digicando.MongODM.Exceptions; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Exceptions; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Linq; @@ -12,7 +12,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public class CollectionRepository : RepositoryBase, @@ -34,7 +34,7 @@ public CollectionRepository(CollectionRepositoryOptions options) } // Properties. - public IMongoCollection Collection => _collection ?? (_collection = DbContext.Database.GetCollection(options.Name)); + public IMongoCollection Collection => _collection ??= DbContext.Database.GetCollection(options.Name); // Public methods. public override async Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) diff --git a/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs index 13ac3f6a..4cae2fdb 100644 --- a/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs +++ b/src/MongODM.Core/Repositories/CollectionRepositoryOptions.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public class CollectionRepositoryOptions : RepositoryOptionsBase { diff --git a/src/MongODM.Core/Repositories/GridFSRepository.cs b/src/MongODM.Core/Repositories/GridFSRepository.cs index d5669dc8..518e5b55 100644 --- a/src/MongODM.Core/Repositories/GridFSRepository.cs +++ b/src/MongODM.Core/Repositories/GridFSRepository.cs @@ -1,7 +1,7 @@ using Digicando.DomainHelper; -using Digicando.MongODM.Exceptions; -using Digicando.MongODM.Models; -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Exceptions; +using Etherna.MongODM.Models; +using Etherna.MongODM.Serialization; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.GridFS; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public class GridFSRepository : RepositoryBase, @@ -33,8 +33,8 @@ public GridFSRepository(GridFSRepositoryOptions options) } // Properties. - public IGridFSBucket GridFSBucket => _gridFSBucket ?? - (_gridFSBucket = new GridFSBucket(DbContext.Database, new GridFSBucketOptions { BucketName = options.Name })); + public IGridFSBucket GridFSBucket => + _gridFSBucket ??= new GridFSBucket(DbContext.Database, new GridFSBucketOptions { BucketName = options.Name }); // Methods. public override Task BuildIndexesAsync(IDocumentSchemaRegister schemaRegister, CancellationToken cancellationToken = default) => Task.CompletedTask; diff --git a/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs index 6ae2f7f8..5364a889 100644 --- a/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs +++ b/src/MongODM.Core/Repositories/GridFSRepositoryOptions.cs @@ -1,7 +1,7 @@ using MongoDB.Bson; using System; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public class GridFSRepositoryOptions : RepositoryOptionsBase { diff --git a/src/MongODM.Core/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs index f2ae91ef..0635231c 100644 --- a/src/MongODM.Core/Repositories/ICollectionRepository.cs +++ b/src/MongODM.Core/Repositories/ICollectionRepository.cs @@ -1,4 +1,4 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using MongoDB.Driver; using MongoDB.Driver.Linq; using System; @@ -6,7 +6,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public interface ICollectionRepository : IRepository { diff --git a/src/MongODM.Core/Repositories/IGridFSRepository.cs b/src/MongODM.Core/Repositories/IGridFSRepository.cs index fd49187a..6b8f4457 100644 --- a/src/MongODM.Core/Repositories/IGridFSRepository.cs +++ b/src/MongODM.Core/Repositories/IGridFSRepository.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using MongoDB.Driver.GridFS; using System.IO; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public interface IGridFSRepository : IRepository { diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index a34723ed..ebe27bfc 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -1,11 +1,11 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Models; +using Etherna.MongODM.Serialization; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public interface IRepository : IDbContextInitializable { diff --git a/src/MongODM.Core/Repositories/IRepositoryRegister.cs b/src/MongODM.Core/Repositories/IRepositoryRegister.cs index 4272561f..3a75332e 100644 --- a/src/MongODM.Core/Repositories/IRepositoryRegister.cs +++ b/src/MongODM.Core/Repositories/IRepositoryRegister.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public interface IRepositoryRegister : IDbContextInitializable { diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index c2178644..e37e57c4 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -1,8 +1,8 @@ using Digicando.DomainHelper; -using Digicando.MongODM.Exceptions; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization; +using Etherna.MongODM.Exceptions; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Serialization; using MoreLinq; using System; using System.Collections; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public abstract class RepositoryBase : IRepository diff --git a/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs b/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs index 212aa5b6..d8c5b0fd 100644 --- a/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryOptionsBase.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public abstract class RepositoryOptionsBase { diff --git a/src/MongODM.Core/Repositories/RepositoryRegister.cs b/src/MongODM.Core/Repositories/RepositoryRegister.cs index 57d8402d..ad1dec50 100644 --- a/src/MongODM.Core/Repositories/RepositoryRegister.cs +++ b/src/MongODM.Core/Repositories/RepositoryRegister.cs @@ -3,7 +3,7 @@ using System.Linq; using System.Reflection; -namespace Digicando.MongODM.Repositories +namespace Etherna.MongODM.Repositories { public class RepositoryRegister : IRepositoryRegister { diff --git a/src/MongODM.Core/Serialization/DocumentSchema.cs b/src/MongODM.Core/Serialization/DocumentSchema.cs index 0f433dc5..6c597760 100644 --- a/src/MongODM.Core/Serialization/DocumentSchema.cs +++ b/src/MongODM.Core/Serialization/DocumentSchema.cs @@ -1,7 +1,7 @@ using MongoDB.Bson.Serialization; using System; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class DocumentSchema { diff --git a/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs b/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs index be41c91c..cc4729d2 100644 --- a/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs +++ b/src/MongODM.Core/Serialization/DocumentSchemaMemberMap.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class DocumentSchemaMemberMap { diff --git a/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs index f90dd901..22eb9520 100644 --- a/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs +++ b/src/MongODM.Core/Serialization/DocumentSchemaRegister.cs @@ -1,6 +1,6 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Serialization.Serializers; +using Etherna.MongODM.Models; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Serialization.Serializers; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; @@ -11,7 +11,7 @@ using System.Threading; using System.Threading.Tasks; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class DocumentSchemaRegister : IDocumentSchemaRegister { diff --git a/src/MongODM.Core/Serialization/DocumentVersion.cs b/src/MongODM.Core/Serialization/DocumentVersion.cs index 5b754353..b31d4082 100644 --- a/src/MongODM.Core/Serialization/DocumentVersion.cs +++ b/src/MongODM.Core/Serialization/DocumentVersion.cs @@ -2,7 +2,7 @@ using System.Text; using System.Text.RegularExpressions; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class DocumentVersion : IComparable { diff --git a/src/MongODM.Core/Serialization/EntityMember.cs b/src/MongODM.Core/Serialization/EntityMember.cs index b2937d2b..0c33ad15 100644 --- a/src/MongODM.Core/Serialization/EntityMember.cs +++ b/src/MongODM.Core/Serialization/EntityMember.cs @@ -1,6 +1,6 @@ using MongoDB.Bson.Serialization; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class EntityMember { diff --git a/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs b/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs index 5770a138..c26927e0 100644 --- a/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs +++ b/src/MongODM.Core/Serialization/ExtendedBsonDocumentReader.cs @@ -1,7 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.IO; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class ExtendedBsonDocumentReader : BsonDocumentReader { diff --git a/src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs b/src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs index 5490453f..c895749f 100644 --- a/src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs +++ b/src/MongODM.Core/Serialization/ExtendedBsonDocumentWriter.cs @@ -1,7 +1,7 @@ using MongoDB.Bson; using MongoDB.Bson.IO; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public class ExtendedBsonDocumentWriter : BsonDocumentWriter { diff --git a/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs b/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs index 9a1b388a..149b6bc2 100644 --- a/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs +++ b/src/MongODM.Core/Serialization/IDocumentSchemaRegister.cs @@ -4,7 +4,7 @@ using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { /// /// Interface for implementation. diff --git a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs index da289e88..6a59c0d5 100644 --- a/src/MongODM.Core/Serialization/IModelSerializerCollector.cs +++ b/src/MongODM.Core/Serialization/IModelSerializerCollector.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM.Serialization +namespace Etherna.MongODM.Serialization { public interface IModelSerializerCollector { diff --git a/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs index a68ec2d9..f61f0105 100644 --- a/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs +++ b/src/MongODM.Core/Serialization/Modifiers/CacheSerializerModifier.cs @@ -1,11 +1,11 @@ -using Digicando.ExecContext; -using Digicando.ExecContext.Exceptions; +using Etherna.ExecContext; +using Etherna.ExecContext.Exceptions; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongODM.Serialization.Modifiers +namespace Etherna.MongODM.Serialization.Modifiers { class CacheSerializerModifier : IDisposable { diff --git a/src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs b/src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs index 0fc2afaa..ad6d5fb0 100644 --- a/src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs +++ b/src/MongODM.Core/Serialization/Modifiers/ISerializerModifierAccessor.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.Serialization.Modifiers +namespace Etherna.MongODM.Serialization.Modifiers { public interface ISerializerModifierAccessor { diff --git a/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs b/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs index e49488ce..8d43886a 100644 --- a/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs +++ b/src/MongODM.Core/Serialization/Modifiers/ReferenceSerializerModifier.cs @@ -1,11 +1,11 @@ -using Digicando.ExecContext; -using Digicando.ExecContext.Exceptions; +using Etherna.ExecContext; +using Etherna.ExecContext.Exceptions; using System; using System.Collections; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongODM.Serialization.Modifiers +namespace Etherna.MongODM.Serialization.Modifiers { class ReferenceSerializerModifier : IDisposable { diff --git a/src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs b/src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs index 61477823..42bb6b07 100644 --- a/src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs +++ b/src/MongODM.Core/Serialization/Modifiers/SerializerModifierAccessor.cs @@ -1,7 +1,7 @@ -using Digicando.ExecContext; +using Etherna.ExecContext; using System; -namespace Digicando.MongODM.Serialization.Modifiers +namespace Etherna.MongODM.Serialization.Modifiers { public class SerializerModifierAccessor : ISerializerModifierAccessor { diff --git a/src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs b/src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs index cae344ac..61982577 100644 --- a/src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/DictionarySerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class DictionarySerializer : DictionarySerializerBase, TKey, TValue>, diff --git a/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs b/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs index 59dfd60d..f108bf27 100644 --- a/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/EnumerableSerializer.cs @@ -2,7 +2,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class EnumerableSerializer : EnumerableSerializerBase, TItem>, diff --git a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs index 6e2aad66..ec1eb85c 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -1,8 +1,8 @@ using Digicando.DomainHelper; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Utility; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; @@ -12,7 +12,7 @@ using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class ExtendedClassMapSerializer : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IClassMapContainerSerializer diff --git a/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs index aecefe8b..77778436 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtraElementsSerializer.cs @@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class ExtraElementsSerializer : SerializerBase { @@ -12,20 +12,20 @@ public class ExtraElementsSerializer : SerializerBase public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) { - if (value is IDictionary) + if (value is IDictionary dictionary) { context.Writer.WriteStartDocument(); - foreach (var pair in (IDictionary)value) + foreach (var pair in dictionary) { context.Writer.WriteName(pair.Key); Serialize(context, args, pair.Value); } context.Writer.WriteEndDocument(); } - else if (value is IList) + else if (value is IList list) { context.Writer.WriteStartArray(); - foreach (var element in (IList)value) + foreach (var element in list) { Serialize(context, args, element); } diff --git a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs index 1ca9da97..326f335e 100644 --- a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs @@ -1,5 +1,5 @@ using Digicando.DomainHelper; -using Digicando.MongODM.ProxyModels; +using Etherna.MongODM.ProxyModels; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.GeoJsonObjectModel; @@ -8,7 +8,7 @@ using System.Linq.Expressions; using System.Reflection; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class GeoPointSerializer : SerializerBase where TInModel : class diff --git a/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs b/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs index db7a7d99..908a7e28 100644 --- a/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/HexToBinaryDataSerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class HexToBinaryDataSerializer : SerializerBase { diff --git a/src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs b/src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs index 4298074f..2e48380b 100644 --- a/src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/IClassMapContainerSerializer.cs @@ -1,9 +1,7 @@ using MongoDB.Bson.Serialization; -using System; using System.Collections.Generic; -using System.Text; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public interface IClassMapContainerSerializer { diff --git a/src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs b/src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs index f69c46a7..0e3705cb 100644 --- a/src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/IReferenceContainerSerializer.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public interface IReferenceContainerSerializer : IClassMapContainerSerializer { diff --git a/src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs index b777860e..d3eff7ab 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReadOnlyDictionarySerializer.cs @@ -3,7 +3,7 @@ using MongoDB.Bson.Serialization.Serializers; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class ReadOnlyDictionarySerializer : DictionarySerializerBase, TKey, TValue>, diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs index 6fe80dc0..3890519d 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs @@ -1,8 +1,8 @@ using Digicando.DomainHelper; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Utility; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Conventions; @@ -13,7 +13,7 @@ using System.Reflection; using System.Threading; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class ReferenceSerializer : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs index 6b7af37a..8b5ad16f 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerAdapter.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { class ReferenceSerializerAdapter : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs index bb0a2100..06213407 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializerSwitch.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using System; using System.Collections.Generic; -namespace Digicando.MongODM.Serialization.Serializers +namespace Etherna.MongODM.Serialization.Serializers { public class ReferenceSerializerSwitch : SerializerBase, IBsonSerializer, IBsonDocumentSerializer, IBsonIdProvider, IReferenceContainerSerializer diff --git a/src/MongODM.Core/Tasks/ITaskRunner.cs b/src/MongODM.Core/Tasks/ITaskRunner.cs index 5ed06fac..51af70d0 100644 --- a/src/MongODM.Core/Tasks/ITaskRunner.cs +++ b/src/MongODM.Core/Tasks/ITaskRunner.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace Digicando.MongODM.Tasks +namespace Etherna.MongODM.Tasks { public interface ITaskRunner { diff --git a/src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs index b70e6993..e72fdbd6 100644 --- a/src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/IUpdateDocDependenciesTask.cs @@ -1,8 +1,8 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using System.Collections.Generic; using System.Threading.Tasks; -namespace Digicando.MongODM.Tasks +namespace Etherna.MongODM.Tasks { public interface IUpdateDocDependenciesTask { diff --git a/src/MongODM.Core/Tasks/Queues.cs b/src/MongODM.Core/Tasks/Queues.cs index 2cbeb97d..d44fde97 100644 --- a/src/MongODM.Core/Tasks/Queues.cs +++ b/src/MongODM.Core/Tasks/Queues.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM.Tasks +namespace Etherna.MongODM.Tasks { public class Queues { diff --git a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs index 9f2f27af..67b24a67 100644 --- a/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs +++ b/src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs @@ -1,12 +1,12 @@ -using Digicando.MongODM.Models; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Models; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization.Modifiers; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Threading.Tasks; -namespace Digicando.MongODM.Tasks +namespace Etherna.MongODM.Tasks { public class UpdateDocDependenciesTask : IUpdateDocDependenciesTask { diff --git a/src/MongODM.Core/Utility/DBCache.cs b/src/MongODM.Core/Utility/DBCache.cs index d6481099..6eaa5b7d 100644 --- a/src/MongODM.Core/Utility/DBCache.cs +++ b/src/MongODM.Core/Utility/DBCache.cs @@ -1,9 +1,9 @@ -using Digicando.ExecContext; -using Digicando.MongODM.Models; +using Etherna.ExecContext; +using Etherna.MongODM.Models; using System; using System.Collections.Generic; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { public class DbCache : IDbCache { diff --git a/src/MongODM.Core/Utility/DBMaintainer.cs b/src/MongODM.Core/Utility/DBMaintainer.cs index 77485a9a..e871015a 100644 --- a/src/MongODM.Core/Utility/DBMaintainer.cs +++ b/src/MongODM.Core/Utility/DBMaintainer.cs @@ -1,10 +1,10 @@ -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Tasks; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Tasks; using MongoDB.Driver; using System; using System.Linq; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { public class DbMaintainer : IDbMaintainer { diff --git a/src/MongODM.Core/Utility/DbContextDependencies.cs b/src/MongODM.Core/Utility/DbContextDependencies.cs index 708abc52..3eb308f3 100644 --- a/src/MongODM.Core/Utility/DbContextDependencies.cs +++ b/src/MongODM.Core/Utility/DbContextDependencies.cs @@ -1,9 +1,9 @@ -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { public class DbContextDependencies : IDbContextDependencies { diff --git a/src/MongODM.Core/Utility/IDBCache.cs b/src/MongODM.Core/Utility/IDBCache.cs index 1c8ca9fd..7979c869 100644 --- a/src/MongODM.Core/Utility/IDBCache.cs +++ b/src/MongODM.Core/Utility/IDBCache.cs @@ -1,7 +1,7 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using System.Collections.Generic; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { /// /// Interface for implementation. diff --git a/src/MongODM.Core/Utility/IDBMaintainer.cs b/src/MongODM.Core/Utility/IDBMaintainer.cs index 032b0186..db9c9da3 100644 --- a/src/MongODM.Core/Utility/IDBMaintainer.cs +++ b/src/MongODM.Core/Utility/IDBMaintainer.cs @@ -1,6 +1,6 @@ -using Digicando.MongODM.ProxyModels; +using Etherna.MongODM.ProxyModels; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { /// /// Interface for implementation. diff --git a/src/MongODM.Core/Utility/IDbContextDependencies.cs b/src/MongODM.Core/Utility/IDbContextDependencies.cs index 1dbd0f02..bc8d335b 100644 --- a/src/MongODM.Core/Utility/IDbContextDependencies.cs +++ b/src/MongODM.Core/Utility/IDbContextDependencies.cs @@ -1,9 +1,9 @@ -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; -namespace Digicando.MongODM.Utility +namespace Etherna.MongODM.Utility { public interface IDbContextDependencies { diff --git a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs index db708889..915b0aab 100644 --- a/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs +++ b/src/MongODM.Hangfire/Filters/AsyncLocalContextHangfireFilter.cs @@ -1,9 +1,9 @@ -using Digicando.ExecContext.AsyncLocal; +using Etherna.ExecContext.AsyncLocal; using Hangfire.Server; using System; using System.Collections.Generic; -namespace Digicando.MongODM.HF.Filters +namespace Etherna.MongODM.HF.Filters { public class AsyncLocalContextHangfireFilter : IServerFilter { diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 60b7fdcf..100d884e 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -3,9 +3,9 @@ netstandard2.0 true - Digicando.MongODM.HF + Etherna.MongODM.HF true - Digicando Srl + Etherna Sagl Linker for use MongoDB with Hangfire 8.0 enable diff --git a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index b24b500b..d86d0da9 100644 --- a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -1,11 +1,11 @@ -using Digicando.ExecContext.AsyncLocal; -using Digicando.MongODM.HF.Filters; -using Digicando.MongODM.Tasks; +using Etherna.ExecContext.AsyncLocal; +using Etherna.MongODM.HF.Filters; +using Etherna.MongODM.Tasks; using Hangfire; using System; using System.Collections.Generic; -namespace Digicando.MongODM.HF.Tasks +namespace Etherna.MongODM.HF.Tasks { public class HangfireTaskRunner : ITaskRunner { diff --git a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs index a461d575..16644c88 100644 --- a/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs +++ b/src/MongODM.Hangfire/Tasks/UpdateDocDependenciesTaskFacade.cs @@ -1,4 +1,4 @@ -using Digicando.MongODM.Tasks; +using Etherna.MongODM.Tasks; using Hangfire; using System; using System.Collections.Generic; @@ -6,7 +6,7 @@ using System.Reflection; using System.Threading.Tasks; -namespace Digicando.MongODM.HF.Tasks +namespace Etherna.MongODM.HF.Tasks { [SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "It is instantiated by Hangfire")] class UpdateDocDependenciesTaskFacade diff --git a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs index 5d25f273..cda2b389 100644 --- a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs +++ b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextHandlerTests.cs @@ -1,7 +1,7 @@ using Moq; using Xunit; -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { public class AsyncLocalContextHandlerTests { diff --git a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs index 6ec4759f..cb5d3059 100644 --- a/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs +++ b/test/ExecutionContext.Tests/AsyncLocal/AsyncLocalContextTests.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Xunit; -namespace Digicando.ExecContext.AsyncLocal +namespace Etherna.ExecContext.AsyncLocal { public class AsyncLocalContextTests { diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index ec7e2c23..8b89265f 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -3,7 +3,7 @@ netcoreapp3.1 true - Digicando.ExecContext + Etherna.ExecContext false enable diff --git a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs index 7c8ea11d..5e0c1747 100644 --- a/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs +++ b/test/ExecutionContext.Tests/ExecutionContextSelectorTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Xunit; -namespace Digicando.ExecContext +namespace Etherna.ExecContext { public class ExecutionContextSelectorTests { diff --git a/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs b/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs index eed1f128..da09fd75 100644 --- a/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs +++ b/test/MongODM.Core.Tests/Comparers/FakeModelComparer.cs @@ -1,8 +1,8 @@ -using Digicando.MongODM.Models; +using Etherna.MongODM.Models; using System.Collections.Generic; using System.Linq; -namespace Digicando.MongODM.Comparers +namespace Etherna.MongODM.Comparers { public class FakeModelComparer : EqualityComparer { diff --git a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs index d60e7a70..59ba262f 100644 --- a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs @@ -1,10 +1,10 @@ using Digicando.DomainHelper; -using Digicando.MongODM.Comparers; -using Digicando.MongODM.Models; -using Digicando.MongODM.Serialization; -using Digicando.MongODM.Serialization.Modifiers; -using Digicando.MongODM.Serialization.Serializers; -using Digicando.MongODM.Utility; +using Etherna.MongODM.Comparers; +using Etherna.MongODM.Models; +using Etherna.MongODM.Serialization; +using Etherna.MongODM.Serialization.Modifiers; +using Etherna.MongODM.Serialization.Serializers; +using Etherna.MongODM.Utility; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -15,7 +15,7 @@ using System.Threading.Tasks; using Xunit; -namespace Digicando.MongODM +namespace Etherna.MongODM { public class ExtendedClassMapSerializerTest { diff --git a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs index 3d075bd2..9fe21968 100644 --- a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs +++ b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs @@ -3,7 +3,7 @@ using System; using System.Linq.Expressions; -namespace Digicando.MongODM.MockHelpers +namespace Etherna.MongODM.MockHelpers { public static class InterceptorMockHelper { diff --git a/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs b/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs index b479c34c..533d7a08 100644 --- a/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs +++ b/test/MongODM.Core.Tests/Models/FakeEntityModelBase.cs @@ -1,6 +1,6 @@ using System; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public abstract class FakeEntityModelBase : ModelBase, IEntityModel { diff --git a/test/MongODM.Core.Tests/Models/FakeModel.cs b/test/MongODM.Core.Tests/Models/FakeModel.cs index 0ee03ee2..a532c116 100644 --- a/test/MongODM.Core.Tests/Models/FakeModel.cs +++ b/test/MongODM.Core.Tests/Models/FakeModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public class FakeModel : FakeEntityModelBase { diff --git a/test/MongODM.Core.Tests/Models/FakeModelProxy.cs b/test/MongODM.Core.Tests/Models/FakeModelProxy.cs index 890f7526..5d7e3fa0 100644 --- a/test/MongODM.Core.Tests/Models/FakeModelProxy.cs +++ b/test/MongODM.Core.Tests/Models/FakeModelProxy.cs @@ -1,4 +1,4 @@ -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public class FakeModelProxy : FakeModel { diff --git a/test/MongODM.Core.Tests/Models/ModelBase.cs b/test/MongODM.Core.Tests/Models/ModelBase.cs index 71691237..c1eb4e2a 100644 --- a/test/MongODM.Core.Tests/Models/ModelBase.cs +++ b/test/MongODM.Core.Tests/Models/ModelBase.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Digicando.MongODM.Models +namespace Etherna.MongODM.Models { public abstract class ModelBase : IModel { diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index bcafb14d..047950dc 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -3,7 +3,7 @@ netcoreapp3.1 true - Digicando.MongODM + Etherna.MongODM false enable diff --git a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs index bb125b1a..473ba8ae 100644 --- a/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs +++ b/test/MongODM.Core.Tests/ReferenceableInterceptorTest.cs @@ -1,14 +1,14 @@ -using Digicando.MongODM.MockHelpers; -using Digicando.MongODM.Models; -using Digicando.MongODM.ProxyModels; -using Digicando.MongODM.Repositories; +using Etherna.MongODM.MockHelpers; +using Etherna.MongODM.Models; +using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.Repositories; using Moq; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Xunit; -namespace Digicando.MongODM +namespace Etherna.MongODM { public class ReferenceableInterceptorTest { From d5a885fc8193b7353c465c71605682e7fcb52a28 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Wed, 3 Jun 2020 19:22:04 +0200 Subject: [PATCH 41/46] Added TryFindOneAsync with predicate --- .../Repositories/CollectionRepository.cs | 19 +++++++++++++++++-- .../Repositories/ICollectionRepository.cs | 10 ++++++++++ src/MongODM.Core/Repositories/IRepository.cs | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/MongODM.Core/Repositories/CollectionRepository.cs b/src/MongODM.Core/Repositories/CollectionRepository.cs index 615ae935..04e4a460 100644 --- a/src/MongODM.Core/Repositories/CollectionRepository.cs +++ b/src/MongODM.Core/Repositories/CollectionRepository.cs @@ -141,6 +141,21 @@ public virtual Task ReplaceAsync( CancellationToken cancellationToken = default) => ReplaceHelperAsync(model, session, updateDependentDocuments, cancellationToken); + public async Task TryFindOneAsync(Expression> predicate, CancellationToken cancellationToken = default) + { + if (predicate is null) + throw new ArgumentNullException(nameof(predicate)); + + try + { + return await FindOneAsync(predicate, cancellationToken); + } + catch (EntityNotFoundException) + { + return null; + } + } + // Protected methods. protected override Task CreateOnDBAsync(IEnumerable models, CancellationToken cancellationToken) => Collection.InsertManyAsync(models, null, cancellationToken); @@ -179,10 +194,10 @@ private async Task FindOneOnDBAsync( var element = await Collection.AsQueryable() .Where(predicate) .SingleOrDefaultAsync(cancellationToken); - if (element as TModel == default(TModel)) + if (element == default(TModel)) throw new EntityNotFoundException("Can't find element"); - return element as TModel; + return element; } private async Task ReplaceHelperAsync( diff --git a/src/MongODM.Core/Repositories/ICollectionRepository.cs b/src/MongODM.Core/Repositories/ICollectionRepository.cs index 0635231c..1eecf98c 100644 --- a/src/MongODM.Core/Repositories/ICollectionRepository.cs +++ b/src/MongODM.Core/Repositories/ICollectionRepository.cs @@ -50,5 +50,15 @@ Task ReplaceAsync( IClientSessionHandle session, bool updateDependentDocuments = true, CancellationToken cancellationToken = default); + + /// + /// Try to find a model and don't throw exception if it is not found + /// + /// Model find predicate + /// The cancellation token + /// The model, null if it doesn't exist + Task TryFindOneAsync( + Expression> predicate, + CancellationToken cancellationToken = default); } } diff --git a/src/MongODM.Core/Repositories/IRepository.cs b/src/MongODM.Core/Repositories/IRepository.cs index ebe27bfc..73fa5760 100644 --- a/src/MongODM.Core/Repositories/IRepository.cs +++ b/src/MongODM.Core/Repositories/IRepository.cs @@ -46,7 +46,7 @@ Task DeleteAsync( CancellationToken cancellationToken = default); /// - /// Find a model and don't throw exception if Id is not found + /// Try to find a model and don't throw exception if it is not found /// /// Model's Id /// The cancellation token From 2e233ae937282df61cd5e4b3d525c1c7efaef868 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 4 Jun 2020 18:03:55 +0200 Subject: [PATCH 42/46] Added package publish with github action workflow --- .github/workflows/build-test-package.yml | 34 +++++++++++++++++++ MongODM.sln | 12 +++++++ nuget.config | 7 ++++ src/ExecutionContext/ExecutionContext.csproj | 2 +- .../MongODM.AspNetCore.csproj | 2 +- src/MongODM.Core/MongODM.Core.csproj | 2 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/build-test-package.yml create mode 100644 nuget.config diff --git a/.github/workflows/build-test-package.yml b/.github/workflows/build-test-package.yml new file mode 100644 index 00000000..2cae3831 --- /dev/null +++ b/.github/workflows/build-test-package.yml @@ -0,0 +1,34 @@ +name: Build test and package .Net Core library - MongODM + +on: + push: + branches: [ master, dev ] + +jobs: + build-test-package: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + with: + fetch-depth: 0 + + - name: Setup .NET Core SDK + uses: actions/setup-dotnet@master + with: + dotnet-version: '3.1.x' + source-url: https://nuget.pkg.github.com/Etherna/index.json + env: + NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Build with dotnet + run: dotnet build --configuration Release + + - name: Run unit tests + run: dotnet test --configuration Release + + - name: Generate nuget package + run: dotnet pack --configuration Release -o nupkg + + - name: Push packages + run: dotnet nuget push './nupkg/*.nupkg' diff --git a/MongODM.sln b/MongODM.sln index 167d7cb2..f1f2e0fc 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -19,6 +19,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Core", "src\MongODM EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongODM.Core.Tests", "test\MongODM.Core.Tests\MongODM.Core.Tests.csproj", "{50D6BEE5-54B5-43F3-BA92-6107AB6E311E}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{03C64D98-FF9F-4760-AE82-203953FF4940}" + ProjectSection(SolutionItems) = preProject + .gitignore = .gitignore + nuget.config = nuget.config + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{D4BB5972-5F7B-43ED-81C1-69ECF0E62D1E}" + ProjectSection(SolutionItems) = preProject + .github\workflows\build-test-package.yml = .github\workflows\build-test-package.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -60,6 +71,7 @@ Global {6374F645-5D17-494E-9529-7F83426900B3} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {4F2498A9-D60D-4D49-95B9-BC78EE2917B5} = {490DAED7-DAD8-459A-A20E-F57F2F6F619E} {50D6BEE5-54B5-43F3-BA92-6107AB6E311E} = {CF1ABDEA-794F-4474-858D-BCB61F367D72} + {D4BB5972-5F7B-43ED-81C1-69ECF0E62D1E} = {03C64D98-FF9F-4760-AE82-203953FF4940} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {43A8089E-9445-4DE1-B509-F049E211B555} diff --git a/nuget.config b/nuget.config new file mode 100644 index 00000000..cc928f05 --- /dev/null +++ b/nuget.config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 843691ab..fd0b041d 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -4,11 +4,11 @@ netstandard2.0 true Etherna.ExecContext - true Etherna Sagl Execution context provider 8.0 enable + https://github.com/Etherna/mongodm diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 16bb295c..a56db6d8 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -4,11 +4,11 @@ netstandard2.0;netcoreapp3.0 true Etherna.MongODM.AspNetCore - true Etherna Sagl Asp.Net Core adapter for MongODM 8.0 enable + https://github.com/Etherna/mongodm diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 7580eb39..78504e73 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -4,11 +4,11 @@ netstandard2.0 true Etherna.MongODM - true Etherna Sagl ODM framework for MongoDB 8.0 enable + https://github.com/Etherna/mongodm diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 100d884e..abb1270c 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -4,11 +4,11 @@ netstandard2.0 true Etherna.MongODM.HF - true Etherna Sagl Linker for use MongoDB with Hangfire 8.0 enable + https://github.com/Etherna/mongodm From f0d580133e86bc0fe0ccf50cd0c9ddd058a2f82a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 5 Jun 2020 01:49:05 +0200 Subject: [PATCH 43/46] Removed DomainHelper dependency Updated external dependencies --- MongODM.sln | 1 - nuget.config | 7 - src/ExecutionContext/ExecutionContext.csproj | 2 +- .../MongODM.AspNetCore.csproj | 2 +- .../Attributes/PropertyAltererAttribute.cs | 19 ++ src/MongODM.Core/MongODM.Core.csproj | 8 +- .../ProxyModels/AuditableInterceptor.cs | 3 +- .../ProxyModels/ModelInterceptorBase.cs | 58 ++++ .../ProxyModels/ReferenceableInterceptor.cs | 4 +- src/MongODM.Core/ReflectionHelper.cs | 263 ++++++++++++++++++ .../Repositories/GridFSRepository.cs | 3 +- .../Repositories/RepositoryBase.cs | 3 +- .../Serializers/ExtendedClassMapSerializer.cs | 3 +- .../Serializers/GeoPointSerializer.cs | 7 +- .../Serializers/ReferenceSerializer.cs | 3 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- .../ExecutionContext.Tests.csproj | 2 +- .../ExtendedClassMapSerializerTest.cs | 3 +- .../MockHelpers/InterceptorMockHelper.cs | 3 +- .../MongODM.Core.Tests.csproj | 2 +- 20 files changed, 360 insertions(+), 38 deletions(-) delete mode 100644 nuget.config create mode 100644 src/MongODM.Core/Attributes/PropertyAltererAttribute.cs create mode 100644 src/MongODM.Core/ProxyModels/ModelInterceptorBase.cs create mode 100644 src/MongODM.Core/ReflectionHelper.cs diff --git a/MongODM.sln b/MongODM.sln index f1f2e0fc..7fa3b3a5 100644 --- a/MongODM.sln +++ b/MongODM.sln @@ -22,7 +22,6 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{03C64D98-FF9F-4760-AE82-203953FF4940}" ProjectSection(SolutionItems) = preProject .gitignore = .gitignore - nuget.config = nuget.config EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{D4BB5972-5F7B-43ED-81C1-69ECF0E62D1E}" diff --git a/nuget.config b/nuget.config deleted file mode 100644 index cc928f05..00000000 --- a/nuget.config +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index fd0b041d..36070483 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index a56db6d8..5ff5e399 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -26,7 +26,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Core/Attributes/PropertyAltererAttribute.cs b/src/MongODM.Core/Attributes/PropertyAltererAttribute.cs new file mode 100644 index 00000000..6059ab62 --- /dev/null +++ b/src/MongODM.Core/Attributes/PropertyAltererAttribute.cs @@ -0,0 +1,19 @@ +using System; + +namespace Etherna.MongODM.Attributes +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public class PropertyAltererAttribute : Attribute + { + /// + /// The constructor + /// + /// The related property name + public PropertyAltererAttribute(string propertyName) + { + PropertyName = propertyName; + } + + public string PropertyName { get; } + } +} diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 78504e73..a3b6d33d 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -16,13 +16,13 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs index 358676c8..eaad5579 100644 --- a/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/AuditableInterceptor.cs @@ -1,6 +1,5 @@ using Castle.DynamicProxy; -using Digicando.DomainHelper.Attributes; -using Digicando.DomainHelper.ProxyModel; +using Etherna.MongODM.Attributes; using System; using System.Collections.Generic; using System.Linq; diff --git a/src/MongODM.Core/ProxyModels/ModelInterceptorBase.cs b/src/MongODM.Core/ProxyModels/ModelInterceptorBase.cs new file mode 100644 index 00000000..5f6232fe --- /dev/null +++ b/src/MongODM.Core/ProxyModels/ModelInterceptorBase.cs @@ -0,0 +1,58 @@ +using Castle.DynamicProxy; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Etherna.MongODM.ProxyModels +{ + public abstract class ModelInterceptorBase : IInterceptor + { + private readonly IEnumerable additionalInterfaces; + + public ModelInterceptorBase(IEnumerable additionalInterfaces) + { + this.additionalInterfaces = additionalInterfaces; + } + + public void Intercept(IInvocation invocation) + { + if (additionalInterfaces.Contains(invocation.Method.DeclaringType)) + { + var handled = InterceptInterface(invocation); + if (handled) + return; + invocation.Proceed(); + } + else + { + // Check model type. + if (invocation.Method.DeclaringType != typeof(TModel) && + !invocation.Method.DeclaringType.IsAssignableFrom(typeof(TModel))) + { + throw new InvalidOperationException(); + } + + InterceptModel(invocation); + } + } + + /// + /// Intercept an extra interface + /// + /// Current invocation + /// Returns true if the call handled the invocation + protected virtual bool InterceptInterface(IInvocation invocation) + { + return false; + } + + /// + /// Intercept a call to the model + /// + /// Current invocation + protected virtual void InterceptModel(IInvocation invocation) + { + invocation.Proceed(); + } + } +} diff --git a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs index 951afa28..3cf8425e 100644 --- a/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs +++ b/src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs @@ -1,7 +1,5 @@ using Castle.DynamicProxy; -using Digicando.DomainHelper; -using Digicando.DomainHelper.Attributes; -using Digicando.DomainHelper.ProxyModel; +using Etherna.MongODM.Attributes; using Etherna.MongODM.Models; using Etherna.MongODM.Repositories; using System; diff --git a/src/MongODM.Core/ReflectionHelper.cs b/src/MongODM.Core/ReflectionHelper.cs new file mode 100644 index 00000000..887881bd --- /dev/null +++ b/src/MongODM.Core/ReflectionHelper.cs @@ -0,0 +1,263 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Threading; + +namespace Etherna.MongODM +{ + public static class ReflectionHelper + { + private static readonly Dictionary> propertyRegister = new Dictionary>(); + private static readonly ReaderWriterLockSlim propertyRegisterLock = new ReaderWriterLockSlim(); + + public static TClass CloneModel(TClass srcObj, params Expression>[] memberLambdas) + where TClass : new() + { + var destObj = new TClass(); + CloneModel(srcObj, destObj, memberLambdas); + return destObj; + } + + public static void CloneModel(TClass srcObj, TClass destObj, params Expression>[] memberLambdas) + { + if (srcObj is null) + throw new ArgumentNullException(nameof(srcObj)); + if (destObj is null) + throw new ArgumentNullException(nameof(destObj)); + + IEnumerable membersToClone; + + if (memberLambdas.Any()) + { + membersToClone = memberLambdas.Select(l => GetMemberInfoFromLambda(l, typeof(TClass))); + } + else // clone full object + { + membersToClone = GetWritableInstanceProperties(typeof(TClass)); + } + + foreach (var member in membersToClone) + { + SetValue(destObj, member, GetValue(srcObj, member)); + } + } + + public static void CloneModel(object srcObj, object destObj, Type actualType) => + CloneModel(srcObj, destObj, GetWritableInstanceProperties(actualType)); + + public static void CloneModel(object srcObj, object destObj, IEnumerable members) + { + foreach (var member in members) + { + SetValue(destObj, member, GetValue(srcObj, member)); + } + } + + public static MemberInfo FindProperty(LambdaExpression lambdaExpression) + { + Expression expressionToCheck = lambdaExpression; + + bool done = false; + + while (!done) + { + switch (expressionToCheck.NodeType) + { + case ExpressionType.Convert: + expressionToCheck = ((UnaryExpression)expressionToCheck).Operand; + break; + case ExpressionType.Lambda: + expressionToCheck = ((LambdaExpression)expressionToCheck).Body; + break; + case ExpressionType.MemberAccess: + var memberExpression = ((MemberExpression)expressionToCheck); + + if (memberExpression.Expression.NodeType != ExpressionType.Parameter && + memberExpression.Expression.NodeType != ExpressionType.Convert) + { + throw new ArgumentException( + $"Expression '{lambdaExpression}' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.", + nameof(lambdaExpression)); + } + + MemberInfo member = memberExpression.Member; + + return member; + default: + done = true; + break; + } + } + + throw new InvalidOperationException(); + } + + public static PropertyInfo FindPropertyImplementation(PropertyInfo interfacePropertyInfo, Type actualType) + { + var interfaceType = interfacePropertyInfo.DeclaringType; + + // An interface map must be used because because there is no + // other officially documented way to derive the explicitly + // implemented property name. + var interfaceMap = actualType.GetInterfaceMap(interfaceType); + + var interfacePropertyAccessors = interfacePropertyInfo.GetAccessors(true); + + var actualPropertyAccessors = interfacePropertyAccessors.Select(interfacePropertyAccessor => + { + var index = Array.IndexOf(interfaceMap.InterfaceMethods, interfacePropertyAccessor); + + return interfaceMap.TargetMethods[index]; + }); + + // Binding must be done by accessor methods because interface + // maps only map accessor methods and do not map properties. + return actualType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) + .Single(propertyInfo => + { + // we are looking for a property that implements all the required accessors + var propertyAccessors = propertyInfo.GetAccessors(true); + return actualPropertyAccessors.All(x => propertyAccessors.Contains(x)); + }); + } + + public static object? GetDefaultValue(Type type) + { + if (type.IsValueType) + { + return Activator.CreateInstance(type); + } + return null; + } + + public static MemberInfo GetMemberInfoFromLambda( + Expression> memberLambda, + Type? actualType = null) + { + var body = memberLambda.Body; + MemberExpression memberExpression; + switch (body.NodeType) + { + case ExpressionType.MemberAccess: + memberExpression = (MemberExpression)body; + break; + case ExpressionType.Convert: + var convertExpression = (UnaryExpression)body; + memberExpression = (MemberExpression)convertExpression.Operand; + break; + default: + throw new InvalidOperationException("Invalid lambda expression"); + } + var memberInfo = memberExpression.Member; + switch (memberInfo.MemberType) + { + case MemberTypes.Field: + break; + case MemberTypes.Property: + if (actualType?.IsInterface == false && + memberInfo.DeclaringType.IsInterface) + { + memberInfo = FindPropertyImplementation((PropertyInfo)memberInfo, actualType); + } + break; + default: + memberInfo = null; + break; + } + if (memberInfo == null) + { + throw new InvalidOperationException("Invalid lambda expression"); + } + return memberInfo; + } + + public static object? GetValue(object source, MemberInfo memberInfo) + { + if (memberInfo is FieldInfo fieldInfo) + return fieldInfo.GetValue(source); + + if (memberInfo is PropertyInfo propertyInfo && propertyInfo.CanRead) + return propertyInfo.GetValue(source); + + return null; + } + + public static TMember GetValueFromLambda(TModel source, Expression> memberLambda) + { + if (source is null) + throw new ArgumentNullException(nameof(source)); + + var memberInfo = GetMemberInfoFromLambda(memberLambda, source.GetType()); + return (TMember)GetValue(source, memberInfo)!; + } + + /// + /// Return the list of writable instance property of a type + /// + /// The model type + /// The list of properties + public static IEnumerable GetWritableInstanceProperties(Type objectType) + { + propertyRegisterLock.EnterReadLock(); + try + { + if (propertyRegister.ContainsKey(objectType)) + { + return propertyRegister[objectType]; + } + } + finally + { + propertyRegisterLock.ExitReadLock(); + } + + propertyRegisterLock.EnterWriteLock(); + try + { + if (!propertyRegister.ContainsKey(objectType)) + { + var typeStack = new List(); + var stackType = objectType; + do + { + typeStack.Add(stackType); + stackType = stackType.BaseType; + } while (stackType != null); + + propertyRegister.Add(objectType, typeStack + .SelectMany(type => type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) + .Where(prop => prop.CanWrite)); + } + return propertyRegister[objectType]; + } + finally + { + propertyRegisterLock.ExitWriteLock(); + } + } + + public static void SetValue(object destination, MemberInfo memberInfo, object? value) + { + if (memberInfo is FieldInfo fieldInfo) + { + if (!fieldInfo.IsInitOnly) + fieldInfo.SetValue(destination, value); + return; + } + + if (memberInfo is PropertyInfo propertyInfo && propertyInfo.CanWrite) + propertyInfo.SetValue(destination, value); + } + + public static void SetValue(TModel destination, Expression> memberLambda, TMember value) + { + if (destination is null) + throw new ArgumentNullException(nameof(destination)); + + var memberInfo = GetMemberInfoFromLambda(memberLambda, destination.GetType()); + SetValue(destination, memberInfo, value); + } + } +} diff --git a/src/MongODM.Core/Repositories/GridFSRepository.cs b/src/MongODM.Core/Repositories/GridFSRepository.cs index 518e5b55..57fb69fd 100644 --- a/src/MongODM.Core/Repositories/GridFSRepository.cs +++ b/src/MongODM.Core/Repositories/GridFSRepository.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.Exceptions; +using Etherna.MongODM.Exceptions; using Etherna.MongODM.Models; using Etherna.MongODM.Serialization; using MongoDB.Bson; diff --git a/src/MongODM.Core/Repositories/RepositoryBase.cs b/src/MongODM.Core/Repositories/RepositoryBase.cs index e37e57c4..3d98ee07 100644 --- a/src/MongODM.Core/Repositories/RepositoryBase.cs +++ b/src/MongODM.Core/Repositories/RepositoryBase.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.Exceptions; +using Etherna.MongODM.Exceptions; using Etherna.MongODM.Models; using Etherna.MongODM.ProxyModels; using Etherna.MongODM.Serialization; diff --git a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs index ec1eb85c..a8266eca 100644 --- a/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ExtendedClassMapSerializer.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.Models; +using Etherna.MongODM.Models; using Etherna.MongODM.ProxyModels; using Etherna.MongODM.Serialization.Modifiers; using Etherna.MongODM.Utility; diff --git a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs index 326f335e..0b117bb6 100644 --- a/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/GeoPointSerializer.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.ProxyModels; +using Etherna.MongODM.ProxyModels; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.GeoJsonObjectModel; @@ -64,8 +63,8 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati // Create point. var coordinate = new GeoJson2DGeographicCoordinates( - (double)ReflectionHelper.GetValue(value, longitudeMemberInfo), - (double)ReflectionHelper.GetValue(value, latitudeMemberInfo)); + (double)ReflectionHelper.GetValue(value, longitudeMemberInfo)!, + (double)ReflectionHelper.GetValue(value, latitudeMemberInfo)!); var point = new GeoJsonPoint(coordinate); // Serialize point. diff --git a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs index 3890519d..eecdcf27 100644 --- a/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs +++ b/src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.Models; +using Etherna.MongODM.Models; using Etherna.MongODM.ProxyModels; using Etherna.MongODM.Serialization.Modifiers; using Etherna.MongODM.Utility; diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index abb1270c..bc2cc591 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index 8b89265f..f070e0bc 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs index 59ba262f..ffff574b 100644 --- a/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs +++ b/test/MongODM.Core.Tests/ExtendedClassMapSerializerTest.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Etherna.MongODM.Comparers; +using Etherna.MongODM.Comparers; using Etherna.MongODM.Models; using Etherna.MongODM.Serialization; using Etherna.MongODM.Serialization.Modifiers; diff --git a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs index 9fe21968..4dba6db5 100644 --- a/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs +++ b/test/MongODM.Core.Tests/MockHelpers/InterceptorMockHelper.cs @@ -1,5 +1,4 @@ -using Digicando.DomainHelper; -using Moq; +using Moq; using System; using System.Linq.Expressions; diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index 047950dc..fc9408f4 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -12,7 +12,7 @@ - + all runtime; build; native; contentfiles; analyzers From ca043f207d0d494b4715590442b2f8c15cb6c8b0 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 25 Jun 2020 15:20:31 +0200 Subject: [PATCH 44/46] Updated dependencies --- src/ExecutionContext/ExecutionContext.csproj | 2 +- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 2 +- src/MongODM.Core/MongODM.Core.csproj | 2 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 4 ++-- test/ExecutionContext.Tests/ExecutionContext.Tests.csproj | 2 +- test/MongODM.Core.Tests/MongODM.Core.Tests.csproj | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 36070483..0e09c4d4 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 5ff5e399..12f68f51 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -26,7 +26,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index a3b6d33d..8d7908c3 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index bc2cc591..4e044aed 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -16,11 +16,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index f070e0bc..de29ac62 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -10,7 +10,7 @@ - + all diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index fc9408f4..d43964eb 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -10,7 +10,7 @@ - + all From 7a368283bc462e9f5f2894942ab0e10849ed3715 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 28 Jun 2020 01:06:14 +0200 Subject: [PATCH 45/46] Updated package repository to MyGet --- .github/workflows/build-test-package.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/build-test-package.yml b/.github/workflows/build-test-package.yml index 2cae3831..55bab03c 100644 --- a/.github/workflows/build-test-package.yml +++ b/.github/workflows/build-test-package.yml @@ -17,9 +17,6 @@ jobs: uses: actions/setup-dotnet@master with: dotnet-version: '3.1.x' - source-url: https://nuget.pkg.github.com/Etherna/index.json - env: - NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} - name: Build with dotnet run: dotnet build --configuration Release @@ -31,4 +28,4 @@ jobs: run: dotnet pack --configuration Release -o nupkg - name: Push packages - run: dotnet nuget push './nupkg/*.nupkg' + run: dotnet nuget push './nupkg/*.nupkg' --api-key ${{secrets.MYGET_APIKEY}} --source https://www.myget.org/F/etherna/api/v3/index.json From 86fa78983eb82aa709817643c5b62ca9f5697fb6 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 3 Jul 2020 03:57:59 +0200 Subject: [PATCH 46/46] Updated dependencies --- src/ExecutionContext/ExecutionContext.csproj | 2 +- src/MongODM.AspNetCore/MongODM.AspNetCore.csproj | 2 +- src/MongODM.Core/MongODM.Core.csproj | 2 +- src/MongODM.Hangfire/MongODM.Hangfire.csproj | 2 +- test/ExecutionContext.Tests/ExecutionContext.Tests.csproj | 2 +- test/MongODM.Core.Tests/MongODM.Core.Tests.csproj | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ExecutionContext/ExecutionContext.csproj b/src/ExecutionContext/ExecutionContext.csproj index 0e09c4d4..28193eb4 100644 --- a/src/ExecutionContext/ExecutionContext.csproj +++ b/src/ExecutionContext/ExecutionContext.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj index 12f68f51..7713d0d5 100644 --- a/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj +++ b/src/MongODM.AspNetCore/MongODM.AspNetCore.csproj @@ -26,7 +26,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Core/MongODM.Core.csproj b/src/MongODM.Core/MongODM.Core.csproj index 8d7908c3..b65bd547 100644 --- a/src/MongODM.Core/MongODM.Core.csproj +++ b/src/MongODM.Core/MongODM.Core.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/MongODM.Hangfire/MongODM.Hangfire.csproj b/src/MongODM.Hangfire/MongODM.Hangfire.csproj index 4e044aed..6d5b2e0c 100644 --- a/src/MongODM.Hangfire/MongODM.Hangfire.csproj +++ b/src/MongODM.Hangfire/MongODM.Hangfire.csproj @@ -16,7 +16,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj index de29ac62..43db5151 100644 --- a/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj +++ b/test/ExecutionContext.Tests/ExecutionContext.Tests.csproj @@ -10,7 +10,7 @@ - + all diff --git a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj index d43964eb..8383895c 100644 --- a/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj +++ b/test/MongODM.Core.Tests/MongODM.Core.Tests.csproj @@ -10,7 +10,7 @@ - + all