Refactoring around the RepositoryResolver to allow the configuration of repositories and cache providers for each repository.

Added implementation of an infrastructure configuration section.
Added unit tests for the RepositoryResolver to verify that it can resolve all repository types and their configured cache provider.
This commit is contained in:
Morten@Thinkpad-X220
2012-10-11 13:11:17 -02:00
parent f0e8ba063e
commit 3f3362e23d
32 changed files with 807 additions and 324 deletions

View File

@@ -0,0 +1,190 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.InfrastructureSettings
{
public class Infrastructure : ConfigurationSection
{
private const string InfrastructureSectionName = "umbraco/infrastructure";
public static Infrastructure Instance
{
get { return (Infrastructure) ConfigurationManager.GetSection(InfrastructureSectionName); }
}
#region RepositoriesSection Property
internal const string RepositoriesPropertyName = "repositories";
[ConfigurationProperty(RepositoriesPropertyName, IsRequired = true, IsKey = false, IsDefaultCollection = false)]
public Repositories Repositories
{
get { return ((Repositories)base[RepositoriesPropertyName]); }
set { base[RepositoriesPropertyName] = value; }
}
#endregion
#region PublishingStrategy Property
internal const string PublishingStrategyPropertyName = "publishingStrategy";
[ConfigurationProperty(PublishingStrategyPropertyName, IsRequired = true, IsKey = false, IsDefaultCollection = false)]
public PublishingProvider PublishingStrategy
{
get { return ((PublishingProvider)base[PublishingStrategyPropertyName]); }
set { base[PublishingStrategyPropertyName] = value; }
}
#endregion
}
public class Repositories : ConfigurationElement
{
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public RepositoryElementCollection Repository
{
get { return ((RepositoryElementCollection)(base[""])); }
}
}
[ConfigurationCollection(typeof(Repository), CollectionType = ConfigurationElementCollectionType.BasicMapAlternate, AddItemName = RepositoryPropertyName)]
public class RepositoryElementCollection : ConfigurationElementCollection
{
internal const string RepositoryPropertyName = "repository";
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMapAlternate;
}
}
protected override string ElementName
{
get
{
return RepositoryPropertyName;
}
}
protected override bool IsElementName(string elementName)
{
return elementName == RepositoryPropertyName;
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Repository)element).InterfaceShortTypeName;
}
protected override ConfigurationElement CreateNewElement()
{
return new Repository();
}
#region Indexer
public Repository this[int index]
{
get { return (Repository)base.BaseGet(index); }
}
public Repository this[string interfaceShortTypeName]
{
get { return (Repository)base.BaseGet(interfaceShortTypeName); }
}
#endregion
#region Add
public void Add(Repository repository)
{
BaseAdd(repository);
}
#endregion
#region Remove
public void Remove(Repository repository)
{
BaseRemove(repository);
}
#endregion
#region GetItem
public Repository GetItemAt(int index)
{
return (Repository)BaseGet(index);
}
public Repository GetItemByKey(string interfaceShortTypeName)
{
return (Repository)BaseGet(interfaceShortTypeName);
}
#endregion
public bool ContainsKey(string interfaceShortName)
{
bool result = false;
object[] keys = this.BaseGetAllKeys();
foreach (object key in keys)
{
if ((string)key == interfaceShortName)
{
result = true;
break;
}
}
return result;
}
}
public class Repository : ConfigurationElement
{
internal const string InterfaceShortTypeNamePropertyName = "interfaceShortTypeName";
[ConfigurationPropertyAttribute(InterfaceShortTypeNamePropertyName, IsRequired = true, IsKey = true, IsDefaultCollection = false)]
public string InterfaceShortTypeName
{
get { return (string) base[InterfaceShortTypeNamePropertyName]; }
set { base[InterfaceShortTypeNamePropertyName] = value; }
}
internal const string RepositoryFullTypeNamePropertyName = "repositoryFullTypeName";
[ConfigurationPropertyAttribute(RepositoryFullTypeNamePropertyName, IsRequired = true, IsKey = false, IsDefaultCollection = false)]
public string RepositoryFullTypeName
{
get { return (string)base[RepositoryFullTypeNamePropertyName]; }
set { base[RepositoryFullTypeNamePropertyName] = value; }
}
internal const string CacheProviderFullTypeNamePropertyName = "cacheProviderFullTypeName";
[ConfigurationPropertyAttribute(CacheProviderFullTypeNamePropertyName, IsRequired = true, IsKey = false, IsDefaultCollection = false)]
public string CacheProviderFullTypeName
{
get { return (string)base[CacheProviderFullTypeNamePropertyName]; }
set { base[CacheProviderFullTypeNamePropertyName] = value; }
}
}
public class PublishingProvider : ConfigurationElement
{
internal const string TypePropertyName = "type";
[ConfigurationPropertyAttribute(TypePropertyName, IsRequired = true, IsKey = false, IsDefaultCollection = false)]
public string Type
{
get { return (string)base[TypePropertyName]; }
set { base[TypePropertyName] = value; }
}
}
}

View File

@@ -1,117 +0,0 @@
using System;
using System.Configuration;
namespace Umbraco.Core.Configuration.Repositories
{
internal class RepositoryConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("repositories", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(RepositoryCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public RepositoryCollection Repositories
{
get
{
return (RepositoryCollection)base["repositories"];
}
}
}
internal class RepositoryCollection : ConfigurationElementCollection
{
public RepositoryCollection()
{
Console.WriteLine("RepositoryCollection Constructor");
}
public RepositoryElement this[int index]
{
get { return (RepositoryElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(RepositoryElement repositoryElement)
{
BaseAdd(repositoryElement);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new RepositoryElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((RepositoryElement)element).Name;
}
public void Remove(RepositoryElement repositoryElement)
{
BaseRemove(repositoryElement.Name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
}
internal class RepositoryElement : ConfigurationElement
{
private const string NameKey = "name";
private const string ModelTypeKey = "modelType";
private const string RepositoryTypeKey = "repositoryType";
public RepositoryElement() { }
public RepositoryElement(string name, string modelType, string repositoryType)
{
Name = name;
ModelType = modelType;
RepositoryType = repositoryType;
}
[ConfigurationProperty(NameKey, IsRequired = true, IsKey = true)]
public string Name
{
get { return (string)this[NameKey]; }
set { this[NameKey] = value; }
}
[ConfigurationProperty(ModelTypeKey, IsRequired = true, IsKey = false)]
public string ModelType
{
get { return (string)this[ModelTypeKey]; }
set { this[ModelTypeKey] = value; }
}
[ConfigurationProperty(RepositoryTypeKey, IsRequired = true, IsKey = false)]
public string RepositoryType
{
get { return (string)this[RepositoryTypeKey]; }
set { this[RepositoryTypeKey] = value; }
}
}
}

View File

@@ -1,61 +0,0 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.Repositories
{
internal sealed class RepositoryMappingCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new RepositoryMappingElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((RepositoryMappingElement)element).InterfaceShortTypeName;
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return RepositoryMappingConstants.ConfigurationElementName; }
}
public RepositoryMappingElement this[int index]
{
get { return (RepositoryMappingElement)this.BaseGet(index); }
set
{
if (this.BaseGet(index) != null)
{
this.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
public new RepositoryMappingElement this[string interfaceShortTypeName]
{
get { return (RepositoryMappingElement)this.BaseGet(interfaceShortTypeName); }
}
public bool ContainsKey(string keyName)
{
bool result = false;
object[] keys = this.BaseGetAllKeys();
foreach (object key in keys)
{
if ((string)key == keyName)
{
result = true;
break;
}
}
return result;
}
}
}

View File

@@ -1,12 +0,0 @@
namespace Umbraco.Core.Configuration.Repositories
{
internal static class RepositoryMappingConstants
{
internal const string CacheProviderFullTypeNameAttributeName = "cacheProviderFullTypeName";
internal const string ConfigurationPropertyName = "repositoryMappings";
internal const string ConfigurationElementName = "repositoryMapping";
internal const string InterfaceShortTypeNameAttributeName = "interfaceShortTypeName";
internal const string RepositoryFullTypeNameAttributeName = "repositoryFullTypeName";
internal const string RepositoryMappingsConfigurationSectionName = "repositoryMappingsConfiguration";
}
}

View File

@@ -1,49 +0,0 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.Repositories
{
internal sealed class RepositoryMappingElement : ConfigurationElement
{
[ConfigurationProperty(RepositoryMappingConstants.InterfaceShortTypeNameAttributeName,
IsKey = true, IsRequired = true)]
public string InterfaceShortTypeName
{
get
{
return (string)this[RepositoryMappingConstants.InterfaceShortTypeNameAttributeName];
}
set
{
this[RepositoryMappingConstants.InterfaceShortTypeNameAttributeName] = value;
}
}
[ConfigurationProperty(RepositoryMappingConstants.RepositoryFullTypeNameAttributeName,
IsRequired = true)]
public string RepositoryFullTypeName
{
get
{
return (string)this[RepositoryMappingConstants.RepositoryFullTypeNameAttributeName];
}
set
{
this[RepositoryMappingConstants.RepositoryFullTypeNameAttributeName] = value;
}
}
[ConfigurationProperty(RepositoryMappingConstants.CacheProviderFullTypeNameAttributeName,
IsRequired = true)]
public string CacheProviderFullTypeName
{
get
{
return (string)this[RepositoryMappingConstants.CacheProviderFullTypeNameAttributeName];
}
set
{
this[RepositoryMappingConstants.CacheProviderFullTypeNameAttributeName] = value;
}
}
}
}

View File

@@ -1,14 +0,0 @@
using System.Configuration;
namespace Umbraco.Core.Configuration.Repositories
{
internal class RepositorySettings : ConfigurationSection
{
[ConfigurationProperty(RepositoryMappingConstants.ConfigurationPropertyName,
IsDefaultCollection = true)]
public RepositoryMappingCollection RepositoryMappings
{
get { return (RepositoryMappingCollection)base[RepositoryMappingConstants.ConfigurationPropertyName]; }
}
}
}