diff --git a/src/Marten/Internal/ProviderGraph.cs b/src/Marten/Internal/ProviderGraph.cs index 924f86cf05..8c41948e54 100644 --- a/src/Marten/Internal/ProviderGraph.cs +++ b/src/Marten/Internal/ProviderGraph.cs @@ -13,8 +13,8 @@ namespace Marten.Internal; public class ProviderGraph: IProviderGraph { - private readonly object _storageLock = new(); private readonly StoreOptions _options; + private readonly object _storageLock = new(); private ImHashMap _storage = ImHashMap.Empty; public ProviderGraph(StoreOptions options) @@ -46,71 +46,78 @@ public DocumentProvider StorageFor() where T : notnull return stored.As>(); } - if (documentType == typeof(IEvent)) - { - var rules = _options.CreateGenerationRules(); - _options.EventGraph.InitializeSynchronously(rules, _options.EventGraph, null); - - _storage = _storage.AddOrUpdate(documentType, _options.EventGraph.Provider); + return CreateDocumentProvider(); + } + } - return _options.EventGraph.Provider.As>(); - } + private DocumentProvider CreateDocumentProvider() where T : notnull + { + var documentType = typeof(T); - var mapping = _options.Storage.FindMapping(documentType); + if (documentType == typeof(IEvent)) + { + var rules = _options.CreateGenerationRules(); + _options.EventGraph.InitializeSynchronously(rules, _options.EventGraph, null); - switch (mapping) - { - case DocumentMapping m: - { - try - { - var builder = new DocumentProviderBuilder(m, _options); + _storage = _storage.AddOrUpdate(documentType, _options.EventGraph.Provider); - var rules = _options.CreateGenerationRules(); - rules.ReferenceTypes(m.DocumentType); - builder.InitializeSynchronously(rules, _options, null); - var slot = builder.BuildProvider(); + return _options.EventGraph.Provider.As>(); + } - _storage = _storage.AddOrUpdate(documentType, slot); + var mapping = _options.Storage.FindMapping(documentType); - return slot; - } - catch (Exception e) - { - if (e.Message.Contains("is inaccessible due to its protection level")) - { - throw new InvalidOperationException( - $"Requested document type '{mapping.DocumentType.FullNameInCode()}' must be scoped as 'public' in order to be used as a document type inside of Marten", - e); - } - - throw; - } - } - case SubClassMapping s: + switch (mapping) + { + case DocumentMapping m: + { + try { - var loader = - typeof(SubClassLoader<,,>).CloseAndBuildAs>(mapping.Root.DocumentType, - documentType, - mapping.IdType); + var builder = new DocumentProviderBuilder(m, _options); + + var rules = _options.CreateGenerationRules(); + rules.ReferenceTypes(m.DocumentType); + builder.InitializeSynchronously(rules, _options, null); + var slot = builder.BuildProvider(); - var slot = loader.BuildPersistence(this, s); _storage = _storage.AddOrUpdate(documentType, slot); return slot; } - case EventMapping em: + catch (Exception e) { - var storage = (IDocumentStorage)em; - var slot = new DocumentProvider(null, storage, storage, storage, storage); - _storage = _storage.AddOrUpdate(documentType, slot); + if (e.Message.Contains("is inaccessible due to its protection level")) + { + throw new InvalidOperationException( + $"Requested document type '{mapping.DocumentType.FullNameInCode()}' must be scoped as 'public' in order to be used as a document type inside of Marten", + e); + } - return slot; + throw; } - default: - throw new NotSupportedException("Unable to build document persistence handlers for " + - mapping.DocumentType.FullNameInCode()); } + case SubClassMapping s: + { + var loader = + typeof(SubClassLoader<,,>).CloseAndBuildAs>(mapping.Root.DocumentType, + documentType, + mapping.IdType); + + var slot = loader.BuildPersistence(this, s); + _storage = _storage.AddOrUpdate(documentType, slot); + + return slot; + } + case EventMapping em: + { + var storage = (IDocumentStorage)em; + var slot = new DocumentProvider(null, storage, storage, storage, storage); + _storage = _storage.AddOrUpdate(documentType, slot); + + return slot; + } + default: + throw new NotSupportedException("Unable to build document persistence handlers for " + + mapping.DocumentType.FullNameInCode()); } }