Revise the blob configurations.

pull/4105/head
Halil İbrahim Kalkan 5 years ago
parent d28db0f4ad
commit 602e8af162

@ -1,10 +1,58 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring namespace Volo.Abp.BlobStoring
{ {
public class BlobContainerConfiguration : Dictionary<string, object> public class BlobContainerConfiguration
{ {
public Type ProviderType { get; set; } public Type ProviderType { get; set; }
[NotNull]
private readonly Dictionary<string, object> _properties;
[CanBeNull]
private readonly BlobContainerConfiguration _fallbackConfiguration;
public BlobContainerConfiguration(BlobContainerConfiguration fallbackConfiguration = null)
{
_fallbackConfiguration = fallbackConfiguration;
_properties = new Dictionary<string, object>();
}
[CanBeNull]
public T GetConfigurationOrDefault<T>(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;
}
} }
} }

@ -4,13 +4,16 @@ using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring namespace Volo.Abp.BlobStoring
{ {
public class BlobContainerConfigurationDictionary : Dictionary<string, BlobContainerConfiguration> public class BlobContainerConfigurationDictionary
{ {
public BlobContainerConfiguration Default { get; } public BlobContainerConfiguration Default { get; }
private readonly Dictionary<string, BlobContainerConfiguration> _containers;
public BlobContainerConfigurationDictionary() public BlobContainerConfigurationDictionary()
{ {
Default = new BlobContainerConfiguration(); Default = new BlobContainerConfiguration();
_containers = new Dictionary<string, BlobContainerConfiguration>();
} }
public BlobContainerConfigurationDictionary Configure<TContainer>( public BlobContainerConfigurationDictionary Configure<TContainer>(
@ -29,7 +32,7 @@ namespace Volo.Abp.BlobStoring
Check.NotNullOrWhiteSpace(name, nameof(name)); Check.NotNullOrWhiteSpace(name, nameof(name));
Check.NotNull(configureAction, nameof(configureAction)); Check.NotNull(configureAction, nameof(configureAction));
configureAction(this.GetOrAdd(name, () => new BlobContainerConfiguration())); configureAction(_containers.GetOrAdd(name, () => new BlobContainerConfiguration(Default)));
return this; return this;
} }
@ -39,5 +42,23 @@ namespace Volo.Abp.BlobStoring
configureAction(Default); configureAction(Default);
return this; return this;
} }
[NotNull]
public BlobContainerConfiguration GetConfiguration<TContainer>([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;
}
} }
} }

@ -26,7 +26,7 @@ namespace Volo.Abp.BlobStoring
public virtual IBlobContainer Create(string name, CancellationToken cancellationToken = default) 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( return new BlobContainerToProviderAdapter(
name, name,
configuration, configuration,

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Shouldly; using Shouldly;
using Volo.Abp.BlobStoring.Fakes;
using Volo.Abp.BlobStoring.TestObjects; using Volo.Abp.BlobStoring.TestObjects;
using Xunit; using Xunit;
@ -19,12 +20,25 @@ namespace Volo.Abp.BlobStoring
public void Should_Property_Set_And_Get_Options_For_Different_Containers() public void Should_Property_Set_And_Get_Options_For_Different_Containers()
{ {
var testContainer1Config = _options.Containers var testContainer1Config = _options.Containers
.GetOrDefault(BlobContainerNameAttribute.GetContainerName<TestContainer1>()); .GetConfiguration(BlobContainerNameAttribute.GetContainerName<TestContainer1>());
testContainer1Config.ShouldContainKeyAndValue("TestConfig1", "TestValue1"); testContainer1Config.ProviderType.ShouldBe(typeof(FakeBlobProvider1));
testContainer1Config.GetConfigurationOrDefault<string>("TestConfig1").ShouldBe("TestValue1");
testContainer1Config.GetConfigurationOrDefault<string>("TestConfigDefault").ShouldBe("TestValueDefault");
var testContainer2Config = _options.Containers var testContainer2Config = _options.Containers
.GetOrDefault(BlobContainerNameAttribute.GetContainerName<TestContainer2>()); .GetConfiguration(BlobContainerNameAttribute.GetContainerName<TestContainer2>());
testContainer2Config.ShouldContainKeyAndValue("TestConfig2", "TestValue2"); 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<TestContainer3>());
config.ProviderType.ShouldBe(typeof(FakeBlobProvider1));
config.GetConfigurationOrNull("TestConfigDefault").ShouldBe("TestValueDefault");
} }
} }
} }

@ -25,17 +25,17 @@ namespace Volo.Abp.BlobStoring
options.Containers options.Containers
.ConfigureDefault(container => .ConfigureDefault(container =>
{ {
container["TestConfigDefault"] = "TestValueDefault"; container.SetConfiguration("TestConfigDefault", "TestValueDefault");
container.ProviderType = typeof(FakeBlobProvider1); container.ProviderType = typeof(FakeBlobProvider1);
}) })
.Configure<TestContainer1>(container => .Configure<TestContainer1>(container =>
{ {
container["TestConfig1"] = "TestValue1"; container.SetConfiguration("TestConfig1", "TestValue1");
container.ProviderType = typeof(FakeBlobProvider1); container.ProviderType = typeof(FakeBlobProvider1);
}) })
.Configure<TestContainer2>(container => .Configure<TestContainer2>(container =>
{ {
container["TestConfig2"] = "TestValue2"; container.SetConfiguration("TestConfig2", "TestValue2");
container.ProviderType = typeof(FakeBlobProvider2); container.ProviderType = typeof(FakeBlobProvider2);
}); });
}); });

Loading…
Cancel
Save