From bd4678e3756efb7fcd26c279ca8c097fdcae504f Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Thu, 18 Jul 2024 11:44:43 +0800 Subject: [PATCH 01/17] add command hook --- .../Commands/DocumentCommand.cs | 0 .../Commands/IDocumentCommandBuilder.cs | 9 +++++++++ .../Commands/IIndexCommand.cs | 0 src/YesSql.Abstractions/IConfiguration.cs | 2 ++ .../Commands/DefaultDocumentCommandBuilder.cs | 20 +++++++++++++++++++ src/YesSql.Core/Configuration.cs | 3 +++ src/YesSql.Core/Session.cs | 6 +++--- 7 files changed, 37 insertions(+), 3 deletions(-) rename src/{YesSql.Core => YesSql.Abstractions}/Commands/DocumentCommand.cs (100%) create mode 100644 src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs rename src/{YesSql.Core => YesSql.Abstractions}/Commands/IIndexCommand.cs (100%) create mode 100644 src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs diff --git a/src/YesSql.Core/Commands/DocumentCommand.cs b/src/YesSql.Abstractions/Commands/DocumentCommand.cs similarity index 100% rename from src/YesSql.Core/Commands/DocumentCommand.cs rename to src/YesSql.Abstractions/Commands/DocumentCommand.cs diff --git a/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs b/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs new file mode 100644 index 00000000..43802163 --- /dev/null +++ b/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs @@ -0,0 +1,9 @@ +namespace YesSql.Commands +{ + public interface IDocumentCommandBuilder + { + DocumentCommand BuildCreateDocumentCommand(object entity, Document document, IStore store, string collection); + DocumentCommand BuildUpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection); + DocumentCommand BuildDeleteDocumentCommand(object entity, Document document, IStore store, string collection); + } +} diff --git a/src/YesSql.Core/Commands/IIndexCommand.cs b/src/YesSql.Abstractions/Commands/IIndexCommand.cs similarity index 100% rename from src/YesSql.Core/Commands/IIndexCommand.cs rename to src/YesSql.Abstractions/Commands/IIndexCommand.cs diff --git a/src/YesSql.Abstractions/IConfiguration.cs b/src/YesSql.Abstractions/IConfiguration.cs index 423be7a4..46e493e8 100644 --- a/src/YesSql.Abstractions/IConfiguration.cs +++ b/src/YesSql.Abstractions/IConfiguration.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Data; +using YesSql.Commands; namespace YesSql { @@ -90,6 +91,7 @@ public interface IConfiguration /// Gets or sets the identity column size. Default is . /// IdentityColumnSize IdentityColumnSize { get; set; } + IDocumentCommandBuilder DocumentCommandBuilder { get; set; } } public static class ConfigurationExtensions diff --git a/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs b/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs new file mode 100644 index 00000000..dd3dfa45 --- /dev/null +++ b/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs @@ -0,0 +1,20 @@ +using System.Reflection.Metadata; + +namespace YesSql.Commands +{ + public class DefaultDocumentCommandBuilder : IDocumentCommandBuilder + { + public DocumentCommand BuildCreateDocumentCommand(object entity, Document document, IStore store, string collection) + { + return new CreateDocumentCommand(document, store, collection); + } + public DocumentCommand BuildUpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection) + { + return new UpdateDocumentCommand(document, store, checkVersion, collection); + } + public DocumentCommand BuildDeleteDocumentCommand(object entity, Document document, IStore store, string collection) + { + return new DeleteDocumentCommand(document, store, collection); + } + } +} diff --git a/src/YesSql.Core/Configuration.cs b/src/YesSql.Core/Configuration.cs index 4bac7823..140f0e41 100644 --- a/src/YesSql.Core/Configuration.cs +++ b/src/YesSql.Core/Configuration.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Data; +using YesSql.Commands; using YesSql.Data; using YesSql.Serialization; using YesSql.Services; @@ -24,6 +25,7 @@ public Configuration() Logger = NullLogger.Instance; ConcurrentTypes = new HashSet(); TableNameConvention = new DefaultTableNameConvention(); + DocumentCommandBuilder = new DefaultDocumentCommandBuilder(); } public IAccessorFactory IdentifierAccessorFactory { get; set; } @@ -42,5 +44,6 @@ public Configuration() public ICommandInterpreter CommandInterpreter { get; set; } public ISqlDialect SqlDialect { get; set; } public IdentityColumnSize IdentityColumnSize { get; set; } = IdentityColumnSize.Int32; + public IDocumentCommandBuilder DocumentCommandBuilder { get; set; } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 44059c4f..a21ce2b3 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -286,7 +286,7 @@ private async Task SaveEntityAsync(object entity, string collection) _commands ??= new List(); - _commands.Add(new CreateDocumentCommand(doc, Store, collection)); + _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildCreateDocumentCommand(entity, doc, Store, collection)); state.IdentityMap.AddDocument(doc); @@ -381,7 +381,7 @@ private async Task UpdateEntityAsync(object entity, bool tracked, string collect _commands ??= new List(); - _commands.Add(new UpdateDocumentCommand(oldDoc, Store, version, collection)); + _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildUpdateDocumentCommand(entity, oldDoc, Store, version, collection)); } private async Task GetDocumentByIdAsync(long id, string collection) @@ -466,7 +466,7 @@ private async Task DeleteEntityAsync(object obj, string collection) _commands ??= new List(); // The command needs to come after any index deletion because of the database constraints - _commands.Add(new DeleteDocumentCommand(doc, Store, collection)); + _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildDeleteDocumentCommand(obj, doc, Store, collection)); } } From 77548d7c962052a5133738fb10cb5a4d046012ce Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Thu, 18 Jul 2024 11:49:06 +0800 Subject: [PATCH 02/17] clear using --- src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs b/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs index dd3dfa45..a39962e9 100644 --- a/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs +++ b/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs @@ -1,5 +1,3 @@ -using System.Reflection.Metadata; - namespace YesSql.Commands { public class DefaultDocumentCommandBuilder : IDocumentCommandBuilder From 75efaa7178b45b0e5040150ecde17d1946424d29 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Thu, 18 Jul 2024 13:59:46 +0800 Subject: [PATCH 03/17] remove sealed from DocumentCommand --- src/YesSql.Core/Commands/CreateDocumentCommand.cs | 2 +- src/YesSql.Core/Commands/DeleteDocumentCommand.cs | 6 +++--- src/YesSql.Core/Commands/UpdateDocumentCommand.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/YesSql.Core/Commands/CreateDocumentCommand.cs b/src/YesSql.Core/Commands/CreateDocumentCommand.cs index ff0fd632..45444d0c 100644 --- a/src/YesSql.Core/Commands/CreateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/CreateDocumentCommand.cs @@ -7,7 +7,7 @@ namespace YesSql.Commands { - public sealed class CreateDocumentCommand : DocumentCommand + public class CreateDocumentCommand : DocumentCommand { private readonly IStore _store; diff --git a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs index 7ca257ae..c0184378 100644 --- a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs +++ b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs @@ -7,7 +7,7 @@ namespace YesSql.Commands { - public sealed class DeleteDocumentCommand : DocumentCommand + public class DeleteDocumentCommand : DocumentCommand { private readonly IStore _store; public override int ExecutionOrder { get; } = 4; @@ -21,12 +21,12 @@ public override Task ExecuteAsync(DbConnection connection, DbTransaction transac { var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id;"; - + if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace(deleteCmd); } - + return connection.ExecuteAsync(deleteCmd, Document, transaction); } diff --git a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs index 7b5f15a8..1aa007c1 100644 --- a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs @@ -7,7 +7,7 @@ namespace YesSql.Commands { - public sealed class UpdateDocumentCommand : DocumentCommand + public class UpdateDocumentCommand : DocumentCommand { private readonly IStore _store; private readonly long _checkVersion; From a4792d72490446a7919955027483defc1670d06e Mon Sep 17 00:00:00 2001 From: Tony Han Date: Sun, 21 Jul 2024 18:05:41 +0800 Subject: [PATCH 04/17] change IDocumentCommandBuilder to IDocumentCommandHandler --- .../DocumentChanged/DocumentChangeContext.cs | 15 +++++++++ .../DocumentChangedInBatchContext.cs | 14 ++++++++ .../Commands/DocumentCommand.cs | 12 +++---- .../Commands/IDocumentCommandBuilder.cs | 9 ----- .../Commands/IDocumentCommandHandler.cs | 16 +++++++++ src/YesSql.Abstractions/IConfiguration.cs | 1 - src/YesSql.Abstractions/ISession.cs | 2 ++ .../Commands/CreateDocumentCommand.cs | 33 ++++++++++++++++--- .../Commands/DefaultDocumentCommandBuilder.cs | 18 ---------- .../Commands/DefaultDocumentCommandHandler.cs | 20 +++++++++++ .../Commands/DeleteDocumentCommand.cs | 33 +++++++++++++++++-- .../Commands/UpdateDocumentCommand.cs | 30 +++++++++++++++-- src/YesSql.Core/Configuration.cs | 2 -- src/YesSql.Core/Session.cs | 10 ++++-- 14 files changed, 166 insertions(+), 49 deletions(-) create mode 100644 src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs create mode 100644 src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs delete mode 100644 src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs create mode 100644 src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs delete mode 100644 src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs create mode 100644 src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs diff --git a/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs new file mode 100644 index 00000000..ac6ce03c --- /dev/null +++ b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs @@ -0,0 +1,15 @@ +using System.Data.Common; + +namespace YesSql.Commands.DocumentChanged +{ + public class DocumentChangeContext + { + public ISession Session { get; set; } + public object Entity { get; set; } + public Document Document { get; set; } + public IStore Store { get; set; } + public DbConnection Connection { get; set; } + public DbTransaction Transaction { get; set; } + public ISqlDialect Dialect { get; set; } + } +} diff --git a/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs new file mode 100644 index 00000000..1982ce73 --- /dev/null +++ b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Data.Common; + +namespace YesSql.Commands.DocumentChanged +{ + public class DocumentChangeInBatchContext + { + public object Entity { get; set; } + public List Queries { get; set; } + public DbCommand BatchCommand { get; set; } + public ISession Session { get; set; } + public Document Document { get; set; } + } +} diff --git a/src/YesSql.Abstractions/Commands/DocumentCommand.cs b/src/YesSql.Abstractions/Commands/DocumentCommand.cs index cc84336d..b1211fde 100644 --- a/src/YesSql.Abstractions/Commands/DocumentCommand.cs +++ b/src/YesSql.Abstractions/Commands/DocumentCommand.cs @@ -9,15 +9,15 @@ namespace YesSql.Commands { public abstract class DocumentCommand : IIndexCommand, ICollectionName { - protected static readonly PropertyInfo[] AllProperties = new PropertyInfo[] - { + protected static readonly PropertyInfo[] AllProperties = + [ typeof(Document).GetProperty("Type") - }; + ]; - protected static readonly PropertyInfo[] AllKeys = new PropertyInfo[] - { + protected static readonly PropertyInfo[] AllKeys = + [ typeof(Document).GetProperty("Id") - }; + ]; public abstract int ExecutionOrder { get; } diff --git a/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs b/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs deleted file mode 100644 index 43802163..00000000 --- a/src/YesSql.Abstractions/Commands/IDocumentCommandBuilder.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace YesSql.Commands -{ - public interface IDocumentCommandBuilder - { - DocumentCommand BuildCreateDocumentCommand(object entity, Document document, IStore store, string collection); - DocumentCommand BuildUpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection); - DocumentCommand BuildDeleteDocumentCommand(object entity, Document document, IStore store, string collection); - } -} diff --git a/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs b/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs new file mode 100644 index 00000000..54bd56b6 --- /dev/null +++ b/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; + +namespace YesSql.Commands +{ + public interface IDocumentCommandHandler + { + Task CreatedAsync(DocumentChangeContext context); + bool CreatedInBatch(DocumentChangeInBatchContext context); + + Task RemovingAsync(DocumentChangeContext context); + bool RemovingInBatch(DocumentChangeInBatchContext context); + Task UpdatedAsync(DocumentChangeContext context); + bool UpdatedInBatch(DocumentChangeInBatchContext context); + } +} diff --git a/src/YesSql.Abstractions/IConfiguration.cs b/src/YesSql.Abstractions/IConfiguration.cs index 46e493e8..880a87d2 100644 --- a/src/YesSql.Abstractions/IConfiguration.cs +++ b/src/YesSql.Abstractions/IConfiguration.cs @@ -91,7 +91,6 @@ public interface IConfiguration /// Gets or sets the identity column size. Default is . /// IdentityColumnSize IdentityColumnSize { get; set; } - IDocumentCommandBuilder DocumentCommandBuilder { get; set; } } public static class ConfigurationExtensions diff --git a/src/YesSql.Abstractions/ISession.cs b/src/YesSql.Abstractions/ISession.cs index 3ce0369d..c1834059 100644 --- a/src/YesSql.Abstractions/ISession.cs +++ b/src/YesSql.Abstractions/ISession.cs @@ -3,6 +3,7 @@ using System.Data; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands; using YesSql.Indexes; namespace YesSql @@ -137,5 +138,6 @@ public interface ISession : IDisposable, IAsyncDisposable /// Gets the instance that created this session. /// IStore Store { get; } + IDocumentCommandHandler DocumentCommandHandler { get; set; } } } diff --git a/src/YesSql.Core/Commands/CreateDocumentCommand.cs b/src/YesSql.Core/Commands/CreateDocumentCommand.cs index 45444d0c..92940164 100644 --- a/src/YesSql.Core/Commands/CreateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/CreateDocumentCommand.cs @@ -4,21 +4,25 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { public class CreateDocumentCommand : DocumentCommand { private readonly IStore _store; - + private readonly ISession _session; + private readonly object _entity; public override int ExecutionOrder { get; } = 0; - public CreateDocumentCommand(Document document, IStore store, string collection) : base(document, collection) + public CreateDocumentCommand(object entity, Document document, IStore store, string collection, ISession session) : base(document, collection) { _store = store; + _session = session; + _entity = entity; } - public override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) + public override async Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) { var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); @@ -28,8 +32,19 @@ public override Task ExecuteAsync(DbConnection connection, DbTransaction transac { logger.LogTrace(insertCmd); } + await connection.ExecuteAsync(insertCmd, Document, transaction); - return connection.ExecuteAsync(insertCmd, Document, transaction); + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.CreatedAsync(context); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index) @@ -45,6 +60,16 @@ public override bool AddToBatch(ISqlDialect dialect, List queries, DbCom .AddParameter("Content_" + index, Document.Content) .AddParameter("Version_" + index, Document.Version); + var context = new DocumentChangeInBatchContext + { + Session = _session, + Document = Document, + Entity = _entity, + BatchCommand = batchCommand, + Queries = queries, + }; + _session.DocumentCommandHandler.CreatedInBatch(context); + return true; } } diff --git a/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs b/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs deleted file mode 100644 index a39962e9..00000000 --- a/src/YesSql.Core/Commands/DefaultDocumentCommandBuilder.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace YesSql.Commands -{ - public class DefaultDocumentCommandBuilder : IDocumentCommandBuilder - { - public DocumentCommand BuildCreateDocumentCommand(object entity, Document document, IStore store, string collection) - { - return new CreateDocumentCommand(document, store, collection); - } - public DocumentCommand BuildUpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection) - { - return new UpdateDocumentCommand(document, store, checkVersion, collection); - } - public DocumentCommand BuildDeleteDocumentCommand(object entity, Document document, IStore store, string collection) - { - return new DeleteDocumentCommand(document, store, collection); - } - } -} diff --git a/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs b/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs new file mode 100644 index 00000000..9666f3d1 --- /dev/null +++ b/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; + +namespace YesSql.Commands +{ + public class DefaultDocumentCommandHandler : IDocumentCommandHandler + { + public Task CreatedAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool CreatedInBatch(DocumentChangeInBatchContext context) => true; + + public Task RemovingAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool RemovingInBatch(DocumentChangeInBatchContext context) => true; + + public Task UpdatedAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool UpdatedInBatch(DocumentChangeInBatchContext context) => true; + } +} diff --git a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs index c0184378..bf52bf3f 100644 --- a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs +++ b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs @@ -4,21 +4,38 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { public class DeleteDocumentCommand : DocumentCommand { private readonly IStore _store; + private readonly ISession _session; + private readonly object _entity; public override int ExecutionOrder { get; } = 4; - public DeleteDocumentCommand(Document document, IStore store, string collection) : base(document, collection) + public DeleteDocumentCommand(object entity, Document document, IStore store, string collection, Session session) : base(document, collection) { _store = store; + _session = session; + _entity = entity; } - public override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) + public async override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) { + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.RemovingAsync(context); + var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id;"; @@ -27,11 +44,21 @@ public override Task ExecuteAsync(DbConnection connection, DbTransaction transac logger.LogTrace(deleteCmd); } - return connection.ExecuteAsync(deleteCmd, Document, transaction); + await connection.ExecuteAsync(deleteCmd, Document, transaction); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand command, List> actions, int index) { + var context = new DocumentChangeInBatchContext + { + Session = _session, + Document = Document, + Entity = _entity, + BatchCommand = command, + Queries = queries, + }; + _session.DocumentCommandHandler.RemovingInBatch(context); + var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id_{index};"; diff --git a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs index 1aa007c1..7b4d5c08 100644 --- a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs @@ -4,20 +4,24 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { public class UpdateDocumentCommand : DocumentCommand { private readonly IStore _store; + private readonly ISession _session; + private readonly object _entity; private readonly long _checkVersion; - public override int ExecutionOrder { get; } = 2; - public UpdateDocumentCommand(Document document, IStore store, long checkVersion, string collection) : base(document, collection) + public UpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection, Session session) : base(document, collection) { _store = store; _checkVersion = checkVersion; + _session = session; + _entity = entity; } public override async Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) @@ -46,7 +50,17 @@ public override async Task ExecuteAsync(DbConnection connection, DbTransaction t throw new ConcurrencyException(); } - return; + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.UpdatedAsync(context); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index) @@ -75,6 +89,16 @@ public override bool AddToBatch(ISqlDialect dialect, List queries, DbCom .AddParameter("Content_" + index, Document.Content) .AddParameter("Version_" + index, Document.Version); + var context = new DocumentChangeInBatchContext + { + Session = _session, + Entity = _entity, + Document = Document, + BatchCommand = batchCommand, + Queries = queries, + }; + _session.DocumentCommandHandler.UpdatedInBatch(context); + return true; } } diff --git a/src/YesSql.Core/Configuration.cs b/src/YesSql.Core/Configuration.cs index 140f0e41..89e13e96 100644 --- a/src/YesSql.Core/Configuration.cs +++ b/src/YesSql.Core/Configuration.cs @@ -25,7 +25,6 @@ public Configuration() Logger = NullLogger.Instance; ConcurrentTypes = new HashSet(); TableNameConvention = new DefaultTableNameConvention(); - DocumentCommandBuilder = new DefaultDocumentCommandBuilder(); } public IAccessorFactory IdentifierAccessorFactory { get; set; } @@ -44,6 +43,5 @@ public Configuration() public ICommandInterpreter CommandInterpreter { get; set; } public ISqlDialect SqlDialect { get; set; } public IdentityColumnSize IdentityColumnSize { get; set; } = IdentityColumnSize.Int32; - public IDocumentCommandBuilder DocumentCommandBuilder { get; set; } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index a21ce2b3..77ba9d4a 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -11,6 +11,7 @@ using YesSql.Data; using YesSql.Indexes; using YesSql.Services; +using static Dapper.SqlMapper; namespace YesSql { @@ -46,8 +47,11 @@ public Session(Store store) { [""] = _defaultState }; + DocumentCommandHandler = new DefaultDocumentCommandHandler(); } + public IDocumentCommandHandler DocumentCommandHandler { get; set; } + public ISession RegisterIndexes(IIndexProvider[] indexProviders, string collection = null) { foreach (var indexProvider in indexProviders) @@ -286,7 +290,7 @@ private async Task SaveEntityAsync(object entity, string collection) _commands ??= new List(); - _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildCreateDocumentCommand(entity, doc, Store, collection)); + _commands.Add(new CreateDocumentCommand(entity,doc, Store, collection, this)); state.IdentityMap.AddDocument(doc); @@ -381,7 +385,7 @@ private async Task UpdateEntityAsync(object entity, bool tracked, string collect _commands ??= new List(); - _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildUpdateDocumentCommand(entity, oldDoc, Store, version, collection)); + _commands.Add(new UpdateDocumentCommand(entity, oldDoc, Store, version, collection, this)); } private async Task GetDocumentByIdAsync(long id, string collection) @@ -466,7 +470,7 @@ private async Task DeleteEntityAsync(object obj, string collection) _commands ??= new List(); // The command needs to come after any index deletion because of the database constraints - _commands.Add(Store.Configuration.DocumentCommandBuilder.BuildDeleteDocumentCommand(obj, doc, Store, collection)); + _commands.Add(new DeleteDocumentCommand(obj, doc, Store, collection, this)); } } From f1b8bee63d265c0f16ac96546ea827226099cca2 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Sun, 21 Jul 2024 19:52:47 +0800 Subject: [PATCH 05/17] add ExtraIndexDescriptors --- src/YesSql.Abstractions/ISession.cs | 2 + src/YesSql.Core/Session.cs | 4 +- test/YesSql.Tests/SqliteTests.cs | 71 +++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/YesSql.Abstractions/ISession.cs b/src/YesSql.Abstractions/ISession.cs index 3ce0369d..702346a8 100644 --- a/src/YesSql.Abstractions/ISession.cs +++ b/src/YesSql.Abstractions/ISession.cs @@ -137,5 +137,7 @@ public interface ISession : IDisposable, IAsyncDisposable /// Gets the instance that created this session. /// IStore Store { get; } + + IEnumerable ExtraIndexDescriptors { get; set; } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 44059c4f..8e9464d7 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -33,6 +33,7 @@ public class Session : ISession protected string _tablePrefix; private readonly ISqlDialect _dialect; private readonly ILogger _logger; + public IEnumerable ExtraIndexDescriptors { get; set; } = []; public Session(Store store) { @@ -1201,9 +1202,8 @@ private IEnumerable GetDescriptors(Type t, string collection) _descriptors.Add(cacheKey, typedDescriptors); } - return typedDescriptors; + return typedDescriptors.Union(ExtraIndexDescriptors); } - private async Task MapNew(Document document, object obj, string collection) { var descriptors = GetDescriptors(obj.GetType(), collection); diff --git a/test/YesSql.Tests/SqliteTests.cs b/test/YesSql.Tests/SqliteTests.cs index d6442ee5..0c3cc1e3 100644 --- a/test/YesSql.Tests/SqliteTests.cs +++ b/test/YesSql.Tests/SqliteTests.cs @@ -1,6 +1,8 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; +using YesSql.Indexes; using YesSql.Provider.Sqlite; using YesSql.Sql; using YesSql.Tests.Indexes; @@ -139,5 +141,74 @@ await builder await session.SaveAsync(property); } + + [Fact] + public async Task ShouldIndexByExtraDescriptors() + { + await using (var connection = _store.Configuration.ConnectionFactory.CreateConnection()) + { + await connection.OpenAsync(); + + await using var transaction = await connection.BeginTransactionAsync(_store.Configuration.IsolationLevel); + var builder = new SchemaBuilder(_store.Configuration, transaction); + + await builder + .DropMapIndexTableAsync(); + + await builder + .CreateMapIndexTableAsync(column => column + .Column(nameof(PropertyIndex.Name), col => col.WithLength(4000)) + .Column(nameof(PropertyIndex.ForRent)) + .Column(nameof(PropertyIndex.IsOccupied)) + .Column(nameof(PropertyIndex.Location), col => col.WithLength(4000)) + ); + + await builder + .AlterTableAsync(nameof(PropertyIndex), table => table + .CreateIndex("IDX_Property", "Name", "ForRent", "IsOccupied", "Location")); + + await transaction.CommitAsync(); + } + + //_store.RegisterIndexes(); + var extraDescriptors = new List + { + new IndexDescriptor + { + //Type = typeof(Property), + //IndexType = typeof(object), + Filter= (entity) => entity is Property, + Map = (entity) => + { + var property = (Property)entity; + return Task.FromResult>([ + new PropertyIndex + { + Name = property.Name, + ForRent = property.ForRent, + IsOccupied = property.IsOccupied, + Location = property.Location + } + ]); + } + } + }; + await using var session = _store.CreateSession(); + session.ExtraIndexDescriptors = extraDescriptors; + + + var property = new Property + { + Name = "test", + IsOccupied = true, + ForRent = true, + Location = new string('*', 4000) + }; + + await session.SaveAsync(property); + + var ls = await session.Query(x => x.Name == "test").ListAsync(); + Assert.NotEmpty(ls); + } } } From 700b2c4741819d0d8f6d8531c856c5434220f807 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Sun, 21 Jul 2024 21:27:59 +0800 Subject: [PATCH 06/17] BuildExtraIndexDescriptors --- src/YesSql.Abstractions/ISession.cs | 1 + src/YesSql.Core/Session.cs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/YesSql.Abstractions/ISession.cs b/src/YesSql.Abstractions/ISession.cs index 702346a8..34c862b3 100644 --- a/src/YesSql.Abstractions/ISession.cs +++ b/src/YesSql.Abstractions/ISession.cs @@ -139,5 +139,6 @@ public interface ISession : IDisposable, IAsyncDisposable IStore Store { get; } IEnumerable ExtraIndexDescriptors { get; set; } + Func>> BuildExtraIndexDescriptors { get; set; } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 8e9464d7..21da3e62 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -35,6 +35,7 @@ public class Session : ISession private readonly ILogger _logger; public IEnumerable ExtraIndexDescriptors { get; set; } = []; + public Func>> BuildExtraIndexDescriptors { get; set; } public Session(Store store) { _store = store; @@ -1202,6 +1203,15 @@ private IEnumerable GetDescriptors(Type t, string collection) _descriptors.Add(cacheKey, typedDescriptors); } + if (BuildExtraIndexDescriptors != null) + { + var dynamicIndexDes = BuildExtraIndexDescriptors().GetAwaiter().GetResult(); + if (dynamicIndexDes != null) + { + ExtraIndexDescriptors.Union(dynamicIndexDes); + } + } + return typedDescriptors.Union(ExtraIndexDescriptors); } private async Task MapNew(Document document, object obj, string collection) From 348978a80c82d80511a4b43d01571c4a39fabc7a Mon Sep 17 00:00:00 2001 From: Tony Han Date: Sun, 21 Jul 2024 23:01:43 +0800 Subject: [PATCH 07/17] fix return typedDescriptors.Union(ExtraIndexDescriptors.Union(dynamicIndexDes)); --- src/YesSql.Core/Session.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 21da3e62..875b0733 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -1208,7 +1208,7 @@ private IEnumerable GetDescriptors(Type t, string collection) var dynamicIndexDes = BuildExtraIndexDescriptors().GetAwaiter().GetResult(); if (dynamicIndexDes != null) { - ExtraIndexDescriptors.Union(dynamicIndexDes); + return typedDescriptors.Union(ExtraIndexDescriptors.Union(dynamicIndexDes)); } } From 55f744471c5c84bf853856051b71fbce666c9a68 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Mon, 22 Jul 2024 13:52:55 +0800 Subject: [PATCH 08/17] update cache key string to key type --- src/YesSql.Core/Commands/IndexCommand.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/YesSql.Core/Commands/IndexCommand.cs b/src/YesSql.Core/Commands/IndexCommand.cs index c0e0b234..0d51b3a1 100644 --- a/src/YesSql.Core/Commands/IndexCommand.cs +++ b/src/YesSql.Core/Commands/IndexCommand.cs @@ -20,7 +20,7 @@ public abstract class IndexCommand : IIndexCommand protected readonly IStore _store; private static readonly ConcurrentDictionary PropertyAccessors = new(); - private static readonly ConcurrentDictionary TypeProperties = new(); + private static readonly ConcurrentDictionary TypeProperties = new(); private static readonly ConcurrentDictionary InsertsList = new(); private static readonly ConcurrentDictionary UpdatesList = new(); @@ -67,13 +67,19 @@ protected static void GetProperties(DbCommand command, object item, string suffi protected static PropertyInfo[] TypePropertiesCache(Type type) { - if (TypeProperties.TryGetValue(type.FullName, out var pis)) + if (TypeProperties.TryGetValue(type, out var pis)) { + var oldType = TypeProperties.FirstOrDefault(x => x.Key.FullName == type.FullName && x.Key != type); + if (oldType.Key != null) + { + + TypeProperties.Remove(oldType.Key, out _); + } return pis; } var properties = type.GetProperties().Where(IsWriteable).ToArray(); - TypeProperties[type.FullName] = properties; + TypeProperties[type] = properties; return properties; } From ec002bbbacc6830b85ae5d960a3b52dd334059a7 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Mon, 22 Jul 2024 19:07:11 +0800 Subject: [PATCH 09/17] add update TypeService --- src/YesSql.Abstractions/IStore.cs | 2 +- src/YesSql.Abstractions/ITypeService.cs | 5 +++ .../PropertyInfoAccessor.cs | 0 src/YesSql.Core/Commands/IndexCommand.cs | 35 +++------------ src/YesSql.Core/Services/DefaultQuery.cs | 2 +- src/YesSql.Core/Services/TypeService.cs | 43 +++++++++++++++++++ src/YesSql.Core/Session.cs | 8 ++-- src/YesSql.Core/Store.cs | 4 +- test/YesSql.Tests/CoreTests.cs | 4 +- 9 files changed, 64 insertions(+), 39 deletions(-) rename src/{YesSql.Core/Serialization => YesSql.Abstractions}/PropertyInfoAccessor.cs (100%) diff --git a/src/YesSql.Abstractions/IStore.cs b/src/YesSql.Abstractions/IStore.cs index 15d73d9c..92733840 100644 --- a/src/YesSql.Abstractions/IStore.cs +++ b/src/YesSql.Abstractions/IStore.cs @@ -43,6 +43,6 @@ public interface IStore : IDisposable /// /// Returns the instance used to create this store. /// - ITypeService TypeNames { get; } + ITypeService TypeService { get; } } } diff --git a/src/YesSql.Abstractions/ITypeService.cs b/src/YesSql.Abstractions/ITypeService.cs index 95775165..dd5d9668 100644 --- a/src/YesSql.Abstractions/ITypeService.cs +++ b/src/YesSql.Abstractions/ITypeService.cs @@ -1,4 +1,6 @@ using System; +using System.Reflection; +using YesSql.Serialization; namespace YesSql { @@ -16,5 +18,8 @@ public interface ITypeService /// Gets the type represented by a string. /// Type this[string s] { get; } + + PropertyInfo[] GetProperties(Type type); + PropertyInfoAccessor GetPropertyAccessors(PropertyInfo property, Func createFactory); } } diff --git a/src/YesSql.Core/Serialization/PropertyInfoAccessor.cs b/src/YesSql.Abstractions/PropertyInfoAccessor.cs similarity index 100% rename from src/YesSql.Core/Serialization/PropertyInfoAccessor.cs rename to src/YesSql.Abstractions/PropertyInfoAccessor.cs diff --git a/src/YesSql.Core/Commands/IndexCommand.cs b/src/YesSql.Core/Commands/IndexCommand.cs index 0d51b3a1..be3103fa 100644 --- a/src/YesSql.Core/Commands/IndexCommand.cs +++ b/src/YesSql.Core/Commands/IndexCommand.cs @@ -2,12 +2,12 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Data; using System.Data.Common; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using YesSql.Data; using YesSql.Indexes; using YesSql.Serialization; @@ -19,8 +19,6 @@ public abstract class IndexCommand : IIndexCommand protected readonly IStore _store; - private static readonly ConcurrentDictionary PropertyAccessors = new(); - private static readonly ConcurrentDictionary TypeProperties = new(); private static readonly ConcurrentDictionary InsertsList = new(); private static readonly ConcurrentDictionary UpdatesList = new(); @@ -47,13 +45,13 @@ public static void ResetQueryCache() UpdatesList.Clear(); } - protected static void GetProperties(DbCommand command, object item, string suffix, ISqlDialect dialect) + protected void GetProperties(DbCommand command, object item, string suffix, ISqlDialect dialect) { var type = item.GetType(); foreach (var property in TypePropertiesCache(type)) { - var accessor = PropertyAccessors.GetOrAdd(property, p => new PropertyInfoAccessor(p)); + var accessor = _store.TypeService.GetPropertyAccessors(property, prop => new PropertyInfoAccessor(prop)); var value = accessor.Get(item); @@ -65,22 +63,9 @@ protected static void GetProperties(DbCommand command, object item, string suffi } } - protected static PropertyInfo[] TypePropertiesCache(Type type) + protected PropertyInfo[] TypePropertiesCache(Type type) { - if (TypeProperties.TryGetValue(type, out var pis)) - { - var oldType = TypeProperties.FirstOrDefault(x => x.Key.FullName == type.FullName && x.Key != type); - if (oldType.Key != null) - { - - TypeProperties.Remove(oldType.Key, out _); - } - return pis; - } - - var properties = type.GetProperties().Where(IsWriteable).ToArray(); - TypeProperties[type] = properties; - return properties; + return _store.TypeService.GetProperties(type); } protected string Inserts(Type type, ISqlDialect dialect) @@ -180,15 +165,7 @@ protected string Updates(Type type, ISqlDialect dialect) return result; } - private static bool IsWriteable(PropertyInfo pi) - { - return - pi.Name != nameof(IIndex.Id) && - // don't read DocumentId when on a MapIndex as it might be used to - // read the DocumentId directly from an Index query - pi.Name != "DocumentId" - ; - } + public abstract bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index); diff --git a/src/YesSql.Core/Services/DefaultQuery.cs b/src/YesSql.Core/Services/DefaultQuery.cs index f47aec88..5d8a8129 100644 --- a/src/YesSql.Core/Services/DefaultQuery.cs +++ b/src/YesSql.Core/Services/DefaultQuery.cs @@ -1141,7 +1141,7 @@ IQuery IQuery.For(bool filterType) if (filterType) { _queryState._sqlBuilder.WhereAnd(_queryState._sqlBuilder.FormatColumn(_queryState._documentTable, "Type", _queryState._store.Configuration.Schema) + " = @Type"); // TODO: investigate, this makes the query 3 times slower on sqlite - _queryState._sqlBuilder.Parameters["@Type"] = _session.Store.TypeNames[typeof(T)]; + _queryState._sqlBuilder.Parameters["@Type"] = _session.Store.TypeService[typeof(T)]; } return new Query(this); diff --git a/src/YesSql.Core/Services/TypeService.cs b/src/YesSql.Core/Services/TypeService.cs index eb1a73e4..ee247128 100644 --- a/src/YesSql.Core/Services/TypeService.cs +++ b/src/YesSql.Core/Services/TypeService.cs @@ -1,6 +1,10 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; using System.Reflection; +using YesSql.Indexes; +using YesSql.Serialization; namespace YesSql.Services { @@ -10,6 +14,9 @@ public class TypeService : ITypeService private readonly ConcurrentDictionary nameTypes = new(); + private static readonly ConcurrentDictionary PropertyAccessors = new(); + private static readonly ConcurrentDictionary TypeProperties = new(); + public string this[Type t] { get @@ -56,5 +63,41 @@ private static bool IsAnonymousType(TypeInfo type) && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic; } + + + + public PropertyInfo[] GetProperties(Type type) + { + if (TypeProperties.TryGetValue(type, out var pis)) + { + return pis; + } + + var properties = type.GetProperties().Where(IsWriteable).ToArray(); + + var oldType = TypeProperties.FirstOrDefault(x => x.Key.FullName == type.FullName && x.Key != type); + if (oldType.Key != null) + { + TypeProperties.Remove(oldType.Key, out _); + } + + TypeProperties[type] = properties; + return properties; + } + + public PropertyInfoAccessor GetPropertyAccessors(PropertyInfo property, Func createFactory) + { + return PropertyAccessors.GetOrAdd(property, createFactory(property)); + } + + private static bool IsWriteable(PropertyInfo pi) + { + return + pi.Name != nameof(IIndex.Id) && + // don't read DocumentId when on a MapIndex as it might be used to + // read the DocumentId directly from an Index query + pi.Name != "DocumentId" + ; + } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 875b0733..2cc4b93e 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -164,7 +164,7 @@ public bool Import(object entity, long id = 0, long version = 0, string collecti var doc = new Document { - Type = Store.TypeNames[entity.GetType()], + Type = Store.TypeService[entity.GetType()], Content = Store.Configuration.ContentSerializer.Serialize(entity) }; @@ -256,7 +256,7 @@ private async Task SaveEntityAsync(object entity, string collection) var doc = new Document { - Type = Store.TypeNames[entity.GetType()] + Type = Store.TypeService[entity.GetType()] }; if (!state.IdentityMap.TryGetDocumentId(entity, out var id)) @@ -530,7 +530,7 @@ public IEnumerable Get(IList documents, string collection) where var result = new List(); var defaultAccessor = _store.GetIdAccessor(typeof(T)); - var typeName = Store.TypeNames[typeof(T)]; + var typeName = Store.TypeService[typeof(T)]; var state = GetState(collection); @@ -549,7 +549,7 @@ public IEnumerable Get(IList documents, string collection) where // If the document type doesn't match the requested one, check it's a base type if (!string.Equals(typeName, d.Type, StringComparison.Ordinal)) { - var itemType = Store.TypeNames[d.Type]; + var itemType = Store.TypeService[d.Type]; // Ignore the document if it can't be casted to the requested type if (!typeof(T).IsAssignableFrom(itemType)) diff --git a/src/YesSql.Core/Store.cs b/src/YesSql.Core/Store.cs index b54376b5..426c1744 100644 --- a/src/YesSql.Core/Store.cs +++ b/src/YesSql.Core/Store.cs @@ -21,7 +21,7 @@ public class Store : IStore public IConfiguration Configuration { get; set; } public ISqlDialect Dialect { get; private set; } - public ITypeService TypeNames { get; private set; } + public ITypeService TypeService { get; private set; } internal readonly ConcurrentDictionary> GroupMethods = new(); @@ -87,7 +87,7 @@ public async Task InitializeAsync() IndexCommand.ResetQueryCache(); ValidateConfiguration(); - TypeNames = new TypeService(); + TypeService = new TypeService(); if (!string.IsNullOrEmpty(Configuration.Schema)) { diff --git a/test/YesSql.Tests/CoreTests.cs b/test/YesSql.Tests/CoreTests.cs index e754ed54..6554f05d 100644 --- a/test/YesSql.Tests/CoreTests.cs +++ b/test/YesSql.Tests/CoreTests.cs @@ -50,7 +50,7 @@ public async Task InitializeAsync() _store = await StoreFactory.CreateAndInitializeAsync(_configuration); await _store.InitializeCollectionAsync("Col1"); - _store.TypeNames[typeof(Person)] = "People"; + _store.TypeService[typeof(Person)] = "People"; await CreateTablesAsync(_configuration); } @@ -58,7 +58,7 @@ public async Task InitializeAsync() { _store = await StoreFactory.CreateAndInitializeAsync(_configuration); await _store.InitializeCollectionAsync("Col1"); - _store.TypeNames[typeof(Person)] = "People"; + _store.TypeService[typeof(Person)] = "People"; } // Clear the tables for each new test From fe020ba7191ab892efb49b0410b0f4b0c3ba6e83 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Mon, 22 Jul 2024 19:13:27 +0800 Subject: [PATCH 10/17] merge handlers --- .../DocumentChanged/DocumentChangeContext.cs | 15 +++++++ .../DocumentChangedInBatchContext.cs | 14 +++++++ .../Commands/DocumentCommand.cs | 12 +++--- .../Commands/IDocumentCommandHandler.cs | 16 ++++++++ .../Commands/IIndexCommand.cs | 0 src/YesSql.Abstractions/ISession.cs | 2 + .../Commands/CreateDocumentCommand.cs | 35 ++++++++++++++--- .../Commands/DefaultDocumentCommandHandler.cs | 20 ++++++++++ .../Commands/DeleteDocumentCommand.cs | 39 ++++++++++++++++--- .../Commands/UpdateDocumentCommand.cs | 32 +++++++++++++-- src/YesSql.Core/Session.cs | 8 ++-- 11 files changed, 169 insertions(+), 24 deletions(-) create mode 100644 src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs create mode 100644 src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs rename src/{YesSql.Core => YesSql.Abstractions}/Commands/DocumentCommand.cs (88%) create mode 100644 src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs rename src/{YesSql.Core => YesSql.Abstractions}/Commands/IIndexCommand.cs (100%) create mode 100644 src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs diff --git a/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs new file mode 100644 index 00000000..ac6ce03c --- /dev/null +++ b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangeContext.cs @@ -0,0 +1,15 @@ +using System.Data.Common; + +namespace YesSql.Commands.DocumentChanged +{ + public class DocumentChangeContext + { + public ISession Session { get; set; } + public object Entity { get; set; } + public Document Document { get; set; } + public IStore Store { get; set; } + public DbConnection Connection { get; set; } + public DbTransaction Transaction { get; set; } + public ISqlDialect Dialect { get; set; } + } +} diff --git a/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs new file mode 100644 index 00000000..1982ce73 --- /dev/null +++ b/src/YesSql.Abstractions/Commands/DocumentChanged/DocumentChangedInBatchContext.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; +using System.Data.Common; + +namespace YesSql.Commands.DocumentChanged +{ + public class DocumentChangeInBatchContext + { + public object Entity { get; set; } + public List Queries { get; set; } + public DbCommand BatchCommand { get; set; } + public ISession Session { get; set; } + public Document Document { get; set; } + } +} diff --git a/src/YesSql.Core/Commands/DocumentCommand.cs b/src/YesSql.Abstractions/Commands/DocumentCommand.cs similarity index 88% rename from src/YesSql.Core/Commands/DocumentCommand.cs rename to src/YesSql.Abstractions/Commands/DocumentCommand.cs index cc84336d..b1211fde 100644 --- a/src/YesSql.Core/Commands/DocumentCommand.cs +++ b/src/YesSql.Abstractions/Commands/DocumentCommand.cs @@ -9,15 +9,15 @@ namespace YesSql.Commands { public abstract class DocumentCommand : IIndexCommand, ICollectionName { - protected static readonly PropertyInfo[] AllProperties = new PropertyInfo[] - { + protected static readonly PropertyInfo[] AllProperties = + [ typeof(Document).GetProperty("Type") - }; + ]; - protected static readonly PropertyInfo[] AllKeys = new PropertyInfo[] - { + protected static readonly PropertyInfo[] AllKeys = + [ typeof(Document).GetProperty("Id") - }; + ]; public abstract int ExecutionOrder { get; } diff --git a/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs b/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs new file mode 100644 index 00000000..54bd56b6 --- /dev/null +++ b/src/YesSql.Abstractions/Commands/IDocumentCommandHandler.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; + +namespace YesSql.Commands +{ + public interface IDocumentCommandHandler + { + Task CreatedAsync(DocumentChangeContext context); + bool CreatedInBatch(DocumentChangeInBatchContext context); + + Task RemovingAsync(DocumentChangeContext context); + bool RemovingInBatch(DocumentChangeInBatchContext context); + Task UpdatedAsync(DocumentChangeContext context); + bool UpdatedInBatch(DocumentChangeInBatchContext context); + } +} diff --git a/src/YesSql.Core/Commands/IIndexCommand.cs b/src/YesSql.Abstractions/Commands/IIndexCommand.cs similarity index 100% rename from src/YesSql.Core/Commands/IIndexCommand.cs rename to src/YesSql.Abstractions/Commands/IIndexCommand.cs diff --git a/src/YesSql.Abstractions/ISession.cs b/src/YesSql.Abstractions/ISession.cs index 34c862b3..c83d59ac 100644 --- a/src/YesSql.Abstractions/ISession.cs +++ b/src/YesSql.Abstractions/ISession.cs @@ -3,6 +3,7 @@ using System.Data; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands; using YesSql.Indexes; namespace YesSql @@ -140,5 +141,6 @@ public interface ISession : IDisposable, IAsyncDisposable IEnumerable ExtraIndexDescriptors { get; set; } Func>> BuildExtraIndexDescriptors { get; set; } + public IDocumentCommandHandler DocumentCommandHandler { get; set; } } } diff --git a/src/YesSql.Core/Commands/CreateDocumentCommand.cs b/src/YesSql.Core/Commands/CreateDocumentCommand.cs index ff0fd632..92940164 100644 --- a/src/YesSql.Core/Commands/CreateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/CreateDocumentCommand.cs @@ -4,21 +4,25 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { - public sealed class CreateDocumentCommand : DocumentCommand + public class CreateDocumentCommand : DocumentCommand { private readonly IStore _store; - + private readonly ISession _session; + private readonly object _entity; public override int ExecutionOrder { get; } = 0; - public CreateDocumentCommand(Document document, IStore store, string collection) : base(document, collection) + public CreateDocumentCommand(object entity, Document document, IStore store, string collection, ISession session) : base(document, collection) { _store = store; + _session = session; + _entity = entity; } - public override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) + public override async Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) { var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); @@ -28,8 +32,19 @@ public override Task ExecuteAsync(DbConnection connection, DbTransaction transac { logger.LogTrace(insertCmd); } + await connection.ExecuteAsync(insertCmd, Document, transaction); - return connection.ExecuteAsync(insertCmd, Document, transaction); + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.CreatedAsync(context); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index) @@ -45,6 +60,16 @@ public override bool AddToBatch(ISqlDialect dialect, List queries, DbCom .AddParameter("Content_" + index, Document.Content) .AddParameter("Version_" + index, Document.Version); + var context = new DocumentChangeInBatchContext + { + Session = _session, + Document = Document, + Entity = _entity, + BatchCommand = batchCommand, + Queries = queries, + }; + _session.DocumentCommandHandler.CreatedInBatch(context); + return true; } } diff --git a/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs b/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs new file mode 100644 index 00000000..9666f3d1 --- /dev/null +++ b/src/YesSql.Core/Commands/DefaultDocumentCommandHandler.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; + +namespace YesSql.Commands +{ + public class DefaultDocumentCommandHandler : IDocumentCommandHandler + { + public Task CreatedAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool CreatedInBatch(DocumentChangeInBatchContext context) => true; + + public Task RemovingAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool RemovingInBatch(DocumentChangeInBatchContext context) => true; + + public Task UpdatedAsync(DocumentChangeContext context) => Task.CompletedTask; + + public bool UpdatedInBatch(DocumentChangeInBatchContext context) => true; + } +} diff --git a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs index 7ca257ae..bf52bf3f 100644 --- a/src/YesSql.Core/Commands/DeleteDocumentCommand.cs +++ b/src/YesSql.Core/Commands/DeleteDocumentCommand.cs @@ -4,34 +4,61 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { - public sealed class DeleteDocumentCommand : DocumentCommand + public class DeleteDocumentCommand : DocumentCommand { private readonly IStore _store; + private readonly ISession _session; + private readonly object _entity; public override int ExecutionOrder { get; } = 4; - public DeleteDocumentCommand(Document document, IStore store, string collection) : base(document, collection) + public DeleteDocumentCommand(object entity, Document document, IStore store, string collection, Session session) : base(document, collection) { _store = store; + _session = session; + _entity = entity; } - public override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) + public async override Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) { + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.RemovingAsync(context); + var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id;"; - + if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace(deleteCmd); } - - return connection.ExecuteAsync(deleteCmd, Document, transaction); + + await connection.ExecuteAsync(deleteCmd, Document, transaction); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand command, List> actions, int index) { + var context = new DocumentChangeInBatchContext + { + Session = _session, + Document = Document, + Entity = _entity, + BatchCommand = command, + Queries = queries, + }; + _session.DocumentCommandHandler.RemovingInBatch(context); + var documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(Collection); var deleteCmd = $"delete from {dialect.QuoteForTableName(_store.Configuration.TablePrefix + documentTable, _store.Configuration.Schema)} where {dialect.QuoteForColumnName("Id")} = @Id_{index};"; diff --git a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs index 7b5f15a8..7b4d5c08 100644 --- a/src/YesSql.Core/Commands/UpdateDocumentCommand.cs +++ b/src/YesSql.Core/Commands/UpdateDocumentCommand.cs @@ -4,20 +4,24 @@ using System.Collections.Generic; using System.Data.Common; using System.Threading.Tasks; +using YesSql.Commands.DocumentChanged; namespace YesSql.Commands { - public sealed class UpdateDocumentCommand : DocumentCommand + public class UpdateDocumentCommand : DocumentCommand { private readonly IStore _store; + private readonly ISession _session; + private readonly object _entity; private readonly long _checkVersion; - public override int ExecutionOrder { get; } = 2; - public UpdateDocumentCommand(Document document, IStore store, long checkVersion, string collection) : base(document, collection) + public UpdateDocumentCommand(object entity, Document document, IStore store, long checkVersion, string collection, Session session) : base(document, collection) { _store = store; _checkVersion = checkVersion; + _session = session; + _entity = entity; } public override async Task ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger) @@ -46,7 +50,17 @@ public override async Task ExecuteAsync(DbConnection connection, DbTransaction t throw new ConcurrencyException(); } - return; + var context = new DocumentChangeContext + { + Session = _session, + Entity = _entity, + Document = Document, + Store = _store, + Connection = connection, + Transaction = transaction, + Dialect = dialect, + }; + await _session.DocumentCommandHandler.UpdatedAsync(context); } public override bool AddToBatch(ISqlDialect dialect, List queries, DbCommand batchCommand, List> actions, int index) @@ -75,6 +89,16 @@ public override bool AddToBatch(ISqlDialect dialect, List queries, DbCom .AddParameter("Content_" + index, Document.Content) .AddParameter("Version_" + index, Document.Version); + var context = new DocumentChangeInBatchContext + { + Session = _session, + Entity = _entity, + Document = Document, + BatchCommand = batchCommand, + Queries = queries, + }; + _session.DocumentCommandHandler.UpdatedInBatch(context); + return true; } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 2cc4b93e..4e41a6df 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -34,6 +34,7 @@ public class Session : ISession private readonly ISqlDialect _dialect; private readonly ILogger _logger; public IEnumerable ExtraIndexDescriptors { get; set; } = []; + public IDocumentCommandHandler DocumentCommandHandler { get; set; } public Func>> BuildExtraIndexDescriptors { get; set; } public Session(Store store) @@ -48,6 +49,7 @@ public Session(Store store) { [""] = _defaultState }; + DocumentCommandHandler = new DefaultDocumentCommandHandler(); } public ISession RegisterIndexes(IIndexProvider[] indexProviders, string collection = null) @@ -288,7 +290,7 @@ private async Task SaveEntityAsync(object entity, string collection) _commands ??= new List(); - _commands.Add(new CreateDocumentCommand(doc, Store, collection)); + _commands.Add(new CreateDocumentCommand(entity,doc, Store, collection, this)); state.IdentityMap.AddDocument(doc); @@ -383,7 +385,7 @@ private async Task UpdateEntityAsync(object entity, bool tracked, string collect _commands ??= new List(); - _commands.Add(new UpdateDocumentCommand(oldDoc, Store, version, collection)); + _commands.Add(new UpdateDocumentCommand(entity, oldDoc, Store, version, collection, this)); } private async Task GetDocumentByIdAsync(long id, string collection) @@ -468,7 +470,7 @@ private async Task DeleteEntityAsync(object obj, string collection) _commands ??= new List(); // The command needs to come after any index deletion because of the database constraints - _commands.Add(new DeleteDocumentCommand(doc, Store, collection)); + _commands.Add(new DeleteDocumentCommand(obj, doc, Store, collection, this)); } } From 23dd83edaaa806738e85729d3330de5a1267ce7e Mon Sep 17 00:00:00 2001 From: Tony Han Date: Mon, 22 Jul 2024 19:18:08 +0800 Subject: [PATCH 11/17] make ITypeService set able --- src/YesSql.Core/Store.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/YesSql.Core/Store.cs b/src/YesSql.Core/Store.cs index 426c1744..650ff6d4 100644 --- a/src/YesSql.Core/Store.cs +++ b/src/YesSql.Core/Store.cs @@ -21,7 +21,7 @@ public class Store : IStore public IConfiguration Configuration { get; set; } public ISqlDialect Dialect { get; private set; } - public ITypeService TypeService { get; private set; } + public ITypeService TypeService { get; set; } internal readonly ConcurrentDictionary> GroupMethods = new(); From 4c402d2210c84c16d8c7b63c9274ab63dceed360 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Mon, 22 Jul 2024 20:38:48 +0800 Subject: [PATCH 12/17] publish 4.0.1-jzbeta-4.0-ExtraDescriptors-11 --- src/YesSql.Abstractions/IStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/YesSql.Abstractions/IStore.cs b/src/YesSql.Abstractions/IStore.cs index 92733840..d160a9f2 100644 --- a/src/YesSql.Abstractions/IStore.cs +++ b/src/YesSql.Abstractions/IStore.cs @@ -43,6 +43,6 @@ public interface IStore : IDisposable /// /// Returns the instance used to create this store. /// - ITypeService TypeService { get; } + ITypeService TypeService { get; set; } } } From 108503aaca0bbe0ee3878ef7593403960c6ce2e8 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Tue, 23 Jul 2024 12:13:25 +0800 Subject: [PATCH 13/17] Update src/YesSql.Core/Services/TypeService.cs --- src/YesSql.Core/Services/TypeService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/YesSql.Core/Services/TypeService.cs b/src/YesSql.Core/Services/TypeService.cs index ee247128..86902cbf 100644 --- a/src/YesSql.Core/Services/TypeService.cs +++ b/src/YesSql.Core/Services/TypeService.cs @@ -65,7 +65,6 @@ private static bool IsAnonymousType(TypeInfo type) } - public PropertyInfo[] GetProperties(Type type) { if (TypeProperties.TryGetValue(type, out var pis)) From 220cf7bea4cd7b56b7e3d419d6a1ed9a9accc66c Mon Sep 17 00:00:00 2001 From: Tony Han Date: Tue, 23 Jul 2024 12:13:55 +0800 Subject: [PATCH 14/17] Update src/YesSql.Core/Services/TypeService.cs --- src/YesSql.Core/Services/TypeService.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/YesSql.Core/Services/TypeService.cs b/src/YesSql.Core/Services/TypeService.cs index 86902cbf..e7b736ad 100644 --- a/src/YesSql.Core/Services/TypeService.cs +++ b/src/YesSql.Core/Services/TypeService.cs @@ -63,8 +63,6 @@ private static bool IsAnonymousType(TypeInfo type) && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic; } - - public PropertyInfo[] GetProperties(Type type) { if (TypeProperties.TryGetValue(type, out var pis)) From 5d6b60187d5941612917077e247ed5be630fc073 Mon Sep 17 00:00:00 2001 From: Tony Han Date: Tue, 23 Jul 2024 12:14:15 +0800 Subject: [PATCH 15/17] Update src/YesSql.Core/Services/TypeService.cs --- src/YesSql.Core/Services/TypeService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/YesSql.Core/Services/TypeService.cs b/src/YesSql.Core/Services/TypeService.cs index e7b736ad..a4c1d7ab 100644 --- a/src/YesSql.Core/Services/TypeService.cs +++ b/src/YesSql.Core/Services/TypeService.cs @@ -63,6 +63,7 @@ private static bool IsAnonymousType(TypeInfo type) && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic; } + public PropertyInfo[] GetProperties(Type type) { if (TypeProperties.TryGetValue(type, out var pis)) From ec9e0d70610b6ab7457ca85d0724b38f7abe4d61 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Tue, 23 Jul 2024 14:30:28 +0800 Subject: [PATCH 16/17] upgrade SqlClient Npgsql --- src/Directory.Packages.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 1b5bc254..d8e3dc43 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -4,9 +4,9 @@ - + - + From a78a8027379c9a32f92e843bc9347ea2f62183c8 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Tue, 23 Jul 2024 16:28:56 +0800 Subject: [PATCH 17/17] update BuildExtraIndexDescriptors --- src/YesSql.Abstractions/ISession.cs | 4 ++-- src/YesSql.Core/Session.cs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/YesSql.Abstractions/ISession.cs b/src/YesSql.Abstractions/ISession.cs index c83d59ac..91dfcfbd 100644 --- a/src/YesSql.Abstractions/ISession.cs +++ b/src/YesSql.Abstractions/ISession.cs @@ -140,7 +140,7 @@ public interface ISession : IDisposable, IAsyncDisposable IStore Store { get; } IEnumerable ExtraIndexDescriptors { get; set; } - Func>> BuildExtraIndexDescriptors { get; set; } - public IDocumentCommandHandler DocumentCommandHandler { get; set; } + Func>> BuildExtraIndexDescriptors { get; set; } + IDocumentCommandHandler DocumentCommandHandler { get; set; } } } diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs index 4e41a6df..3bb8e239 100644 --- a/src/YesSql.Core/Session.cs +++ b/src/YesSql.Core/Session.cs @@ -36,7 +36,7 @@ public class Session : ISession public IEnumerable ExtraIndexDescriptors { get; set; } = []; public IDocumentCommandHandler DocumentCommandHandler { get; set; } - public Func>> BuildExtraIndexDescriptors { get; set; } + public Func>> BuildExtraIndexDescriptors { get; set; } public Session(Store store) { _store = store; @@ -290,7 +290,7 @@ private async Task SaveEntityAsync(object entity, string collection) _commands ??= new List(); - _commands.Add(new CreateDocumentCommand(entity,doc, Store, collection, this)); + _commands.Add(new CreateDocumentCommand(entity, doc, Store, collection, this)); state.IdentityMap.AddDocument(doc); @@ -1184,7 +1184,7 @@ private Func GetGroupingMethod(IndexDescriptor descriptor) /// /// Resolves all the descriptors registered on the Store and the Session /// - private IEnumerable GetDescriptors(Type t, string collection) + private async Task> GetDescriptorsAsync(Type t, string collection) { _descriptors ??= new Dictionary>(); @@ -1207,7 +1207,7 @@ private IEnumerable GetDescriptors(Type t, string collection) if (BuildExtraIndexDescriptors != null) { - var dynamicIndexDes = BuildExtraIndexDescriptors().GetAwaiter().GetResult(); + var dynamicIndexDes = await BuildExtraIndexDescriptors(t, collection); if (dynamicIndexDes != null) { return typedDescriptors.Union(ExtraIndexDescriptors.Union(dynamicIndexDes)); @@ -1218,7 +1218,7 @@ private IEnumerable GetDescriptors(Type t, string collection) } private async Task MapNew(Document document, object obj, string collection) { - var descriptors = GetDescriptors(obj.GetType(), collection); + var descriptors = await GetDescriptorsAsync(obj.GetType(), collection); var state = GetState(collection); @@ -1278,7 +1278,7 @@ private async Task MapNew(Document document, object obj, string collection) /// private async Task MapDeleted(Document document, object obj, string collection) { - var descriptors = GetDescriptors(obj.GetType(), collection); + var descriptors = await GetDescriptorsAsync(obj.GetType(), collection); var state = GetState(collection);