-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/wpm 44 add configuration based indexes (#70)
Add alternative approach to configure collections via class map configuration --------- Co-authored-by: Jules Tremblay <[email protected]>
- Loading branch information
Showing
26 changed files
with
670 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Workleap.Extensions.Mongo.Abstractions/IMongoCollectionBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using MongoDB.Bson.Serialization; | ||
|
||
namespace Workleap.Extensions.Mongo; | ||
|
||
public interface IMongoCollectionBuilder<TDocument> | ||
where TDocument : class | ||
{ | ||
IMongoCollectionBuilder<TDocument> CollectionName(string collectionName); | ||
|
||
IMongoCollectionBuilder<TDocument> IndexProvider<TIndexProvider>() | ||
where TIndexProvider : MongoIndexProvider<TDocument>; | ||
|
||
IMongoCollectionBuilder<TDocument> BsonClassMap(Action<BsonClassMap<TDocument>> classMapInitializer); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Workleap.Extensions.Mongo.Abstractions/IMongoCollectionConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Workleap.Extensions.Mongo; | ||
|
||
public interface IMongoCollectionConfiguration<TDocument> | ||
where TDocument : class | ||
{ | ||
void Configure(IMongoCollectionBuilder<TDocument> builder); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Workleap.Extensions.Mongo.Abstractions/MongoCollectionNameCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
|
||
namespace Workleap.Extensions.Mongo; | ||
|
||
internal static class MongoCollectionNameCache | ||
{ | ||
private static readonly ConcurrentDictionary<Type, string> CollectionNames = new(); | ||
|
||
public static string GetCollectionName(Type documentType) | ||
{ | ||
if (CollectionNames.TryGetValue(documentType, out var collectionName)) | ||
{ | ||
return collectionName; | ||
} | ||
|
||
// Configuration based CollectionNames are set manually by calling SetCollectionName. | ||
// When we reach here, we can validate the Attribute flow because it was not a document from the Configuration flow. | ||
if (!documentType.IsConcreteMongoDocumentType()) | ||
{ | ||
throw new ArgumentException(documentType + " must be a concrete type that implements " + nameof(IMongoDocument)); | ||
} | ||
|
||
return CollectionNames.GetOrAdd(documentType, static documentType => | ||
{ | ||
if (documentType.GetCustomAttribute<MongoCollectionAttribute>() is { } attribute) | ||
{ | ||
return attribute.Name; | ||
} | ||
|
||
throw new ArgumentException(documentType + " must be decorated with " + nameof(MongoCollectionAttribute) + " or be registered by a " + typeof(IMongoCollectionConfiguration<>).MakeGenericType(documentType).Name); | ||
}); | ||
} | ||
|
||
public static string GetCollectionName<TDocument>() | ||
where TDocument : class | ||
{ | ||
return GetCollectionName(typeof(TDocument)); | ||
} | ||
|
||
internal static void SetCollectionName(Type documentType, string collectionName) | ||
{ | ||
if (!CollectionNames.TryAdd(documentType, collectionName)) | ||
{ | ||
throw new ArgumentException($"Collection name for {documentType} already set."); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Workleap.Extensions.Mongo.Abstractions/MongoConfigurationIndexStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace Workleap.Extensions.Mongo; | ||
|
||
internal static class MongoConfigurationIndexStore | ||
{ | ||
private static readonly ConcurrentDictionary<Type, Type?> IndexProviderTypes = new(); | ||
|
||
internal static void AddIndexProviderType(Type documentType, Type? indexProviderType) | ||
{ | ||
if (!IndexProviderTypes.TryAdd(documentType, indexProviderType)) | ||
{ | ||
throw new ArgumentException($"IndexProviderType for {documentType} already set."); | ||
} | ||
} | ||
|
||
internal static IReadOnlyDictionary<Type, Type?> GetIndexProviderTypes() => IndexProviderTypes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/Workleap.Extensions.Mongo.Abstractions/MongoReflectionCache.cs
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
src/Workleap.Extensions.Mongo.Abstractions/MongoTypeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Reflection; | ||
|
||
namespace Workleap.Extensions.Mongo; | ||
|
||
internal static class MongoTypeExtensions | ||
{ | ||
internal static bool IsConcreteMongoDocumentType(this Type type) => !type.IsAbstract && typeof(IMongoDocument).IsAssignableFrom(type); | ||
|
||
internal static bool IsMongoCollectionConfigurationInterface(this Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IMongoCollectionConfiguration<>); | ||
|
||
internal static void EnsureHasPublicParameterlessConstructor(this Type type) | ||
{ | ||
if (!type.HasPublicParameterlessConstructor()) | ||
{ | ||
throw new InvalidOperationException($"Type {type}' must have a public parameterless constructor"); | ||
} | ||
} | ||
|
||
private static bool HasPublicParameterlessConstructor(this Type type) => type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, binder: null, Type.EmptyTypes, modifiers: null) != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.