diff --git a/Kentico.Kontent.Delivery.Abstractions/IDeliveryClientFactory.cs b/Kentico.Kontent.Delivery.Abstractions/IDeliveryClientFactory.cs index e2cc9b61..5a5d3107 100644 --- a/Kentico.Kontent.Delivery.Abstractions/IDeliveryClientFactory.cs +++ b/Kentico.Kontent.Delivery.Abstractions/IDeliveryClientFactory.cs @@ -1,7 +1,7 @@ namespace Kentico.Kontent.Delivery.Abstractions { /// - /// Defines a methods for getting a + /// Defines a method for getting a named /// public interface IDeliveryClientFactory { @@ -12,10 +12,10 @@ public interface IDeliveryClientFactory /// Returns an instance with the given name. IDeliveryClient Get(string name); - /// - /// Returns a default instance of the . - /// - /// Returns a default instance of the . + /// + /// Returns a default instance of the . + /// + /// Returns a default instance of the . IDeliveryClient Get(); } } diff --git a/Kentico.Kontent.Delivery.Caching.Tests/CacheManagerFactoryTests.cs b/Kentico.Kontent.Delivery.Caching.Tests/CacheManagerFactoryTests.cs new file mode 100644 index 00000000..056a14ce --- /dev/null +++ b/Kentico.Kontent.Delivery.Caching.Tests/CacheManagerFactoryTests.cs @@ -0,0 +1,40 @@ +using FakeItEasy; +using FluentAssertions; +using Kentico.Kontent.Delivery.Caching.Factories; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Options; +using Xunit; + +namespace Kentico.Kontent.Delivery.Caching.Tests +{ + public class CacheManagerFactoryTests + { + private IOptions _options; + private IDistributedCache _distributedCache; + private IMemoryCache _memoryCache; + + public CacheManagerFactoryTests() + { + _options = A.Fake>(); + _distributedCache = A.Fake(); + _memoryCache = A.Fake(); + } + + [Fact] + public void Create_DistributedCache() + { + var deliveryCacheManager = CacheManagerFactory.Create(_distributedCache, _options); + + deliveryCacheManager.Should().NotBeNull(); + } + + [Fact] + public void Create_MemoryCache() + { + var deliveryCacheManager = CacheManagerFactory.Create(_memoryCache, _options); + + deliveryCacheManager.Should().NotBeNull(); + } + } +} diff --git a/Kentico.Kontent.Delivery.Caching.Tests/Factories/DeliveryCacheManagerFactoryTests.cs b/Kentico.Kontent.Delivery.Caching.Tests/Factories/DeliveryCacheManagerFactoryTests.cs deleted file mode 100644 index 60e87813..00000000 --- a/Kentico.Kontent.Delivery.Caching.Tests/Factories/DeliveryCacheManagerFactoryTests.cs +++ /dev/null @@ -1,60 +0,0 @@ -using FluentAssertions; -using Kentico.Kontent.Delivery.Abstractions; -using Kentico.Kontent.Delivery.Caching.Extensions; -using Kentico.Kontent.Delivery.Extensions; -using Microsoft.Extensions.DependencyInjection; -using System; -using Xunit; - -namespace Kentico.Kontent.Delivery.Caching.Tests.Factories -{ - public class DeliveryCacheManagerFactoryTests - { - private readonly ServiceCollection _serviceCollection; - - private const string _clientName = "ClientName"; - - public DeliveryCacheManagerFactoryTests() - { - _serviceCollection = new ServiceCollection(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void GetNamedDeliveryCacheManager_WithCorrectName_GetDeliveryCacheManager(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient(_clientName, new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache(_clientName, new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var result = factory.Get(_clientName); - - result.Should().NotBeNull(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void GetNamedDeliveryCacheManager_WithWrongName_GetNull(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var result = factory.Get("WrongName"); - - result.Should().BeNull(); - } - } -} diff --git a/Kentico.Kontent.Delivery.Caching.Tests/ServiceCollectionExtensionsTests.cs b/Kentico.Kontent.Delivery.Caching.Tests/ServiceCollectionExtensionsTests.cs index 995fc50a..56808943 100644 --- a/Kentico.Kontent.Delivery.Caching.Tests/ServiceCollectionExtensionsTests.cs +++ b/Kentico.Kontent.Delivery.Caching.Tests/ServiceCollectionExtensionsTests.cs @@ -26,112 +26,16 @@ public void AddDeliveryClientCacheWithDeliveryCacheOptions_ThrowsMissingTypeRegi Assert.Throws(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions() { CacheType = cacheType })); } - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void AddDeliveryClient_WithNoCache_GetClient(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var client = factory.Get(); - - client.Should().NotBeNull(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void AddDeliveryClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient(new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var client = factory.Get("WrongName"); - - client.Should().BeNull(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNamedClient(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var client = factory.Get("named"); - - client.Should().NotBeNull(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void AddDeliveryNamedClient_CacheWithDeliveryCacheOptions_GetNull(CacheTypeEnum cacheType) - { - _serviceCollection.AddDeliveryClient("named", new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }); - _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions() - { - CacheType = cacheType - }); - - var sp = _serviceCollection.BuildServiceProvider(); - var factory = sp.GetRequiredService(); - - var client = factory.Get("WrongName"); - - client.Should().BeNull(); - } - - [Theory] - [InlineData(CacheTypeEnum.Memory)] - [InlineData(CacheTypeEnum.Distributed)] - public void AddDeliveryClientCacheNamedWithDeliveryCacheOptions_ThrowsInvalidOperationException(CacheTypeEnum cacheType) - { - Assert.Throws(() => _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions() { CacheType = cacheType })); - } - [Fact] public void AddDeliveryClientCacheWithNullDeliveryCacheOptions_ThrowsArgumentNullException() { Assert.Throws(() => _serviceCollection.AddDeliveryClientCache(null)); } - [Fact] - public void AddDeliveryClientCacheNamedWithNullDeliveryCacheOptions_ThrowsArgumentNullException() - { - Assert.Throws(() => _serviceCollection.AddDeliveryClientCache("named", null)); - } - [Fact] public void AddDeliveryClientCacheWitNoPreviousRegistrationDeliveryClient_ThrowsMissingTypeRegistrationException() { Assert.Throws(() => _serviceCollection.AddDeliveryClientCache(new DeliveryCacheOptions())); } - - [Fact] - public void AddDeliveryClientNamedCacheWitNoPreviousRegistrationDeliveryClient_ThrowsMissingTypeRegistrationException() - { - Assert.Throws(() => _serviceCollection.AddDeliveryClientCache("named", new DeliveryCacheOptions())); - } } } diff --git a/Kentico.Kontent.Delivery.Caching/DeliveryCacheOptions.cs b/Kentico.Kontent.Delivery.Caching/DeliveryCacheOptions.cs index d3f0bd95..8d8d9a70 100644 --- a/Kentico.Kontent.Delivery.Caching/DeliveryCacheOptions.cs +++ b/Kentico.Kontent.Delivery.Caching/DeliveryCacheOptions.cs @@ -31,6 +31,6 @@ public class DeliveryCacheOptions /// /// Name of an instance the options are bound to. /// - internal string Name { get; set; } + public string Name { get; set; } } } diff --git a/Kentico.Kontent.Delivery.Caching/Extensions/DeliveryCacheOptionsExtensions.cs b/Kentico.Kontent.Delivery.Caching/Extensions/DeliveryCacheOptionsExtensions.cs index 0c70adac..d54e695d 100644 --- a/Kentico.Kontent.Delivery.Caching/Extensions/DeliveryCacheOptionsExtensions.cs +++ b/Kentico.Kontent.Delivery.Caching/Extensions/DeliveryCacheOptionsExtensions.cs @@ -8,7 +8,7 @@ public static void Configure(this DeliveryCacheOptions o, DeliveryCacheOptions o o.DefaultExpiration = options.DefaultExpiration; o.DefaultExpirationType = options.DefaultExpirationType; o.StaleContentExpiration = options.StaleContentExpiration; - o.Name = o.Name; + o.Name = options.Name; } } } diff --git a/Kentico.Kontent.Delivery.Caching/Extensions/ServiceCollectionExtensions.cs b/Kentico.Kontent.Delivery.Caching/Extensions/ServiceCollectionExtensions.cs index 423e241b..986a6d29 100644 --- a/Kentico.Kontent.Delivery.Caching/Extensions/ServiceCollectionExtensions.cs +++ b/Kentico.Kontent.Delivery.Caching/Extensions/ServiceCollectionExtensions.cs @@ -32,26 +32,13 @@ public static IServiceCollection AddDeliveryClientCache(this IServiceCollection } /// - /// Registers a delegate that will be used to configure a cached . + /// Registers a . /// /// A instance for registering and resolving dependencies. - /// A name of named client which want to use cached - /// A instance. - /// The instance with cache services registered in it - public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options) - { - if (options == null) - { - throw new ArgumentNullException(nameof(options), "The Delivery cache options object is not specified."); - } - - return services - .RegisterCacheOptions(options, name) - .RegisterDependencies(options.CacheType, name) - .Decorate(); - } - - private static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options, string name = null) + /// A instance. + /// A name of named client which want to use cached. + /// + public static IServiceCollection RegisterCacheOptions(this IServiceCollection services, DeliveryCacheOptions options, string name = null) { if (name == null) { @@ -65,7 +52,14 @@ private static IServiceCollection RegisterCacheOptions(this IServiceCollection s return services; } - private static IServiceCollection RegisterDependencies(this IServiceCollection services, CacheTypeEnum cacheType, string name = null) + /// + /// Registers cache dependencies. + /// + /// A instance for registering and resolving dependencies. + /// A + /// A name of named client which want to use cached. + /// + public static IServiceCollection RegisterDependencies(this IServiceCollection services, CacheTypeEnum cacheType, string name = null) { switch (cacheType) { diff --git a/Kentico.Kontent.Delivery.Caching/Factories/CacheManagerFactory.cs b/Kentico.Kontent.Delivery.Caching/Factories/CacheManagerFactory.cs new file mode 100644 index 00000000..762ac811 --- /dev/null +++ b/Kentico.Kontent.Delivery.Caching/Factories/CacheManagerFactory.cs @@ -0,0 +1,37 @@ +using Kentico.Kontent.Delivery.Abstractions; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Options; + +namespace Kentico.Kontent.Delivery.Caching.Factories +{ + /// + /// A factory for manually create an instance. + /// + public static class CacheManagerFactory + { + /// + /// Creates an instance with a distributed cache. + /// + /// A instance. + /// A + /// The instance with a distribute cache. + public static IDeliveryCacheManager Create(IDistributedCache distributedCache, + IOptions options) + { + return new DistributedCacheManager(distributedCache, options); + } + + /// + /// Creates an instance with a memory cache. + /// + /// A instance. + /// A + /// The instance with a memory cache. + public static IDeliveryCacheManager Create(IMemoryCache memoryCache, + IOptions options) + { + return new MemoryCacheManager(memoryCache, options); + } + } +} diff --git a/Kentico.Kontent.Delivery.Caching/Kentico.Kontent.Delivery.Caching.csproj b/Kentico.Kontent.Delivery.Caching/Kentico.Kontent.Delivery.Caching.csproj index ba95f55f..587550c6 100644 --- a/Kentico.Kontent.Delivery.Caching/Kentico.Kontent.Delivery.Caching.csproj +++ b/Kentico.Kontent.Delivery.Caching/Kentico.Kontent.Delivery.Caching.csproj @@ -37,6 +37,9 @@ <_Parameter1>Kentico.Kontent.Delivery.Caching.Tests + + <_Parameter1>Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests + diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientCacheFactoryTests.cs b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientCacheFactoryTests.cs new file mode 100644 index 00000000..0b27c5a7 --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientCacheFactoryTests.cs @@ -0,0 +1,55 @@ +using FakeItEasy; +using FluentAssertions; +using Kentico.Kontent.Delivery.Abstractions; +using Kentico.Kontent.Delivery.Caching; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using Xunit; + +namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests +{ + public class DeliveryClientCacheFactoryTests + { + private readonly IOptionsMonitor _deliveryCacheOptionsMock; + private readonly IDeliveryClientFactory _innerDeliveryClientFactoryMock; + private readonly IServiceProvider _serviceProvider; + + private const string _clientName = "ClientName"; + + public DeliveryClientCacheFactoryTests() + { + _deliveryCacheOptionsMock = A.Fake>(); + _innerDeliveryClientFactoryMock = A.Fake(); + _serviceProvider = new ServiceCollection().BuildServiceProvider(); + } + + [Fact] + public void GetNamedCacheClient_WithCorrectName_GetClient() + { + var deliveryCacheOptions = new DeliveryCacheOptions(); + A.CallTo(() => _deliveryCacheOptionsMock.Get(_clientName)) + .Returns(deliveryCacheOptions); + + var deliveryClientFactory = new DeliveryClientCacheFactory(_innerDeliveryClientFactoryMock, _deliveryCacheOptionsMock, _serviceProvider); + + var result = deliveryClientFactory.Get(_clientName); + + result.Should().NotBeNull(); + } + + [Fact] + public void GetNamedCacheClient_WithWrongName_GetNull() + { + var deliveryCacheOptions = new DeliveryCacheOptions(); + A.CallTo(() => _deliveryCacheOptionsMock.Get(_clientName)) + .Returns(deliveryCacheOptions); + + var deliveryClientFactory = new DeliveryClientCacheFactory(_innerDeliveryClientFactoryMock, _deliveryCacheOptionsMock, _serviceProvider); + + var result = deliveryClientFactory.Get("WrongName"); + + result.Should().NotBeNull(); + } + } +} diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientFactoryTests.cs b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientFactoryTests.cs new file mode 100644 index 00000000..8d12db1c --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/DeliveryClientFactoryTests.cs @@ -0,0 +1,55 @@ +using Autofac; +using FakeItEasy; +using FluentAssertions; +using Kentico.Kontent.Delivery.Abstractions; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using Xunit; + +namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests +{ + public class DeliveryClientFactoryTests + { + private readonly IOptionsMonitor _deliveryOptionsMock; + private IComponentContext _componentContext; + private readonly IServiceProvider _serviceProvider; + + private const string _clientName = "ClientName"; + + public DeliveryClientFactoryTests() + { + _deliveryOptionsMock = A.Fake>(); + _componentContext = A.Fake(); + _serviceProvider = new ServiceCollection().BuildServiceProvider(); + } + + [Fact] + public void GetNamedClient_WithCorrectName_GetClient() + { + var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }; + A.CallTo(() => _deliveryOptionsMock.Get(_clientName)) + .Returns(deliveryOptions); + + var deliveryClientFactory = new DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider, _componentContext); + + var result = deliveryClientFactory.Get(_clientName); + + result.Should().NotBeNull(); + } + + [Fact] + public void GetNamedClient_WithWrongName_GetNull() + { + var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }; + A.CallTo(() => _deliveryOptionsMock.Get(_clientName)) + .Returns(deliveryOptions); + + var deliveryClientFactory = new DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider, _componentContext); + + var result = deliveryClientFactory.Get("WrongName"); + + result.Should().BeNull(); + } + } +} diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests.csproj b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests.csproj new file mode 100644 index 00000000..eb87b07a --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests.csproj @@ -0,0 +1,29 @@ + + + + net5.0 + + false + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/Kentico.Kontent.Delivery.Caching/DeliveryClientCacheFactory.cs b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientCacheFactory.cs similarity index 63% rename from Kentico.Kontent.Delivery.Caching/DeliveryClientCacheFactory.cs rename to Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientCacheFactory.cs index a61e9538..27b0e37a 100644 --- a/Kentico.Kontent.Delivery.Caching/DeliveryClientCacheFactory.cs +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientCacheFactory.cs @@ -1,4 +1,6 @@ using Kentico.Kontent.Delivery.Abstractions; +using Kentico.Kontent.Delivery.Caching; +using Kentico.Kontent.Delivery.Caching.Factories; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; @@ -6,36 +8,28 @@ using System; using System.Collections.Concurrent; -namespace Kentico.Kontent.Delivery.Caching +namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection { - /// - /// A factory class for - /// - public class DeliveryClientCacheFactory : IDeliveryClientFactory + internal class DeliveryClientCacheFactory : IDeliveryClientFactory { - private readonly IDeliveryClientFactory _innerClientFactory; private readonly IOptionsMonitor _deliveryCacheOptions; private readonly IServiceProvider _serviceProvider; + private readonly IDeliveryClientFactory _innerDeliveryClientFactory; private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); /// /// Initializes a new instance of the class. /// - /// Factory to be decorated. + /// Factory to be decorated. /// Cache configuration options. /// An instance. - public DeliveryClientCacheFactory(IDeliveryClientFactory clientFactory, IOptionsMonitor deliveryCacheOptions, IServiceProvider serviceProvider) + public DeliveryClientCacheFactory(IDeliveryClientFactory deliveryClientFactory, IOptionsMonitor deliveryCacheOptions, IServiceProvider serviceProvider) { - _innerClientFactory = clientFactory; _deliveryCacheOptions = deliveryCacheOptions; _serviceProvider = serviceProvider; + _innerDeliveryClientFactory = deliveryClientFactory; } - /// - /// Returns a named instance of the wrapped in a caching layer. - /// - /// A name of the configuration to be used to instantiate the client. - /// Returns an instance with the given name. public IDeliveryClient Get(string name) { if (name == null) @@ -45,7 +39,7 @@ public IDeliveryClient Get(string name) if (!_cache.TryGetValue(name, out var client)) { - client = _innerClientFactory.Get(name); + client = _innerDeliveryClientFactory.Get(name); var cacheOptions = _deliveryCacheOptions.Get(name); if (cacheOptions.Name == name) { @@ -54,12 +48,12 @@ public IDeliveryClient Get(string name) if (cacheOptions.CacheType == CacheTypeEnum.Memory) { var memoryCache = _serviceProvider.GetService(); - manager = new MemoryCacheManager(memoryCache, Options.Create(cacheOptions)); + manager = CacheManagerFactory.Create(memoryCache, Options.Create(cacheOptions)); } else { var distributedCache = _serviceProvider.GetService(); - manager = new DistributedCacheManager(distributedCache, Options.Create(cacheOptions)); + manager = CacheManagerFactory.Create(distributedCache, Options.Create(cacheOptions)); } // Decorate the client with a caching layer @@ -72,10 +66,6 @@ public IDeliveryClient Get(string name) return client; } - /// - public IDeliveryClient Get() - { - return _innerClientFactory.Get(); - } + public IDeliveryClient Get() => _innerDeliveryClientFactory.Get(); } } diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientFactory.cs b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientFactory.cs new file mode 100644 index 00000000..d402e876 --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/DeliveryClientFactory.cs @@ -0,0 +1,88 @@ +using Autofac; +using Autofac.Core.Registration; +using Kentico.Kontent.Delivery.Abstractions; +using Kentico.Kontent.Delivery.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using System; +using System.Collections.Concurrent; + +namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection +{ + internal class DeliveryClientFactory : IDeliveryClientFactory + { + private readonly IOptionsMonitor _deliveryOptions; + private readonly IServiceProvider _serviceProvider; + private readonly IComponentContext _componentContext; + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); + + /// + /// Initializes a new instance of the class. + /// + /// Used for notifications when instances change. + /// An instance. + /// An autofac component context. + public DeliveryClientFactory(IOptionsMonitor deliveryOptions, IServiceProvider serviceProvider, + IComponentContext componentContext) + { + _deliveryOptions = deliveryOptions; + _serviceProvider = serviceProvider; + _componentContext = componentContext; + } + + /// + public IDeliveryClient Get(string name) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (!_cache.TryGetValue(name, out var client)) + { + var deliveryClientOptions = _deliveryOptions.Get(name); + + // Validate that the option object is indeed configured + if (deliveryClientOptions.ProjectId != null) + { + client = Build(deliveryClientOptions, name); + + _cache.TryAdd(name, client); + } + } + + return client; + } + + public IDeliveryClient Get() => GetService(); + + private IDeliveryClient Build(DeliveryOptions options, string name) + { + return Delivery.DeliveryClientFactory.Create( + new DeliveryOptionsMonitor(options, name), + GetNamedServiceOrDefault(name), + GetNamedServiceOrDefault(name), + GetNamedServiceOrDefault(name), + GetNamedServiceOrDefault(name), + GetNamedServiceOrDefault(name)); + } + + private T GetNamedServiceOrDefault(string name) + { + try + { + return _componentContext.ResolveNamed(name); + } + catch (ComponentNotRegisteredException) + { + return GetService(); + } + } + + private T GetService() + { + return _serviceProvider.GetService(); + } + } +} diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Extensions/ServiceCollectionExtensions.cs b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..fc2aafe4 --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,108 @@ +using Kentico.Kontent.Delivery.Abstractions; +using Kentico.Kontent.Delivery.Caching; +using Kentico.Kontent.Delivery.Caching.Extensions; +using Kentico.Kontent.Delivery.Configuration; +using Kentico.Kontent.Delivery.Helpers; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; + +namespace Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Extensions +{ + /// + /// A class which contains extension methods on for registering an instance. + /// + public static class ServiceCollectionExtensions + { + /// + /// Registers a delegate that will be used to configure a named via + /// + ///The name of the client configuration + /// A instance for registering and resolving dependencies. + /// A function that is provided with an instance of and expected to return a valid instance of . + /// The instance with registered in it + public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, Func buildDeliveryOptions) + { + if (buildDeliveryOptions == null) + { + throw new ArgumentNullException(nameof(buildDeliveryOptions), "The function for creating Delivery options is null."); + } + + return services + .RegisterOptions(DeliveryOptionsHelpers.Build(buildDeliveryOptions), name) + .RegisterDependencies(true) + .RegisterDeliveryClientFactory(); + } + + /// + /// Registers a delegate that will be used to configure a named via + /// + ///The name of the client configuration + /// A instance for registering and resolving dependencies. + /// A instance. Options themselves are not further validated (see ). + /// The instance with registered in it + public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, DeliveryOptions deliveryOptions) + { + if (deliveryOptions == null) + { + throw new ArgumentNullException(nameof(deliveryOptions), "The Delivery options object is not specified."); + } + + return services + .RegisterOptions(deliveryOptions, name) + .RegisterDependencies(true) + .RegisterDeliveryClientFactory(); + } + + /// + /// Registers a delegate that will be used to configure a named via + /// + /// A instance for registering and resolving dependencies. + /// The name of the client configuration + /// A set of key/value application configuration properties. + /// The section name of the configuration that keeps the properties. The default value is DeliveryOptions. + /// The instance with registered in it + public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, IConfiguration configuration, string configurationSectionName = "DeliveryOptions") + { + return services + .Configure(name, configuration.GetSection(configurationSectionName)) + .RegisterDependencies(true) + .RegisterDeliveryClientFactory(); + } + + /// + /// Registers a delegate that will be used to configure a cached . + /// + /// A instance for registering and resolving dependencies. + /// A name of named client which want to use cached. + /// A instance. + /// The instance with cache services registered in it + public static IServiceCollection AddDeliveryClientCache(this IServiceCollection services, string name, DeliveryCacheOptions options) + { + if (options == null) + { + throw new ArgumentNullException(nameof(options), "The Delivery cache options object is not specified."); + } + + return services + .RegisterCacheOptions(options, name) + .RegisterDependencies(options.CacheType, name) + .RegisterDeliveryClientFactoryCacheDecorator(); + } + + private static IServiceCollection RegisterDeliveryClientFactoryCacheDecorator(this IServiceCollection services) + { + return services.Decorate(); + } + + private static IServiceCollection RegisterDeliveryClientFactory(this IServiceCollection services) + { + return services.AddSingleton(); + } + + private static IServiceCollection RegisterOptions(this IServiceCollection services, DeliveryOptions options, string name) + { + return services.Configure(name, (o) => o.Configure(options)); + } + } +} diff --git a/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.csproj b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.csproj new file mode 100644 index 00000000..4025d551 --- /dev/null +++ b/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection/Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.csproj @@ -0,0 +1,41 @@ + + + + netstandard2.0 + Kentico Software + Kentico Kontent + © 2020 Kentico Software. All rights reserved. + Kentico Kontent Delivery Extensions Autofac Dependency Injection + MIT + https://github.com/Kentico/kontent-delivery-sdk-net + https://github.com/Kentico/Home/blob/master/images/kk-logo-nuget.png?raw=true + https://github.com/Kentico/kontent-delivery-sdk-net.git + git + kentico;mvc;aspnet;aspnetmvc;extensions + true + true + true + true + snupkg + + + + + <_Parameter1>Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests + + + + + + + + + + + + + + + + + diff --git a/Kentico.Kontent.Delivery.Tests/Extensions/ServiceCollectionsExtensionsTests.cs b/Kentico.Kontent.Delivery.Tests/Extensions/ServiceCollectionsExtensionsTests.cs index c5363809..1911ff80 100644 --- a/Kentico.Kontent.Delivery.Tests/Extensions/ServiceCollectionsExtensionsTests.cs +++ b/Kentico.Kontent.Delivery.Tests/Extensions/ServiceCollectionsExtensionsTests.cs @@ -33,7 +33,6 @@ public class ServiceCollectionsExtensionsTests { typeof(IPropertyMapper), typeof(PropertyMapper) }, { typeof(IRetryPolicyProvider), typeof(DefaultRetryPolicyProvider) }, { typeof(IDeliveryClient), typeof(DeliveryClient) }, - { typeof(IDeliveryClientFactory), typeof(DeliveryClientFactory) }, } ); @@ -52,18 +51,6 @@ public ServiceCollectionsExtensionsTests() _serviceCollection = new ServiceCollection(); } - [Fact] - public void AddDeliveryFactoryClientWithNullDeliveryOptionsBuilder_ThrowsArgumentNullException() - { - Assert.Throws(() => _serviceCollection.AddDeliveryClient("named", buildDeliveryOptions: null)); - } - - [Fact] - public void AddDeliveryFactoryClientWithNullDeliveryOptions_ThrowsArgumentNullException() - { - Assert.Throws(() => _serviceCollection.AddDeliveryClient("named", deliveryOptions: null)); - } - [Fact] public void AddDeliveryClientWithNullDeliveryOptions_ThrowsArgumentNullException() { @@ -84,38 +71,6 @@ public void AddDeliveryClientWithOptions_AllServicesAreRegistered() AssertDefaultServiceCollection(provider, _expectedInterfacesWithImplementationTypes); } - [Fact] - public void AddDeliveryClientFactoryWithOptions_AllServicesAreRegistered() - { - _serviceCollection.AddDeliveryClient("named", new DeliveryOptions { ProjectId = ProjectId }); - var provider = _serviceCollection.BuildServiceProvider(); - AssertDefaultServiceCollection(provider, _expectedInterfacesWithImplementationTypes); - } - - [Fact] - public void AddDeliveryClientFactoryWithDeliveryClient_AllServicesAreRegistered() - { - _serviceCollection.AddDeliveryClient("named", (builder) => - builder.WithProjectId(ProjectId) - .UseProductionApi() - .Build()); - - var provider = _serviceCollection.BuildServiceProvider(); - AssertDefaultServiceCollection(provider, _expectedInterfacesWithImplementationTypes); - } - - [Fact] - public void AddDeliveryClientFactoryWithOptions_DeliveryClientIsRegistered() - { - _serviceCollection.AddDeliveryClient("named", new DeliveryOptions { ProjectId = ProjectId }); - var provider = _serviceCollection.BuildServiceProvider(); - var deliveryClientFactory = provider.GetRequiredService(); - - var deliveryClient = deliveryClientFactory.Get("named"); - - Assert.NotNull(deliveryClient); - } - [Theory] [MemberData(nameof(DeliveryOptionsConfigurationParameters))] public void AddDeliveryClientWithConfiguration_AllServicesAreRegistered(string fileNamePostfix, string customSectionName = null) diff --git a/Kentico.Kontent.Delivery.Tests/Factories/DeliveryClientFactoryTests.cs b/Kentico.Kontent.Delivery.Tests/Factories/DeliveryClientFactoryTests.cs index daabfea3..79b5717b 100644 --- a/Kentico.Kontent.Delivery.Tests/Factories/DeliveryClientFactoryTests.cs +++ b/Kentico.Kontent.Delivery.Tests/Factories/DeliveryClientFactoryTests.cs @@ -1,52 +1,43 @@ using System; -using FakeItEasy; using FluentAssertions; using Kentico.Kontent.Delivery.Abstractions; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Xunit; +using Kentico.Kontent.Delivery.Extensions; namespace Kentico.Kontent.Delivery.Tests.Factories { public class DeliveryClientFactoryTests { - private readonly IOptionsMonitor _deliveryOptionsMock; - private readonly IServiceProvider _serviceProvider; - - private const string _clientName = "ClientName"; + private readonly ServiceCollection _serviceCollection; public DeliveryClientFactoryTests() { - _deliveryOptionsMock = A.Fake>(); - _serviceProvider = new ServiceCollection().BuildServiceProvider(); + _serviceCollection = new ServiceCollection(); } [Fact] - public void GetNamedClient_WithCorrectName_GetClient() + public void GetNamedClient_GetNull() { - var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }; - A.CallTo(() => _deliveryOptionsMock.Get(_clientName)) - .Returns(deliveryOptions); - - var deliveryClientFactory = new Delivery.DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider); + var deliveryClientFactory = new Delivery.DeliveryClientFactory(_serviceCollection.BuildServiceProvider()); - var result = deliveryClientFactory.Get(_clientName); + var result = deliveryClientFactory.Get("clientName"); - result.Should().NotBeNull(); + result.Should().BeNull(); } [Fact] - public void GetNamedClient_WithWrongName_GetNull() + public void GetClient_GetClient() { - var deliveryOptions = new DeliveryOptions() { ProjectId = Guid.NewGuid().ToString() }; - A.CallTo(() => _deliveryOptionsMock.Get(_clientName)) - .Returns(deliveryOptions); + _serviceCollection.AddDeliveryClient(new DeliveryOptions + { + ProjectId = Guid.NewGuid().ToString() + }); + var deliveryClientFactory = new Delivery.DeliveryClientFactory(_serviceCollection.BuildServiceProvider()); - var deliveryClientFactory = new Delivery.DeliveryClientFactory(_deliveryOptionsMock, _serviceProvider); + var result = deliveryClientFactory.Get(); - var result = deliveryClientFactory.Get("WrongName"); - - result.Should().BeNull(); + result.Should().NotBeNull(); } } } diff --git a/Kentico.Kontent.Delivery.sln b/Kentico.Kontent.Delivery.sln index 0eb9038b..a083bf85 100644 --- a/Kentico.Kontent.Delivery.sln +++ b/Kentico.Kontent.Delivery.sln @@ -26,7 +26,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Kontent.ImageTransf EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Kontent.ImageTransformation.Tests", "Kentico.Kontent.ImageTransformation.Tests\Kentico.Kontent.ImageTransformation.Tests.csproj", "{8942D9E3-A966-406C-AA19-2B85286744C9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kentico.Kontent.Delivery.Benchmarks", "Kentico.Kontent.Delivery.Benchmarks\Kentico.Kontent.Delivery.Benchmarks.csproj", "{FAA8F5E9-A262-4F7E-AD38-E72B8A5137F6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Kontent.Delivery.Benchmarks", "Kentico.Kontent.Delivery.Benchmarks\Kentico.Kontent.Delivery.Benchmarks.csproj", "{FAA8F5E9-A262-4F7E-AD38-E72B8A5137F6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection", "Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection\Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.csproj", "{90E5B147-37F0-4BE7-95BA-DC8D142FD8C7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests", "Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests\Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests.csproj", "{CDCEB346-D1BA-4CF3-9553-298465AC3FF8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -74,6 +78,14 @@ Global {FAA8F5E9-A262-4F7E-AD38-E72B8A5137F6}.Debug|Any CPU.Build.0 = Debug|Any CPU {FAA8F5E9-A262-4F7E-AD38-E72B8A5137F6}.Release|Any CPU.ActiveCfg = Release|Any CPU {FAA8F5E9-A262-4F7E-AD38-E72B8A5137F6}.Release|Any CPU.Build.0 = Release|Any CPU + {90E5B147-37F0-4BE7-95BA-DC8D142FD8C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {90E5B147-37F0-4BE7-95BA-DC8D142FD8C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {90E5B147-37F0-4BE7-95BA-DC8D142FD8C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {90E5B147-37F0-4BE7-95BA-DC8D142FD8C7}.Release|Any CPU.Build.0 = Release|Any CPU + {CDCEB346-D1BA-4CF3-9553-298465AC3FF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDCEB346-D1BA-4CF3-9553-298465AC3FF8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDCEB346-D1BA-4CF3-9553-298465AC3FF8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDCEB346-D1BA-4CF3-9553-298465AC3FF8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Kentico.Kontent.Delivery/DeliveryClientFactory.cs b/Kentico.Kontent.Delivery/DeliveryClientFactory.cs index 93b4860c..01ed12dd 100644 --- a/Kentico.Kontent.Delivery/DeliveryClientFactory.cs +++ b/Kentico.Kontent.Delivery/DeliveryClientFactory.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Concurrent; using Kentico.Kontent.Delivery.Abstractions; -using Kentico.Kontent.Delivery.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Newtonsoft.Json; @@ -11,67 +9,52 @@ namespace Kentico.Kontent.Delivery /// /// A factory class for /// - public class DeliveryClientFactory : IDeliveryClientFactory + public sealed class DeliveryClientFactory : IDeliveryClientFactory { - private readonly IOptionsMonitor _deliveryOptions; private readonly IServiceProvider _serviceProvider; - private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); /// /// Initializes a new instance of the class. /// - /// Used for notifications when instances change. /// An instance. - public DeliveryClientFactory(IOptionsMonitor deliveryOptions, IServiceProvider serviceProvider) + public DeliveryClientFactory(IServiceProvider serviceProvider) { - _deliveryOptions = deliveryOptions; _serviceProvider = serviceProvider; } /// - public IDeliveryClient Get(string name) - { - if (name == null) - { - throw new ArgumentNullException(nameof(name)); - } - - if (!_cache.TryGetValue(name, out var client)) - { - var deliveryClientOptions = _deliveryOptions.Get(name); - - // Validate that the option object is indeed configured - if (deliveryClientOptions.ProjectId != null) - { - client = Build(deliveryClientOptions, name); - - _cache.TryAdd(name, client); - } - } - - return client; - } + public IDeliveryClient Get(string name) => null; - /// + /// public IDeliveryClient Get() { return _serviceProvider.GetRequiredService(); } - private IDeliveryClient Build(DeliveryOptions options, string name) - { - return new DeliveryClient( - new DeliveryOptionsMonitor(options, name), - GetService(), - GetService(), - GetService(), - GetService(), - GetService()); - } - - private T GetService() + /// + /// Creates a instance manually. + /// + /// A + /// A instance. + /// A instance. + /// A instance. + /// A instance. + /// A instance. + /// + public static IDeliveryClient Create( + IOptionsMonitor options, + IModelProvider modelProvider, + IRetryPolicyProvider retryPolicyProvider, + ITypeProvider typeProvider, + IDeliveryHttpClient deliveryHttpClient, + JsonSerializer jsonSerializer) { - return _serviceProvider.GetService(); + return new DeliveryClient(options, + modelProvider, + retryPolicyProvider, + typeProvider, + deliveryHttpClient, + jsonSerializer); } } } diff --git a/Kentico.Kontent.Delivery/Extensions/DeliveryOptionsExtensions.cs b/Kentico.Kontent.Delivery/Extensions/DeliveryOptionsExtensions.cs index 21ea8b78..0c89b7f3 100644 --- a/Kentico.Kontent.Delivery/Extensions/DeliveryOptionsExtensions.cs +++ b/Kentico.Kontent.Delivery/Extensions/DeliveryOptionsExtensions.cs @@ -2,8 +2,16 @@ namespace Kentico.Kontent.Delivery.Extensions { - internal static class DeliveryOptionsExtensions + /// + /// Extensions for a + /// + public static class DeliveryOptionsExtensions { + /// + /// Maps a to each other. + /// + /// + /// public static void Configure(this DeliveryOptions o, DeliveryOptions options) { o.ProjectId = options.ProjectId; diff --git a/Kentico.Kontent.Delivery/Extensions/ServiceCollectionExtensions.cs b/Kentico.Kontent.Delivery/Extensions/ServiceCollectionExtensions.cs index ec425480..bbaa4d9c 100644 --- a/Kentico.Kontent.Delivery/Extensions/ServiceCollectionExtensions.cs +++ b/Kentico.Kontent.Delivery/Extensions/ServiceCollectionExtensions.cs @@ -7,6 +7,7 @@ using Kentico.Kontent.Delivery.ContentItems; using Kentico.Kontent.Delivery.ContentItems.ContentLinks; using Kentico.Kontent.Delivery.ContentItems.InlineContentItems; +using Kentico.Kontent.Delivery.Helpers; using Kentico.Kontent.Delivery.RetryPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -21,59 +22,6 @@ namespace Kentico.Kontent.Delivery.Extensions /// public static class ServiceCollectionExtensions { - /// - /// Registers a delegate that will be used to configure a named via - /// - ///The name of the client configuration - /// A instance for registering and resolving dependencies. - /// A function that is provided with an instance of and expected to return a valid instance of . - /// The instance with registered in it - public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, Func buildDeliveryOptions) - { - if (buildDeliveryOptions == null) - { - throw new ArgumentNullException(nameof(buildDeliveryOptions), "The function for creating Delivery options is null."); - } - - return services - .BuildOptions(buildDeliveryOptions, name) - .RegisterDependencies(); - } - - /// - /// Registers a delegate that will be used to configure a named via - /// - ///The name of the client configuration - /// A instance for registering and resolving dependencies. - /// A instance. Options themselves are not further validated (see ). - /// The instance with registered in it - public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, DeliveryOptions deliveryOptions) - { - if (deliveryOptions == null) - { - throw new ArgumentNullException(nameof(deliveryOptions), "The Delivery options object is not specified."); - } - - return services - .RegisterOptions(deliveryOptions, name) - .RegisterDependencies(); - } - - /// - /// Registers a delegate that will be used to configure a named via - /// - /// A instance for registering and resolving dependencies. - /// The name of the client configuration - /// A set of key/value application configuration properties. - /// The section name of the configuration that keeps the properties. The default value is DeliveryOptions. - /// The instance with registered in it - public static IServiceCollection AddDeliveryClient(this IServiceCollection services, string name, IConfiguration configuration, string configurationSectionName = "DeliveryOptions") - { - return services - .Configure(name, configuration.GetSection(configurationSectionName)) - .RegisterDependencies(); - } - /// /// Registers a instance to an interface in . /// @@ -88,7 +36,7 @@ public static IServiceCollection AddDeliveryClient(this IServiceCollection servi } return services - .BuildOptions(buildDeliveryOptions) + .RegisterOptions(DeliveryOptionsHelpers.Build(buildDeliveryOptions)) .RegisterDependencies(); } @@ -147,20 +95,17 @@ public static IServiceCollection AddDeliveryInlineContentItemsResolver, TInlineContentItemsResolver>() .AddSingleton(provider => provider.CreateDescriptor()); - private static void TryAddDeliveryInlineContentItemsResolver(this IServiceCollection services) - where TInlineContentItemsResolver : class, IInlineContentItemsResolver + /// + /// Registers dependencies. + /// + /// A instance for registering and resolving dependencies. + /// A parameter indicates if the registration is for a named client. + /// The instance with dependencies. + public static IServiceCollection RegisterDependencies(this IServiceCollection services, bool isNamedRegistration = false) { - if (services.Any(descriptor => descriptor.ServiceType == typeof(IInlineContentItemsResolver))) - return; + if (!isNamedRegistration) + services.TryAddSingleton(); - services.AddDeliveryInlineContentItemsResolver(); - } - - private static ITypelessInlineContentItemsResolver CreateDescriptor(this IServiceProvider provider) - => TypelessInlineContentItemsResolver.Create(provider.GetService>()); - - private static IServiceCollection RegisterDependencies(this IServiceCollection services) - { services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(new DeliveryHttpClient(new HttpClient())); @@ -173,39 +118,32 @@ private static IServiceCollection RegisterDependencies(this IServiceCollection s services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(new DeliveryJsonSerializer()); - services.TryAddSingleton(); services.TryAddSingleton(); return services; } - // Options here are not validated on purpose, it is left to users to validate them if they want to. - private static IServiceCollection RegisterOptions(this IServiceCollection services, DeliveryOptions options, string name = null) + private static IServiceCollection RegisterOptions(this IServiceCollection services, DeliveryOptions options) { - if (name == null) - { - services.Configure((o) => o.Configure(options)); - } - else - { - services.Configure(name, (o) => o.Configure(options)); - } + return services.Configure((o) => o.Configure(options)); + } - return services; + private static void TryAddDeliveryInlineContentItemsResolver(this IServiceCollection services) + where TInlineContentItemsResolver : class, IInlineContentItemsResolver + { + if (services.Any(descriptor => descriptor.ServiceType == typeof(IInlineContentItemsResolver))) + return; + + services.AddDeliveryInlineContentItemsResolver(); } + private static ITypelessInlineContentItemsResolver CreateDescriptor(this IServiceProvider provider) + => TypelessInlineContentItemsResolver.Create(provider.GetService>()); + private static IServiceCollection LoadOptionsConfiguration(this IServiceCollection services, IConfiguration configuration, string configurationSectionName) => services .Configure(configurationSectionName == null ? configuration : configuration.GetSection(configurationSectionName)); - - private static IServiceCollection BuildOptions(this IServiceCollection services, Func buildDeliveryOptions, string name = null) - { - var builder = DeliveryOptionsBuilder.CreateInstance(); - var options = buildDeliveryOptions(builder); - - return services.RegisterOptions(options, name); - } } } diff --git a/Kentico.Kontent.Delivery/Helpers/DeliveryOptionsHelpers.cs b/Kentico.Kontent.Delivery/Helpers/DeliveryOptionsHelpers.cs new file mode 100644 index 00000000..0924df69 --- /dev/null +++ b/Kentico.Kontent.Delivery/Helpers/DeliveryOptionsHelpers.cs @@ -0,0 +1,23 @@ +using Kentico.Kontent.Delivery.Abstractions; +using Kentico.Kontent.Delivery.Configuration; +using System; + +namespace Kentico.Kontent.Delivery.Helpers +{ + /// + /// Helpers for a + /// + public static class DeliveryOptionsHelpers + { + /// + /// Builds the instance from the delegate. + /// + /// + /// + public static DeliveryOptions Build(Func buildDeliveryOptions) + { + var builder = DeliveryOptionsBuilder.CreateInstance(); + return buildDeliveryOptions(builder); + } + } +} diff --git a/Kentico.Kontent.Delivery/Properties/AssemblyInfo.cs b/Kentico.Kontent.Delivery/Properties/AssemblyInfo.cs index 4e41f48d..6710c64a 100644 --- a/Kentico.Kontent.Delivery/Properties/AssemblyInfo.cs +++ b/Kentico.Kontent.Delivery/Properties/AssemblyInfo.cs @@ -3,6 +3,7 @@ [assembly: ComVisible(false)] [assembly: Guid("103c7376-d90c-4207-9933-c9e55a16d902")] +[assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Extensions.Autofac.DependencyInjection.Tests")] [assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Tests")] [assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Caching.Tests")] [assembly: InternalsVisibleTo("Kentico.Kontent.Delivery.Rx.Tests")]