| | 1 | | using Google.Protobuf.Reflection; |
| | 2 | | using MRA.Infrastructure.Settings; |
| | 3 | | using MRA.Infrastructure.Database.Documents.Interfaces; |
| | 4 | |
|
| | 5 | | namespace MRA.Infrastructure.Database.Documents.MongoDb; |
| | 6 | |
|
| | 7 | | public class DocumentTypeRegistry |
| | 8 | | { |
| 1 | 9 | | private readonly Dictionary<string, Type> _collectionTypeMapping = new Dictionary<string, Type>(); |
| | 10 | |
|
| 1 | 11 | | public DocumentTypeRegistry(AppSettings appConfig) |
| | 12 | | { |
| 1 | 13 | | RegisterDocumentType<InspirationMongoDocument>(appConfig.Database.Collections.Inspirations); |
| 1 | 14 | | RegisterDocumentType<DrawingMongoDocument>(appConfig.Database.Collections.Drawings); |
| 1 | 15 | | RegisterDocumentType<CollectionMongoDocument>(appConfig.Database.Collections.Collections); |
| 1 | 16 | | } |
| | 17 | |
|
| | 18 | | private void RegisterDocumentType<TDocument>(string collection) where TDocument : IDocument |
| | 19 | | { |
| 3 | 20 | | _collectionTypeMapping[collection] = typeof(TDocument); |
| 3 | 21 | | } |
| | 22 | |
|
| | 23 | | public Type GetDocumentType(string collection) |
| | 24 | | { |
| 0 | 25 | | if (_collectionTypeMapping.TryGetValue(collection, out var type)) |
| | 26 | | { |
| 0 | 27 | | return type; |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | throw new InvalidOperationException($"No document type registered for collection '{collection}'."); |
| | 31 | | } |
| | 32 | | } |