Пример #1
0
        async Task <Container> GetContainerValueFactoryAsync()
        {
            try
            {
                Database database =
                    await _cosmosClientProvider.UseClientAsync(
                        client => client.CreateDatabaseIfNotExistsAsync(_options.DatabaseId)).ConfigureAwait(false);

                ContainerProperties containerProperties = new()
                {
                    Id = _options.ContainerPerItemType
                        ? _cosmosContainerNameProvider.GetContainerName <TItem>()
                        : _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;
            }
        }
        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;
                }
            });
        }
        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;
                }
            });
        }