#20337: Re-implementing PlatformConfiguration as an injectable dependency (Orchard.Azure)

Work Item: 20337
This commit is contained in:
Lombiq
2014-03-07 02:03:40 +01:00
committed by nightwolf226
parent edaa00143c
commit 852d08a3f1
7 changed files with 54 additions and 31 deletions

View File

@@ -100,6 +100,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Services\Environment\Configuration\DefaultPlatformConfigurationAccessor.cs" />
<Compile Include="Services\Environment\Configuration\IPlatformConfigurationAccessor.cs" />
<Compile Include="Services\TaskLease\AzureMachineNameProvider.cs" />
<Content Include="Web.config" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -129,7 +131,6 @@
<Compile Include="Services\Caching\Output\AzureOutputCacheStorageProvider.cs" />
<Compile Include="Services\Caching\Output\IAzureOutputCacheHolder.cs" />
<Compile Include="Services\Environment\Configuration\AzureBlobShellSettingsManager.cs" />
<Compile Include="Services\Environment\Configuration\PlatformConfiguration.cs" />
<Compile Include="Services\FileSystems\AzureFileSystem.cs" />
<Compile Include="Services\FileSystems\CloudBlobContainerExtensions.cs" />
<Compile Include="Services\FileSystems\Media\AzureBlobStorageProvider.cs" />

View File

@@ -6,11 +6,11 @@ namespace Orchard.Azure.Services.Caching {
public class CacheClientConfiguration {
public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix) {
public static CacheClientConfiguration FromPlatformConfiguration(string tenant, string settingNamePrefix, IPlatformConfigurationAccessor pca) {
return new CacheClientConfiguration {
HostIdentifier = PlatformConfiguration.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix),
CacheName = PlatformConfiguration.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix),
AuthorizationToken = PlatformConfiguration.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix),
HostIdentifier = pca.GetSetting(Constants.CacheHostIdentifierSettingName, tenant, settingNamePrefix),
CacheName = pca.GetSetting(Constants.CacheCacheNameSettingName, tenant, settingNamePrefix),
AuthorizationToken = pca.GetSetting(Constants.CacheAuthorizationTokenSettingName, tenant, settingNamePrefix),
};
}

View File

@@ -1,13 +1,19 @@
using System.Collections.Generic;
using Microsoft.ApplicationServer.Caching;
using NHibernate.Cache;
using Orchard.Azure.Services.Environment.Configuration;
using System;
using System.Collections.Generic;
namespace Orchard.Azure.Services.Caching.Database {
public class AzureCacheProvider : ICacheProvider {
private DataCache _dataCache;
private readonly IPlatformConfigurationAccessor _pca;
public AzureCacheProvider(IPlatformConfigurationAccessor pca) {
_pca = pca;
}
public ICache BuildCache(string regionName, IDictionary<string, string> properties) {
@@ -39,7 +45,7 @@ namespace Orchard.Azure.Services.Caching.Database {
if (properties.TryGetValue("compression_enabled", out enableCompressionString))
enableCompression = Boolean.Parse(enableCompressionString);
configuration = CacheClientConfiguration.FromPlatformConfiguration(tenantName, Constants.DatabaseCacheSettingNamePrefix);
configuration = CacheClientConfiguration.FromPlatformConfiguration(tenantName, Constants.DatabaseCacheSettingNamePrefix, _pca);
configuration.CompressionIsEnabled = enableCompression;
configuration.Validate();
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationServer.Caching;
using Orchard.Azure.Services.Environment.Configuration;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions;
using Orchard.Logging;
@@ -17,7 +18,7 @@ namespace Orchard.Azure.Services.Caching.Output {
private readonly DataCache _cache;
private readonly string _regionAlphaNumeric;
public AzureOutputCacheStorageProvider(ShellSettings shellSettings, IAzureOutputCacheHolder cacheHolder) {
public AzureOutputCacheStorageProvider(ShellSettings shellSettings, IAzureOutputCacheHolder cacheHolder, IPlatformConfigurationAccessor pca) {
var region = shellSettings.Name;
@@ -32,7 +33,7 @@ namespace Orchard.Azure.Services.Caching.Output {
CacheClientConfiguration cacheConfig;
try {
cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(shellSettings.Name, Constants.OutputCacheSettingNamePrefix);
cacheConfig = CacheClientConfiguration.FromPlatformConfiguration(shellSettings.Name, Constants.OutputCacheSettingNamePrefix, pca);
cacheConfig.Validate();
}
catch (Exception ex) {

View File

@@ -1,21 +1,21 @@
using Microsoft.WindowsAzure;
namespace Orchard.Azure.Services.Environment.Configuration {
public static class PlatformConfiguration {
/// <summary>
/// Reads a setting from platform configuration, trying first prefixed with the current tenant name and
/// secondly with no prefix.
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="tenant">The curren tenant's name.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
public static string GetSetting(string name, string tenant, string namePrefix = null) {
var tenantName = tenant + ":" + (namePrefix ?? string.Empty) + name;
var fallbackName = (namePrefix ?? string.Empty) + name;
return CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName);
}
}
using Microsoft.WindowsAzure;
namespace Orchard.Azure.Services.Environment.Configuration {
public class DefaultPlatformConfigurationAccessor : IPlatformConfigurationAccessor {
/// <summary>
/// Reads a setting from platform configuration, trying first prefixed with the current tenant name and
/// secondly with no prefix.
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="tenant">The curren tenant's name.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
public string GetSetting(string name, string tenant, string namePrefix = null) {
var tenantName = tenant + ":" + (namePrefix ?? string.Empty) + name;
var fallbackName = (namePrefix ?? string.Empty) + name;
return CloudConfigurationManager.GetSetting(tenantName) ?? CloudConfigurationManager.GetSetting(fallbackName);
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Orchard.Azure.Services.Environment.Configuration {
public interface IPlatformConfigurationAccessor : IDependency {
/// <summary>
/// Reads a setting using the available implementation(s).
/// </summary>
/// <param name="name">The name of the setting to read.</param>
/// <param name="tenant">The curren tenant's name.</param>
/// <param name="namePrefix">An optional prefix to prepend the setting name with.</param>
/// <returns>The value of the setting if found with or without tenant name prefix, otherwise null.</returns>
/// <see cref="DefaultPlatformConfigurationAccessor" />
string GetSetting(string name, string tenant, string namePrefix);
}
}

View File

@@ -11,8 +11,8 @@ namespace Orchard.Azure.Services.FileSystems.Media {
[OrchardSuppressDependency("Orchard.FileSystems.Media.FileSystemStorageProvider")]
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
public AzureBlobStorageProvider(ShellSettings shellSettings, IMimeTypeProvider mimeTypeProvider)
: this(PlatformConfiguration.GetSetting(Constants.MediaStorageStorageConnectionStringSettingName, shellSettings.Name), Constants.MediaStorageContainerName, shellSettings.Name, mimeTypeProvider) {
public AzureBlobStorageProvider(ShellSettings shellSettings, IMimeTypeProvider mimeTypeProvider, IPlatformConfigurationAccessor pca)
: this(pca.GetSetting(Constants.MediaStorageStorageConnectionStringSettingName, shellSettings.Name, null), Constants.MediaStorageContainerName, shellSettings.Name, mimeTypeProvider) {
}
public AzureBlobStorageProvider(string storageConnectionString, string containerName, string rootFolderPath, IMimeTypeProvider mimeTypeProvider)