diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfiguration.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfiguration.cs index d5e480c3aa..c649ea3ffa 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfiguration.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfiguration.cs @@ -1,10 +1,58 @@ using System; using System.Collections.Generic; +using JetBrains.Annotations; namespace Volo.Abp.BlobStoring { - public class BlobContainerConfiguration : Dictionary + public class BlobContainerConfiguration { public Type ProviderType { get; set; } + + [NotNull] + private readonly Dictionary _properties; + + [CanBeNull] + private readonly BlobContainerConfiguration _fallbackConfiguration; + + public BlobContainerConfiguration(BlobContainerConfiguration fallbackConfiguration = null) + { + _fallbackConfiguration = fallbackConfiguration; + _properties = new Dictionary(); + } + + [CanBeNull] + public T GetConfigurationOrDefault(string name, T defaultValue = default) + { + return (T) GetConfigurationOrNull(name, defaultValue); + } + + [CanBeNull] + public object GetConfigurationOrNull(string name, object defaultValue = null) + { + return _properties.GetOrDefault(name) ?? + _fallbackConfiguration?.GetConfigurationOrNull(name, defaultValue) ?? + defaultValue; + } + + [NotNull] + public BlobContainerConfiguration SetConfiguration([NotNull] string name, [CanBeNull] object value) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + Check.NotNull(value, nameof(value)); + + _properties[name] = value; + + return this; + } + + [NotNull] + public BlobContainerConfiguration ClearConfiguration([NotNull] string name) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + + _properties.Remove(name); + + return this; + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfigurationDictionary.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfigurationDictionary.cs index 43ac7dad68..9475e6c355 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfigurationDictionary.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerConfigurationDictionary.cs @@ -4,13 +4,16 @@ using JetBrains.Annotations; namespace Volo.Abp.BlobStoring { - public class BlobContainerConfigurationDictionary : Dictionary + public class BlobContainerConfigurationDictionary { public BlobContainerConfiguration Default { get; } + private readonly Dictionary _containers; + public BlobContainerConfigurationDictionary() { Default = new BlobContainerConfiguration(); + _containers = new Dictionary(); } public BlobContainerConfigurationDictionary Configure( @@ -29,7 +32,7 @@ namespace Volo.Abp.BlobStoring Check.NotNullOrWhiteSpace(name, nameof(name)); Check.NotNull(configureAction, nameof(configureAction)); - configureAction(this.GetOrAdd(name, () => new BlobContainerConfiguration())); + configureAction(_containers.GetOrAdd(name, () => new BlobContainerConfiguration(Default))); return this; } @@ -39,5 +42,23 @@ namespace Volo.Abp.BlobStoring configureAction(Default); return this; } + + [NotNull] + public BlobContainerConfiguration GetConfiguration([NotNull] string name) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + + return _containers.GetOrDefault(name) ?? + Default; + } + + [NotNull] + public BlobContainerConfiguration GetConfiguration([NotNull] string name) + { + Check.NotNullOrWhiteSpace(name, nameof(name)); + + return _containers.GetOrDefault(name) ?? + Default; + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerFactory.cs b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerFactory.cs index 245070d591..3ea949071c 100644 --- a/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerFactory.cs +++ b/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerFactory.cs @@ -26,7 +26,7 @@ namespace Volo.Abp.BlobStoring public virtual IBlobContainer Create(string name, CancellationToken cancellationToken = default) { - var configuration = Options.Containers.GetOrDefault(name) ?? Options.Containers.Default; + var configuration = Options.Containers.GetConfiguration(name); return new BlobContainerToProviderAdapter( name, configuration, diff --git a/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringOptions_Tests.cs b/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringOptions_Tests.cs index 0593029f10..84193664fd 100644 --- a/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringOptions_Tests.cs +++ b/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringOptions_Tests.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Microsoft.Extensions.Options; using Shouldly; +using Volo.Abp.BlobStoring.Fakes; using Volo.Abp.BlobStoring.TestObjects; using Xunit; @@ -19,12 +20,25 @@ namespace Volo.Abp.BlobStoring public void Should_Property_Set_And_Get_Options_For_Different_Containers() { var testContainer1Config = _options.Containers - .GetOrDefault(BlobContainerNameAttribute.GetContainerName()); - testContainer1Config.ShouldContainKeyAndValue("TestConfig1", "TestValue1"); + .GetConfiguration(BlobContainerNameAttribute.GetContainerName()); + testContainer1Config.ProviderType.ShouldBe(typeof(FakeBlobProvider1)); + testContainer1Config.GetConfigurationOrDefault("TestConfig1").ShouldBe("TestValue1"); + testContainer1Config.GetConfigurationOrDefault("TestConfigDefault").ShouldBe("TestValueDefault"); var testContainer2Config = _options.Containers - .GetOrDefault(BlobContainerNameAttribute.GetContainerName()); - testContainer2Config.ShouldContainKeyAndValue("TestConfig2", "TestValue2"); + .GetConfiguration(BlobContainerNameAttribute.GetContainerName()); + testContainer2Config.ProviderType.ShouldBe(typeof(FakeBlobProvider2)); + testContainer2Config.GetConfigurationOrNull("TestConfig2").ShouldBe("TestValue2"); + testContainer2Config.GetConfigurationOrNull("TestConfigDefault").ShouldBe("TestValueDefault"); + } + + [Fact] + public void Should_Fallback_To_Default_Configuration_If_Not_Specialized() + { + var config = _options.Containers + .GetConfiguration(BlobContainerNameAttribute.GetContainerName()); + config.ProviderType.ShouldBe(typeof(FakeBlobProvider1)); + config.GetConfigurationOrNull("TestConfigDefault").ShouldBe("TestValueDefault"); } } } \ No newline at end of file diff --git a/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringTestModule.cs b/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringTestModule.cs index dc29bff71c..041f9e19f4 100644 --- a/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringTestModule.cs +++ b/framework/test/Volo.Abp.BlobStoring.Tests/Volo/Abp/BlobStoring/AbpBlobStoringTestModule.cs @@ -25,17 +25,17 @@ namespace Volo.Abp.BlobStoring options.Containers .ConfigureDefault(container => { - container["TestConfigDefault"] = "TestValueDefault"; + container.SetConfiguration("TestConfigDefault", "TestValueDefault"); container.ProviderType = typeof(FakeBlobProvider1); }) .Configure(container => { - container["TestConfig1"] = "TestValue1"; + container.SetConfiguration("TestConfig1", "TestValue1"); container.ProviderType = typeof(FakeBlobProvider1); }) .Configure(container => { - container["TestConfig2"] = "TestValue2"; + container.SetConfiguration("TestConfig2", "TestValue2"); container.ProviderType = typeof(FakeBlobProvider2); }); });