public DefaultCosmosContainerProvider(
            ICosmosClientProvider cosmosClientProvider,
            ICosmosPartitionKeyPathProvider cosmosPartitionKeyPathProvider,
            IOptions <RepositoryOptions> options,
            ILogger <DefaultCosmosContainerProvider <TItem> > logger)
        {
            _cosmosClientProvider = cosmosClientProvider ?? throw new ArgumentNullException(
                                              nameof(cosmosClientProvider), Resources.CosmosClientProviderIsRequired);

            _cosmosPartitionKeyPathProvider = cosmosPartitionKeyPathProvider ?? throw new ArgumentNullException(
                                                        nameof(cosmosPartitionKeyPathProvider), Resources.CosmosPartitionKeyNameProviderIsRequired);

            _options = options?.Value ?? throw new ArgumentNullException(
                                 nameof(options), Resources.RepositoryOptionsAreRequired);

            _ = _options.CosmosConnectionString ?? throw new ArgumentNullException(
                          nameof(options), Resources.TheBlankIsRequired.Format(nameof(_options.CosmosConnectionString)));

            if (_options.ContainerPerItemType is false)
            {
                _ = _options.DatabaseId ?? throw new ArgumentNullException(
                              nameof(options),
                              Resources.TheBlankIsRequiredWhenContainerPerItemTypeIsFalse.Format(nameof(_options.DatabaseId)));

                _ = _options.ContainerId ?? throw new ArgumentNullException(
                              nameof(options),
                              Resources.TheBlankIsRequiredWhenContainerPerItemTypeIsFalse.Format(nameof(_options.ContainerId)));
            }

            _logger = logger ?? throw new ArgumentNullException(
                                nameof(logger), Resources.TheBlankIsRequired.Format(nameof(logger)));

            _lazyContainer = new Lazy <Task <Container> >(
                async() =>
            {
                try
                {
                    Database database =
                        await _cosmosClientProvider.UseClientAsync(
                            client => client.CreateDatabaseIfNotExistsAsync(_options.DatabaseId)).ConfigureAwait(false);

                    ContainerProperties containerProperties = new ContainerProperties
                    {
                        Id = _options.ContainerPerItemType ? typeof(TItem).Name : _options.ContainerId,
                        PartitionKeyPath = _cosmosPartitionKeyPathProvider.GetPartitionKeyPath <TItem>()
                    };

                    Container container =
                        await database.CreateContainerIfNotExistsAsync(containerProperties).ConfigureAwait(false);

                    return(container);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message, ex);

                    throw;
                }
            });
        }
예제 #2
0
        public DefaultCosmosContainerProvider(
            ICosmosClientProvider cosmosClientProvider,
            ICosmosPartitionKeyPathProvider cosmosPartitionKeyPathProvider,
            ICosmosContainerNameProvider cosmosContainerNameProvider,
            IOptions <RepositoryOptions> options,
            ILogger <DefaultCosmosContainerProvider <TItem> > logger)
        {
            _cosmosClientProvider = cosmosClientProvider
                                    ?? throw new ArgumentNullException(
                                              nameof(cosmosClientProvider), "Cosmos client provider is required.");

            _cosmosPartitionKeyPathProvider = cosmosPartitionKeyPathProvider
                                              ?? throw new ArgumentNullException(
                                                        nameof(cosmosPartitionKeyPathProvider), "Cosmos partition key name provider is required.");

            _cosmosContainerNameProvider = cosmosContainerNameProvider
                                           ?? throw new ArgumentNullException(
                                                     nameof(cosmosContainerNameProvider), "Cosmos container name provider is required.");

            _options = options?.Value
                       ?? throw new ArgumentNullException(
                                 nameof(options), "Repository options are required.");

            _logger = logger
                      ?? throw new ArgumentNullException($"The {nameof(logger)} is required.");

            if (_options.CosmosConnectionString is null)
            {
                throw new ArgumentNullException($"The {nameof(_options.CosmosConnectionString)} is required.");
            }
            if (_options.ContainerPerItemType is false)
            {
                if (_options.DatabaseId is null)
                {
                    throw new ArgumentNullException(
                              $"The {nameof(_options.DatabaseId)} is required when container per item type is false.");
                }
                if (_options.ContainerId is null)
                {
                    throw new ArgumentNullException(
                              $"The {nameof(_options.ContainerId)} is required when container per item type is false.");
                }
            }

            _lazyContainer = new Lazy <Task <Container> >(async() => await GetContainerValueFactoryAsync());
            _cosmosContainerNameProvider = cosmosContainerNameProvider;
        }
        public DefaultCosmosContainerProvider(
            ICosmosClientProvider cosmosClientProvider,
            ICosmosPartitionKeyPathProvider cosmosPartitionKeyPathProvider,
            IOptions <RepositoryOptions> options,
            ILogger <DefaultCosmosContainerProvider <TItem> > logger)
        {
            _cosmosClientProvider = cosmosClientProvider
                                    ?? throw new ArgumentNullException(
                                              nameof(cosmosClientProvider), "Cosmos client provider is required.");

            _cosmosPartitionKeyPathProvider = cosmosPartitionKeyPathProvider
                                              ?? throw new ArgumentNullException(
                                                        nameof(cosmosPartitionKeyPathProvider), "Cosmos partition key name provider is required.");

            _options = options?.Value
                       ?? throw new ArgumentNullException(
                                 nameof(options), "Repository options are required.");

            if (_options.CosmosConnectionString is null)
            {
                throw new ArgumentNullException($"The {nameof(_options.CosmosConnectionString)} is required.");
            }
            if (_options.ContainerPerItemType is false)
            {
                if (_options.DatabaseId is null)
                {
                    throw new ArgumentNullException(
                              $"The {nameof(_options.DatabaseId)} is required when container per item type is false.");
                }
                if (_options.ContainerId is null)
                {
                    throw new ArgumentNullException(
                              $"The {nameof(_options.ContainerId)} is required when container per item type is false.");
                }
            }
            if (logger is null)
            {
                throw new ArgumentNullException($"The {nameof(logger)} is required.");
            }

            _logger        = logger;
            _lazyContainer = new Lazy <Task <Container> >(async() =>
            {
                try
                {
                    Database database =
                        await _cosmosClientProvider.UseClientAsync(
                            client => client.CreateDatabaseIfNotExistsAsync(_options.DatabaseId)).ConfigureAwait(false);

                    ContainerProperties containerProperties = new ContainerProperties
                    {
                        Id = _options.ContainerPerItemType ? typeof(TItem).Name : _options.ContainerId,
                        PartitionKeyPath = _cosmosPartitionKeyPathProvider.GetPartitionKeyPath <TItem>()
                    };

                    Container container =
                        await database.CreateContainerIfNotExistsAsync(
                            containerProperties).ConfigureAwait(false);

                    return(container);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message, ex);

                    throw;
                }
            });
        }