Skip to content

Commit

Permalink
Extracted CreateDocumentProvider method
Browse files Browse the repository at this point in the history
  • Loading branch information
felixkmh committed Apr 2, 2024
1 parent d791b5a commit cfa8215
Showing 1 changed file with 57 additions and 50 deletions.
107 changes: 57 additions & 50 deletions src/Marten/Internal/ProviderGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type, object> _storage = ImHashMap<Type, object>.Empty;

public ProviderGraph(StoreOptions options)
Expand Down Expand Up @@ -46,71 +46,78 @@ public DocumentProvider<T> StorageFor<T>() where T : notnull
return stored.As<DocumentProvider<T>>();
}

if (documentType == typeof(IEvent))
{
var rules = _options.CreateGenerationRules();
_options.EventGraph.InitializeSynchronously(rules, _options.EventGraph, null);

_storage = _storage.AddOrUpdate(documentType, _options.EventGraph.Provider);
return CreateDocumentProvider<T>();
}
}

return _options.EventGraph.Provider.As<DocumentProvider<T>>();
}
private DocumentProvider<T> CreateDocumentProvider<T>() 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<T>();
return _options.EventGraph.Provider.As<DocumentProvider<T>>();
}

_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<ISubClassLoader<T>>(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<T>();

var slot = loader.BuildPersistence(this, s);
_storage = _storage.AddOrUpdate(documentType, slot);

return slot;
}
case EventMapping em:
catch (Exception e)
{
var storage = (IDocumentStorage<T>)em;
var slot = new DocumentProvider<T>(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<ISubClassLoader<T>>(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<T>)em;
var slot = new DocumentProvider<T>(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());
}
}

Expand Down

0 comments on commit cfa8215

Please sign in to comment.