From: <der...@us...> - 2007-07-27 13:44:01
|
Revision: 48 http://structuremap.svn.sourceforge.net/structuremap/?rev=48&view=rev Author: derrickrapp Date: 2007-07-27 06:43:55 -0700 (Fri, 27 Jul 2007) Log Message: ----------- Added support for specific implementations of generic interfaces. Such as MyIntObject : MyInterface<int> Modified Paths: -------------- trunk/Source/CommonAssemblyInfo.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs trunk/Source/StructureMap.Testing.GenericWidgets/Widgets.cs Modified: trunk/Source/CommonAssemblyInfo.cs =================================================================== --- trunk/Source/CommonAssemblyInfo.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/CommonAssemblyInfo.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -5,7 +5,7 @@ //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. -// Runtime Version:2.0.50727.42 +// Runtime Version:2.0.50727.832 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -7,11 +7,11 @@ { public static bool CanBeCast(Type pluginType, Type pluggedType) { - bool isGenericComparison = pluginType.IsGenericType && pluggedType.IsGenericType; - if (!isGenericComparison) - { - return false; - } + //bool isGenericComparison = pluginType.IsGenericType && pluggedType.IsGenericType; + //if (!isGenericComparison) + //{ + // return false; + //} try { Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -190,7 +190,15 @@ public Plugin CreateTemplatedClone(params Type[] types) { - Type templatedType = _pluggedType.MakeGenericType(types); + Type templatedType; + if (_pluggedType.IsGenericType) + { + templatedType = _pluggedType.MakeGenericType(types); + } + else + { + templatedType = _pluggedType; + } Plugin templatedPlugin = new Plugin(templatedType, _concreteKey, _definitionSource); templatedPlugin._setters = _setters; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -1,4 +1,5 @@ using System; +using System.Reflection; using StructureMap.Interceptors; using StructureMap.Source; @@ -118,7 +119,7 @@ Type templatedType = _pluginType.MakeGenericType(templateTypes); PluginFamily templatedFamily = new PluginFamily(templatedType); templatedFamily._defaultKey = _defaultKey; - templatedFamily._source = _source; + templatedFamily._source = new MemoryMementoSource(); templatedFamily._definitionSource = _definitionSource; foreach (InstanceFactoryInterceptor interceptor in _interceptionChain) @@ -129,13 +130,50 @@ foreach (Plugin plugin in _plugins) { - Plugin templatedPlugin = plugin.CreateTemplatedClone(templateTypes); - templatedFamily.Plugins.Add(templatedPlugin); + if (IsOfCorrectGenericType(plugin, templateTypes)) + { + Plugin templatedPlugin = plugin.CreateTemplatedClone(templateTypes); + templatedFamily.Plugins.Add(templatedPlugin); + foreach (InstanceMemento memento in _source.GetAllMementos()) + { + if (memento.ConcreteKey == plugin.ConcreteKey) + { + templatedFamily._source.AddExternalMemento(memento); + } + } + } } return templatedFamily; } + private bool IsOfCorrectGenericType(Plugin plugin, params Type[] templateTypes) + { + bool isValid = true; + + Type interfaceType = plugin.PluggedType.GetInterface(_pluginType.Name); + if (interfaceType == null) + { + interfaceType = plugin.PluggedType.BaseType; + } + Type[] pluginArgs = _pluginType.GetGenericArguments(); + Type[] pluggableArgs = interfaceType.GetGenericArguments(); + + if (templateTypes.Length != pluginArgs.Length && + pluginArgs.Length != pluggableArgs.Length) + { + return false; + } + + for (int i = 0; i < templateTypes.Length; i++) + { + isValid &= templateTypes[i] == pluggableArgs[i] || + pluginArgs[i].IsGenericParameter && + pluggableArgs[i].IsGenericParameter; + } + return isValid; + } + #region properties /// <summary> Modified: trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -1,3 +1,5 @@ +using System.Collections; +using System.Collections.Generic; using NUnit.Framework; using StructureMap.Graph; using StructureMap.Testing.GenericWidgets; @@ -86,5 +88,28 @@ Assert.AreNotSame(object1, object4); } + + [Test] + public void AllTypesWithSpecificImplementation() + { + IList objectConcepts = manager.GetAllInstances(typeof(IConcept<object>)); + + Assert.IsNotNull(objectConcepts); + Assert.AreEqual(2, objectConcepts.Count); + + IList stringConcepts = manager.GetAllInstances(typeof(IConcept<string>)); + + Assert.IsNotNull(stringConcepts); + Assert.AreEqual(1, stringConcepts.Count); + } + + [Test] + public void SpecificImplementation() + { + IConcept<object> concept = (IConcept<object>)manager.CreateInstance(typeof(IConcept<object>), "Specific"); + + Assert.IsNotNull(concept); + Assert.IsInstanceOfType(typeof(SpecificConcept), concept); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing.GenericWidgets/Widgets.cs =================================================================== --- trunk/Source/StructureMap.Testing.GenericWidgets/Widgets.cs 2007-05-02 01:32:00 UTC (rev 47) +++ trunk/Source/StructureMap.Testing.GenericWidgets/Widgets.cs 2007-07-27 13:43:55 UTC (rev 48) @@ -57,6 +57,21 @@ } } + [PluginFamily("Default")] + public interface IConcept<T> + { + } + + [Pluggable("Default")] + public class GenericConcept<T> : IConcept<T> + { + } + + [Pluggable("Specific")] + public class SpecificConcept : IConcept<object> + { + } + public interface IThing<T, U> { } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |