Example #1
0
        public DocumentProvider <T> StorageFor <T>() where T : notnull
        {
            var documentType = typeof(T);

            if (_storage.TryFind(documentType, out var stored))
            {
                return(stored.As <DocumentProvider <T> >());
            }

            if (documentType == typeof(IEvent))
            {
                var slot = EventDocumentStorageGenerator.BuildProvider(_options);

                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot.As <DocumentProvider <T> >());
            }

            var mapping = _options.Storage.FindMapping(documentType);

            switch (mapping)
            {
            case DocumentMapping m:
            {
                var builder = new DocumentPersistenceBuilder(m, _options);
                var slot    = builder.Generate <T>();

                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot);
            }

            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> {
                    Lightweight = storage, IdentityMap = storage, DirtyTracking = storage, QueryOnly = storage
                };
                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot);
            }

            default:
                throw new NotSupportedException("Unable to build document persistence handlers for " + mapping.DocumentType.FullNameInCode());
            }
        }
Example #2
0
        public DocumentProvider <T> StorageFor <T>()
        {
            var documentType = typeof(T);

            if (_storage.TryFind(documentType, out var stored))
            {
                return(stored.As <DocumentProvider <T> >());
            }

            if (documentType == typeof(IEvent))
            {
                var storage = new EventDocumentStorage(_options.Events, new EventQueryMapping(_options), _options.Serializer());
                var slot    = new DocumentProvider <IEvent>
                {
                    DirtyTracking = storage,
                    Lightweight   = storage,
                    IdentityMap   = storage,
                    QueryOnly     = storage
                };

                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot.As <DocumentProvider <T> >());
            }

            var mapping = _options.Storage.FindMapping(documentType);

            if (mapping is DocumentMapping m)
            {
                var builder = new DocumentPersistenceBuilder(m, _options);
                var slot    = builder.Generate <T>();

                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot);
            }

            if (mapping is 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);
            }

            if (mapping is EventMapping em)
            {
                var storage = (IDocumentStorage <T>)em;
                var slot    = new DocumentProvider <T> {
                    Lightweight = storage, IdentityMap = storage, DirtyTracking = storage, QueryOnly = storage
                };
                _storage = _storage.AddOrUpdate(documentType, slot);

                return(slot);
            }

            throw new NotSupportedException("Unable to build document persistence handlers for " + mapping.DocumentType.FullNameInCode());
        }
        private DocumentPersistence <T> CreateSlot()
        {
            var builder = new DocumentPersistenceBuilder(Mapping, Options);

            return(builder.Generate <T>());
        }