Merge pull request #52 from norvegec/master

Added ManagedDictionary.Merge(object) support for generic dictionary
This commit is contained in:
Marko Lahma
2013-11-23 05:27:00 -08:00
2 changed files with 66 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
#region License
/*
* Copyright <20> 2002-2011 the original author or authors.
* Copyright <20> 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -203,31 +203,37 @@ namespace Spring.Objects.Factory.Config
/// <exception cref="InvalidOperationException">If merging is not enabled for this instance,
/// (i.e. <code>MergeEnabled</code> equals <code>false</code>.</exception>
public object Merge(object parent)
{
if (!this.mergeEnabled)
{
throw new InvalidOperationException(
"Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
}
if (parent == null)
{
return this;
}
IDictionary pDict = parent as IDictionary;
if (pDict == null)
{
throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
}
IDictionary merged = new ManagedDictionary();
foreach (DictionaryEntry dictionaryEntry in pDict)
{
{
if (!this.mergeEnabled)
{
throw new InvalidOperationException(
"Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
}
if (parent == null)
{
return this;
}
var pDict = parent as IDictionary;
if (pDict == null)
{
throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
}
var merged = new ManagedDictionary();
var pManagedDict = pDict as ManagedDictionary;
if (pManagedDict != null)
{
merged.KeyTypeName = pManagedDict.keyTypeName;
merged.valueTypeName = pManagedDict.valueTypeName;
}
foreach (DictionaryEntry dictionaryEntry in pDict)
{
merged[dictionaryEntry.Key] = dictionaryEntry.Value;
}
foreach (DictionaryEntry entry in this)
{
merged[entry.Key] = entry.Value;
}
return merged;
return merged;
}
}
}
}

View File

@@ -1,19 +1,19 @@
#region License
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
@@ -146,5 +146,30 @@ namespace Spring.Objects.Factory.Support
Assert.AreEqual(1, resolved.Count);
Assert.AreEqual(typeof(List<string>), resolved["key"].GetType());
}
[Test]
public void ResolvesMergedGenericType()
{
ManagedDictionary parent = new ManagedDictionary();
parent.Add("one", 1);
parent.Add("two", 2);
parent.KeyTypeName = "string";
parent.ValueTypeName = "int";
ManagedDictionary child = new ManagedDictionary();
child.MergeEnabled = true;
child.Add("one", -1);
child.Add("three", 3);
ManagedDictionary merged = (ManagedDictionary) child.Merge(parent);
IDictionary resolved = (IDictionary) merged.Resolve("somename", new RootObjectDefinition(typeof(object)), "prop",
(name, definition, argumentName, element) => element);
Assert.IsInstanceOf<IDictionary<string,int>>(resolved);
Assert.AreEqual(3, resolved.Count);
Assert.AreEqual(typeof(int), resolved["two"].GetType());
Assert.AreEqual(-1, resolved["one"]);
}
}
}