From: <and...@us...> - 2009-10-07 17:18:08
|
Revision: 267 http://structuremap.svn.sourceforge.net/structuremap/?rev=267&view=rev Author: andreas_ohlund Date: 2009-10-07 17:17:56 +0000 (Wed, 07 Oct 2009) Log Message: ----------- WithCtorArg can now be used with both simple and non simple types Modified Paths: -------------- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-10-07 16:59:34 UTC (rev 266) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-10-07 17:17:56 UTC (rev 267) @@ -23,7 +23,12 @@ /// <returns></returns> public T EqualTo(object propertyValue) { - _instance.SetProperty(_propertyName, propertyValue.ToString()); + if(propertyValue.GetType().IsSimple()) + _instance.SetProperty(_propertyName, propertyValue.ToString()); + else + { + _instance.SetChild(_propertyName,new LiteralInstance(propertyValue)); + } return (T) _instance; } Modified: trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs 2009-10-07 16:59:34 UTC (rev 266) +++ trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs 2009-10-07 17:17:56 UTC (rev 267) @@ -98,8 +98,31 @@ }); } + [Test] + public void specify_ctorarg_with_non_simple_argument() + { + var widget = new ColorWidget("Red"); + var container = new Container(x => x.ForRequestedType<ClassWithWidget>() + .TheDefault.Is.OfConcreteType<ClassWithWidget>() + .WithCtorArg("widget").EqualTo(widget)); + Assert.AreSame(widget, container.GetInstance<ClassWithWidget>().Widget); + } + + [Test] + public void specify_a_non_simple_property_with_equal_to() + { + var widget = new ColorWidget("Red"); + var container = new Container(x => x.ForRequestedType<ClassWithWidgetProperty>() + .TheDefault.Is.OfConcreteType<ClassWithWidgetProperty>() + .WithProperty(o => o.Widget).EqualTo(widget)); + + Assert.AreSame(widget, container.GetInstance<ClassWithWidgetProperty>().Widget); + } + + + [Test] public void specify_an_array_as_a_constructor() { IWidget widget1 = new AWidget(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <and...@us...> - 2009-10-07 18:25:58
|
Revision: 268 http://structuremap.svn.sourceforge.net/structuremap/?rev=268&view=rev Author: andreas_ohlund Date: 2009-10-07 18:25:50 +0000 (Wed, 07 Oct 2009) Log Message: ----------- Fixed bug where FillAllPropertiesOfType didn't work for already configured instances Added missing test for the JITException bug Modified Paths: -------------- trunk/Source/StructureMap/Graph/PluginCache.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.Testing/Bugs/FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs trunk/Source/StructureMap.Testing/Bugs/StaticPropertyCausesJITExceptionTester.cs Modified: trunk/Source/StructureMap/Graph/PluginCache.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginCache.cs 2009-10-07 17:17:56 UTC (rev 267) +++ trunk/Source/StructureMap/Graph/PluginCache.cs 2009-10-07 18:25:50 UTC (rev 268) @@ -106,7 +106,19 @@ public static void UseSetterRule(Predicate<PropertyInfo> predicate) { _setterRules.Add(predicate); - _plugins.Each(plugin => plugin.UseSetterRule(predicate)); + _plugins.Each(plugin => + { + plugin.UseSetterRule(predicate); + + //does any of the registered plugins have a setter matching the predicate? + if (plugin.PluggedType.GetProperties().Any(s => predicate(s))) + { + //rebuild the builder if nessesary + if (_builders[plugin.PluggedType] != null) + createAndStoreBuilders(new[] { plugin }); + } + }); + } } } \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Bugs/FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs 2009-10-07 18:25:50 UTC (rev 268) @@ -0,0 +1,51 @@ +using System.Collections.Generic; +using NUnit.Framework; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug + { + + [Test] + public void Structuremap_should_autowire_setter_dependencies_regardless_of_order() + { + var container = new Container(); + + container.Configure(x => + { + x.ForConcreteType<ClassWithSetterDependency>(); + }); + + + container.Configure(x => + { + x.ForRequestedType<ISomeDependency>() + .TheDefaultIsConcreteType<ClassThatImplementsDependency>(); + + x.FillAllPropertiesOfType<ISomeDependency>(); + + + }); + + + + container.GetInstance<ClassWithSetterDependency>().Dependency.ShouldNotBeNull(); + } + + } + + + public class ClassWithSetterDependency + { + public ISomeDependency Dependency { get; set; } + public IList<string> SystemDependency { get; set; } + } + + public interface ISomeDependency { } + + public class ClassThatImplementsDependency : ISomeDependency { } + + public interface INonConfiguredInterface { } + +} Added: trunk/Source/StructureMap.Testing/Bugs/StaticPropertyCausesJITExceptionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/StaticPropertyCausesJITExceptionTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/StaticPropertyCausesJITExceptionTester.cs 2009-10-07 18:25:50 UTC (rev 268) @@ -0,0 +1,34 @@ +using NUnit.Framework; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class StaticPropertyCausesJITExceptionTester + { + [Test] + public void Get_instance_should_work() + { + var container = new Container(x => x.ForConcreteType<ClassWithStaticProperty>()); + + container.GetInstance<ClassWithStaticProperty>(); + } + } + + public class ClassWithStaticProperty + { + + private static volatile bool disabled; + + public static bool Disabled + { + get + { + return disabled; + } + set + { + disabled = value; + } + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-07 17:17:56 UTC (rev 267) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-07 18:25:50 UTC (rev 268) @@ -179,6 +179,7 @@ <Compile Include="AutoWiringExamples.cs" /> <Compile Include="BidirectionalDependencies.cs" /> <Compile Include="Bugs\BuildUpBug.cs" /> + <Compile Include="Bugs\FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs" /> <Compile Include="Bugs\HttpSessionNullRefBug.cs" /> <Compile Include="Bugs\IDictionaryAndXmlBugTester.cs" /> <Compile Include="Bugs\LambdaCreatesNullBugTester.cs" /> @@ -186,6 +187,7 @@ <Compile Include="Bugs\ScanIndexerBugTester.cs" /> <Compile Include="Bugs\SingletonShouldBeLazy.cs" /> <Compile Include="Bugs\SpecifyScopeInConfigureTester.cs" /> + <Compile Include="Bugs\StaticPropertyCausesJITExceptionTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="BuildUpIntegratedTester.cs" /> <Compile Include="BuildUpTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-10-08 16:44:21
|
Revision: 269 http://structuremap.svn.sourceforge.net/structuremap/?rev=269&view=rev Author: flimflan Date: 2009-10-08 16:44:14 +0000 (Thu, 08 Oct 2009) Log Message: ----------- Removed code that tried to parse ClassName/AssemblyName independently - just work with AssemblyQualifiedName directly - fixes bug when specifying assembly qualified generics in XML configuration Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Configuration/FamilyParser.cs trunk/Source/StructureMap/Diagnostics/ValidationError.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/TypePath.cs trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs trunk/Source/StructureMap.Testing/Graph/TypePathTester.cs trunk/Source/StructureMap.Testing/ImplicitPluginFromPluggedTypeAttributeTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -32,8 +32,8 @@ { throw new StructureMapException( 303, - TypePath.GetAssemblyQualifiedName(_pluggedType), - TypePath.GetAssemblyQualifiedName(pluginType)); + _pluggedType.AssemblyQualifiedName, + pluginType.AssemblyQualifiedName); } } } Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -104,7 +104,7 @@ { if (graph.Registries.Contains(this)) return; - graph.Log.StartSource("Registry: " + TypePath.GetAssemblyQualifiedName(GetType())); + graph.Log.StartSource("Registry: " + GetType().AssemblyQualifiedName); _basicActions.ForEach(action => action()); _actions.ForEach(action => action(graph)); Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -94,7 +94,7 @@ InstanceMemento sourceMemento = new XmlAttributeInstanceMemento(node); string context = string.Format("MementoSource for {0}\n{1}", - TypePath.GetAssemblyQualifiedName(family.PluginType), node.OuterXml); + family.PluginType.AssemblyQualifiedName, node.OuterXml); _builder.WithSystemObject<MementoSource>(sourceMemento, context, source => family.AddMementoSource(source)); }); @@ -121,8 +121,7 @@ private void attachInterceptors(PluginFamily family, XmlElement familyElement) { - string contextBase = string.Format("creating an InstanceInterceptor for {0}\n", - TypePath.GetAssemblyQualifiedName(family.PluginType)); + string contextBase = string.Format("creating an InstanceInterceptor for {0}\n", family.PluginType.AssemblyQualifiedName); familyElement.ForEachChild("*/Interceptor").Do(element => { var interceptorMemento = new XmlAttributeInstanceMemento(element); Modified: trunk/Source/StructureMap/Diagnostics/ValidationError.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/ValidationError.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Diagnostics/ValidationError.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -29,7 +29,7 @@ writer.WriteLine( "-----------------------------------------------------------------------------------------------------"); writer.WriteLine("Validation Error in Method {0} of Instance '{1}' ({2})\n in PluginType {3}", MethodName, - Instance.Name, description, TypePath.GetAssemblyQualifiedName(PluginType)); + Instance.Name, description, PluginType.AssemblyQualifiedName); writer.WriteLine(); writer.WriteLine(Exception.ToString()); writer.WriteLine(); Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -218,7 +218,7 @@ { if (string.IsNullOrEmpty(memento.InstanceKey)) { - memento.InstanceKey = "DefaultInstanceOf" + TypePath.GetAssemblyQualifiedName(PluginType); + memento.InstanceKey = "DefaultInstanceOf" + PluginType.AssemblyQualifiedName; } AddInstance(memento); Modified: trunk/Source/StructureMap/Graph/TypePath.cs =================================================================== --- trunk/Source/StructureMap/Graph/TypePath.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap/Graph/TypePath.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -10,45 +10,23 @@ [Serializable] public class TypePath { - private readonly string _assemblyName = string.Empty; - private readonly string _className; - public TypePath(string assemblyName, string className) { - _className = className; - _assemblyName = assemblyName; + AssemblyQualifiedName = className + "," + assemblyName; } public TypePath(Type type) { - _className = type.FullName; - _assemblyName = type.Assembly.GetName().Name; + AssemblyQualifiedName = type.AssemblyQualifiedName; } public TypePath(string assemblyQualifiedName) { - string[] parts = assemblyQualifiedName.Split(','); - - _className = parts[0].Trim(); - - if (parts.Length > 1) _assemblyName = parts[1].Trim(); + AssemblyQualifiedName = assemblyQualifiedName; } - public string AssemblyQualifiedName - { - get { return _className + "," + _assemblyName; } - } + public string AssemblyQualifiedName { get; private set; } - public string AssemblyName - { - get { return _assemblyName; } - } - - public string ClassName - { - get { return _className; } - } - public static TypePath CreateFromXmlNode(XmlNode node) { string typeName = node.Attributes[XmlConstants.TYPE_ATTRIBUTE].Value; @@ -63,12 +41,6 @@ element.SetAttribute(XmlConstants.ASSEMBLY, type.Assembly.GetName().Name); } - public static string GetAssemblyQualifiedName(Type type) - { - var path = new TypePath(type); - return path.AssemblyQualifiedName; - } - public Type FindType() { try Modified: trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -23,7 +23,7 @@ "; XmlElement element = DataMother.BuildDocument(xml).DocumentElement; - element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof (ClassWithStringAndIntArray))); + element.SetAttribute("PluggedType", typeof (ClassWithStringAndIntArray).AssemblyQualifiedName); var memento = new XmlAttributeInstanceMemento(element); var graph = new PluginGraph(); @@ -53,7 +53,7 @@ "; XmlElement element = DataMother.BuildDocument(xml).DocumentElement; - element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof (ClassWithDictionary))); + element.SetAttribute("PluggedType", typeof (ClassWithDictionary).AssemblyQualifiedName); var memento = new XmlNodeInstanceMemento(element, "Type", "Key"); @@ -84,7 +84,7 @@ "; XmlElement element = DataMother.BuildDocument(xml).DocumentElement; - element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof (ClassWithDictionary))); + element.SetAttribute("PluggedType", typeof (ClassWithDictionary).AssemblyQualifiedName); var memento = new XmlAttributeInstanceMemento(element); Modified: trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -154,7 +154,7 @@ { if (typeof (IBootstrapper).IsAssignableFrom(type)) { - Debug.WriteLine(TypePath.GetAssemblyQualifiedName(type)); + Debug.WriteLine(type.AssemblyQualifiedName); } } } Modified: trunk/Source/StructureMap.Testing/Graph/TypePathTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TypePathTester.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap.Testing/Graph/TypePathTester.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -1,5 +1,7 @@ using NUnit.Framework; using StructureMap.Graph; +using StructureMap.Testing.GenericWidgets; +using StructureMap.Testing.Widget; namespace StructureMap.Testing.Graph { @@ -21,5 +23,15 @@ var path = new TypePath(GetType()); path.FindType(); } + + [Test] + public void can_parse_assembly_qualified_generics() + { + var sampleGenericType = typeof(IConcept<AWidget>); + var genericAssemblyQualifiedName = sampleGenericType.AssemblyQualifiedName; + + var path = new TypePath(genericAssemblyQualifiedName); + path.FindType().ShouldEqual(sampleGenericType); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/ImplicitPluginFromPluggedTypeAttributeTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ImplicitPluginFromPluggedTypeAttributeTester.cs 2009-10-07 18:25:50 UTC (rev 268) +++ trunk/Source/StructureMap.Testing/ImplicitPluginFromPluggedTypeAttributeTester.cs 2009-10-08 16:44:14 UTC (rev 269) @@ -21,7 +21,7 @@ Type pluggedType = typeof (StubbedGateway); var values = new NameValueCollection(); - values.Add(XmlConstants.PLUGGED_TYPE, TypePath.GetAssemblyQualifiedName(pluggedType)); + values.Add(XmlConstants.PLUGGED_TYPE, pluggedType.AssemblyQualifiedName); _memento = new MemoryInstanceMemento(string.Empty, "Frank", values); _expectedPlugin = new Plugin(pluggedType); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-10-19 02:59:24
|
Revision: 270 http://structuremap.svn.sourceforge.net/structuremap/?rev=270&view=rev Author: flimflan Date: 2009-10-19 02:59:11 +0000 (Mon, 19 Oct 2009) Log Message: ----------- Experimental support for type scanning which needs to know about all available plugin/concrete types when making a decision. SingleImplementationScanner automatically registers a concrete type if it is the only implementation of an interface. All names/API subject to change... Modified Paths: -------------- trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/Util/Cache.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-10-08 16:44:14 UTC (rev 269) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -81,6 +81,7 @@ /// <param name="scanner"></param> void With(ITypeScanner scanner); + void With(IHeavyweightTypeScanner heavyweightScanner); /// <summary> /// Adds the DefaultConventionScanner to the scanning operations. I.e., a concrete /// class named "Something" that implements "ISomething" will be automatically @@ -189,6 +190,7 @@ private readonly List<Predicate<Type>> _excludes = new List<Predicate<Type>>(); private readonly List<Predicate<Type>> _includes = new List<Predicate<Type>>(); private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); + private readonly List<IHeavyweightTypeScanner> _heavyweightScanners = new List<IHeavyweightTypeScanner>(); public AssemblyScanner() { @@ -204,7 +206,11 @@ internal void ScanForAll(PluginGraph pluginGraph) { + var heavyweightScan = configureHeavyweightScan(); + _assemblies.ForEach(assem => scanTypesInAssembly(assem, pluginGraph)); + + performHeavyweightScan(pluginGraph, heavyweightScan); } private void scanTypesInAssembly(Assembly assembly, PluginGraph graph) @@ -225,6 +231,23 @@ } } + private TypeMapBuilder configureHeavyweightScan() + { + var typeMapBuilder = new TypeMapBuilder(); + if (_heavyweightScanners.Count > 0) + { + With(typeMapBuilder); + } + return typeMapBuilder; + } + + private void performHeavyweightScan(PluginGraph pluginGraph, TypeMapBuilder typeMapBuilder) + { + var typeMaps = typeMapBuilder.GetTypeMaps(); + _heavyweightScanners.ForEach(scanner => scanner.Process(pluginGraph, typeMaps)); + typeMapBuilder.Dispose(); + } + private bool isInTheExcludes(Type type) { if (_excludes.Count == 0) return false; @@ -282,7 +305,14 @@ _scanners.Add(scanner); } + + public void With(IHeavyweightTypeScanner heavyweightScanner) + { + if (_heavyweightScanners.Contains(heavyweightScanner)) return; + _heavyweightScanners.Add(heavyweightScanner); + } + public void WithDefaultConventions() { With<DefaultConventionScanner>(); Added: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs (rev 0) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using StructureMap.Util; + +namespace StructureMap.Graph +{ + public interface IHeavyweightTypeScanner + { + void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps); + } + + + public class TypeMap + { + internal TypeMap(Type pluginType, IList<Type> concreteTypes) + { + PluginType = pluginType; + ConcreteTypes = new ReadOnlyCollection<Type>(concreteTypes); + } + + public IList<Type> ConcreteTypes { get; private set; } + public Type PluginType { get; private set; } + } + + internal class TypeMapBuilder : TypeRules, ITypeScanner, IDisposable + { + private readonly Cache<Type, List<Type>> _implementations = new Cache<Type, List<Type>>(t => new List<Type>()); + + public void Process(Type type, PluginGraph graph) + { + if (!IsConcrete(type) || !Constructor.HasConstructors(type)) return; + var pluginTypes = FindPluginTypes(type); + + foreach (var pluginType in pluginTypes) + { + _implementations[pluginType].Add(type); + } + } + + private IEnumerable<Type> FindPluginTypes(Type type) + { + // lets not worry about abstract base classes for now + return type.GetInterfaces().Where(t => t.IsPublic); + } + + public IEnumerable<TypeMap> GetTypeMaps() + { + return _implementations.Contents().Select(pair => new TypeMap(pair.Key, pair.Value)); + } + + public void Dispose() + { + _implementations.Clear(); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-10-08 16:44:14 UTC (rev 269) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -1,5 +1,4 @@ using System; -using StructureMap; namespace StructureMap.Graph { Added: trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs (rev 0) +++ trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.Linq; + +namespace StructureMap.Graph +{ + public class SingleImplementationScanner : TypeRules, IHeavyweightTypeScanner + { + public void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps) + { + foreach(var map in typeMaps.Where(map => map.ConcreteTypes.Count == 1)) + { + graph.AddType(map.PluginType, map.ConcreteTypes[0]); + } + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-10-08 16:44:14 UTC (rev 269) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-10-19 02:59:11 UTC (rev 270) @@ -395,9 +395,11 @@ <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> + <Compile Include="Graph\IHeavyweightTypeScanner.cs" /> <Compile Include="Graph\ITypeScanner.cs" /> <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> + <Compile Include="Graph\SingleImplementationScanner.cs" /> <Compile Include="IContext.cs" /> <Compile Include="Pipeline\ConditionalInstance.cs" /> <Compile Include="Pipeline\HttpContextLifecycle.cs" /> Modified: trunk/Source/StructureMap/Util/Cache.cs =================================================================== --- trunk/Source/StructureMap/Util/Cache.cs 2009-10-08 16:44:14 UTC (rev 269) +++ trunk/Source/StructureMap/Util/Cache.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -176,6 +176,11 @@ return returnValue; } + public IDictionary<KEY, VALUE> Contents() + { + return _values; + } + public void Remove(KEY key) { if (_values.ContainsKey(key)) Added: trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2009-10-19 02:59:11 UTC (rev 270) @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using StructureMap.Graph; +using StructureMap.Util; + +namespace StructureMap.Testing.Graph +{ + [TestFixture] + public class SingleImplementationScannerTester + { + [Test] + public void registers_plugins_that_only_have_a_single_implementation() + { + var container = new Container(registry => + { + registry.Scan(x => + { + x.TheCallingAssembly(); + x.IncludeNamespaceContainingType<SingleImplementationScannerTester>(); + x.With(new SingleImplementationScanner()); + }); + }); + + container.GetInstance<IOnlyHaveASingleConcreteImplementation>() + .ShouldBeOfType<MyNameIsNotConventionallyRelatedToMyInterface>(); + } + + [Test] + public void other() + { + typeof(Type).IsValueType.ShouldBeFalse(); + } + } + + + public interface IOnlyHaveASingleConcreteImplementation + { + + } + public class MyNameIsNotConventionallyRelatedToMyInterface : IOnlyHaveASingleConcreteImplementation + { + + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-08 16:44:14 UTC (rev 269) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-19 02:59:11 UTC (rev 270) @@ -252,6 +252,7 @@ <Compile Include="Graph\ExceptionHandling\StructureMapExceptionTester.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Graph\SingleImplementationScannerTester.cs" /> <Compile Include="Graph\TestExplicitArguments.cs" /> <Compile Include="Graph\FillDependenciesTester.cs"> <SubType>Code</SubType> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-10-24 23:19:26
|
Revision: 271 http://structuremap.svn.sourceforge.net/structuremap/?rev=271&view=rev Author: flimflan Date: 2009-10-24 23:19:17 +0000 (Sat, 24 Oct 2009) Log Message: ----------- Resolved Code Access Security issue when running under ASP.NET attempting to read default config file (reported by Eric Sowell). - When configuration your container, you must specify IgnoreDefaultFile = true Modified Paths: -------------- trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs trunk/Source/StructureMap/Emitting/DynamicAssembly.cs Modified: trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs 2009-10-19 02:59:11 UTC (rev 270) +++ trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs 2009-10-24 23:19:17 UTC (rev 271) @@ -126,9 +126,10 @@ private void addConfigurationFromStructureMapConfig(ICollection<ConfigurationParser> list) { -// Pick up the configuration in the default StructureMap.config - string pathToStructureMapConfig = GetStructureMapConfigurationPath(); - if (shouldUseStructureMapConfigFileAt(pathToStructureMapConfig)) + if (_ignoreDefaultFile) return; + // Pick up the configuration in the default StructureMap.config + var pathToStructureMapConfig = GetStructureMapConfigurationPath(); + if ((_useAndEnforceExistenceOfDefaultFile || File.Exists(pathToStructureMapConfig))) { _log.Try(() => { @@ -138,14 +139,7 @@ } } - private bool shouldUseStructureMapConfigFileAt(string pathToStructureMapConfig) - { - return - (_useAndEnforceExistenceOfDefaultFile || - File.Exists(pathToStructureMapConfig)) && !_ignoreDefaultFile; - } - public static ConfigurationParser[] GetParsers(XmlNode node, GraphLog log) { var builder = new ConfigurationParserBuilder(log); Modified: trunk/Source/StructureMap/Emitting/DynamicAssembly.cs =================================================================== --- trunk/Source/StructureMap/Emitting/DynamicAssembly.cs 2009-10-19 02:59:11 UTC (rev 270) +++ trunk/Source/StructureMap/Emitting/DynamicAssembly.cs 2009-10-24 23:19:17 UTC (rev 271) @@ -16,7 +16,6 @@ private AssemblyBuilder _assemblyBuilder; private bool _isCompiled; private ModuleBuilder _module; - private string DLLName; public DynamicAssembly(string name) { @@ -46,14 +45,9 @@ assemName.CultureInfo = new CultureInfo("en"); assemName.SetPublicKeyToken(null); - DLLName = Name + ".dll"; - _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.RunAndSave); - //_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); + _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run); - _module = _assemblyBuilder.DefineDynamicModule(Name, DLLName); - - - //_module = _assemblyBuilder.DefineDynamicModule(Name); + _module = _assemblyBuilder.DefineDynamicModule(Name); } @@ -84,13 +78,8 @@ newClass.Bake(); } - //_assemblyBuilder.Save(_name + ".dll"); - - //assemBuilder.Save(DLLName); - //Assembly assem = AppDomain.CurrentDomain.Load(this.Name); _isCompiled = true; return _assemblyBuilder; - //return assem; } } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-11-02 00:32:25
|
Revision: 272 http://structuremap.svn.sourceforge.net/structuremap/?rev=272&view=rev Author: flimflan Date: 2009-11-02 00:32:15 +0000 (Mon, 02 Nov 2009) Log Message: ----------- Moved all functionality from TypeRules base class to TypeExtensions Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs trunk/Source/StructureMap/Model.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/LiteralInstance.cs trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs trunk/Source/StructureMap/Pipeline/UserControlInstance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs Removed Paths: ------------- trunk/Source/StructureMap/Graph/TypeRules.cs trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -28,7 +28,7 @@ throw new StructureMapException(180, _pluggedType.AssemblyQualifiedName); } - if (!TypeRules.CanBeCast(pluginType, _pluggedType)) + if (!_pluggedType.CanBeCastTo(pluginType)) { throw new StructureMapException( 303, Modified: trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -5,13 +5,13 @@ namespace StructureMap.Configuration { - public class PrimitiveArrayReader : TypeRules, ITypeReader + public class PrimitiveArrayReader : ITypeReader { #region ITypeReader Members public bool CanProcess(Type pluginType) { - return IsPrimitiveArray(pluginType); + return pluginType.IsPrimitiveArray(); } public Instance Read(XmlNode node, Type pluginType) Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Container.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -10,7 +10,7 @@ namespace StructureMap { - public class Container : TypeRules, IContainer + public class Container : IContainer { private InterceptorLibrary _interceptorLibrary; private PipelineGraph _pipelineGraph; @@ -276,7 +276,7 @@ [Obsolete("Please use GetInstance(Type) instead")] public object FillDependencies(Type type) { - if (!IsConcrete(type)) + if (!type.IsConcrete()) { throw new StructureMapException(230, type.FullName); } @@ -614,7 +614,7 @@ /// <param name="object"></param> public void Inject(Type pluginType, object @object) { - if (!CanBeCast(pluginType, @object.GetType())) + if (!@object.GetType().CanBeCastTo(pluginType)) { throw new StructureMapException(220, pluginType.FullName, @object.GetType().FullName); Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -4,7 +4,7 @@ namespace StructureMap.Graph { - public class Constructor : TypeRules + public class Constructor { private ConstructorInfo _ctor; private readonly Type _pluggedType; @@ -69,7 +69,7 @@ foreach (ParameterInfo parameter in _ctor.GetParameters()) { - if (!IsAutoFillable(parameter.ParameterType)) + if (!parameter.ParameterType.IsAutoFillable()) { return false; } @@ -125,11 +125,11 @@ private void visitParameter(ParameterInfo info, Type parameterType, IArgumentVisitor visitor) { - if (IsPrimitive(parameterType)) visitor.PrimitiveParameter(info); - if (IsChild(parameterType)) visitor.ChildParameter(info); - if (IsChildArray(parameterType)) visitor.ChildArrayParameter(info); - if (IsEnum(parameterType)) visitor.EnumParameter(info); - if (IsString(parameterType)) visitor.StringParameter(info); + if (parameterType.IsPrimitive()) visitor.PrimitiveParameter(info); + if (parameterType.IsChild()) visitor.ChildParameter(info); + if (parameterType.IsChildArray()) visitor.ChildArrayParameter(info); + if (parameterType.IsEnum) visitor.EnumParameter(info); + if (parameterType.IsString()) visitor.StringParameter(info); } public bool HasArguments() Modified: trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -2,7 +2,7 @@ namespace StructureMap.Graph { - public class FindAllTypesFilter : TypeRules, ITypeScanner + public class FindAllTypesFilter : ITypeScanner { private readonly Type _pluginType; private Func<Type, string> _getName = type => PluginCache.GetPlugin(type).ConcreteKey; Modified: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -24,13 +24,13 @@ public Type PluginType { get; private set; } } - internal class TypeMapBuilder : TypeRules, ITypeScanner, IDisposable + internal class TypeMapBuilder : ITypeScanner, IDisposable { private readonly Cache<Type, List<Type>> _implementations = new Cache<Type, List<Type>>(t => new List<Type>()); public void Process(Type type, PluginGraph graph) { - if (!IsConcrete(type) || !Constructor.HasConstructors(type)) return; + if (!type.IsConcrete() || !Constructor.HasConstructors(type)) return; var pluginTypes = FindPluginTypes(type); foreach (var pluginType in pluginTypes) Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -7,13 +7,13 @@ void Process(Type type, PluginGraph graph); } - public class DefaultConventionScanner : TypeRules, ITypeScanner + public class DefaultConventionScanner : ITypeScanner { #region ITypeScanner Members public void Process(Type type, PluginGraph graph) { - if (!IsConcrete(type)) return; + if (!type.IsConcrete()) return; Type pluginType = FindPluginType(type); if (pluginType != null && Constructor.HasConstructors(type)) Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -11,7 +11,7 @@ /// PluginFamily\x92s PluginType. The properties of a Plugin are the CLR Type of the concrete class, /// and the human-friendly concrete key that StructureMap will use to identify the Type. /// </summary> - public class Plugin : TypeRules + public class Plugin { public static readonly string DEFAULT = "DEFAULT"; private readonly Constructor _constructor; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -11,7 +11,7 @@ /// the system. A PluginFamily defines a CLR Type that StructureMap can build, and all of the possible /// Plugin\x92s implementing the CLR Type. /// </summary> - public class PluginFamily : TypeRules, IPluginFamily + public class PluginFamily : IPluginFamily { private readonly Cache<string, Instance> _instances = new Cache<string, Instance>(delegate { return null; }); private readonly List<InstanceMemento> _mementoList = new List<InstanceMemento>(); @@ -173,7 +173,7 @@ private void assertPluggability(Type pluggedType) { - if (!CanBeCast(_pluginType, pluggedType)) + if (!pluggedType.CanBeCastTo(_pluginType)) { throw new StructureMapException(104, pluggedType, _pluginType); } @@ -291,7 +291,7 @@ public void AddType(Type concreteType) { - if (!CanBeCast(_pluginType, concreteType)) return; + if (!concreteType.CanBeCastTo(_pluginType)) return; if (FindPlugin(concreteType) == null) { @@ -301,7 +301,7 @@ public void AddType(Type concreteType, string name) { - if (!CanBeCast(_pluginType, concreteType)) return; + if (!concreteType.CanBeCastTo(_pluginType)) return; if (FindPlugin(name) == null) { Modified: trunk/Source/StructureMap/Graph/SetterProperty.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -7,7 +7,7 @@ /// <summary> /// Represents a PropertyInfo of a Plugin.PluggedType that is filled by Setter Injection /// </summary> - public class SetterProperty : TypeRules + public class SetterProperty { private readonly PropertyInfo _property; @@ -34,7 +34,7 @@ public bool CanBeAutoFilled { - get { return IsAutoFillable(_property.PropertyType); } + get { return _property.PropertyType.IsAutoFillable(); } } public void Visit(IArgumentVisitor visitor) @@ -44,11 +44,11 @@ // Ignore indexer properties if (_property.GetIndexParameters().Length > 0) return; - if (IsPrimitive(propertyType)) visitor.PrimitiveSetter(_property, IsMandatory); - if (IsChild(propertyType)) visitor.ChildSetter(_property, IsMandatory); - if (IsChildArray(propertyType)) visitor.ChildArraySetter(_property, IsMandatory); - if (IsEnum(propertyType)) visitor.EnumSetter(_property, IsMandatory); - if (IsString(propertyType)) visitor.StringSetter(_property, IsMandatory); + if (propertyType.IsPrimitive()) visitor.PrimitiveSetter(_property, IsMandatory); + if (propertyType.IsChild()) visitor.ChildSetter(_property, IsMandatory); + if (propertyType.IsChildArray()) visitor.ChildArraySetter(_property, IsMandatory); + if (propertyType.IsEnum) visitor.EnumSetter(_property, IsMandatory); + if (propertyType.IsString()) visitor.StringSetter(_property, IsMandatory); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -3,7 +3,7 @@ namespace StructureMap.Graph { - public class SingleImplementationScanner : TypeRules, IHeavyweightTypeScanner + public class SingleImplementationScanner : IHeavyweightTypeScanner { public void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps) { Deleted: trunk/Source/StructureMap/Graph/TypeRules.cs =================================================================== --- trunk/Source/StructureMap/Graph/TypeRules.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Graph/TypeRules.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,117 +0,0 @@ -using System; - -namespace StructureMap.Graph -{ - public class TypeRules - { - /// <summary> - /// Determines if the pluggedType can be upcast to the pluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool CanBeCast(Type pluginType, Type pluggedType) - { - if (pluggedType.IsInterface || pluggedType.IsAbstract) - { - return false; - } - - if (noPublicConstructors(pluggedType)) - { - return false; - } - - if (IsGeneric(pluginType)) - { - return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); - } - - if (IsGeneric(pluggedType)) - { - return false; - } - - - return pluginType.IsAssignableFrom(pluggedType); - } - - public static bool IsGeneric(Type pluggedType) - { - return pluggedType.IsGenericTypeDefinition || pluggedType.ContainsGenericParameters; - } - - /// <summary> - /// Determines if the PluggedType is a valid Plugin into the - /// PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool IsExplicitlyMarkedAsPlugin(Type pluginType, Type pluggedType) - { - bool returnValue = false; - - bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); - if (markedAsPlugin) - { - returnValue = CanBeCast(pluginType, pluggedType); - } - - return returnValue; - } - - private static bool noPublicConstructors(Type pluggedType) - { - return pluggedType.GetConstructors().Length == 0; - } - - protected bool IsString(Type type) - { - return type.Equals(typeof (string)); - } - - protected bool IsPrimitive(Type type) - { - return type.IsPrimitive && !IsString(type) && type != typeof (IntPtr); - } - - protected internal bool IsSimple(Type type) - { - return type.IsPrimitive || IsString(type) || IsEnum(type); - } - - protected bool IsEnum(Type type) - { - return type.IsEnum; - } - - protected bool IsChild(Type type) - { - return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); - } - - protected bool IsChildArray(Type type) - { - return type.IsArray && !IsSimple(type.GetElementType()); - } - - protected bool IsPrimitiveArray(Type type) - { - return type.IsArray && IsSimple(type.GetElementType()); - } - - protected internal bool IsConcrete(Type type) - { - return !type.IsInterface && !type.IsAbstract; - } - - - protected bool IsAutoFillable(Type type) - { - return IsChild(type) || IsChildArray(type); - } - } - - -} \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceBuilderList.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilderList.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/InstanceBuilderList.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -42,7 +42,7 @@ } // Add a missing PluggedType if we can - if (TypeRules.CanBeCast(_pluginType, pluggedType)) + if (pluggedType.CanBeCastTo(_pluginType)) { var plugin = new Plugin(pluggedType); processPlugin(plugin); Modified: trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -35,7 +35,7 @@ public bool MatchesType(Type type) { - return TypeRules.CanBeCast(_pluginType, type); + return type.CanBeCastTo(_pluginType); } public object Process(object target, IContext context) Modified: trunk/Source/StructureMap/Model.cs =================================================================== --- trunk/Source/StructureMap/Model.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Model.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -64,7 +64,7 @@ bool HasImplementationsFor<T>(); } - public class Model : TypeRules, IModel + public class Model : IModel { private readonly PipelineGraph _graph; Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -62,7 +62,7 @@ Type specificType = _pluggedType.IsGenericTypeDefinition ? _pluggedType.MakeGenericType(templateTypes) : _pluggedType; - if (TypeRules.CanBeCast(family.PluginType, specificType)) + if (specificType.CanBeCastTo(family.PluginType)) { var instance = new ConfiguredInstance(specificType); instance._arrays = _arrays; Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -178,7 +178,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _pluggedType); + return _pluggedType.CanBeCastTo(family.PluginType); } internal override bool Matches(Plugin plugin) Modified: trunk/Source/StructureMap/Pipeline/LiteralInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -36,7 +36,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _object.GetType()); + return _object.GetType().CanBeCastTo(family.PluginType); } protected override string getDescription() Modified: trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -27,7 +27,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return TypeRules.CanBeCast(family.PluginType, _prototype.GetType()); + return _prototype.GetType().CanBeCastTo(family.PluginType); } protected override string getDescription() Modified: trunk/Source/StructureMap/Pipeline/UserControlInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -29,7 +29,7 @@ Control control = new Page().LoadControl(_url); Type pluggedType = control.GetType(); - if (!TypeRules.CanBeCast(pluginType, pluggedType)) + if (!pluggedType.CanBeCastTo(pluginType)) { throw new StructureMapException(303, pluginType, pluggedType); } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-11-02 00:32:15 UTC (rev 272) @@ -151,7 +151,6 @@ <Compile Include="Graph\IArgumentVisitor.cs" /> <Compile Include="Graph\IPluginFamily.cs" /> <Compile Include="Graph\AssemblyScanner.cs" /> - <Compile Include="Graph\TypeRules.cs" /> <Compile Include="InstanceBuilderList.cs" /> <Compile Include="InstanceCache.cs" /> <Compile Include="InstanceFamily.cs" /> Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -17,26 +17,11 @@ return new ReferencedInstance(key); } - public static bool IsSimple(this Type type) - { - return new TypeRules().IsSimple(type); - } - - public static bool IsConcrete(this Type type) - { - return new TypeRules().IsConcrete(type); - } - public static bool IsGeneric(this Type type) { return type.IsGenericTypeDefinition || type.ContainsGenericParameters; } - public static bool CanBeCastTo(this Type pluggedType, Type pluginType) - { - return TypeRules.CanBeCast(pluginType, pluggedType); - } - public static bool IsConcreteAndAssignableTo(this Type pluggedType, Type pluginType) { return pluggedType.IsConcrete() && pluginType.IsAssignableFrom(pluggedType); @@ -87,7 +72,7 @@ if (type.IsGenericType) { string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = string.Join(", ", parameters); + var parameterList = String.Join(", ", parameters); return "{0}<{1}>".ToFormat(type.Name, parameterList); } @@ -99,12 +84,109 @@ if (type.IsGenericType) { string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = string.Join(", ", parameters); + var parameterList = String.Join(", ", parameters); return "{0}<{1}>".ToFormat(type.Name, parameterList); } return type.FullName; } + /// <summary> + /// Determines if the pluggedType can be upcast to the pluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool CanBeCastTo(this Type pluggedType, Type pluginType) + { + if (pluggedType.IsInterface || pluggedType.IsAbstract) + { + return false; + } + + if (noPublicConstructors(pluggedType)) + { + return false; + } + + if (IsGeneric(pluginType)) + { + return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); + } + + if (IsGeneric(pluggedType)) + { + return false; + } + + + return pluginType.IsAssignableFrom(pluggedType); + } + + + /// <summary> + /// Determines if the PluggedType is a valid Plugin into the + /// PluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool IsExplicitlyMarkedAsPlugin(this Type pluggedType, Type pluginType) + { + bool returnValue = false; + + bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); + if (markedAsPlugin) + { + returnValue = CanBeCastTo(pluggedType, pluginType); + } + + return returnValue; + } + + private static bool noPublicConstructors(Type pluggedType) + { + return pluggedType.GetConstructors().Length == 0; + } + + public static bool IsString(this Type type) + { + return type.Equals(typeof (string)); + } + + public static bool IsPrimitive(this Type type) + { + return type.IsPrimitive && !IsString(type) && type != typeof (IntPtr); + } + + public static bool IsSimple(this Type type) + { + return type.IsPrimitive || IsString(type) || type.IsEnum; + } + + public static bool IsChild(this Type type) + { + return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); + } + + public static bool IsChildArray(this Type type) + { + return type.IsArray && !IsSimple(type.GetElementType()); + } + + public static bool IsPrimitiveArray(this Type type) + { + return type.IsArray && IsSimple(type.GetElementType()); + } + + public static bool IsConcrete(this Type type) + { + return !type.IsAbstract; + } + + public static bool IsAutoFillable(this Type type) + { + return IsChild(type) || IsChildArray(type); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -137,11 +137,10 @@ [Test] public void CanPlugGenericConcreteClassIntoGenericInterfaceWithNoGenericParametersSpecified() { - bool canPlug = TypeRules.CanBeCast(typeof (IGenericService<>), typeof (GenericService<>)); + bool canPlug = typeof (GenericService<>).CanBeCastTo(typeof (IGenericService<>)); Assert.IsTrue(canPlug); } - [Test] public void Define_profile_with_generics_and_concrete_type() { Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -181,7 +181,7 @@ var family = new PluginFamily(typeof (IGateway), graph); var instance = new ConfiguredInstance(typeof (ColorRule)); - Assert.IsFalse(TypeRules.CanBeCast(typeof (IGateway), typeof (Rule))); + Assert.IsFalse(typeof (Rule).CanBeCastTo(typeof (IGateway))); family.AddInstance(instance); Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -97,13 +97,13 @@ [Test] public void BadPluginToAbstractClass() { - Assert.AreEqual(false, TypeRules.CanBeCast(_widgetmaker, _colorWidget), "ColorWidget is NOT a WidgetMaker"); + Assert.AreEqual(false, _colorWidget.CanBeCastTo(_widgetmaker), "ColorWidget is NOT a WidgetMaker"); } [Test] public void BadPluginToInterface() { - Assert.AreEqual(false, TypeRules.CanBeCast(_iwidget, _moneywidgetmaker), + Assert.AreEqual(false, _moneywidgetmaker.CanBeCastTo(_iwidget), "MoneyWidgetMaker is NOT an IWidget"); } @@ -190,15 +190,13 @@ [Test] public void CanNotPluginWithoutAttribute() { - string msg = "NotPluggableWidget cannot plug into IWidget automatically"; - Assert.AreEqual(false, TypeRules.IsExplicitlyMarkedAsPlugin(_iwidget, typeof (NotPluggable)), msg); + Assert.IsFalse(typeof (NotPluggable).IsExplicitlyMarkedAsPlugin(_iwidget), "NotPluggableWidget cannot plug into IWidget automatically"); } [Test] public void CanPluginWithAttribute() { - Assert.AreEqual(true, TypeRules.IsExplicitlyMarkedAsPlugin(_iwidget, _colorWidget), - "ColorWidget plugs into IWidget"); + Assert.IsTrue(_colorWidget.IsExplicitlyMarkedAsPlugin(_iwidget), "ColorWidget plugs into IWidget"); } @@ -300,14 +298,14 @@ [Test] public void GoodPluginToAbstractClass() { - Assert.AreEqual(true, TypeRules.CanBeCast(_widgetmaker, _moneywidgetmaker), + Assert.AreEqual(true, _moneywidgetmaker.CanBeCastTo(_widgetmaker), "MoneyWidgetMaker is a WidgetMaker"); } [Test] public void GoodPluginToInterface() { - Assert.AreEqual(true, TypeRules.CanBeCast(_iwidget, _colorWidget), "ColorWidget is an IWidget"); + Assert.AreEqual(true, _colorWidget.CanBeCastTo(_iwidget), "ColorWidget is an IWidget"); } [Test] Modified: trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -31,7 +31,7 @@ [Test] public void DoNotFindPluginWithNoPublicCTOR() { - Assert.IsFalse(TypeRules.CanBeCast(typeof (TypeIWantToFind), typeof (GreenType))); + Assert.IsFalse(typeof (GreenType).CanBeCastTo(typeof (TypeIWantToFind))); } Deleted: trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,86 +0,0 @@ -using NUnit.Framework; -using StructureMap.Graph; -using StructureMap.Testing.Widget2; -using StructureMap.Testing.Widget3; - -namespace StructureMap.Testing.Pipeline -{ - [TestFixture] - public class TypeRulesTester : TypeRules - { - #region Setup/Teardown - - [SetUp] - public void SetUp() - { - } - - #endregion - - [Test] - public void IsChild() - { - Assert.IsFalse(IsChild(typeof (int))); - Assert.IsFalse(IsChild(typeof (bool))); - Assert.IsFalse(IsChild(typeof (double))); - Assert.IsFalse(IsChild(typeof (string))); - Assert.IsFalse(IsChild(typeof (BreedEnum))); - Assert.IsFalse(IsChild(typeof (IGateway[]))); - Assert.IsTrue(IsChild(typeof (IGateway))); - } - - [Test] - public void IsChildArray() - { - Assert.IsFalse(IsChildArray(typeof (int))); - Assert.IsFalse(IsChildArray(typeof (bool))); - Assert.IsFalse(IsChildArray(typeof (double))); - Assert.IsFalse(IsChildArray(typeof (double[]))); - Assert.IsFalse(IsChildArray(typeof (string))); - Assert.IsFalse(IsChildArray(typeof (string[]))); - Assert.IsFalse(IsChildArray(typeof (BreedEnum))); - Assert.IsTrue(IsChildArray(typeof (IGateway[]))); - Assert.IsFalse(IsChildArray(typeof (IGateway))); - } - - [Test] - public void IsEnum() - { - Assert.IsFalse(IsEnum(typeof (int))); - Assert.IsFalse(IsEnum(typeof (bool))); - Assert.IsFalse(IsEnum(typeof (double))); - Assert.IsFalse(IsEnum(typeof (string))); - Assert.IsTrue(IsEnum(typeof (BreedEnum))); - Assert.IsFalse(IsEnum(typeof (IGateway))); - } - - [Test] - public void IsPrimitive() - { - Assert.IsTrue(IsPrimitive(typeof (int))); - Assert.IsTrue(IsPrimitive(typeof (bool))); - Assert.IsTrue(IsPrimitive(typeof (double))); - Assert.IsFalse(IsPrimitive(typeof (string))); - Assert.IsFalse(IsPrimitive(typeof (BreedEnum))); - Assert.IsFalse(IsPrimitive(typeof (IGateway))); - } - - [Test] - public void IsSimple() - { - Assert.IsTrue(IsSimple(typeof (int))); - Assert.IsTrue(IsSimple(typeof (bool))); - Assert.IsTrue(IsSimple(typeof (double))); - Assert.IsTrue(IsSimple(typeof (string))); - Assert.IsTrue(IsSimple(typeof (BreedEnum))); - Assert.IsFalse(IsSimple(typeof (IGateway))); - } - - [Test] - public void IsString() - { - Assert.IsTrue(IsString(typeof (string))); - Assert.IsFalse(IsString(typeof (int))); - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-11-02 00:32:15 UTC (rev 272) @@ -374,7 +374,6 @@ <Compile Include="Pipeline\SmartInstanceTester.cs" /> <Compile Include="Pipeline\StubBuildSession.cs" /> <Compile Include="Pipeline\ThreadLocalStorageLifecycleTester.cs" /> - <Compile Include="Pipeline\TypeRulesTester.cs" /> <Compile Include="PrimitiveArrayTester.cs" /> <Compile Include="Properties\Settings.Designer.cs"> <AutoGen>True</AutoGen> Modified: trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-10-24 23:19:17 UTC (rev 271) +++ trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-11-02 00:32:15 UTC (rev 272) @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using NUnit.Framework; +using NUnit.Framework; using StructureMap.Testing.GenericWidgets; +using StructureMap.Testing.Widget2; +using StructureMap.Testing.Widget3; namespace StructureMap.Testing { @@ -39,5 +37,61 @@ typeof(Service2).FindInterfaceThatCloses(typeof(IService<>)) .ShouldBeNull(); } + + [Test] + public void IsChild() + { + Assert.IsFalse(TypeExtensions.IsChild(typeof(int))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(bool))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(double))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(string))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(BreedEnum))); + Assert.IsFalse(TypeExtensions.IsChild(typeof(IGateway[]))); + Assert.IsTrue(TypeExtensions.IsChild(typeof(IGateway))); + } + + [Test] + public void IsChildArray() + { + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(int))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(bool))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(double))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(double[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(string))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(string[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(BreedEnum))); + Assert.IsTrue(TypeExtensions.IsChildArray(typeof(IGateway[]))); + Assert.IsFalse(TypeExtensions.IsChildArray(typeof(IGateway))); + } + + [Test] + public void IsPrimitive() + { + Assert.IsTrue(typeof(int).IsPrimitive()); + Assert.IsTrue(typeof(bool).IsPrimitive()); + Assert.IsTrue(typeof(double).IsPrimitive()); + Assert.IsFalse(typeof(string).IsPrimitive()); + Assert.IsFalse(typeof(BreedEnum).IsPrimitive()); + Assert.IsFalse(typeof(IGateway).IsPrimitive()); + } + + [Test] + public void IsSimple() + { + Assert.IsTrue(TypeExtensions.IsSimple(typeof(int))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(bool))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(double))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(string))); + Assert.IsTrue(TypeExtensions.IsSimple(typeof(BreedEnum))); + Assert.IsFalse(TypeExtensions.IsSimple(typeof(IGateway))); + } + + [Test] + public void IsString() + { + Assert.IsTrue(TypeExtensions.IsString(typeof(string))); + Assert.IsFalse(TypeExtensions.IsString(typeof(int))); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-11-02 15:08:09
|
Revision: 273 http://structuremap.svn.sourceforge.net/structuremap/?rev=273&view=rev Author: flimflan Date: 2009-11-02 15:07:59 +0000 (Mon, 02 Nov 2009) Log Message: ----------- Move TypeExtensions to StructureMap.TypeRules namespace so that it is not automatically imported by consumers. Modified Paths: -------------- trunk/Source/StructureMap/CloseGenericTypeExpression.cs trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs trunk/Source/StructureMap/Configuration/DSL/SetterConvention.cs trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/LiteralInstance.cs trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs trunk/Source/StructureMap/Pipeline/UserControlInstance.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Examples/Interception.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs Modified: trunk/Source/StructureMap/CloseGenericTypeExpression.cs =================================================================== --- trunk/Source/StructureMap/CloseGenericTypeExpression.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/CloseGenericTypeExpression.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using StructureMap.TypeRules; namespace StructureMap { Modified: trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,5 +1,6 @@ using System; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Configuration.DSL { Modified: trunk/Source/StructureMap/Configuration/DSL/SetterConvention.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/SetterConvention.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Configuration/DSL/SetterConvention.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.Reflection; using StructureMap.Graph; using StructureMap; +using StructureMap.TypeRules; namespace StructureMap.Configuration.DSL { Modified: trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.Xml; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.TypeRules; namespace StructureMap.Configuration { Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Container.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -7,6 +7,7 @@ using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; +using StructureMap.TypeRules; namespace StructureMap { Modified: trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.IO; using System.Text; using StructureMap.Pipeline; +using StructureMap.TypeRules; namespace StructureMap.Diagnostics { Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System; using System.Linq.Expressions; using System.Reflection; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,4 +1,5 @@ using System; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using StructureMap.TypeRules; using StructureMap.Util; namespace StructureMap.Graph Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,4 +1,5 @@ using System; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.Collections.Generic; using StructureMap.Attributes; using StructureMap.Pipeline; +using StructureMap.TypeRules; using StructureMap.Util; namespace StructureMap.Graph Modified: trunk/Source/StructureMap/Graph/SetterProperty.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Graph/SetterProperty.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System; using System.Reflection; using StructureMap.Attributes; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/InstanceBuilderList.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilderList.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/InstanceBuilderList.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using System.Collections.Generic; using StructureMap.Emitting; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap { Modified: trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,5 +1,6 @@ using System; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Interceptors { Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,5 +1,6 @@ using System; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/Pipeline/LiteralInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,5 +1,6 @@ using System; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,4 +1,5 @@ using System.Configuration; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,5 +1,6 @@ using System; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/Pipeline/UserControlInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System; using System.Web.UI; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Pipeline { Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -4,189 +4,192 @@ namespace StructureMap { - public static class TypeExtensions + namespace TypeRules { - public static bool IsInNamespace(this Type type, string nameSpace) + public static class TypeExtensions { - return type.Namespace.StartsWith(nameSpace); - } + public static bool IsInNamespace(this Type type, string nameSpace) + { + return type.Namespace.StartsWith(nameSpace); + } - public static ReferencedInstance GetReferenceTo(this Type type) - { - string key = PluginCache.GetPlugin(type).ConcreteKey; - return new ReferencedInstance(key); - } + public static ReferencedInstance GetReferenceTo(this Type type) + { + string key = PluginCache.GetPlugin(type).ConcreteKey; + return new ReferencedInstance(key); + } - public static bool IsGeneric(this Type type) - { - return type.IsGenericTypeDefinition || type.ContainsGenericParameters; - } + public static bool IsGeneric(this Type type) + { + return type.IsGenericTypeDefinition || type.ContainsGenericParameters; + } - public static bool IsConcreteAndAssignableTo(this Type pluggedType, Type pluginType) - { - return pluggedType.IsConcrete() && pluginType.IsAssignableFrom(pluggedType); - } + public static bool IsConcreteAndAssignableTo(this Type pluggedType, Type pluginType) + { + return pluggedType.IsConcrete() && pluginType.IsAssignableFrom(pluggedType); + } - public static bool ImplementsInterfaceTemplate(this Type pluggedType, Type templateType) - { - if (!pluggedType.IsConcrete()) return false; + public static bool ImplementsInterfaceTemplate(this Type pluggedType, Type templateType) + { + if (!pluggedType.IsConcrete()) return false; - foreach (var interfaceType in pluggedType.GetInterfaces()) + foreach (var interfaceType in pluggedType.GetInterfaces()) + { + if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + { + return true; + } + } + + return false; + } + + public static Type FindInterfaceThatCloses(this Type pluggedType, Type templateType) { - if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + if (!pluggedType.IsConcrete()) return null; + + foreach (var interfaceType in pluggedType.GetInterfaces()) { - return true; + if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + { + return interfaceType; + } } + + return pluggedType.BaseType == typeof(object) ? null : pluggedType.BaseType.FindInterfaceThatCloses(templateType); } - return false; - } + public static bool IsNullable(this Type type) + { + return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + } - public static Type FindInterfaceThatCloses(this Type pluggedType, Type templateType) - { - if (!pluggedType.IsConcrete()) return null; + public static Type GetInnerTypeFromNullable(this Type nullableType) + { + return nullableType.GetGenericArguments()[0]; + } - foreach (var interfaceType in pluggedType.GetInterfaces()) + public static string GetName(this Type type) { - if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + if (type.IsGenericType) { - return interfaceType; + string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); + var parameterList = String.Join(", ", parameters); + return "{0}<{1}>".ToFormat(type.Name, parameterList); } + + return type.Name; } - return pluggedType.BaseType == typeof(object) ? null : pluggedType.BaseType.FindInterfaceThatCloses(templateType); - } + public static string GetFullName(this Type type) + { + if (type.IsGenericType) + { + string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); + var parameterList = String.Join(", ", parameters); + return "{0}<{1}>".ToFormat(type.Name, parameterList); + } - public static bool IsNullable(this Type type) - { - return type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>); - } + return type.FullName; + } - public static Type GetInnerTypeFromNullable(this Type nullableType) - { - return nullableType.GetGenericArguments()[0]; - } + /// <summary> + /// Determines if the pluggedType can be upcast to the pluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool CanBeCastTo(this Type pluggedType, Type pluginType) + { + if (pluggedType.IsInterface || pluggedType.IsAbstract) + { + return false; + } - public static string GetName(this Type type) - { - if (type.IsGenericType) - { - string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = String.Join(", ", parameters); - return "{0}<{1}>".ToFormat(type.Name, parameterList); + if (noPublicConstructors(pluggedType)) + { + return false; + } + + if (IsGeneric(pluginType)) + { + return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); + } + + if (IsGeneric(pluggedType)) + { + return false; + } + + + return pluginType.IsAssignableFrom(pluggedType); } - return type.Name; - } - public static string GetFullName(this Type type) - { - if (type.IsGenericType) + /// <summary> + /// Determines if the PluggedType is a valid Plugin into the + /// PluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="pluggedType"></param> + /// <returns></returns> + public static bool IsExplicitlyMarkedAsPlugin(this Type pluggedType, Type pluginType) { - string[] parameters = Array.ConvertAll(type.GetGenericArguments(), t => t.GetName()); - var parameterList = String.Join(", ", parameters); - return "{0}<{1}>".ToFormat(type.Name, parameterList); + bool returnValue = false; + + bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); + if (markedAsPlugin) + { + returnValue = CanBeCastTo(pluggedType, pluginType); + } + + return returnValue; } - return type.FullName; - } + private static bool noPublicConstructors(Type pluggedType) + { + return pluggedType.GetConstructors().Length == 0; + } - /// <summary> - /// Determines if the pluggedType can be upcast to the pluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool CanBeCastTo(this Type pluggedType, Type pluginType) - { - if (pluggedType.IsInterface || pluggedType.IsAbstract) + public static bool IsString(this Type type) { - return false; + return type.Equals(typeof(string)); } - if (noPublicConstructors(pluggedType)) + public static bool IsPrimitive(this Type type) { - return false; + return type.IsPrimitive && !IsString(type) && type != typeof(IntPtr); } - if (IsGeneric(pluginType)) + public static bool IsSimple(this Type type) { - return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); + return type.IsPrimitive || IsString(type) || type.IsEnum; } - if (IsGeneric(pluggedType)) + public static bool IsChild(this Type type) { - return false; + return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); } + public static bool IsChildArray(this Type type) + { + return type.IsArray && !IsSimple(type.GetElementType()); + } - return pluginType.IsAssignableFrom(pluggedType); - } + public static bool IsPrimitiveArray(this Type type) + { + return type.IsArray && IsSimple(type.GetElementType()); + } + public static bool IsConcrete(this Type type) + { + return !type.IsAbstract; + } - /// <summary> - /// Determines if the PluggedType is a valid Plugin into the - /// PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool IsExplicitlyMarkedAsPlugin(this Type pluggedType, Type pluginType) - { - bool returnValue = false; - - bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); - if (markedAsPlugin) + public static bool IsAutoFillable(this Type type) { - returnValue = CanBeCastTo(pluggedType, pluginType); + return IsChild(type) || IsChildArray(type); } - - return returnValue; } - - private static bool noPublicConstructors(Type pluggedType) - { - return pluggedType.GetConstructors().Length == 0; - } - - public static bool IsString(this Type type) - { - return type.Equals(typeof (string)); - } - - public static bool IsPrimitive(this Type type) - { - return type.IsPrimitive && !IsString(type) && type != typeof (IntPtr); - } - - public static bool IsSimple(this Type type) - { - return type.IsPrimitive || IsString(type) || type.IsEnum; - } - - public static bool IsChild(this Type type) - { - return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); - } - - public static bool IsChildArray(this Type type) - { - return type.IsArray && !IsSimple(type.GetElementType()); - } - - public static bool IsPrimitiveArray(this Type type) - { - return type.IsArray && IsSimple(type.GetElementType()); - } - - public static bool IsConcrete(this Type type) - { - return !type.IsAbstract; - } - - public static bool IsAutoFillable(this Type type) - { - return IsChild(type) || IsChildArray(type); - } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Examples/Interception.cs =================================================================== --- trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -7,6 +7,7 @@ using StructureMap.Configuration.DSL; using StructureMap.Interceptors; using StructureMap; +using StructureMap.TypeRules; namespace StructureMap.Testing.Examples { Modified: trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -4,6 +4,7 @@ using StructureMap.Graph; using StructureMap.Pipeline; using StructureMap.Testing.GenericWidgets; +using StructureMap.TypeRules; namespace StructureMap.Testing { Modified: trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -7,6 +7,7 @@ using StructureMap.Testing.Widget; using System.Linq; using StructureMap.Testing.Widget5; +using StructureMap.TypeRules; namespace StructureMap.Testing.Graph { Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -9,6 +9,7 @@ using StructureMap.Pipeline; using StructureMap.Testing.Widget; using StructureMap.Testing.Widget3; +using StructureMap.TypeRules; using Rule=StructureMap.Testing.Widget.Rule; namespace StructureMap.Testing.Graph Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -11,6 +11,7 @@ using StructureMap.Testing.Widget; using StructureMap.Testing.Widget2; using StructureMap.Testing.Widget3; +using StructureMap.TypeRules; namespace StructureMap.Testing.Graph { Modified: trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -1,6 +1,7 @@ using System.Collections.Generic; using NUnit.Framework; using StructureMap.Graph; +using StructureMap.TypeRules; namespace StructureMap.Testing.Graph { Modified: trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-11-02 00:32:15 UTC (rev 272) +++ trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-11-02 15:07:59 UTC (rev 273) @@ -2,6 +2,7 @@ using StructureMap.Testing.GenericWidgets; using StructureMap.Testing.Widget2; using StructureMap.Testing.Widget3; +using StructureMap.TypeRules; namespace StructureMap.Testing { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-11-13 17:50:20
|
Revision: 274 http://structuremap.svn.sourceforge.net/structuremap/?rev=274&view=rev Author: flimflan Date: 2009-11-13 17:33:27 +0000 (Fri, 13 Nov 2009) Log Message: ----------- Fix crash when calling WhatDoIHave() on a nested container. Modified Paths: -------------- trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs trunk/Source/StructureMap.Testing/SpecificationExtensions.cs Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-11-02 15:07:59 UTC (rev 273) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-11-13 17:33:27 UTC (rev 274) @@ -41,15 +41,16 @@ } } - private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph) + private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph, GraphLog log) { _profileManager = profileManager; _genericsGraph = genericsGraph; + _log = log; } public PipelineGraph Clone() { - var clone = new PipelineGraph(_profileManager.Clone(), _genericsGraph.Clone()) + var clone = new PipelineGraph(_profileManager.Clone(), _genericsGraph.Clone(), _log) { _missingFactory = _missingFactory }; Modified: trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs 2009-11-02 15:07:59 UTC (rev 273) +++ trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs 2009-11-13 17:33:27 UTC (rev 274) @@ -1,7 +1,7 @@ using NUnit.Framework; using StructureMap.Attributes; -using StructureMap.Configuration.DSL; using StructureMap.Testing.GenericWidgets; +using StructureMap.Testing.Graph; using StructureMap.Testing.Widget; namespace StructureMap.Testing.Pipeline @@ -190,5 +190,17 @@ parentWidget.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("red"); childWidget1.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("green"); } + + [Test] + public void allow_nested_container_to_report_what_it_has() + { + var container = new Container(x => x.For<IAutomobile>().Use<Mustang>()); + + var nestedContainer = container.GetNestedContainer(); + nestedContainer.Inject<IEngine>(new PushrodEngine()); + + container.WhatDoIHave().ShouldNotBeEmpty().ShouldNotContain(typeof(IEngine).Name); + nestedContainer.WhatDoIHave().ShouldNotBeEmpty().ShouldContain(typeof(IEngine).Name); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/SpecificationExtensions.cs =================================================================== --- trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-11-02 15:07:59 UTC (rev 273) +++ trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-11-13 17:33:27 UTC (rev 274) @@ -106,9 +106,10 @@ Assert.IsNotEmpty(collection); } - public static void ShouldNotBeEmpty(this string aString) + public static string ShouldNotBeEmpty(this string aString) { Assert.IsNotEmpty(aString); + return aString; } public static void ShouldContain(this string actual, string expected) @@ -116,6 +117,12 @@ StringAssert.Contains(expected, actual); } + public static string ShouldNotContain(this string actual, string expected) + { + Assert.IsTrue(!actual.Contains(expected)); + return actual; + } + public static string ShouldBeEqualIgnoringCase(this string actual, string expected) { StringAssert.AreEqualIgnoringCase(expected, actual); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-03 20:55:01
|
Revision: 275 http://structuremap.svn.sourceforge.net/structuremap/?rev=275&view=rev Author: jeremydmiller Date: 2009-12-03 20:54:50 +0000 (Thu, 03 Dec 2009) Log Message: ----------- convenience method on ChildInstance. Other convenience stuff for Dovetail Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs trunk/Source/StructureMap.Testing/PerRequestInterceptorTester.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Example.cs trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeOnOpenGenericsTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -109,7 +109,13 @@ /// <returns></returns> public SmartInstance<CONCRETETYPE> Use<CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE { - return TheDefault.Is.OfConcreteType<CONCRETETYPE>(); + // This is *my* team's naming convention for generic parameters + // I know you may not like it, but it's my article so there + var instance = new SmartInstance<CONCRETETYPE>(); + + registerDefault(instance); + + return instance; } /// <summary> @@ -147,13 +153,37 @@ /// Convenience method to mark a PluginFamily as a Singleton /// </summary> /// <returns></returns> - public CreatePluginFamilyExpression<PLUGINTYPE> AsSingletons() + public CreatePluginFamilyExpression<PLUGINTYPE> Singleton() { - _alterations.Add(family => family.SetScopeTo(InstanceScope.Singleton)); + return lifecycleIs(InstanceScope.Singleton); + } + + private CreatePluginFamilyExpression<PLUGINTYPE> lifecycleIs(InstanceScope lifecycle) + { + _alterations.Add(family => family.SetScopeTo(lifecycle)); return this; } /// <summary> + /// Convenience method to mark a PluginFamily as a Hybrid lifecycle + /// </summary> + /// <returns></returns> + public CreatePluginFamilyExpression<PLUGINTYPE> HybridHttpOrThreadLocalScoped() + { + return lifecycleIs(InstanceScope.Hybrid); + } + + /// <summary> + /// Convenience method to mark a PluginFamily as HttpContext scoped + /// </summary> + /// <returns></returns> + public CreatePluginFamilyExpression<PLUGINTYPE> HttpContextScoped() + { + return lifecycleIs(InstanceScope.HttpContext); + } + + + /// <summary> /// Register an Action to run against any object of this PluginType immediately after /// it is created, but before the new object is passed back to the caller /// </summary> Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -169,7 +169,7 @@ /// <returns></returns> public CreatePluginFamilyExpression<PLUGINTYPE> ForSingletonOf<PLUGINTYPE>() { - return ForRequestedType<PLUGINTYPE>().AsSingletons(); + return ForRequestedType<PLUGINTYPE>().Singleton(); } /// <summary> @@ -412,7 +412,7 @@ /// <returns></returns> public CreatePluginFamilyExpression<PLUGINTYPE> For<PLUGINTYPE>() { - return ForRequestedType<PLUGINTYPE>(); + return new CreatePluginFamilyExpression<PLUGINTYPE>(this); } /// <summary> Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/Container.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -23,7 +23,15 @@ var expression = new ConfigurationExpression(); action(expression); - construct(expression.BuildGraph()); + // As explained later in the article, + // PluginGraph is part of the Semantic Model + // of StructureMap + PluginGraph graph = expression.BuildGraph(); + + // Take the PluginGraph object graph and + // dynamically emit classes to build the + // configured objects + construct(graph); } public Container(Registry registry) Added: trunk/Source/StructureMap/Example.cs =================================================================== --- trunk/Source/StructureMap/Example.cs (rev 0) +++ trunk/Source/StructureMap/Example.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -0,0 +1,101 @@ +using System; + +namespace StructureMap +{ + public interface IMessageSender + { + void SendMessage(string text, string sender, string receiver); + } + + public class FluentMessageSender + { + private readonly IMessageSender _messageSender; + + public FluentMessageSender(IMessageSender sender) + { + _messageSender = sender; + } + + public SendExpression SendText(string text) + { + return new SendExpression(text, _messageSender); + } + + public class SendExpression : ToExpression + { + private readonly string _text; + private readonly IMessageSender _messageSender; + private string _sender; + + public SendExpression(string text, IMessageSender messageSender) + { + _text = text; + _messageSender = messageSender; + } + + public ToExpression From(string sender) + { + _sender = sender; + return this; + } + + void ToExpression.To(string receiver) + { + _messageSender.SendMessage(_text, _sender, receiver); + } + } + + public interface ToExpression + { + void To(string receiver); + } + } + + public class SendMessageRequest + { + public string Text { get; set; } + public string Sender { get; set; } + public string Receiver { get; set; } + } + + public class ParameterObjectMessageSender + { + public void Send(SendMessageRequest request) + { + // send the message + } + } + + public class APIConsumer + { + // Snippet from a class that uses IMessageSender + public void SendMessage(IMessageSender sender) + { + // Is this right? + sender.SendMessage("the message body", "PARTNER001", "PARTNER002"); + + // or this? + sender.SendMessage("PARTNER001", "the message body", "PARTNER002"); + + // or this? + sender.SendMessage("PARTNER001", "PARTNER002", "the message body"); + } + + public void SendMessageFluently(FluentMessageSender sender) + { + sender + .SendText("the message body") + .From("PARTNER001").To("PARTNER002"); + } + + public void SendMessageAsParameter(ParameterObjectMessageSender sender) + { + sender.Send(new SendMessageRequest() + { + Text = "the message body", + Receiver = "PARTNER001", + Sender = "PARTNER002" + }); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -116,14 +116,27 @@ /// <returns></returns> public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>() { - string propertyName = findPropertyName<CONSTRUCTORARGUMENTTYPE>(); + Type dependencyType = typeof(CONSTRUCTORARGUMENTTYPE); + return Child(dependencyType); + } + + /// <summary> + /// Start the definition of a child instance for type CONSTRUCTORARGUMENTTYPE + /// </summary> + /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> + /// <returns></returns> + public ChildInstanceExpression Child(Type dependencyType) + { + string propertyName = findPropertyName(dependencyType); + ChildInstanceExpression child = Child(propertyName); - child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); + child.ChildType = dependencyType; return child; } + /// <summary> /// Inline definition of a constructor or a setter property dependency /// </summary> Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -226,12 +226,19 @@ protected string findPropertyName<PLUGINTYPE>() { + Type dependencyType = typeof(PLUGINTYPE); + + return findPropertyName(dependencyType); + } + + protected string findPropertyName(Type dependencyType) + { var plugin = new Plugin(_pluggedType); - string propertyName = plugin.FindArgumentNameForType<T>(); + string propertyName = plugin.FindArgumentNameForType(dependencyType); if (string.IsNullOrEmpty(propertyName)) { - throw new StructureMapException(305, typeof(PLUGINTYPE)); + throw new StructureMapException(305, dependencyType); } return propertyName; Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-03 20:54:50 UTC (rev 275) @@ -390,6 +390,7 @@ <Compile Include="Emitting\BuildUpMethod.cs" /> <Compile Include="Emitting\Parameters\Methods.cs" /> <Compile Include="ErrorMessages.cs" /> + <Compile Include="Example.cs" /> <Compile Include="Extensions.cs" /> <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap/StructureMapException.resx 2009-12-03 20:54:50 UTC (rev 275) @@ -277,4 +277,7 @@ <data name="245" xml:space="preserve"> <value>Error while trying to create an InstanceBuilder for {0}</value> </data> + <data name="25" xml:space="preserve"> + <value>Only a concrete type may be used here</value> + </data> </root> \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeOnOpenGenericsTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeOnOpenGenericsTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeOnOpenGenericsTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -0,0 +1,27 @@ +using NUnit.Framework; +using StructureMap.Attributes; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class SpecifyScopeOnOpenGenericsTester + { + + [Test] + public void should_obey_scope_set_on_open_type() + { + var container = new Container(x => + { + x.For(typeof (IOpenType<>)).CacheBy(InstanceScope.Singleton).Use(typeof (OpenType<>)); + }); + + var o1 = container.GetInstance<IOpenType<string>>(); + var o2 = container.GetInstance<IOpenType<string>>(); + + o1.ShouldBeTheSameAs(o2); + } + } + + public interface IOpenType<T>{} + public class OpenType<T> : IOpenType<T>{} +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -145,7 +145,7 @@ { var registry = new Registry(); CreatePluginFamilyExpression<IGateway> expression = - registry.BuildInstancesOf<IGateway>().AsSingletons(); + registry.BuildInstancesOf<IGateway>().Singleton(); Assert.IsNotNull(expression); PluginGraph pluginGraph = registry.Build(); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -200,7 +200,7 @@ { IContainer container = new Container(r => { - r.ForRequestedType<Processor>().TheDefault.Is.OfConcreteType<Processor>() + r.For<Processor>().Use<Processor>() .WithCtorArg("name").EqualTo("Jeremy") .TheArrayOf<IHandler>().Contains(x => { Modified: trunk/Source/StructureMap.Testing/PerRequestInterceptorTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/PerRequestInterceptorTester.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/PerRequestInterceptorTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -59,17 +59,17 @@ ObjectFactory.Initialize( x => { - x.BuildInstancesOf<Session>() + x.ForRequestedType<Session>() .AlwaysUnique() .TheDefaultIsConcreteType<Session>(); - x.BuildInstancesOf<Model1>() + x.ForRequestedType<Model1>() .TheDefaultIsConcreteType<Model1>(); - x.BuildInstancesOf<Model2>() + x.ForRequestedType<Model2>() .TheDefaultIsConcreteType<Model2>(); - x.BuildInstancesOf<Shell>() + x.ForRequestedType<Shell>() .TheDefaultIsConcreteType<Shell>(); }); Modified: trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -46,7 +46,7 @@ { var container = new Container(x => { - x.ForRequestedType<IService>().AsSingletons().AddInstances(o => + x.ForRequestedType<IService>().Singleton().AddInstances(o => { o.Is.ConstructedBy(() => new ColorService("Red")).WithName("Red"); o.Is.ConstructedBy(() => new ColorService("Green")).WithName("Green"); Modified: trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2009-12-03 20:54:50 UTC (rev 275) @@ -4,6 +4,7 @@ using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Testing.Configuration.DSL; using StructureMap.Testing.GenericWidgets; using StructureMap.Testing.Widget; using StructureMap.Testing.Widget2; @@ -63,7 +64,6 @@ var instance = new ConfiguredInstance(GetType()); - using (mocks.Record()) { Expect.Call(builder.BuildInstance(instance, session)).Return(theObjectBuilt); @@ -277,5 +277,20 @@ instance.Build(GetType(), new StubBuildSession(), builder); }); } + + [Test] + public void use_the_child_function() + { + var theRule = new ARule(); + + var container = new Container(x => + { + x.For(typeof (ClassWithDependency)).Use(typeof (ClassWithDependency)).Child(typeof (Rule)).Is(theRule); + }); + + container.GetInstance<ClassWithDependency>().Rule.ShouldBeTheSameAs(theRule); + } } + + } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-11-13 17:33:27 UTC (rev 274) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-03 20:54:50 UTC (rev 275) @@ -187,6 +187,7 @@ <Compile Include="Bugs\ScanIndexerBugTester.cs" /> <Compile Include="Bugs\SingletonShouldBeLazy.cs" /> <Compile Include="Bugs\SpecifyScopeInConfigureTester.cs" /> + <Compile Include="Bugs\SpecifyScopeOnOpenGenericsTester.cs" /> <Compile Include="Bugs\StaticPropertyCausesJITExceptionTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="BuildUpIntegratedTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-19 02:29:11
|
Revision: 278 http://structuremap.svn.sourceforge.net/structuremap/?rev=278&view=rev Author: jeremydmiller Date: 2009-12-19 02:29:03 +0000 (Sat, 19 Dec 2009) Log Message: ----------- fixing a lil' bitty bug with TryGetInstance and open generic types Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs trunk/Source/StructureMap/Extensions.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginCache.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -90,6 +90,7 @@ return alterAndContinue(family => family.AddInstance(instance)); } + /// <summary> /// Sets the object creation of the instances of the PluginType. For example: PerRequest, /// Singleton, ThreadLocal, HttpContext, or Hybrid Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Container.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -447,7 +447,7 @@ }; // Fixes a mild bug. The child container should inject itself - container._pipelineGraph.Inject<IContainer>(container); + container.Configure(x => x.For<IContainer>().Use(container)); return container; } Modified: trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs =================================================================== --- trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Reflection; using StructureMap.Graph; +using System.Linq; namespace StructureMap.Emitting { @@ -18,10 +19,7 @@ string assemblyName = "Builders" + guidString(); _dynamicAssembly = new DynamicAssembly(assemblyName); - foreach (Plugin plugin in plugins) - { - processPlugin(plugin); - } + plugins.Where(x => x.IsNotOpenGeneric()).Each(x => processPlugin(x)); } private static string guidString() Modified: trunk/Source/StructureMap/Extensions.cs =================================================================== --- trunk/Source/StructureMap/Extensions.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Extensions.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -7,6 +7,16 @@ { internal static class StringExtensions { + public static IEnumerable<T> Each<T>(this IEnumerable<T> enumerable, Action<T> action) + { + foreach (T target in enumerable) + { + action(target); + } + + return enumerable; + } + public static string ToFormat(this string template, params object[] parameters) { return string.Format(template, parameters); Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -3,6 +3,7 @@ using System.Linq.Expressions; using System.Reflection; using StructureMap.Pipeline; +using StructureMap.TypeRules; namespace StructureMap.Graph { @@ -165,5 +166,10 @@ { _setters.UseSetterRule(rule); } + + public bool IsNotOpenGeneric() + { + return !_pluggedType.IsGeneric(); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginCache.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginCache.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Graph/PluginCache.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -4,6 +4,7 @@ using System.Reflection; using StructureMap.Emitting; using StructureMap.Util; +using StructureMap.TypeRules; namespace StructureMap.Graph { Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -202,7 +202,16 @@ { if (instance == null) return; - _children.Add(name, instance); + if (_children.ContainsKey(name)) + { + _children[name] = instance; + } + else + { + _children.Add(name, instance); + } + + } protected void setChildArray(string name, Instance[] array) Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -4,6 +4,7 @@ using StructureMap.Diagnostics; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.TypeRules; namespace StructureMap { @@ -228,10 +229,13 @@ public bool HasDefaultForPluginType(Type pluginType) { - var typeToFind = pluginType.IsGenericType ? pluginType.GetGenericTypeDefinition() : pluginType; - var configuration = PluginTypes.FirstOrDefault(p => p.PluginType == typeToFind); - - return configuration == null ? false : configuration.Default != null; + var factory = ForType(pluginType); + if (_profileManager.GetDefault(pluginType) != null) + { + return true; + } + + return (factory.AllInstances.Count() == 1); } public bool HasInstance(Type pluginType, string instanceKey) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -8,6 +8,17 @@ { public static class TypeExtensions { + public static bool Closes(this Type type, Type openType) + { + var baseType = type.BaseType; + if (baseType == null) return false; + + var closes = baseType.IsGenericType && baseType.GetGenericTypeDefinition() == openType; + if (closes) return true; + + return type.BaseType == null ? false : type.BaseType.Closes(openType); + } + public static bool IsInNamespace(this Type type, string nameSpace) { return type.Namespace.StartsWith(nameSpace); @@ -48,13 +59,20 @@ { if (!pluggedType.IsConcrete()) return null; - foreach (var interfaceType in pluggedType.GetInterfaces()) + if (templateType.IsInterface) { - if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + foreach (var interfaceType in pluggedType.GetInterfaces()) { - return interfaceType; + if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == templateType) + { + return interfaceType; + } } } + else if (pluggedType.BaseType.IsGenericType && pluggedType.BaseType.GetGenericTypeDefinition() == templateType) + { + return pluggedType.BaseType; + } return pluggedType.BaseType == typeof(object) ? null : pluggedType.BaseType.FindInterfaceThatCloses(templateType); } @@ -183,7 +201,7 @@ public static bool IsConcrete(this Type type) { - return !type.IsAbstract; + return !type.IsAbstract && !type.IsInterface; } public static bool IsAutoFillable(this Type type) Added: trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -0,0 +1,57 @@ +using System.Diagnostics; +using NUnit.Framework; +using StructureMap.TypeRules; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class TryGetInstanceWithOpenGenericsBugTester + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void can_get_closing_type_if_starting_from_a_base_type() + { + typeof (ClosedClass<string>).FindInterfaceThatCloses(typeof (IOpenClass<>)).ShouldEqual( + typeof (IOpenClass<string>)); + + + } + + [Test] + public void try_get_instance_fills_from_open_generic() + { + var container = new Container(x => + { + x.For(typeof(IOpenClass<>)).AddType(typeof(ClosedClass<>)); + }); + + container.TryGetInstance<IOpenClass<string>>().ShouldBeOfType<ClosedClass<string>>(); + } + + [Test] + public void try_get_instance_fills_from_open_generic_on_conventions() + { + var container = new Container(x => + { + x.Scan(o => + { + o.TheCallingAssembly(); + o.ConnectImplementationsToTypesClosing(typeof(IOpenClass<>)); + }); + }); + + Debug.WriteLine(container.WhatDoIHave()); + + container.GetInstance<IOpenClass<string>>().ShouldBeOfType<ClosedStringClass>(); + container.TryGetInstance<IOpenClass<string>>().ShouldBeOfType<ClosedStringClass>(); + } + } + + public class IOpenClass<T>{} + public class ClosedClass<T> : IOpenClass<T>{} + public class ClosedStringClass : IOpenClass<string>{} +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs 2009-12-19 02:29:03 UTC (rev 278) @@ -14,7 +14,32 @@ { } + public class ContainerHolder + { + private readonly IContainer _container; + + public ContainerHolder(IContainer container) + { + _container = container; + } + + public IContainer Container { get { return _container; } } + } + [Test] + public void the_nested_container_will_deliver_itself_into_a_constructor_of_something_else() + { + var parent = new Container(x => + { + x.For<IWidget>().Use<AWidget>(); + }); + + var child = parent.GetNestedContainer(); + child.GetInstance<ContainerHolder>().Container.ShouldBeTheSameAs(child); + + } + + [Test] public void the_nested_container_delivers_itself_as_the_IContainer() { var parent = new Container(x => Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-05 18:40:41 UTC (rev 277) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-19 02:29:03 UTC (rev 278) @@ -189,6 +189,7 @@ <Compile Include="Bugs\SpecifyScopeInConfigureTester.cs" /> <Compile Include="Bugs\SpecifyScopeOnOpenGenericsTester.cs" /> <Compile Include="Bugs\StaticPropertyCausesJITExceptionTester.cs" /> + <Compile Include="Bugs\TryGetInstanceWithOpenGenericsBugTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="BuildUpIntegratedTester.cs" /> <Compile Include="BuildUpTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-19 03:05:47
|
Revision: 279 http://structuremap.svn.sourceforge.net/structuremap/?rev=279&view=rev Author: jeremydmiller Date: 2009-12-19 03:05:37 +0000 (Sat, 19 Dec 2009) Log Message: ----------- Fixing an old, old bug with defensive programming being too tight around checking CanCastTo() Modified Paths: -------------- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.Testing/Bugs/InjectByFuncWithNoPublicConstructors.cs Modified: trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -22,10 +22,11 @@ public void Process(Type type, PluginGraph graph) { - if (!type.CanBeCastTo(_pluginType)) return; - - var name = _getName(type); - graph.AddType(_pluginType, type, name); + if (type.CanBeCastTo(_pluginType) && Constructor.HasConstructors(type)) + { + var name = _getName(type); + graph.AddType(_pluginType, type, name); + } } #endregion Modified: trunk/Source/StructureMap/InstanceBuilderList.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilderList.cs 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap/InstanceBuilderList.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -43,7 +43,7 @@ } // Add a missing PluggedType if we can - if (pluggedType.CanBeCastTo(_pluginType)) + if (pluggedType.CanBeCastTo(_pluginType) && Constructor.HasConstructors(pluggedType)) { var plugin = new Plugin(pluggedType); processPlugin(plugin); Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -179,7 +179,7 @@ protected override bool canBePartOfPluginFamily(PluginFamily family) { - return _pluggedType.CanBeCastTo(family.PluginType); + return _pluggedType.CanBeCastTo(family.PluginType) && Constructor.HasConstructors(_pluggedType); } internal override bool Matches(Plugin plugin) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -124,11 +124,6 @@ return false; } - if (noPublicConstructors(pluggedType)) - { - return false; - } - if (IsGeneric(pluginType)) { return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); @@ -164,11 +159,6 @@ return returnValue; } - private static bool noPublicConstructors(Type pluggedType) - { - return pluggedType.GetConstructors().Length == 0; - } - public static bool IsString(this Type type) { return type.Equals(typeof(string)); Added: trunk/Source/StructureMap.Testing/Bugs/InjectByFuncWithNoPublicConstructors.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/InjectByFuncWithNoPublicConstructors.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/InjectByFuncWithNoPublicConstructors.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -0,0 +1,34 @@ +using NUnit.Framework; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class InjectByFuncWithNoPublicConstructors + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void register_with_generic() + { + var container = new Container(x => + { + x.For<ClassThatIsBuiltByStatic>().Use(c => ClassThatIsBuiltByStatic.Build()); + }); + + container.GetInstance<ClassThatIsBuiltByStatic>().ShouldNotBeNull(); + } + } + + public class ClassThatIsBuiltByStatic + { + public static ClassThatIsBuiltByStatic Build() + { + return new ClassThatIsBuiltByStatic(); + } + + private ClassThatIsBuiltByStatic(){} + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap.Testing/Graph/TypeFindingTester.cs 2009-12-19 03:05:37 UTC (rev 279) @@ -29,13 +29,7 @@ private IContainer container; - [Test] - public void DoNotFindPluginWithNoPublicCTOR() - { - Assert.IsFalse(typeof (GreenType).CanBeCastTo(typeof (TypeIWantToFind))); - } - [Test] public void FoundTheRightNumberOfInstancesForATypeWithNoPlugins() { Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-19 02:29:03 UTC (rev 278) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-19 03:05:37 UTC (rev 279) @@ -182,6 +182,7 @@ <Compile Include="Bugs\FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs" /> <Compile Include="Bugs\HttpSessionNullRefBug.cs" /> <Compile Include="Bugs\IDictionaryAndXmlBugTester.cs" /> + <Compile Include="Bugs\InjectByFuncWithNoPublicConstructors.cs" /> <Compile Include="Bugs\LambdaCreatesNullBugTester.cs" /> <Compile Include="Bugs\MixedConfigureAndInitializeMissingInstanceProblem.cs" /> <Compile Include="Bugs\ScanIndexerBugTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-19 03:41:54
|
Revision: 280 http://structuremap.svn.sourceforge.net/structuremap/?rev=280&view=rev Author: jeremydmiller Date: 2009-12-19 03:41:43 +0000 (Sat, 19 Dec 2009) Log Message: ----------- killed off the obsolete configure() method in Registry. Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs trunk/Source/StructureMap.Testing.Widget5/WidgetRegistry.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-19 03:05:37 UTC (rev 279) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-19 03:41:43 UTC (rev 280) @@ -75,19 +75,22 @@ private readonly List<Action<PluginGraph>> _actions = new List<Action<PluginGraph>>(); private readonly List<Action> _basicActions = new List<Action>(); - public Registry() + /// <summary> + /// Imports the configuration from another registry into this registry. + /// </summary> + /// <typeparam name="T"></typeparam> + public void IncludeRegistry<T>() where T : Registry, new() { - configure(); + _actions.Add(g => new T().ConfigurePluginGraph(g)); } /// <summary> - /// You can overide this method as a place to put the Registry DSL - /// declarations. This is not mandatory. + /// Imports the configuration from another registry into this registry. /// </summary> - [Obsolete("configure() is unnecessary. All declarations can be made in the constructor of a Registry or any other method")] - protected virtual void configure() + /// <param name="registry"></param> + public void IncludeRegistry(Registry registry) { - // no-op; + _actions.Add(registry.ConfigurePluginGraph); } protected void registerAction(Action action) Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2009-12-19 03:05:37 UTC (rev 279) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2009-12-19 03:41:43 UTC (rev 280) @@ -88,15 +88,17 @@ { private int _count; + public TestRegistry2() + { + _count++; + } + public int ExecutedCount { get { return _count; } } - protected override void configure() - { - _count++; - } + } public class FakeGateway : IGateway Modified: trunk/Source/StructureMap.Testing.Widget5/WidgetRegistry.cs =================================================================== --- trunk/Source/StructureMap.Testing.Widget5/WidgetRegistry.cs 2009-12-19 03:05:37 UTC (rev 279) +++ trunk/Source/StructureMap.Testing.Widget5/WidgetRegistry.cs 2009-12-19 03:41:43 UTC (rev 280) @@ -5,7 +5,7 @@ { public class RedGreenRegistry : Registry { - protected override void configure() + public RedGreenRegistry() { InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Red").WithName("Red"); InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Green").WithName( @@ -29,14 +29,13 @@ public class YellowBlueRegistry : Registry { - protected override void configure() + public YellowBlueRegistry() { InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Yellow").WithName( "Yellow"); InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithProperty("color").EqualTo("Blue").WithName("Blue"); } - public override bool Equals(object obj) { if (this == obj) return true; @@ -68,7 +67,7 @@ public class BrownBlackRegistry : Registry { - protected override void configure() + public BrownBlackRegistry() { InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithProperty("color").EqualTo("Brown").WithName( "Brown"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-12-21 23:11:33
|
Revision: 282 http://structuremap.svn.sourceforge.net/structuremap/?rev=282&view=rev Author: flimflan Date: 2009-12-21 23:11:23 +0000 (Mon, 21 Dec 2009) Log Message: ----------- Allow specifying a child array by type for a configured instance without using generics. Modified Paths: -------------- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-19 04:03:09 UTC (rev 281) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-21 23:11:23 UTC (rev 282) @@ -96,6 +96,14 @@ return new ChildArrayExpression(this, propertyName); } + public ChildArrayExpression ChildArray(Type pluginType) + { + //TODO: add validation check + validateTypeIsArray(pluginType); + var propertyName = findPropertyName(pluginType); + return ChildArray(propertyName); + } + /// <summary> /// Inline definition of a dependency array like IService[] or IHandler[] /// </summary> @@ -103,10 +111,7 @@ /// <returns></returns> public ChildArrayExpression ChildArray<PLUGINTYPE>() { - validateTypeIsArray<PLUGINTYPE>(); - - string propertyName = findPropertyName<PLUGINTYPE>(); - return ChildArray<PLUGINTYPE>(propertyName); + return ChildArray(typeof (PLUGINTYPE)); } /// <summary> Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-19 04:03:09 UTC (rev 281) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-21 23:11:23 UTC (rev 282) @@ -255,7 +255,12 @@ protected static void validateTypeIsArray<PLUGINTYPE>() { - if (!typeof (PLUGINTYPE).IsArray) + validateTypeIsArray(typeof(PLUGINTYPE)); + } + + protected static void validateTypeIsArray(Type pluginType) + { + if (!pluginType.IsArray) { throw new StructureMapException(307); } Modified: trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2009-12-19 04:03:09 UTC (rev 281) +++ trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2009-12-21 23:11:23 UTC (rev 282) @@ -198,6 +198,40 @@ } [Test] + public void HasProperty_for_generic_child_array_when_property_name_is_inferred() + { + var instance = new ConfiguredInstance(typeof(UsesGateways)); + + IConfiguredInstance configuredInstance = instance; + configuredInstance.HasProperty("gateways").ShouldBeFalse(); + + instance.ChildArray<IGateway[]>().Contains(new DefaultInstance()); + configuredInstance.HasProperty("gateways").ShouldBeTrue(); + } + + [Test] + public void HasProperty_for_child_array_when_property_name_is_inferred() + { + var instance = new ConfiguredInstance(typeof(UsesGateways)); + + IConfiguredInstance configuredInstance = instance; + configuredInstance.HasProperty("gateways").ShouldBeFalse(); + + instance.ChildArray(typeof(IGateway[])).Contains(new DefaultInstance()); + configuredInstance.HasProperty("gateways").ShouldBeTrue(); + } + + public class UsesGateways + { + private readonly IGateway[] _gateways; + + public UsesGateways(IGateway[] gateways) + { + _gateways = gateways; + } + } + + [Test] public void Property_cannot_be_found_so_throw_205() { try This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-23 19:37:00
|
Revision: 283 http://structuremap.svn.sourceforge.net/structuremap/?rev=283&view=rev Author: jeremydmiller Date: 2009-12-23 19:36:53 +0000 (Wed, 23 Dec 2009) Log Message: ----------- added more convenience methods to the generic family expression Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs trunk/Source/StructureMap.Testing/Examples/Interception.cs trunk/Source/StructureMap.Testing/Examples/RegisteringWithTheAPI.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap.Testing/Bugs/AddValueDirectlyWithGenericUsage.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -81,6 +81,7 @@ /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> /// <returns></returns> + [Obsolete("Prefer the usage For<ISomething>().Use<Something>()")] public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIsConcreteType<CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE { Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -67,6 +67,19 @@ return TheDefaultIsConcreteType(concreteType); } + /// <summary> + /// Shortcut to add a value by type + /// </summary> + /// <param name="value"></param> + /// <returns></returns> + public LiteralInstance Use(object value) + { + var instance = new LiteralInstance(value); + Use(instance); + + return instance; + } + /// <summary> /// Shortcut method to add an additional Instance to this Plugin Type Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -103,6 +103,7 @@ /// </summary> /// <typeparam name="PLUGGEDTYPE"></typeparam> /// <returns></returns> + [Obsolete("Favor For<ISomething>().Use<Something>()")] SmartInstance<PLUGGEDTYPE> OfConcreteType<PLUGGEDTYPE>() where PLUGGEDTYPE : T; /// <summary> @@ -112,6 +113,7 @@ /// </summary> /// <param name="type"></param> /// <returns></returns> + [Obsolete("Favor For<ISomething>().Use(typeof(Something))")] ConfiguredInstance OfConcreteType(Type type); /// <summary> @@ -120,6 +122,7 @@ /// </summary> /// <param name="func"></param> /// <returns></returns> + [Obsolete("Prefer For<T>().Use(Func<T> func) or For<T>().Add(Func<T> func)")] ConstructorInstance<T> ConstructedBy(Func<T> func); /// <summary> Modified: trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs =================================================================== --- trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -1,4 +1,4 @@ -using System.Diagnostics; +using System.Diagnostics; using System.Windows.Forms; namespace StructureMap.DebuggerVisualizers.Testing Added: trunk/Source/StructureMap.Testing/Bugs/AddValueDirectlyWithGenericUsage.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/AddValueDirectlyWithGenericUsage.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/AddValueDirectlyWithGenericUsage.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -0,0 +1,23 @@ +using NUnit.Framework; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class AddValueDirectlyWithGenericUsage + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void should_be_able_to_resolve_from_the_generic_family_expression() + { + var widget = new AWidget(); + var container = new Container(x => x.For(typeof (IWidget)).Use(widget).WithName("mine")); + + container.GetInstance<IWidget>("mine").ShouldBeTheSameAs(widget); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap.Testing/Diagnostics/DoctorTester.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.IO; using System.Reflection; Modified: trunk/Source/StructureMap.Testing/Examples/Interception.cs =================================================================== --- trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.ComTypes; Modified: trunk/Source/StructureMap.Testing/Examples/RegisteringWithTheAPI.cs =================================================================== --- trunk/Source/StructureMap.Testing/Examples/RegisteringWithTheAPI.cs 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap.Testing/Examples/RegisteringWithTheAPI.cs 2009-12-23 19:36:53 UTC (rev 283) @@ -1,4 +1,4 @@ -using StructureMap.Configuration.DSL; +using StructureMap.Configuration.DSL; namespace StructureMap.Testing.Examples { Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-21 23:11:23 UTC (rev 282) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-23 19:36:53 UTC (rev 283) @@ -178,6 +178,7 @@ <Compile Include="AutoMocking\RhinoMockRepositoryProxyTester.cs" /> <Compile Include="AutoWiringExamples.cs" /> <Compile Include="BidirectionalDependencies.cs" /> + <Compile Include="Bugs\AddValueDirectlyWithGenericUsage.cs" /> <Compile Include="Bugs\BuildUpBug.cs" /> <Compile Include="Bugs\FillAllPropertiesShouldWorkForAlreadyConfiguredPluginsBug.cs" /> <Compile Include="Bugs\HttpSessionNullRefBug.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2009-12-23 19:55:04
|
Revision: 284 http://structuremap.svn.sourceforge.net/structuremap/?rev=284&view=rev Author: flimflan Date: 2009-12-23 19:54:52 +0000 (Wed, 23 Dec 2009) Log Message: ----------- Get rid of uses of obsolete InstanceOf<T> Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap.AutoMocking/AutoMocker.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-23 19:36:53 UTC (rev 283) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-23 19:54:52 UTC (rev 284) @@ -103,7 +103,12 @@ return alterAndContinue(family => family.AddInstance(instance)); } + public GenericFamilyExpression Add(object instance) + { + return Add(new LiteralInstance(instance)); + } + /// <summary> /// Sets the object creation of the instances of the PluginType. For example: PerRequest, /// Singleton, ThreadLocal, HttpContext, or Hybrid Modified: trunk/Source/StructureMap.AutoMocking/AutoMocker.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/AutoMocker.cs 2009-12-23 19:36:53 UTC (rev 283) +++ trunk/Source/StructureMap.AutoMocking/AutoMocker.cs 2009-12-23 19:54:52 UTC (rev 284) @@ -4,6 +4,7 @@ using System.Reflection; using StructureMap.Graph; using System.Linq; +using StructureMap.Pipeline; namespace StructureMap.AutoMocking { @@ -215,7 +216,7 @@ public T AddAdditionalMockFor<T>() where T : class { var mock = _serviceLocator.Service<T>(); - _container.Configure(r => r.InstanceOf<T>().Is.Object(mock)); + _container.Configure(r => r.For(typeof (T)).Add(mock)); return mock; } @@ -265,7 +266,7 @@ { foreach (T t in stubs) { - x.InstanceOf<T>().Is.Object(t); + x.For(typeof(T)).Add(t); } }); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-24 04:53:31
|
Revision: 285 http://structuremap.svn.sourceforge.net/structuremap/?rev=285&view=rev Author: jeremydmiller Date: 2009-12-24 04:53:19 +0000 (Thu, 24 Dec 2009) Log Message: ----------- Work on the new ConstructorInstance, EnumerableInstance Modified Paths: -------------- trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/CloseGenericTypeExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Emitting/Parameters/Methods.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap/Pipeline/SmartInstance.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.AutoMocking/AutoMockedContainer.cs trunk/Source/StructureMap.Testing/BuildSessionTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyMergeTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/TestExplicitArguments.cs trunk/Source/StructureMap.Testing/Pipeline/MissingInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerMergeTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs trunk/Source/StructureMap.Testing/Pipeline/ReferencedInstanceTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.Widget/Hierarchy.cs Added Paths: ----------- trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs trunk/Source/StructureMap/Pipeline/LambdaInstance.cs trunk/Source/StructureMap/Pipeline/ListCoercion.cs trunk/Source/StructureMap/Pipeline/ObjectInstance.cs trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/EnumerableInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/LambdaInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ObjectInstanceTester.cs Removed Paths: ------------- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/HttpSessionBuildPolicy.cs trunk/Source/StructureMap/Pipeline/Lifecycle.cs trunk/Source/StructureMap/Pipeline/LiteralInstance.cs trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/LiteralInstanceTester.cs Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/BuildSession.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -153,7 +153,7 @@ return result; } - + [Obsolete("Move all of this into the new EnumerableInstance")] public virtual Array CreateInstanceArray(Type pluginType, Instance[] instances) { if (instances == null) Modified: trunk/Source/StructureMap/CloseGenericTypeExpression.cs =================================================================== --- trunk/Source/StructureMap/CloseGenericTypeExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/CloseGenericTypeExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -40,7 +40,7 @@ private void closeType(Type type) { - if (!type.IsGeneric()) + if (!type.IsOpenGeneric()) { throw new StructureMapException(285); } Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -124,7 +124,7 @@ /// </summary> /// <param name="func"></param> /// <returns></returns> - public ConstructorInstance<PLUGINTYPE> Use(Func<IContext, PLUGINTYPE> func) + public LambdaInstance<PLUGINTYPE> Use(Func<IContext, PLUGINTYPE> func) { return TheDefault.Is.ConstructedBy(func); } @@ -134,7 +134,7 @@ /// </summary> /// <param name="object"></param> /// <returns></returns> - public LiteralInstance Use(PLUGINTYPE @object) + public ObjectInstance Use(PLUGINTYPE @object) { return TheDefault.IsThis(@object); } Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -72,9 +72,9 @@ /// </summary> /// <param name="value"></param> /// <returns></returns> - public LiteralInstance Use(object value) + public ObjectInstance Use(object value) { - var instance = new LiteralInstance(value); + var instance = new ObjectInstance(value); Use(instance); return instance; @@ -105,7 +105,7 @@ public GenericFamilyExpression Add(object instance) { - return Add(new LiteralInstance(instance)); + return Add(new ObjectInstance(instance)); } Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -26,7 +26,7 @@ /// </summary> /// <param name="obj"></param> /// <returns></returns> - LiteralInstance IsThis(T obj); + ObjectInstance IsThis(T obj); } public interface ThenItExpression<T> @@ -95,7 +95,7 @@ /// </summary> /// <param name="theObject"></param> /// <returns></returns> - LiteralInstance Object(T theObject); + ObjectInstance Object(T theObject); /// <summary> /// Build the Instance with the constructor function and setter arguments. Starts @@ -123,7 +123,7 @@ /// <param name="func"></param> /// <returns></returns> [Obsolete("Prefer For<T>().Use(Func<T> func) or For<T>().Add(Func<T> func)")] - ConstructorInstance<T> ConstructedBy(Func<T> func); + LambdaInstance<T> ConstructedBy(Func<T> func); /// <summary> /// Create an Instance that builds an object by calling a Lambda or @@ -132,7 +132,7 @@ /// </summary> /// <param name="func"></param> /// <returns></returns> - ConstructorInstance<T> ConstructedBy(Func<IContext, T> func); + LambdaInstance<T> ConstructedBy(Func<IContext, T> func); /// <summary> /// Use the Instance of this PluginType with the specified name. This is @@ -204,9 +204,9 @@ returnInstance(instance); } - public LiteralInstance IsThis(T obj) + public ObjectInstance IsThis(T obj) { - return returnInstance(new LiteralInstance(obj)); + return returnInstance(new ObjectInstance(obj)); } #endregion @@ -232,9 +232,9 @@ return returnInstance(new ConfiguredInstance(type)); } - public LiteralInstance Object(T theObject) + public ObjectInstance Object(T theObject) { - return returnInstance(new LiteralInstance(theObject)); + return returnInstance(new ObjectInstance(theObject)); } public ReferencedInstance TheInstanceNamed(string name) @@ -247,14 +247,14 @@ return returnInstance(new DefaultInstance()); } - public ConstructorInstance<T> ConstructedBy(Func<T> func) + public LambdaInstance<T> ConstructedBy(Func<T> func) { - return returnInstance(new ConstructorInstance<T>(func)); + return returnInstance(new LambdaInstance<T>(func)); } - public ConstructorInstance<T> ConstructedBy(Func<IContext, T> func) + public LambdaInstance<T> ConstructedBy(Func<IContext, T> func) { - return returnInstance(new ConstructorInstance<T>(func)); + return returnInstance(new LambdaInstance<T>(func)); } public PrototypeInstance PrototypeOf(T template) Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -169,7 +169,7 @@ /// <returns></returns> public ProfileExpression Use(Func<T> func) { - var instance = new ConstructorInstance<T>(func); + var instance = new LambdaInstance<T>(func); return Use(instance); } @@ -180,7 +180,7 @@ /// <returns></returns> public ProfileExpression Use(T t) { - var instance = new LiteralInstance(t); + var instance = new ObjectInstance(t); return Use(instance); } Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -395,7 +395,7 @@ /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <returns></returns> - public ConstructorInstance<T> Redirect<T, U>() where T : class where U : class + public LambdaInstance<T> Redirect<T, U>() where T : class where U : class { return For<T>().TheDefault.Is.ConstructedBy(c => { Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Container.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -561,7 +561,7 @@ public OpenGenericTypeExpression(Type templateType, Container container) { - if (!templateType.IsGeneric()) + if (!templateType.IsOpenGeneric()) { throw new StructureMapException(285); } @@ -599,7 +599,7 @@ /// <param name="object"></param> public void Inject<T>(string name, T @object) { - LiteralInstance instance = new LiteralInstance(@object).WithName(name); + ObjectInstance instance = new ObjectInstance(@object).WithName(name); _transientCache.Set(typeof(T), instance, @object); _pipelineGraph.AddInstance<T>(instance); } @@ -630,7 +630,7 @@ } - var instance = new LiteralInstance(@object); + var instance = new ObjectInstance(@object); _transientCache.Set(pluginType, instance, @object); _pipelineGraph.SetDefault(pluginType, instance); } Modified: trunk/Source/StructureMap/Emitting/Parameters/Methods.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/Methods.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Emitting/Parameters/Methods.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -24,7 +24,7 @@ }, null); public static MethodInfo GET_CHILD = - ReflectionHelper.GetMethod<IConfiguredInstance>(i => i.GetChild(null, null, null)); + ReflectionHelper.GetMethod<IConfiguredInstance>(i => i.Get(null, null, null)); public static MethodInfo GET_CHILDREN_ARRAY = ReflectionHelper.GetMethod<IConfiguredInstance>(i => i.GetChildrenArray(null)); Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Linq.Expressions; using System.Reflection; using StructureMap.TypeRules; @@ -81,15 +82,9 @@ public string FindFirstConstructorArgumentOfType(Type type) { - foreach (ParameterInfo info in _ctor.GetParameters()) - { - if (info.ParameterType.Equals(type)) - { - return info.Name; - } - } - - return null; + return _ctor.GetParameters() + .Where(x => x.ParameterType == type) + .Select(x => x.Name).FirstOrDefault(); } @@ -157,5 +152,12 @@ _ctor = ctor; } + + public Type FindArgumentType(string name) + { + return _ctor.GetParameters() + .Where(x => x.Name == name) + .Select(x => x.ParameterType).FirstOrDefault(); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -41,7 +41,7 @@ { _openType = openType; - if (!_openType.IsGeneric()) + if (!_openType.IsOpenGeneric()) { throw new ApplicationException("This scanning convention can only be used with open generic types"); } Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -169,7 +169,12 @@ public bool IsNotOpenGeneric() { - return !_pluggedType.IsGeneric(); + return !_pluggedType.IsOpenGeneric(); } + + public Type FindArgumentType(string argumentName) + { + return _constructor.FindArgumentType(argumentName) ?? _setters.FindArgumentType(argumentName); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Reflection; +using System.Linq; namespace StructureMap.Graph { @@ -129,5 +130,12 @@ { _properties.FindAll(setter => rule(setter.Property)).ForEach(setter => setter.IsMandatory = true); } + + public Type FindArgumentType(string name) + { + return _properties + .Where(x => x.Name == name) + .Select(x => x.Property.PropertyType).FirstOrDefault(); + } } } \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Linq; + +namespace StructureMap.Pipeline +{ + public class ArrayCoercion<T> : IEnumerableCoercion where T : class + { + public object Convert(IEnumerable<object> enumerable) + { + return enumerable.Select(x => x as T).ToArray(); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -322,7 +322,7 @@ public ConfiguredInstance Is(object value) { - var instance = new LiteralInstance(value); + var instance = new ObjectInstance(value); return Is(instance); } Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -87,7 +87,7 @@ return _properties[propertyName]; } - object IConfiguredInstance.GetChild(string propertyName, Type pluginType, BuildSession buildSession) + object IConfiguredInstance.Get(string propertyName, Type pluginType, BuildSession buildSession) { return getChild(propertyName, pluginType, buildSession); } @@ -130,7 +130,7 @@ setProperty(propertyName, propertyValue); } - void IConfiguredInstance.SetChild(string name, Instance instance) + void IConfiguredInstance.Set(string name, Instance instance) { setChild(name, instance); } Deleted: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -1,41 +0,0 @@ -using System; - -namespace StructureMap.Pipeline -{ - public class ConstructorInstance<T> : ExpressedInstance<ConstructorInstance<T>> - { - private readonly Func<IContext, T> _builder; - - public ConstructorInstance(Func<IContext, T> builder) - { - _builder = builder; - } - - public ConstructorInstance(Func<T> func) - { - _builder = s => func(); - } - - protected override ConstructorInstance<T> thisInstance - { - get { return this; } - } - - protected override object build(Type pluginType, BuildSession session) - { - try - { - return _builder(session); - } - catch (Exception ex) - { - throw new StructureMapException(207, ex, Name, pluginType); - } - } - - protected override string getDescription() - { - return "Instance is created by Func<object> function: " + _builder; - } - } -} \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using StructureMap.Graph; +using StructureMap.TypeRules; +using StructureMap.Util; + +namespace StructureMap.Pipeline +{ + public class ConstructorInstance : Instance + { + private readonly Cache<string, Instance> _dependencies = new Cache<string, Instance>(); + private readonly Plugin _plugin; + + public ConstructorInstance(Type pluggedType) + { + _plugin = PluginCache.GetPlugin(pluggedType); + } + + protected override string getDescription() + { + return "Configured Instance of " + _plugin.PluggedType.AssemblyQualifiedName; + } + + public void SetChild(string name, Instance instance) + { + _dependencies[name] = instance; + } + + public void SetValue(string name, object value) + { + var dependencyType = _plugin.FindArgumentType(name); + var instance = buildInstanceForType(dependencyType, value); + _dependencies[name] = instance; + } + + public void SetCollection(string name, IEnumerable<Instance> children) + { + var dependencyType = _plugin.FindArgumentType(name); + var instance = new EnumerableInstance(dependencyType, children); + _dependencies[name] = instance; + } + + private static Instance buildInstanceForType(Type dependencyType, object value) + { + if (value == null) return new NullInstance(); + + + if (dependencyType.IsSimple() || dependencyType.IsNullable() || dependencyType == typeof(Guid) || dependencyType == typeof(DateTime)) + { + var converter = TypeDescriptor.GetConverter(dependencyType); + var convertedValue = converter.ConvertFrom(value); + return new ObjectInstance(convertedValue); + } + + + return new ObjectInstance(value); + } + + public object Get(string propertyName, Type pluginType, BuildSession session) + { + return _dependencies[propertyName].Build(pluginType, session); + } + + protected override object build(Type pluginType, BuildSession session) + { + throw new NotImplementedException(); + } + + public object Build(Type pluginType, BuildSession session, InstanceBuilder builder) + { + throw new NotImplementedException(); + } + + public static ConstructorInstance For<T>() + { + return new ConstructorInstance(typeof(T)); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace StructureMap.Pipeline +{ + public class EnumerableInstance : Instance + { + private readonly IEnumerable<Instance> _children; + private readonly IEnumerableCoercion _coercion; + private readonly string _description; + + public EnumerableInstance(Type propertyType, IEnumerable<Instance> children) + { + _description = propertyType.FullName; + + _children = children; + _coercion = DetermineCoercion(propertyType); + } + + public static IEnumerableCoercion DetermineCoercion(Type propertyType) + { + var coercionType = determineCoercionType(propertyType); + return (IEnumerableCoercion) Activator.CreateInstance(coercionType); + } + + private static readonly List<Type> _enumerableTypes = new List<Type>(){typeof(IEnumerable<>), typeof(IList<>), typeof(List<>)}; + private static Type determineCoercionType(Type propertyType) + { + if (propertyType.IsArray) + { + return typeof (ArrayCoercion<>).MakeGenericType(propertyType.GetElementType()); + } + + if (propertyType.IsGenericType ) + { + var templateType = propertyType.GetGenericTypeDefinition(); + if (_enumerableTypes.Contains(templateType)) + { + return typeof (ListCoercion<>).MakeGenericType(propertyType.GetGenericArguments().First()); + } + } + + throw new ArgumentException("Only IEnumerable<T> types can be passed to this constructor. {0} is invalid".ToFormat(propertyType.AssemblyQualifiedName)); + } + + protected override string getDescription() + { + return _description; + } + + protected override object build(Type pluginType, BuildSession session) + { + var objects = _children.Select(x => x.Build(pluginType, session)); + return _coercion.Convert(objects); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -54,7 +54,7 @@ if (arg.Value == null) continue; instance.SetProperty(arg.Key, arg.Value.ToString()); - instance.SetChild(arg.Key, new LiteralInstance(arg.Value)); + instance.Set(arg.Key, new ObjectInstance(arg.Value)); } } Deleted: trunk/Source/StructureMap/Pipeline/HttpSessionBuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HttpSessionBuildPolicy.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/HttpSessionBuildPolicy.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -1,47 +0,0 @@ -using System; -using System.Collections; -using System.Web; -using StructureMap.Attributes; - -namespace StructureMap.Pipeline -{ - public class HttpSessionBuildPolicy : HttpContextBuildPolicy - { - protected override IDictionary findHttpDictionary() - { - return new SessionWrapper(HttpContext.Current.Session); - } - - public override string ToString() - { - return InstanceScope.HttpSession.ToString(); - } - } - - - public class HttpSessionLifecycle : HttpContextLifecycle - { - protected override IDictionary findHttpDictionary() - { - return new SessionWrapper(HttpContext.Current.Session); - } - } - - public class HybridSessionLifecycle : HttpLifecycleBase<HttpSessionLifecycle, ThreadLocalStorageLifecycle> - { - - } - - public class HybridSessionBuildPolicy : HttpBuildPolicyBase<HttpSessionBuildPolicy, ThreadLocalStoragePolicy> - { - public override IBuildPolicy Clone() - { - return new HybridSessionBuildPolicy() { InnerPolicy = InnerPolicy.Clone() }; - } - - public override string ToString() - { - return InstanceScope.HybridHttpSession.ToString(); - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -6,13 +6,29 @@ { string Name { get; } Type PluggedType { get; } + + + + [Obsolete] Instance[] GetChildrenArray(string propertyName); + + [Obsolete] string GetProperty(string propertyName); - object GetChild(string propertyName, Type pluginType, BuildSession buildSession); + + + object Get(string propertyName, Type pluginType, BuildSession buildSession); + object Build(Type pluginType, BuildSession session, InstanceBuilder builder); + bool HasProperty(string propertyName); + + [Obsolete] void SetProperty(string name, string value); - void SetChild(string name, Instance instance); + + + void Set(string name, Instance instance); + + [Obsolete] void SetChildArray(string name, Type type, Instance[] children); } } \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace StructureMap.Pipeline +{ + public interface IEnumerableCoercion + { + object Convert(IEnumerable<object> enumerable); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -90,7 +90,7 @@ { Instance childInstance = _memento.ReadChildInstance(name, _pluginGraph, childType); - _instance.SetChild(name, childInstance); + _instance.Set(name, childInstance); } private void copyChildArray(string name, Type childType) Copied: trunk/Source/StructureMap/Pipeline/LambdaInstance.cs (from rev 282, trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs) =================================================================== --- trunk/Source/StructureMap/Pipeline/LambdaInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/LambdaInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,41 @@ +using System; + +namespace StructureMap.Pipeline +{ + public class LambdaInstance<T> : ExpressedInstance<LambdaInstance<T>> + { + private readonly Func<IContext, T> _builder; + + public LambdaInstance(Func<IContext, T> builder) + { + _builder = builder; + } + + public LambdaInstance(Func<T> func) + { + _builder = s => func(); + } + + protected override LambdaInstance<T> thisInstance + { + get { return this; } + } + + protected override object build(Type pluginType, BuildSession session) + { + try + { + return _builder(session); + } + catch (Exception ex) + { + throw new StructureMapException(207, ex, Name, pluginType); + } + } + + protected override string getDescription() + { + return "Instance is created by Func<object> function: " + _builder; + } + } +} \ No newline at end of file Deleted: trunk/Source/StructureMap/Pipeline/Lifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Lifecycle.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/Lifecycle.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -1,201 +0,0 @@ -using System; -using System.Collections.Generic; -using StructureMap.Attributes; -using StructureMap.Interceptors; -using StructureMap.Util; - -namespace StructureMap.Pipeline -{ - public interface IObjectCache - { - object Locker { get; } - - int Count - { - get; - } - - object Get(Type pluginType, Instance instance); - void Set(Type pluginType, Instance instance, object value); - void DisposeAndClear(); - } - - public class NulloObjectCache : IObjectCache - { - public object Locker - { - get { return new object(); } - } - - public int Count - { - get { return 0; } - } - - public object Get(Type pluginType, Instance instance) - { - return null; - } - - public void Set(Type pluginType, Instance instance, object value) - { - // no-op - } - - public void DisposeAndClear() - { - // no-op - } - } - - public class MainObjectCache : IObjectCache - { - private readonly IDictionary<InstanceKey, object> _objects = new Dictionary<InstanceKey,object>(); - private readonly object _locker = new object(); - - public object Locker - { - get { return _locker; } - } - - public int Count - { - get { return _objects.Count; } - } - - public object Get(Type pluginType, Instance instance) - { - var key = new InstanceKey(instance, pluginType); - return _objects.ContainsKey(key) ? _objects[key] : null; - } - - public void Set(Type pluginType, Instance instance, object value) - { - var key = new InstanceKey(instance, pluginType); - _objects.Add(key, value); - } - - public void DisposeAndClear() - { - - lock (Locker) - { - foreach (var @object in _objects.Values) - { - IDisposable disposable = @object as IDisposable; - if (disposable != null) - { - try - { - disposable.Dispose(); - } - catch (Exception) { } - } - } - - _objects.Clear(); - } - } - } - - - public interface ILifecycle - { - void EjectAll(); - IObjectCache FindCache(); - } - - public static class Lifecycles - { - public static ILifecycle GetLifecycle(InstanceScope scope) - { - switch (scope) - { - case InstanceScope.PerRequest: - return null; - - case InstanceScope.Singleton: - return new SingletonLifecycle(); - - case InstanceScope.HttpContext: - return new HttpContextLifecycle(); - - case InstanceScope.ThreadLocal: - return new ThreadLocalStorageLifecycle(); - - case InstanceScope.Hybrid: - return new HybridLifecycle(); - - case InstanceScope.HttpSession: - return new HttpSessionLifecycle(); - - case InstanceScope.HybridHttpSession: - return new HybridSessionLifecycle(); - } - - throw new ArgumentOutOfRangeException("scope"); - } - } - - public class ObjectBuilder - { - private readonly PipelineGraph _pipeline; - private readonly InterceptorLibrary _library; - private readonly IObjectCache _defaultCache; - - public ObjectBuilder(PipelineGraph pipeline, InterceptorLibrary library, IObjectCache defaultCache) - { - _pipeline = pipeline; - _library = library; - _defaultCache = defaultCache; - } - - public object Resolve(Type pluginType, Instance instance, BuildSession session) - { - var cache = FindCache(pluginType, instance, session); - lock (cache.Locker) - { - var returnValue = cache.Get(pluginType, instance); - if (returnValue == null) - { - returnValue = ConstructNew(pluginType, instance, session); - - - cache.Set(pluginType, instance, returnValue); - } - - return returnValue; - } - } - - public object ConstructNew(Type pluginType, Instance instance, BuildSession session) - { - object returnValue = instance.Build(pluginType, session); - return ApplyInterception(pluginType, returnValue, session, instance); - } - - public virtual object ApplyInterception(Type pluginType, object actualValue, BuildSession session, Instance instance) - { - if (actualValue == null) return null; - - try - { - return _library.FindInterceptor(actualValue.GetType()).Process(actualValue, session); - } - catch (Exception e) - { - throw new StructureMapException(308, e, instance.Name, actualValue.GetType()); - } - - - } - - public IObjectCache FindCache(Type pluginType, Instance instance, BuildSession session) - { - var lifecycle = _pipeline.ForType(pluginType).Lifecycle; - return lifecycle == null - ? _defaultCache - : lifecycle.FindCache(); - } - } -} \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/ListCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ListCoercion.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ListCoercion.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Linq; + +namespace StructureMap.Pipeline +{ + public class ListCoercion<T> : IEnumerableCoercion where T : class + { + public object Convert(IEnumerable<object> enumerable) + { + return enumerable.Select(x => x as T).ToList(); + } + } +} \ No newline at end of file Deleted: trunk/Source/StructureMap/Pipeline/LiteralInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/LiteralInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -1,58 +0,0 @@ -using System; -using StructureMap.Graph; -using StructureMap.TypeRules; - -namespace StructureMap.Pipeline -{ - public class LiteralInstance : ExpressedInstance<LiteralInstance>, IDisposable - { - private object _object; - - public LiteralInstance(object anObject) - { - if (anObject == null) - { - throw new ArgumentNullException("anObject"); - } - - _object = anObject; - } - - - protected override LiteralInstance thisInstance - { - get { return this; } - } - - public object Object - { - get { return _object; } - } - - protected override object build(Type pluginType, BuildSession session) - { - return _object; - } - - - protected override bool canBePartOfPluginFamily(PluginFamily family) - { - return _object.GetType().CanBeCastTo(family.PluginType); - } - - protected override string getDescription() - { - return "Object: " + _object; - } - - public override string ToString() - { - return string.Format("LiteralInstance: {0}", _object); - } - - public void Dispose() - { - _object = null; - } - } -} \ No newline at end of file Copied: trunk/Source/StructureMap/Pipeline/ObjectInstance.cs (from rev 282, trunk/Source/StructureMap/Pipeline/LiteralInstance.cs) =================================================================== --- trunk/Source/StructureMap/Pipeline/ObjectInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -0,0 +1,71 @@ +using System; +using StructureMap.Graph; +using StructureMap.TypeRules; + +namespace StructureMap.Pipeline +{ + public class NullInstance : Instance + { + protected override string getDescription() + { + return "NULL"; + } + + protected override object build(Type pluginType, BuildSession session) + { + return null; + } + } + + public class ObjectInstance : ExpressedInstance<ObjectInstance>, IDisposable + { + private object _object; + + public ObjectInstance(object anObject) + { + if (anObject == null) + { + throw new ArgumentNullException("anObject"); + } + + _object = anObject; + } + + + protected override ObjectInstance thisInstance + { + get { return this; } + } + + public object Object + { + get { return _object; } + } + + protected override object build(Type pluginType, BuildSession session) + { + return _object; + } + + + protected override bool canBePartOfPluginFamily(PluginFamily family) + { + return _object.GetType().CanBeCastTo(family.PluginType); + } + + protected override string getDescription() + { + return "Object: " + _object; + } + + public override string ToString() + { + return string.Format("LiteralInstance: {0}", _object); + } + + public void Dispose() + { + _object = null; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -28,7 +28,7 @@ _instance.SetProperty(_propertyName, propertyValue.ToString()); else { - _instance.SetChild(_propertyName,new LiteralInstance(propertyValue)); + _instance.Set(_propertyName,new ObjectInstance(propertyValue)); } return (T) _instance; } Modified: trunk/Source/StructureMap/Pipeline/SmartInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -366,7 +366,7 @@ /// <returns></returns> public SmartInstance<T> Is(CHILD value) { - return Is(new LiteralInstance(value)); + return Is(new ObjectInstance(value)); } /// <summary> Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -187,7 +187,7 @@ public Instance Inject<PLUGINTYPE>(PLUGINTYPE instance) { - var literalInstance = new LiteralInstance(instance); + var literalInstance = new ObjectInstance(instance); ForType(typeof (PLUGINTYPE)).AddInstance(literalInstance); SetDefault(typeof (PLUGINTYPE), literalInstance); Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-24 04:53:19 UTC (rev 285) @@ -158,13 +158,13 @@ <Compile Include="PipelineGraph.cs" /> <Compile Include="Pipeline\ConfiguredInstance.cs" /> <Compile Include="Pipeline\ConfiguredInstance.Expressions.cs" /> - <Compile Include="Pipeline\ConstructorInstance.cs" /> + <Compile Include="Pipeline\LambdaInstance.cs" /> <Compile Include="Pipeline\DefaultInstance.cs" /> <Compile Include="Pipeline\IConfiguredInstance.cs" /> <Compile Include="Pipeline\ILocationPolicy.cs" /> <Compile Include="Pipeline\Instance.cs" /> <Compile Include="Pipeline\InstanceMementoPropertyReader.cs" /> - <Compile Include="Pipeline\LiteralInstance.cs" /> + <Compile Include="Pipeline\ObjectInstance.cs" /> <Compile Include="Pipeline\Profile.cs" /> <Compile Include="Pipeline\ProfileManager.cs" /> <Compile Include="Pipeline\PrototypeInstance.cs" /> @@ -401,14 +401,19 @@ <Compile Include="Graph\PluginCache.cs" /> <Compile Include="Graph\SingleImplementationScanner.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="Pipeline\ArrayCoercion.cs" /> <Compile Include="Pipeline\ConditionalInstance.cs" /> + <Compile Include="Pipeline\ConstructorInstance.cs" /> + <Compile Include="Pipeline\EnumerableInstance.cs" /> <Compile Include="Pipeline\HttpContextLifecycle.cs" /> <Compile Include="Pipeline\HttpLifecycleBase.cs" /> <Compile Include="Pipeline\HttpSessionLifecycle.cs" /> <Compile Include="Pipeline\HybridSessionLifecycle.cs" /> + <Compile Include="Pipeline\IEnumerableCoercion.cs" /> <Compile Include="Pipeline\ILifecycle.cs" /> <Compile Include="Pipeline\IObjectCache.cs" /> <Compile Include="Pipeline\Lifecycles.cs" /> + <Compile Include="Pipeline\ListCoercion.cs" /> <Compile Include="Pipeline\MainObjectCache.cs" /> <Compile Include="Pipeline\NulloObjectCache.cs" /> <Compile Include="Pipeline\ObjectBuilder.cs" /> Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -30,11 +30,12 @@ return new ReferencedInstance(key); } - public static bool IsGeneric(this Type type) + public static bool IsOpenGeneric(this Type type) { return type.IsGenericTypeDefinition || type.ContainsGenericParameters; } + public static bool IsConcreteAndAssignableTo(this Type pluggedType, Type pluginType) { return pluggedType.IsConcrete() && pluginType.IsAssignableFrom(pluggedType); @@ -124,12 +125,12 @@ return false; } - if (IsGeneric(pluginType)) + if (IsOpenGeneric(pluginType)) { return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); } - if (IsGeneric(pluggedType)) + if (IsOpenGeneric(pluggedType)) { return false; } Modified: trunk/Source/StructureMap.AutoMocking/AutoMockedContainer.cs =================================================================== --- trunk/Source/StructureMap.AutoMocking/AutoMockedContainer.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.AutoMocking/AutoMockedContainer.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -29,7 +29,7 @@ { object service = _locator.Service(pluginType); - var instance = new LiteralInstance(service); + var instance = new ObjectInstance(service); profileManager.SetDefault(pluginType, instance); } Modified: trunk/Source/StructureMap.Testing/BuildSessionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -57,7 +57,7 @@ var session = new BuildSession(new PluginGraph()); var session2 = new BuildSession(new PluginGraph()); - var instance = new ConstructorInstance<ColorRule>(() => + var instance = new LambdaInstance<ColorRule>(() => { count++; return new ColorRule("Red"); @@ -98,7 +98,7 @@ int count = 0; var session = new BuildSession(new PluginGraph()); - var instance = new ConstructorInstance<ColorRule>(() => + var instance = new LambdaInstance<ColorRule>(() => { count++; return new ColorRule("Red"); @@ -121,7 +121,7 @@ { int count = 0; - var instance = new ConstructorInstance<ColorRule>(() => + var instance = new LambdaInstance<ColorRule>(() => { count++; return new ColorRule("Red"); @@ -221,8 +221,8 @@ PluginGraph graph = new PluginGraph(); PluginFamily family = graph.FindFamily(typeof(IService)); - family.AddInstance(new LiteralInstance(red).WithName("red")); - family.AddInstance(new LiteralInstance(green).WithName("green")); + family.AddInstance(new ObjectInstance(red).WithName("red")); + family.AddInstance(new ObjectInstance(green).WithName("green")); var session = new BuildSession(graph); session.TryGetInstance<IService>("red").ShouldBeTheSameAs(red); @@ -237,8 +237,8 @@ PluginGraph graph = new PluginGraph(); PluginFamily family = graph.FindFamily(typeof(IService)); - family.AddInstance(new LiteralInstance(red).WithName("red")); - family.AddInstance(new LiteralInstance(green).WithName("green")); + family.AddInstance(new ObjectInstance(red).WithName("red")); + family.AddInstance(new ObjectInstance(green).WithName("green")); var session = new BuildSession(graph); session.GetInstance<IService>("red").ShouldBeTheSameAs(red); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -82,7 +82,7 @@ .For<IWidget>().Use(theWidget); PluginGraph graph = registry.Build(); - var instance = (LiteralInstance) graph.ProfileManager.GetDefault(typeof (IWidget), "something"); + var instance = (ObjectInstance) graph.ProfileManager.GetDefault(typeof (IWidget), "something"); Assert.AreSame(theWidget, instance.Object); } @@ -98,7 +98,7 @@ .For<IWidget>().UseNamedInstance(theDefaultName) .For<Rule>().UseNamedInstance("DefaultRule"); - LiteralInstance masterInstance = + ObjectInstance masterInstance = registry.InstanceOf<IWidget>().Is.Object(new AWidget()).WithName(theDefaultName); ProfileManager manager = registry.Build().ProfileManager; Modified: trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -31,10 +31,10 @@ return session.BuildErrors[0]; } - private ConstructorInstance<IWidget> errorInstance() + private LambdaInstance<IWidget> errorInstance() { return - new ConstructorInstance<IWidget>(delegate() { throw new NotSupportedException("You can't make me!"); }); + new LambdaInstance<IWidget>(delegate() { throw new NotSupportedException("You can't make me!"); }); } [Test] @@ -157,7 +157,7 @@ { var session = new ValidationBuildSession(new PluginGraph()); - var instance = new LiteralInstance(new ColorWidget("Red")); + var instance = new ObjectInstance(new ColorWidget("Red")); object widget1 = session.CreateInstance(typeof (IWidget), instance); object widget2 = session.CreateInstance(typeof (IWidget), instance); @@ -177,7 +177,7 @@ [Test] public void Validate_a_single_object_with_both_a_passing_validation_method_and_a_failing_validation_method() { - var instance = new LiteralInstance(new WidgetWithOneValidationFailure()); + var instance = new ObjectInstance(new WidgetWithOneValidationFailure()); ValidationBuildSession session = validatedSession(registry => registry.InstanceOf<IWidget>().IsThis(instance)); Modified: trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -289,8 +289,8 @@ { IInstanceFactory factory = getISomethingFactory(); - factory.AddInstance(new LiteralInstance(_red).WithName("Red")); - factory.AddInstance(new LiteralInstance(_blue).WithName("Blue")); + factory.AddInstance(new ObjectInstance(_red).WithName("Red")); + factory.AddInstance(new ObjectInstance(_blue).WithName("Blue")); factory.FindInstance("Red").ShouldNotBeNull(); @@ -324,12 +324,12 @@ { IInstanceFactory factory = getISomethingFactory(); - factory.AddInstance(new LiteralInstance(_red).WithName("Red")); - var oldBlue = new LiteralInstance(_blue).WithName("Blue"); + factory.AddInstance(new ObjectInstance(_red).WithName("Red")); + var oldBlue = new ObjectInstance(_blue).WithName("Blue"); factory.AddInstance(oldBlue); // Replace Blue - var newBlue = new LiteralInstance(_orange).WithName("Blue"); + var newBlue = new ObjectInstance(_orange).WithName("Blue"); factory.AddInstance(newBlue); factory.FindInstance("Blue").ShouldBeTheSameAs(newBlue); Modified: trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -82,9 +82,9 @@ var factory = new InstanceFactory(typeof (IWidget)); var family = new PluginFamily(typeof (IWidget)); - family.AddInstance(new LiteralInstance(new AWidget()).WithName("New")); - family.AddInstance(new LiteralInstance(new AWidget()).WithName("New2")); - family.AddInstance(new LiteralInstance(new AWidget()).WithName("New3")); + family.AddInstance(new ObjectInstance(new AWidget()).WithName("New")); + family.AddInstance(new ObjectInstance(new AWidget()).WithName("New2")); + family.AddInstance(new ObjectInstance(new AWidget()).WithName("New3")); factory.ImportFrom(family); @@ -130,11 +130,11 @@ public void Merge_from_PluginFamily_will_not_replace_an_existing_instance() { var factory = new InstanceFactory(typeof (IWidget)); - LiteralInstance instance1 = new LiteralInstance(new AWidget()).WithName("New"); + ObjectInstance instance1 = new ObjectInstance(new AWidget()).WithName("New"); factory.AddInstance(instance1); var family = new PluginFamily(typeof (IWidget)); - family.AddInstance(new LiteralInstance(new AWidget()).WithName("New")); + family.AddInstance(new ObjectInstance(new AWidget()).WithName("New")); factory.ImportFrom(family); @@ -191,7 +191,7 @@ [Test] public void the_instances_are_cloned_so_that_new_instances_are_NOT_injected_into_() { - clone.AddInstance(new LiteralInstance(new DefaultGateway())); + clone.AddInstance(new ObjectInstance(new DefaultGateway())); factory.AllInstances.Count().ShouldEqual(2); clone.AllInstances.Count().ShouldEqual(3); Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyMergeTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyMergeTester.cs 2009-12-23 19:54:52 UTC (rev 284) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyMergeTester.cs 2009-12-24 04:53:19 UTC (rev 285) @@ -12,7 +12,7 @@ public void Add_instance_that_does_not_exist_in_destination() { var source = new PluginFamily(typeof (IWidget)); - var sourceInstance = new LiteralInstance(new AWidget()); + var sourceInstance = new ObjectInstance(new AWidget()); source.AddInstance(sourceInstance); var destination = new PluginFamily(typeof (IWidget)); @@ -25,11 +25,11 @@ public void Do_not_override_named_instance() { var source = new PluginFamily(typeof (IWidget)); - LiteralInstance sourceInstance = new LiteralInstance(new AWidget()).WithName("New"); + ObjectInstance sourceInstance = new ObjectInstance(new AWidget()).WithName("New"); source.AddInstance(sourceInstance); var destination = new PluginFamily(typeof (IWidget)); - LiteralInstance destinationInstance = new LiteralInstance(new AWidget()).WithName("New"); + ObjectInstance destinationInstance = new ObjectInstance(new AWidget()).WithName("New"); destination.AddInstance(destinationInstance); destination.ImportFrom(source); Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- tru... [truncated message content] |
From: <jer...@us...> - 2009-12-26 05:33:50
|
Revision: 290 http://structuremap.svn.sourceforge.net/structuremap/?rev=290&view=rev Author: jeremydmiller Date: 2009-12-26 05:33:42 +0000 (Sat, 26 Dec 2009) Log Message: ----------- The big, big, big switch from IL generation to dynamic Func's. Ding dong, the witch is dead. Added Paths: ----------- trunk/Source/StructureMap/Construction/ trunk/Source/StructureMap/Construction/BuilderCompiler.cs trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs trunk/Source/StructureMap/Construction/IArguments.cs trunk/Source/StructureMap/Construction/InstanceBuilder.cs trunk/Source/StructureMap/Construction/SetterBuilder.cs Added: trunk/Source/StructureMap/Construction/BuilderCompiler.cs =================================================================== --- trunk/Source/StructureMap/Construction/BuilderCompiler.cs (rev 0) +++ trunk/Source/StructureMap/Construction/BuilderCompiler.cs 2009-12-26 05:33:42 UTC (rev 290) @@ -0,0 +1,113 @@ +using System; +using System.Linq; +using System.Reflection; +using StructureMap.Graph; + +namespace StructureMap.Construction +{ + public class BuilderCompiler + { + public static InstanceBuilder CreateBuilder(Plugin plugin) + { + return getCompiler(plugin).CreateBuilder(plugin); + } + + public static Func<IArguments, object> CompileCreator(Plugin plugin) + { + FuncCompiler compiler = getCompiler(plugin); + + return compiler.Compile(plugin); + } + + private static FuncCompiler getCompiler(Plugin plugin) + { + var compilerType = typeof (FuncCompiler<>).MakeGenericType(plugin.PluggedType); + return (FuncCompiler)Activator.CreateInstance(compilerType); + } + + public static Action<IArguments, object> CompileBuildUp(Plugin plugin) + { + FuncCompiler compiler = getCompiler(plugin); + + return compiler.BuildUp(plugin); + } + + public interface FuncCompiler + { + Func<IArguments, object> Compile(Plugin plugin); + Action<IArguments, object> BuildUp(Plugin plugin); + + InstanceBuilder CreateBuilder(Plugin plugin); + } + + public class FuncCompiler<T> : FuncCompiler + { + private readonly SetterBuilder<T> _setterBuilder = new SetterBuilder<T>(); + + public InstanceBuilder CreateBuilder(Plugin plugin) + { + var ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); + var setters = this.buildUp(plugin); + + Func<IArguments, object> creator = args => + { + var target = ctor(args); + setters(args, target); + return target; + }; + + Action<IArguments, object> builder = (args, o) => setters(args, (T) o); + + return new InstanceBuilder(plugin.PluggedType, creator, builder); + } + + public Func<IArguments, object> Compile(Plugin plugin) + { + var ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); + var buildUp = this.buildUp(plugin); + + return args => + { + // Call the constructor + var target = ctor(args); + buildUp(args, target); + + return target; + }; + } + + private Action<IArguments, T> buildUp(Plugin plugin) + { + var mandatories = plugin.Setters.Where(x => x.IsMandatory) + .Select(x => _setterBuilder.BuildMandatorySetter((PropertyInfo)x.Property)) + .ToArray(); + + + var optionals = plugin.Setters.Where(x => !x.IsMandatory) + .Select(x => _setterBuilder.BuildOptionalSetter(x.Property)) + .ToArray(); + + return (args, target) => + { + // Call the mandatory setters + for (int i = 0; i < mandatories.Length; i++) + { + mandatories[i](args, target); + } + + // Call the optional setters + for (int i = 0; i < optionals.Length; i++) + { + optionals[i](args, target); + } + }; + } + + public Action<IArguments, object> BuildUp(Plugin plugin) + { + var func = buildUp(plugin); + return (args, raw) => func(args, (T)raw); + } + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs =================================================================== --- trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs 2009-12-26 05:33:42 UTC (rev 290) @@ -0,0 +1,38 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using StructureMap.Graph; + +namespace StructureMap.Construction +{ + public class ConstructorFunctionBuilder<T> + { + public Func<IArguments, T> CreateBuilder() + { + Plugin plugin = PluginCache.GetPlugin(typeof (T)); + return CreateBuilder(plugin); + } + + public Func<IArguments, T> CreateBuilder(Plugin plugin) + { + ConstructorInfo constructor = plugin.GetConstructor(); + + var args = Expression.Parameter(typeof (IArguments), "x"); + + + var arguments = constructor.GetParameters().Select(param => ToParameterValueGetter(args, param.ParameterType, param.Name)); + + var ctorCall = Expression.New(constructor, arguments); + + var lambda = Expression.Lambda(typeof (Func<IArguments, T>), ctorCall, args); + return (Func<IArguments, T>) lambda.Compile(); + } + + public static Expression ToParameterValueGetter(ParameterExpression args, Type type, string argName) + { + MethodInfo method = typeof(IArguments).GetMethod("Get").MakeGenericMethod(type); + return Expression.Call(args, method, Expression.Constant(argName)); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Construction/IArguments.cs =================================================================== --- trunk/Source/StructureMap/Construction/IArguments.cs (rev 0) +++ trunk/Source/StructureMap/Construction/IArguments.cs 2009-12-26 05:33:42 UTC (rev 290) @@ -0,0 +1,8 @@ +namespace StructureMap.Construction +{ + public interface IArguments + { + T Get<T>(string propertyName); + bool Has(string propertyName); + } +} \ No newline at end of file Copied: trunk/Source/StructureMap/Construction/InstanceBuilder.cs (from rev 284, trunk/Source/StructureMap/InstanceBuilder.cs) =================================================================== --- trunk/Source/StructureMap/Construction/InstanceBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Construction/InstanceBuilder.cs 2009-12-26 05:33:42 UTC (rev 290) @@ -0,0 +1,46 @@ +using System; +using StructureMap.Pipeline; + +namespace StructureMap.Construction +{ + public interface IInstanceBuilder + { + Type PluggedType { get; } + object BuildInstance(IArguments args); + void BuildUp(IArguments args, object target); + } + + /// <summary> + /// Base class for creating an object instance from an InstanceMemento. SubClasses are + /// emitted for each concrete Plugin with constructor parameters. + /// </summary> + public class InstanceBuilder : IInstanceBuilder + { + private readonly Type _pluggedType; + private readonly Func<IArguments, object> _constructor; + private readonly Action<IArguments, object> _buildUp; + + public InstanceBuilder(Type pluggedType, Func<IArguments, object> constructor, Action<IArguments, object> buildUp) + { + _pluggedType = pluggedType; + _constructor = constructor; + _buildUp = buildUp; + } + + public Type PluggedType { get + { + return _pluggedType; + } + } + + public virtual object BuildInstance(IArguments args) + { + return _constructor(args); + } + + public virtual void BuildUp(IArguments args, object target) + { + _buildUp(args, target); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Construction/SetterBuilder.cs =================================================================== --- trunk/Source/StructureMap/Construction/SetterBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Construction/SetterBuilder.cs 2009-12-26 05:33:42 UTC (rev 290) @@ -0,0 +1,47 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; + +namespace StructureMap.Construction +{ + public class SetterBuilder<T> + { + public Action<IArguments, T> BuildMandatorySetter(string propertyName) + { + PropertyInfo property = typeof (T).GetProperty(propertyName); + return BuildMandatorySetter(property); + } + + public Action<IArguments, T> BuildMandatorySetter(PropertyInfo property) + { + var args = Expression.Parameter(typeof (IArguments), "args"); + var target = Expression.Parameter(typeof (T), "target"); + + + var getValue = ConstructorFunctionBuilder<T>.ToParameterValueGetter(args, property.PropertyType, property.Name); + var method = property.GetSetMethod(); + + var callSetMethod = Expression.Call(target, method, getValue); + + var lambda = Expression.Lambda(typeof (Action<IArguments, T>), callSetMethod, args, target); + + return (Action<IArguments, T>) lambda.Compile(); + } + + public Action<IArguments, T> BuildOptionalSetter(PropertyInfo property) + { + var name = property.Name; + var func = BuildMandatorySetter(property); + return (args, target) => + { + if (args.Has(name)) func(args, target); + }; + } + + public Action<IArguments, T> BuildOptionalSetter(string propertyName) + { + PropertyInfo property = typeof(T).GetProperty(propertyName); + return BuildOptionalSetter(property); + } + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 05:37:20
|
Revision: 295 http://structuremap.svn.sourceforge.net/structuremap/?rev=295&view=rev Author: jeremydmiller Date: 2009-12-26 05:37:13 +0000 (Sat, 26 Dec 2009) Log Message: ----------- The big, big, big switch from IL generation to dynamic Func's. Ding dong, the witch is dead. Modified Paths: -------------- trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/IContext.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/TypeExtensions.cs Removed Paths: ------------- trunk/Source/StructureMap/InstanceBuilder.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/InstanceFamily.cs Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/BuildSession.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -1,9 +1,11 @@ using System; +using System.Collections; using System.Collections.Generic; using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; using StructureMap.Util; +using System.Linq; namespace StructureMap { @@ -75,7 +77,7 @@ } } - T IContext.GetInstance<T>() + public T GetInstance<T>() { return (T) CreateInstance(typeof (T)); } @@ -120,6 +122,8 @@ return list; } + + #endregion public virtual object CreateInstance(Type pluginType, string name) @@ -172,6 +176,16 @@ return array; } + public IEnumerable<T> GetAllInstances<T>() + { + return forType(typeof (T)).AllInstances.Select(x => GetInstance<T>()); + } + + public IEnumerable<object> GetAllInstances(Type pluginType) + { + return forType(pluginType).AllInstances.Select(x => CreateInstance(pluginType, x)); + } + public virtual object CreateInstance(Type pluginType) { return _defaults[pluginType](); Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/Container.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using StructureMap.Configuration.DSL; +using StructureMap.Construction; using StructureMap.Diagnostics; using StructureMap.Exceptions; using StructureMap.Graph; @@ -241,8 +242,9 @@ IConfiguredInstance instance = _pipelineGraph.GetDefault(pluggedType) as IConfiguredInstance ?? new ConfiguredInstance(pluggedType); - InstanceBuilder builder = PluginCache.FindBuilder(pluggedType); - builder.BuildUp(instance, withNewSession(Plugin.DEFAULT), target); + IInstanceBuilder builder = PluginCache.FindBuilder(pluggedType); + var arguments = new Arguments(instance, withNewSession(Plugin.DEFAULT)); + builder.BuildUp(arguments, target); } /// <summary> @@ -481,11 +483,11 @@ defaultInstance = new ConfiguredInstance(pluginType); } - var basicInstance = defaultInstance as BasicInstance; + var basicInstance = defaultInstance as ConstructorInstance; Instance instance = basicInstance == null ? defaultInstance - : new ExplicitInstance(pluginType, args, basicInstance); + : basicInstance.Override(args); BuildSession session = withNewSession(requestedName); @@ -516,11 +518,10 @@ _pipelineGraph = new PipelineGraph(pluginGraph); - PluginCache.Compile(); - _pipelineGraph.Inject<IContainer>(this); } + [Obsolete("delegate to something cleaner in BuildSession")] private IList<T> getListOfTypeWithSession<T>(BuildSession session) { var list = new List<T>(); Modified: trunk/Source/StructureMap/IContext.cs =================================================================== --- trunk/Source/StructureMap/IContext.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/IContext.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -67,10 +67,18 @@ /// <summary> /// Gets all objects in the current object graph that can be cast - /// to T + /// to T that have already been created /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> IEnumerable<T> All<T>() where T : class; + + + /// <summary> + /// Creates/Resolves every configured instance of PlutinType T + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + IEnumerable<T> GetAllInstances<T>(); } } \ No newline at end of file Deleted: trunk/Source/StructureMap/InstanceBuilder.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilder.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/InstanceBuilder.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -1,25 +0,0 @@ -using System; -using StructureMap.Pipeline; - -namespace StructureMap -{ -#pragma warning disable 169 - /// <summary> - /// Base class for creating an object instance from an InstanceMemento. SubClasses are - /// emitted for each concrete Plugin with constructor parameters. - /// </summary> - public abstract class InstanceBuilder - { - private Container _manager; - - // DO NOT ELIMINATE THIS METHOD - public InstanceBuilder(){} - - public abstract Type PluggedType { get; } - - public abstract object BuildInstance(IConfiguredInstance instance, BuildSession session); - - public virtual void BuildUp(IConfiguredInstance instance, BuildSession session, object target) { } - } -#pragma warning restore 169 -} \ No newline at end of file Deleted: trunk/Source/StructureMap/InstanceBuilderList.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilderList.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/InstanceBuilderList.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using StructureMap.Emitting; -using StructureMap.Graph; -using StructureMap.TypeRules; - -namespace StructureMap -{ - public class InstanceBuilderList - { - private readonly Dictionary<string, Type> _aliases = new Dictionary<string, Type>(); - private readonly Dictionary<Type, InstanceBuilder> _builders = new Dictionary<Type, InstanceBuilder>(); - private readonly Type _pluginType; - - - public InstanceBuilderList(Type pluginType, IEnumerable<Plugin> plugins) - { - _pluginType = pluginType; - processPlugins(plugins); - } - - public InstanceBuilderList(Type pluginType) - { - _pluginType = pluginType; - } - - public int BuilderCount - { - get { return _builders.Count; } - } - - - public InstanceBuilder FindByType(Type pluggedType) - { - if (pluggedType == null) - { - return null; - } - - if (_builders.ContainsKey(pluggedType)) - { - return _builders[pluggedType]; - } - - // Add a missing PluggedType if we can - if (pluggedType.CanBeCastTo(_pluginType) && Constructor.HasConstructors(pluggedType)) - { - var plugin = new Plugin(pluggedType); - processPlugin(plugin); - - return _builders[pluggedType]; - } - - return null; - } - - public InstanceBuilder FindByConcreteKey(string concreteKey) - { - if (_aliases.ContainsKey(concreteKey)) - { - Type pluggedType = _aliases[concreteKey]; - return FindByType(pluggedType); - } - - return null; - } - - private void processPlugin(Plugin plugin) - { - processPlugins(new[] {plugin}); - } - - private void processPlugins(IEnumerable<Plugin> plugins) - { - foreach (Plugin plugin in plugins) - { - if (_aliases.ContainsKey(plugin.ConcreteKey)) - { - continue; - } - - _aliases.Add(plugin.ConcreteKey, plugin.PluggedType); - } - - List<InstanceBuilder> list = createInstanceBuilders(plugins); - foreach (InstanceBuilder builder in list) - { - _builders.Add(builder.PluggedType, builder); - } - } - - private List<InstanceBuilder> createInstanceBuilders(IEnumerable<Plugin> plugins) - { - var list = new List<Plugin>(); - foreach (Plugin plugin in plugins) - { - if (!_builders.ContainsKey(plugin.PluggedType)) - { - list.Add(plugin); - } - } - - var builderAssembly = new InstanceBuilderAssembly(list); - return builderAssembly.Compile(); - } - - public void Add(Plugin plugin) - { - Add(new[] {plugin}); - } - - public void Add(IEnumerable<Plugin> plugins) - { - processPlugins(plugins); - } - } -} \ No newline at end of file Deleted: trunk/Source/StructureMap/InstanceFamily.cs =================================================================== --- trunk/Source/StructureMap/InstanceFamily.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/InstanceFamily.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -1,6 +0,0 @@ -namespace StructureMap -{ - public class InstanceFamily - { - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/InstanceMemento.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -193,7 +193,6 @@ return getPropertyValue(XmlConstants.PLUGGED_TYPE); } - // TODO -- this is where we can read other types public Instance ReadInstance(PluginGraph pluginGraph, Type pluginType) { try Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -132,6 +132,7 @@ return _factories[pluginType]; } + [Obsolete("Replace this with a Cache")] private void createFactoryIfMissing(Type pluginType) { if (!_factories.ContainsKey(pluginType)) Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 05:37:13 UTC (rev 295) @@ -144,16 +144,13 @@ <Compile Include="Diagnostics\TextLine.cs" /> <Compile Include="Diagnostics\TextReportWriter.cs" /> <Compile Include="Diagnostics\WhatDoIHaveWriter.cs" /> - <Compile Include="Emitting\ArgumentEmitter.cs" /> <Compile Include="Exceptions\StructureMapConfigurationException.cs" /> <Compile Include="ExplicitArgsExpression.cs" /> <Compile Include="Graph\Constructor.cs" /> <Compile Include="Graph\IArgumentVisitor.cs" /> <Compile Include="Graph\IPluginFamily.cs" /> <Compile Include="Graph\AssemblyScanner.cs" /> - <Compile Include="InstanceBuilderList.cs" /> <Compile Include="InstanceCache.cs" /> - <Compile Include="InstanceFamily.cs" /> <Compile Include="Interceptors\MatchedTypeInterceptor.cs" /> <Compile Include="PipelineGraph.cs" /> <Compile Include="Pipeline\ConfiguredInstance.cs" /> @@ -217,39 +214,6 @@ <Compile Include="Configuration\XmlConstants.cs"> <SubType>Code</SubType> </Compile> - <Compile Include="Emitting\BuildInstanceMethod.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\ClassBuilder.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\DynamicAssembly.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\InstanceBuilderAssembly.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Method.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\ChildArrayParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\ChildParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\EnumParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\ParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\PrimitiveParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> - <Compile Include="Emitting\Parameters\StringParameterEmitter.cs"> - <SubType>Code</SubType> - </Compile> <Compile Include="Exceptions\InstancePropertyValueException.cs"> <SubType>Code</SubType> </Compile> @@ -285,9 +249,6 @@ <SubType>Code</SubType> </Compile> <Compile Include="IContainer.cs" /> - <Compile Include="InstanceBuilder.cs"> - <SubType>Code</SubType> - </Compile> <Compile Include="InstanceFactory.cs"> <SubType>Code</SubType> </Compile> @@ -382,13 +343,16 @@ <Compile Include="Configuration\PrimitiveArrayReader.cs" /> <Compile Include="Configuration\TypeReaderFactory.cs" /> <Compile Include="Configuration\XmlExtensions.cs" /> + <Compile Include="Construction\BuilderCompiler.cs" /> + <Compile Include="Construction\ConstructorFunctionBuilder.cs" /> + <Compile Include="Construction\IArguments.cs" /> + <Compile Include="Construction\InstanceBuilder.cs" /> + <Compile Include="Construction\SetterBuilder.cs" /> <Compile Include="Diagnostics\Doctor.cs" /> <Compile Include="Diagnostics\DoctorReport.cs" /> <Compile Include="Diagnostics\DoctorResult.cs" /> <Compile Include="Diagnostics\DoctorRunner.cs" /> <Compile Include="Diagnostics\ValidationError.cs" /> - <Compile Include="Emitting\BuildUpMethod.cs" /> - <Compile Include="Emitting\Parameters\Methods.cs" /> <Compile Include="ErrorMessages.cs" /> <Compile Include="Example.cs" /> <Compile Include="Extensions.cs" /> @@ -401,6 +365,7 @@ <Compile Include="Graph\PluginCache.cs" /> <Compile Include="Graph\SingleImplementationScanner.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="Pipeline\Arguments.cs" /> <Compile Include="Pipeline\ArrayCoercion.cs" /> <Compile Include="Pipeline\ConditionalInstance.cs" /> <Compile Include="Pipeline\ConstructorInstance.cs" /> Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 05:36:53 UTC (rev 294) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 05:37:13 UTC (rev 295) @@ -1,13 +1,41 @@ using System; +using System.Collections.Generic; using StructureMap.Graph; using StructureMap.Pipeline; namespace StructureMap { + public static class BasicExtensions + { + public static void TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, + Action<TValue> action) + { + TValue value; + if (dictionary.TryGetValue(key, out value)) + { + action(value); + } + } + + public static T As<T>(this object target) where T : class + { + return target as T; + } + + public static bool IsIn<T>(this T target, IList<T> list) + { + return list.Contains(target); + } + } + namespace TypeRules { public static class TypeExtensions { + + + + public static bool Closes(this Type type, Type openType) { var baseType = type.BaseType; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 17:37:37
|
Revision: 297 http://structuremap.svn.sourceforge.net/structuremap/?rev=297&view=rev Author: jeremydmiller Date: 2009-12-26 17:37:30 +0000 (Sat, 26 Dec 2009) Log Message: ----------- Redo of the assembly scanning. Mild optimization Modified Paths: -------------- trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/StructureMap.csproj Added Paths: ----------- trunk/Source/StructureMap/Graph/CompositeFilter.cs trunk/Source/StructureMap/Graph/CompositePredicate.cs trunk/Source/StructureMap/Graph/IAssemblyScanner.cs Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 05:45:56 UTC (rev 296) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -5,194 +5,44 @@ using System.Linq; using System.Reflection; using StructureMap.TypeRules; +using StructureMap.Util; namespace StructureMap.Graph { - public interface IAssemblyScanner + public class TypePool { - #region Designating Assemblies + private readonly Cache<Assembly, Type[]> _types = new Cache<Assembly, Type[]>(); - /// <summary> - /// Add an Assembly to the scanning operation - /// </summary> - /// <param name="assembly"></param> - void Assembly(Assembly assembly); + public TypePool(PluginGraph graph) + { + _types.OnMissing = assembly => + { + try + { + return assembly.GetExportedTypes(); + } + catch (Exception ex) + { + graph.Log.RegisterError(170, ex, assembly.FullName); + return new Type[0]; + } + }; + } - /// <summary> - /// Add an Assembly by name to the scanning operation - /// </summary> - /// <param name="assemblyName"></param> - void Assembly(string assemblyName); - - /// <summary> - /// Add the currently executing Assembly to the scanning operation - /// </summary> - void TheCallingAssembly(); - - /// <summary> - /// Add the Assembly that contains type T to the scanning operation - /// </summary> - /// <typeparam name="T"></typeparam> - void AssemblyContainingType<T>(); - - /// <summary> - /// Add the Assembly that contains type to the scanning operation - /// </summary> - /// <param name="type"></param> - void AssemblyContainingType(Type type); - - /// <summary> - /// Sweep the designated path and add any Assembly's found in this folder to the - /// scanning operation - /// </summary> - /// <param name="path"></param> - void AssembliesFromPath(string path); - - /// <summary> - /// Sweep the designated path and add any Assembly's found in this folder to the - /// scanning operation. The assemblyFilter can be used to filter or limit the - /// Assembly's that are picked up. - /// </summary> - /// <param name="path"></param> - /// <param name="assemblyFilter"></param> - void AssembliesFromPath(string path, Predicate<Assembly> assemblyFilter); - - /// <summary> - /// Sweep the application base directory of current app domain and add any Assembly's - /// found to the scanning operation. - /// </summary> - void AssembliesFromApplicationBaseDirectory(); - - /// <summary> - /// Sweep the application base directory of current app domain and add any Assembly's - /// found to the scanning operation. The assemblyFilter can be used to filter or limit the - /// Assembly's that are picked up. - /// </summary> - void AssembliesFromApplicationBaseDirectory(Predicate<Assembly> assemblyFilter); - - #endregion - - #region Adding TypeScanners - - /// <summary> - /// Adds an ITypeScanner object to the scanning operation - /// </summary> - /// <param name="scanner"></param> - void With(ITypeScanner scanner); - - void With(IHeavyweightTypeScanner heavyweightScanner); - - /// <summary> - /// Adds the DefaultConventionScanner to the scanning operations. I.e., a concrete - /// class named "Something" that implements "ISomething" will be automatically - /// added to PluginType "ISomething" - /// </summary> - void WithDefaultConventions(); - - /// <summary> - /// Creates and adds a new ITypeScanner of type T to this scanning operation - /// </summary> - /// <typeparam name="T"></typeparam> - void With<T>() where T : ITypeScanner, new(); - - #endregion - - #region Other options - - /// <summary> - /// Directs the scanning operation to automatically detect and include any Registry - /// classes found in the Assembly's being scanned - /// </summary> - void LookForRegistries(); - - /// <summary> - /// Add all concrete types of the Plugin Type as Instances of Plugin Type - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - FindAllTypesFilter AddAllTypesOf<PLUGINTYPE>(); - - /// <summary> - /// Add all concrete types of the Plugin Type as Instances of Plugin Type - /// </summary> - /// <param name="pluginType"></param> - FindAllTypesFilter AddAllTypesOf(Type pluginType); - - /// <summary> - /// Makes this scanning operation ignore all [PluginFamily] and [Pluggable] attributes - /// </summary> - void IgnoreStructureMapAttributes(); - - #endregion - - #region Filtering types - - /// <summary> - /// Exclude types that match the Predicate from being scanned - /// </summary> - /// <param name="exclude"></param> - void Exclude(Predicate<Type> exclude); - - /// <summary> - /// Exclude all types in this nameSpace or its children from the scanning operation - /// </summary> - /// <param name="nameSpace"></param> - void ExcludeNamespace(string nameSpace); - - /// <summary> - /// Exclude all types in this nameSpace or its children from the scanning operation - /// </summary> - /// <typeparam name="T"></typeparam> - void ExcludeNamespaceContainingType<T>(); - - /// <summary> - /// Only include types matching the Predicate in the scanning operation. You can - /// use multiple Include() calls in a single scanning operation - /// </summary> - /// <param name="predicate"></param> - void Include(Predicate<Type> predicate); - - /// <summary> - /// Only include types from this nameSpace or its children in the scanning operation. You can - /// use multiple Include() calls in a single scanning operation - /// </summary> - /// <param name="nameSpace"></param> - void IncludeNamespace(string nameSpace); - - /// <summary> - /// Only include types from this nameSpace or its children in the scanning operation. You can - /// use multiple Include() calls in a single scanning operation - /// </summary> - /// <typeparam name="T"></typeparam> - void IncludeNamespaceContainingType<T>(); - - /// <summary> - /// Exclude this specific type from the scanning operation - /// </summary> - /// <typeparam name="T"></typeparam> - void ExcludeType<T>(); - - // ... Other methods - - #endregion - - // ... Other methods - - /// <summary> - /// Scans for PluginType's and Concrete Types that close the given open generic type - /// </summary> - /// <example> - /// - /// </example> - /// <param name="openGenericType"></param> - void ConnectImplementationsToTypesClosing(Type openGenericType); + public IEnumerable<Type> For(IEnumerable<Assembly> assemblies, CompositeFilter<Type> filter) + { + return assemblies.SelectMany(x => _types[x].Where(filter.Matches)); + } } + public class AssemblyScanner : IAssemblyScanner { private readonly List<Assembly> _assemblies = new List<Assembly>(); - private readonly List<Predicate<Type>> _excludes = new List<Predicate<Type>>(); + private readonly CompositeFilter<Type> _filter = new CompositeFilter<Type>(); + private readonly List<IHeavyweightTypeScanner> _heavyweightScanners = new List<IHeavyweightTypeScanner>(); - private readonly List<Predicate<Type>> _includes = new List<Predicate<Type>>(); + private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); public AssemblyScanner() @@ -292,9 +142,9 @@ } - public void Exclude(Predicate<Type> exclude) + public void Exclude(Func<Type, bool> exclude) { - _excludes.Add(exclude); + _filter.Excludes += exclude; } public void ExcludeNamespace(string nameSpace) @@ -307,9 +157,9 @@ ExcludeNamespace(typeof (T).Namespace); } - public void Include(Predicate<Type> predicate) + public void Include(Func<Type, bool> predicate) { - _includes.Add(predicate); + _filter.Includes += predicate; } public void IncludeNamespace(string nameSpace) @@ -381,31 +231,17 @@ internal void ScanForAll(PluginGraph pluginGraph) { - TypeMapBuilder heavyweightScan = configureHeavyweightScan(); + //TypeMapBuilder heavyweightScan = configureHeavyweightScan(); - _assemblies.ForEach(assem => scanTypesInAssembly(assem, pluginGraph)); - - performHeavyweightScan(pluginGraph, heavyweightScan); - } - - private void scanTypesInAssembly(Assembly assembly, PluginGraph graph) - { - try + pluginGraph.Types.For(_assemblies, _filter).Each(type => { - foreach (Type type in assembly.GetExportedTypes()) - { - if (!isInTheIncludes(type)) continue; - if (isInTheExcludes(type)) continue; + _scanners.Each(x => x.Process(type, pluginGraph)); + }); - _scanners.ForEach(scanner => scanner.Process(type, graph)); - } - } - catch (Exception ex) - { - graph.Log.RegisterError(170, ex, assembly.FullName); - } + //performHeavyweightScan(pluginGraph, heavyweightScan); } + private TypeMapBuilder configureHeavyweightScan() { var typeMapBuilder = new TypeMapBuilder(); @@ -416,6 +252,7 @@ return typeMapBuilder; } + [Obsolete] private void performHeavyweightScan(PluginGraph pluginGraph, TypeMapBuilder typeMapBuilder) { IEnumerable<TypeMap> typeMaps = typeMapBuilder.GetTypeMaps(); @@ -423,31 +260,6 @@ typeMapBuilder.Dispose(); } - private bool isInTheExcludes(Type type) - { - if (_excludes.Count == 0) return false; - - foreach (var exclude in _excludes) - { - if (exclude(type)) return true; - } - - return false; - } - - private bool isInTheIncludes(Type type) - { - if (_includes.Count == 0) return true; - - - foreach (var include in _includes) - { - if (include(type)) return true; - } - - return false; - } - public bool Contains(string assemblyName) { foreach (Assembly assembly in _assemblies) Added: trunk/Source/StructureMap/Graph/CompositeFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/CompositeFilter.cs (rev 0) +++ trunk/Source/StructureMap/Graph/CompositeFilter.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -0,0 +1,16 @@ +namespace StructureMap.Graph +{ + public class CompositeFilter<T> + { + private readonly CompositePredicate<T> _excludes = new CompositePredicate<T>(); + private readonly CompositePredicate<T> _includes = new CompositePredicate<T>(); + + public CompositePredicate<T> Includes { get { return _includes; } set { } } + public CompositePredicate<T> Excludes { get { return _excludes; } set { } } + + public bool Matches(T target) + { + return Includes.MatchesAny(target) && Excludes.DoesNotMatcheAny(target); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Graph/CompositePredicate.cs =================================================================== --- trunk/Source/StructureMap/Graph/CompositePredicate.cs (rev 0) +++ trunk/Source/StructureMap/Graph/CompositePredicate.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace StructureMap.Graph +{ + public class CompositePredicate<T> + { + private readonly List<Func<T, bool>> _list = new List<Func<T, bool>>(); + private Func<T, bool> _matchesAll = x => true; + private Func<T, bool> _matchesAny = x => true; + private Func<T, bool> _matchesNone = x => false; + + public void Add(Func<T, bool> filter) + { + _matchesAll = x => _list.All(predicate => predicate(x)); + _matchesAny = x => _list.Any(predicate => predicate(x)); + _matchesNone = x => !MatchesAny(x); + + _list.Add(filter); + } + + public static CompositePredicate<T> operator +(CompositePredicate<T> invokes, Func<T, bool> filter) + { + invokes.Add(filter); + return invokes; + } + + public bool MatchesAll(T target) + { + return _matchesAll(target); + } + + public bool MatchesAny(T target) + { + return _matchesAny(target); + } + + public bool MatchesNone(T target) + { + return _matchesNone(target); + } + + public bool DoesNotMatcheAny(T target) + { + return _list.Count == 0 ? true : !MatchesAny(target); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Graph/IAssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IAssemblyScanner.cs (rev 0) +++ trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -0,0 +1,184 @@ +using System; +using System.Reflection; + +namespace StructureMap.Graph +{ + public interface IAssemblyScanner + { + #region Designating Assemblies + + /// <summary> + /// Add an Assembly to the scanning operation + /// </summary> + /// <param name="assembly"></param> + void Assembly(Assembly assembly); + + /// <summary> + /// Add an Assembly by name to the scanning operation + /// </summary> + /// <param name="assemblyName"></param> + void Assembly(string assemblyName); + + /// <summary> + /// Add the currently executing Assembly to the scanning operation + /// </summary> + void TheCallingAssembly(); + + /// <summary> + /// Add the Assembly that contains type T to the scanning operation + /// </summary> + /// <typeparam name="T"></typeparam> + void AssemblyContainingType<T>(); + + /// <summary> + /// Add the Assembly that contains type to the scanning operation + /// </summary> + /// <param name="type"></param> + void AssemblyContainingType(Type type); + + /// <summary> + /// Sweep the designated path and add any Assembly's found in this folder to the + /// scanning operation + /// </summary> + /// <param name="path"></param> + void AssembliesFromPath(string path); + + /// <summary> + /// Sweep the designated path and add any Assembly's found in this folder to the + /// scanning operation. The assemblyFilter can be used to filter or limit the + /// Assembly's that are picked up. + /// </summary> + /// <param name="path"></param> + /// <param name="assemblyFilter"></param> + void AssembliesFromPath(string path, Predicate<Assembly> assemblyFilter); + + /// <summary> + /// Sweep the application base directory of current app domain and add any Assembly's + /// found to the scanning operation. + /// </summary> + void AssembliesFromApplicationBaseDirectory(); + + /// <summary> + /// Sweep the application base directory of current app domain and add any Assembly's + /// found to the scanning operation. The assemblyFilter can be used to filter or limit the + /// Assembly's that are picked up. + /// </summary> + void AssembliesFromApplicationBaseDirectory(Predicate<Assembly> assemblyFilter); + + #endregion + + #region Adding TypeScanners + + /// <summary> + /// Adds an ITypeScanner object to the scanning operation + /// </summary> + /// <param name="scanner"></param> + void With(ITypeScanner scanner); + + void With(IHeavyweightTypeScanner heavyweightScanner); + + /// <summary> + /// Adds the DefaultConventionScanner to the scanning operations. I.e., a concrete + /// class named "Something" that implements "ISomething" will be automatically + /// added to PluginType "ISomething" + /// </summary> + void WithDefaultConventions(); + + /// <summary> + /// Creates and adds a new ITypeScanner of type T to this scanning operation + /// </summary> + /// <typeparam name="T"></typeparam> + void With<T>() where T : ITypeScanner, new(); + + #endregion + + #region Other options + + /// <summary> + /// Directs the scanning operation to automatically detect and include any Registry + /// classes found in the Assembly's being scanned + /// </summary> + void LookForRegistries(); + + /// <summary> + /// Add all concrete types of the Plugin Type as Instances of Plugin Type + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + FindAllTypesFilter AddAllTypesOf<PLUGINTYPE>(); + + /// <summary> + /// Add all concrete types of the Plugin Type as Instances of Plugin Type + /// </summary> + /// <param name="pluginType"></param> + FindAllTypesFilter AddAllTypesOf(Type pluginType); + + /// <summary> + /// Makes this scanning operation ignore all [PluginFamily] and [Pluggable] attributes + /// </summary> + void IgnoreStructureMapAttributes(); + + #endregion + + #region Filtering types + + /// <summary> + /// Exclude types that match the Predicate from being scanned + /// </summary> + /// <param name="exclude"></param> + void Exclude(Func<Type, bool> exclude); + + /// <summary> + /// Exclude all types in this nameSpace or its children from the scanning operation + /// </summary> + /// <param name="nameSpace"></param> + void ExcludeNamespace(string nameSpace); + + /// <summary> + /// Exclude all types in this nameSpace or its children from the scanning operation + /// </summary> + /// <typeparam name="T"></typeparam> + void ExcludeNamespaceContainingType<T>(); + + /// <summary> + /// Only include types matching the Predicate in the scanning operation. You can + /// use multiple Include() calls in a single scanning operation + /// </summary> + /// <param name="predicate"></param> + void Include(Func<Type, bool> predicate); + + /// <summary> + /// Only include types from this nameSpace or its children in the scanning operation. You can + /// use multiple Include() calls in a single scanning operation + /// </summary> + /// <param name="nameSpace"></param> + void IncludeNamespace(string nameSpace); + + /// <summary> + /// Only include types from this nameSpace or its children in the scanning operation. You can + /// use multiple Include() calls in a single scanning operation + /// </summary> + /// <typeparam name="T"></typeparam> + void IncludeNamespaceContainingType<T>(); + + /// <summary> + /// Exclude this specific type from the scanning operation + /// </summary> + /// <typeparam name="T"></typeparam> + void ExcludeType<T>(); + + // ... Other methods + + #endregion + + // ... Other methods + + /// <summary> + /// Scans for PluginType's and Concrete Types that close the given open generic type + /// </summary> + /// <example> + /// + /// </example> + /// <param name="openGenericType"></param> + void ConnectImplementationsToTypesClosing(Type openGenericType); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-12-26 05:45:56 UTC (rev 296) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -7,6 +7,7 @@ namespace StructureMap.Graph { + [Obsolete] public interface IHeavyweightTypeScanner { void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps); Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 05:45:56 UTC (rev 296) +++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 17:37:30 UTC (rev 297) @@ -33,6 +33,33 @@ void AddType(Type pluggedType); } + + public class WeakReference<T> + { + private readonly Func<T> _builder; + private readonly WeakReference _reference; + + public WeakReference(Func<T> builder) + { + _builder = builder; + _reference = new WeakReference(_builder()); + } + + public T Value + { + get + { + if (!_reference.IsAlive) + { + _reference.Target = _builder(); + } + + return (T)_reference.Target; + } + } + } + + /// <summary> /// Models the runtime configuration of a StructureMap Container /// </summary> @@ -47,17 +74,21 @@ private readonly List<AssemblyScanner> _scanners = new List<AssemblyScanner>(); private GraphLog _log = new GraphLog(); private bool _sealed; + private readonly WeakReference<TypePool> _types; public PluginGraph() { _pluginFamilies = new PluginFamilyCollection(this); + _types = new WeakReference<TypePool>(() => new TypePool(this)); } - public PluginGraph(AssemblyScanner assemblies) + public TypePool Types { - _pluginFamilies = new PluginFamilyCollection(this); - _scanners.Add(assemblies); + get + { + return _types.Value; + } } public List<Registry> Registries { get { return _registries; } } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 05:45:56 UTC (rev 296) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 17:37:30 UTC (rev 297) @@ -356,9 +356,12 @@ <Compile Include="ErrorMessages.cs" /> <Compile Include="Example.cs" /> <Compile Include="Extensions.cs" /> + <Compile Include="Graph\CompositeFilter.cs" /> + <Compile Include="Graph\CompositePredicate.cs" /> <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> + <Compile Include="Graph\IAssemblyScanner.cs" /> <Compile Include="Graph\IHeavyweightTypeScanner.cs" /> <Compile Include="Graph\ITypeScanner.cs" /> <Compile Include="Graph\PluggableAttributeScanner.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 18:18:54
|
Revision: 298 http://structuremap.svn.sourceforge.net/structuremap/?rev=298&view=rev Author: jeremydmiller Date: 2009-12-26 18:18:44 +0000 (Sat, 26 Dec 2009) Log Message: ----------- Overhaul of the "single implementation scanning" feature Modified Paths: -------------- trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/IAssemblyScanner.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs Added Paths: ----------- trunk/Source/StructureMap/Graph/ImplementationMap.cs Removed Paths: ------------- trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -41,9 +41,8 @@ private readonly List<Assembly> _assemblies = new List<Assembly>(); private readonly CompositeFilter<Type> _filter = new CompositeFilter<Type>(); - private readonly List<IHeavyweightTypeScanner> _heavyweightScanners = new List<IHeavyweightTypeScanner>(); - private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); + private readonly List<Action<PluginGraph>> _postScanningActions = new List<Action<PluginGraph>>(); public AssemblyScanner() { @@ -74,13 +73,6 @@ _scanners.Add(scanner); } - public void With(IHeavyweightTypeScanner heavyweightScanner) - { - if (_heavyweightScanners.Contains(heavyweightScanner)) return; - - _heavyweightScanners.Add(heavyweightScanner); - } - public void WithDefaultConventions() { With<DefaultConventionScanner>(); @@ -182,6 +174,15 @@ With(new GenericConnectionScanner(openGenericType)); } + + private readonly ImplementationMap _implementationMap = new ImplementationMap(); + + public void SingleImplementationsOfInterface() + { + _scanners.Fill(_implementationMap); + _postScanningActions.Add(graph => _implementationMap.RegisterSingleImplementations(graph)); + } + public void AssembliesFromApplicationBaseDirectory() { AssembliesFromApplicationBaseDirectory(a => true); @@ -231,35 +232,15 @@ internal void ScanForAll(PluginGraph pluginGraph) { - //TypeMapBuilder heavyweightScan = configureHeavyweightScan(); - pluginGraph.Types.For(_assemblies, _filter).Each(type => { _scanners.Each(x => x.Process(type, pluginGraph)); }); - //performHeavyweightScan(pluginGraph, heavyweightScan); + _postScanningActions.Each(x => x(pluginGraph)); } - private TypeMapBuilder configureHeavyweightScan() - { - var typeMapBuilder = new TypeMapBuilder(); - if (_heavyweightScanners.Count > 0) - { - With(typeMapBuilder); - } - return typeMapBuilder; - } - - [Obsolete] - private void performHeavyweightScan(PluginGraph pluginGraph, TypeMapBuilder typeMapBuilder) - { - IEnumerable<TypeMap> typeMaps = typeMapBuilder.GetTypeMaps(); - _heavyweightScanners.ForEach(scanner => scanner.Process(pluginGraph, typeMaps)); - typeMapBuilder.Dispose(); - } - public bool Contains(string assemblyName) { foreach (Assembly assembly in _assemblies) Modified: trunk/Source/StructureMap/Graph/IAssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -75,8 +75,6 @@ /// <param name="scanner"></param> void With(ITypeScanner scanner); - void With(IHeavyweightTypeScanner heavyweightScanner); - /// <summary> /// Adds the DefaultConventionScanner to the scanning operations. I.e., a concrete /// class named "Something" that implements "ISomething" will be automatically @@ -180,5 +178,12 @@ /// </example> /// <param name="openGenericType"></param> void ConnectImplementationsToTypesClosing(Type openGenericType); + + /// <summary> + /// Directs the scanning to automatically register any type that is the single + /// implementation of an interface against that interface. + /// The filters apply + /// </summary> + void SingleImplementationsOfInterface(); } } \ No newline at end of file Added: trunk/Source/StructureMap/Graph/ImplementationMap.cs =================================================================== --- trunk/Source/StructureMap/Graph/ImplementationMap.cs (rev 0) +++ trunk/Source/StructureMap/Graph/ImplementationMap.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StructureMap.TypeRules; +using StructureMap.Util; + +namespace StructureMap.Graph +{ + public class ImplementationMap : ITypeScanner + { + private readonly Cache<Type, List<Type>> _types = new Cache<Type, List<Type>>(t => new List<Type>()); + + public void Register(Type interfaceType, Type concreteType) + { + _types[interfaceType].Add(concreteType); + } + + public void RegisterType(Type type) + { + if (!type.CanBeCreated()) return; + + type.GetInterfaces().Where(i => i.IsVisible).Each(i => Register(i, type)); + } + + public void Process(Type type, PluginGraph graph) + { + RegisterType(type); + } + + public void RegisterSingleImplementations(PluginGraph graph) + { + _types.Each((pluginType, types) => + { + if (types.Count == 1) + { + graph.AddType(pluginType, types[0]); + } + }); + } + } +} \ No newline at end of file Deleted: trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap/Graph/SingleImplementationScanner.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -1,16 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace StructureMap.Graph -{ - public class SingleImplementationScanner : IHeavyweightTypeScanner - { - public void Process(PluginGraph graph, IEnumerable<TypeMap> typeMaps) - { - foreach (TypeMap map in typeMaps.Where(map => map.ConcreteTypes.Count == 1)) - { - graph.AddType(map.PluginType, map.ConcreteTypes[0]); - } - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 18:18:44 UTC (rev 298) @@ -362,11 +362,10 @@ <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> <Compile Include="Graph\IAssemblyScanner.cs" /> - <Compile Include="Graph\IHeavyweightTypeScanner.cs" /> + <Compile Include="Graph\ImplementationMap.cs" /> <Compile Include="Graph\ITypeScanner.cs" /> <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> - <Compile Include="Graph\SingleImplementationScanner.cs" /> <Compile Include="IContext.cs" /> <Compile Include="Pipeline\Arguments.cs" /> <Compile Include="Pipeline\ArrayCoercion.cs" /> Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -7,6 +7,13 @@ { public static class BasicExtensions { + public static void Fill<T>(this IList<T> list, T value) + { + if (list.Contains(value)) return; + list.Add(value); + } + + public static void TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Action<TValue> action) { @@ -139,6 +146,19 @@ return type.FullName; } + public static bool CanBeCreated(this Type type) + { + return type.IsConcrete() && Constructor.HasConstructors(type); + } + + public static IEnumerable<Type> AllInterfaces(this Type type) + { + foreach (var @interface in type.GetInterfaces()) + { + yield return @interface; + } + } + /// <summary> /// Determines if the pluggedType can be upcast to the pluginType /// </summary> Modified: trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -13,7 +13,7 @@ typeof (Type).IsValueType.ShouldBeFalse(); } - [Test, Ignore("Come back to this")] + [Test] public void registers_plugins_that_only_have_a_single_implementation() { var container = new Container(registry => @@ -22,7 +22,7 @@ { x.TheCallingAssembly(); x.IncludeNamespaceContainingType<SingleImplementationScannerTester>(); - x.With(new SingleImplementationScanner()); + x.SingleImplementationsOfInterface(); }); }); Modified: trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-12-26 17:37:30 UTC (rev 297) +++ trunk/Source/StructureMap.Testing/TypeExtensionsTester.cs 2009-12-26 18:18:44 UTC (rev 298) @@ -102,5 +102,22 @@ Assert.IsTrue(typeof (string).IsString()); Assert.IsFalse(typeof (int).IsString()); } + + [Test] + public void get_all_interfaces() + { + typeof(C3).AllInterfaces().ShouldHaveTheSameElementsAs(typeof(I1), typeof(I2), typeof(I3)); + typeof(C2).AllInterfaces().ShouldHaveTheSameElementsAs(typeof(I1), typeof(I2)); + typeof(C1).AllInterfaces().ShouldHaveTheSameElementsAs(typeof(I1)); + } } + + + public interface I1{} + public interface I2{} + public interface I3{} + + public class C1 : I1{} + public class C2 : C1, I2{} + public class C3 : C2, I3{} } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 18:39:48
|
Revision: 300 http://structuremap.svn.sourceforge.net/structuremap/?rev=300&view=rev Author: jeremydmiller Date: 2009-12-26 18:39:40 +0000 (Sat, 26 Dec 2009) Log Message: ----------- code cleanup Modified Paths: -------------- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs trunk/Source/StructureMap/Configuration/DictionaryReader.cs trunk/Source/StructureMap/Configuration/FamilyParser.cs trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs trunk/Source/StructureMap/Configuration/XmlExtensions.cs trunk/Source/StructureMap/Construction/BuilderCompiler.cs trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs trunk/Source/StructureMap/Construction/SetterBuilder.cs trunk/Source/StructureMap/Diagnostics/BuildError.cs trunk/Source/StructureMap/Diagnostics/CharacterWidth.cs trunk/Source/StructureMap/Diagnostics/DoctorRunner.cs trunk/Source/StructureMap/Diagnostics/Error.cs trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs trunk/Source/StructureMap/Diagnostics/ValidationError.cs trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs trunk/Source/StructureMap/Example.cs trunk/Source/StructureMap/Exceptions/InstancePropertyValueException.cs trunk/Source/StructureMap/Exceptions/MissingPluginFamilyException.cs trunk/Source/StructureMap/Exceptions/StructureMapConfigurationException.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/ImplementationMap.cs trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/Interceptors/CompoundInterceptor.cs trunk/Source/StructureMap/Interceptors/Interceptors.cs trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs trunk/Source/StructureMap/Pipeline/InstanceKey.cs trunk/Source/StructureMap/Pipeline/Lifecycles.cs trunk/Source/StructureMap/Pipeline/MainObjectCache.cs trunk/Source/StructureMap/Pipeline/SessionWrapper.cs trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs trunk/Source/StructureMap/Pipeline/UserControlInstance.cs trunk/Source/StructureMap/Source/BasicXmlMementoSource.cs trunk/Source/StructureMap/Source/MemoryMementoSource.cs trunk/Source/StructureMap/SystemRegistry.cs trunk/Source/StructureMap/TypeExtensions.cs Modified: trunk/Source/StructureMap/Configuration/ConfigurationParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -62,16 +62,12 @@ } - public string FilePath - { - get { return _filePath; } - set { _filePath = value; } - } + public string FilePath { get { return _filePath; } set { _filePath = value; } } public void ForEachFile(GraphLog log, Action<string> action) { string includePath = getIncludePath(); - + // Find the text in every child node of _structureMapNode and // perform an action with that text _structureMapNode.ForTextInChild("Include/@File").Do(fileName => Modified: trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/ConfigurationParserBuilder.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -17,6 +17,11 @@ public class ConfigurationParserBuilder : IConfigurationParserBuilder { + /// <summary> + /// The name of the default configuration file. The value is always <c>StructurMap.config</c> + /// </summary> + public static readonly string DefaultConfigurationFilename = "StructureMap.config"; + private readonly GraphLog _log; private readonly List<string> _otherFiles = new List<string>(); private readonly List<ConfigurationParser> _parsers = new List<ConfigurationParser>(); @@ -32,24 +37,12 @@ #region IConfigurationParserBuilder Members - public bool UseAndEnforceExistenceOfDefaultFile - { - get { return _useAndEnforceExistenceOfDefaultFile; } - set { _useAndEnforceExistenceOfDefaultFile = value; } - } + public bool UseAndEnforceExistenceOfDefaultFile { get { return _useAndEnforceExistenceOfDefaultFile; } set { _useAndEnforceExistenceOfDefaultFile = value; } } - public bool IgnoreDefaultFile - { - get { return _ignoreDefaultFile; } - set { _ignoreDefaultFile = value; } - } + public bool IgnoreDefaultFile { get { return _ignoreDefaultFile; } set { _ignoreDefaultFile = value; } } - public bool PullConfigurationFromAppConfig - { - get { return _pullConfigurationFromAppConfig; } - set { _pullConfigurationFromAppConfig = value; } - } + public bool PullConfigurationFromAppConfig { get { return _pullConfigurationFromAppConfig; } set { _pullConfigurationFromAppConfig = value; } } public void IncludeFile(string filename) { @@ -91,7 +84,7 @@ ConfigurationParser childParser = ConfigurationParser.FromFile(filename); list.Add(childParser); }) - .AndReportErrorAs(150, filename)); + .AndReportErrorAs(150, filename)); } } @@ -114,7 +107,7 @@ { foreach (string filename in _otherFiles) { - var absolutePath = locateFileAsAbsolutePath(filename); + string absolutePath = locateFileAsAbsolutePath(filename); _log.Try(() => { ConfigurationParser parser = ConfigurationParser.FromFile(absolutePath); @@ -128,7 +121,7 @@ { if (_ignoreDefaultFile) return; // Pick up the configuration in the default StructureMap.config - var pathToStructureMapConfig = GetStructureMapConfigurationPath(); + string pathToStructureMapConfig = GetStructureMapConfigurationPath(); if ((_useAndEnforceExistenceOfDefaultFile || File.Exists(pathToStructureMapConfig))) { _log.Try(() => @@ -150,11 +143,6 @@ } /// <summary> - /// The name of the default configuration file. The value is always <c>StructurMap.config</c> - /// </summary> - public static readonly string DefaultConfigurationFilename = "StructureMap.config"; - - /// <summary> /// Returns the absolute path to the StructureMap.config file /// </summary> /// <returns></returns> @@ -166,8 +154,8 @@ private static string locateFileAsAbsolutePath(string filename) { if (Path.IsPathRooted(filename)) return filename; - var basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; - var configPath = Path.Combine(basePath, filename); + string basePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; + string configPath = Path.Combine(basePath, filename); if (!File.Exists(configPath)) { Modified: trunk/Source/StructureMap/Configuration/DictionaryReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DictionaryReader.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/DictionaryReader.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -63,10 +63,7 @@ _dictionary.Add(key, theValue); } - public object Object - { - get { return _dictionary; } - } + public object Object { get { return _dictionary; } } #endregion } @@ -96,10 +93,7 @@ _collection.Add(name, value); } - public object Object - { - get { return _collection; } - } + public object Object { get { return _collection; } } #endregion } Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -121,7 +121,8 @@ private void attachInterceptors(PluginFamily family, XmlElement familyElement) { - string contextBase = string.Format("creating an InstanceInterceptor for {0}\n", family.PluginType.AssemblyQualifiedName); + string contextBase = string.Format("creating an InstanceInterceptor for {0}\n", + family.PluginType.AssemblyQualifiedName); familyElement.ForEachChild("*/Interceptor").Do(element => { var interceptorMemento = new XmlAttributeInstanceMemento(element); Modified: trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,6 +1,5 @@ using System; using System.Xml; -using StructureMap.Graph; using StructureMap.Pipeline; using StructureMap.TypeRules; Modified: trunk/Source/StructureMap/Configuration/XmlExtensions.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlExtensions.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Configuration/XmlExtensions.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -16,26 +16,6 @@ return new XmlTextExpression(node, xpath); } - public class XmlTextExpression - { - private readonly XmlNodeList _list; - - internal XmlTextExpression(XmlNode parent, string attributePath) - { - _list = parent.SelectNodes(attributePath); - } - - public void Do(Action<string> action) - { - if (_list == null) return; - - foreach (XmlNode node in _list) - { - action(node.InnerText); - } - } - } - public static XmlNodeExpression ForEachChild(this XmlNode node, string xpath) { return new XmlNodeExpression(node, xpath); @@ -113,8 +93,26 @@ #region Nested type: XmlTextExpression + public class XmlTextExpression + { + private readonly XmlNodeList _list; + internal XmlTextExpression(XmlNode parent, string attributePath) + { + _list = parent.SelectNodes(attributePath); + } + public void Do(Action<string> action) + { + if (_list == null) return; + + foreach (XmlNode node in _list) + { + action(node.InnerText); + } + } + } + #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Construction/BuilderCompiler.cs =================================================================== --- trunk/Source/StructureMap/Construction/BuilderCompiler.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Construction/BuilderCompiler.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -21,8 +21,8 @@ private static FuncCompiler getCompiler(Plugin plugin) { - var compilerType = typeof (FuncCompiler<>).MakeGenericType(plugin.PluggedType); - return (FuncCompiler)Activator.CreateInstance(compilerType); + Type compilerType = typeof (FuncCompiler<>).MakeGenericType(plugin.PluggedType); + return (FuncCompiler) Activator.CreateInstance(compilerType); } public static Action<IArguments, object> CompileBuildUp(Plugin plugin) @@ -32,6 +32,8 @@ return compiler.BuildUp(plugin); } + #region Nested type: FuncCompiler + public interface FuncCompiler { Func<IArguments, object> Compile(Plugin plugin); @@ -46,12 +48,12 @@ public InstanceBuilder CreateBuilder(Plugin plugin) { - var ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); - var setters = this.buildUp(plugin); + Func<IArguments, T> ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); + Action<IArguments, T> setters = buildUp(plugin); Func<IArguments, object> creator = args => { - var target = ctor(args); + T target = ctor(args); setters(args, target); return target; }; @@ -63,27 +65,33 @@ public Func<IArguments, object> Compile(Plugin plugin) { - var ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); - var buildUp = this.buildUp(plugin); + Func<IArguments, T> ctor = new ConstructorFunctionBuilder<T>().CreateBuilder(plugin); + Action<IArguments, T> buildUp = this.buildUp(plugin); return args => { // Call the constructor - var target = ctor(args); + T target = ctor(args); buildUp(args, target); return target; }; } + public Action<IArguments, object> BuildUp(Plugin plugin) + { + Action<IArguments, T> func = buildUp(plugin); + return (args, raw) => func(args, (T) raw); + } + private Action<IArguments, T> buildUp(Plugin plugin) { - var mandatories = plugin.Setters.Where(x => x.IsMandatory) - .Select(x => _setterBuilder.BuildMandatorySetter((PropertyInfo)x.Property)) + Action<IArguments, T>[] mandatories = plugin.Setters.Where(x => x.IsMandatory) + .Select(x => _setterBuilder.BuildMandatorySetter((PropertyInfo) x.Property)) .ToArray(); - var optionals = plugin.Setters.Where(x => !x.IsMandatory) + Action<IArguments, T>[] optionals = plugin.Setters.Where(x => !x.IsMandatory) .Select(x => _setterBuilder.BuildOptionalSetter(x.Property)) .ToArray(); @@ -102,12 +110,8 @@ } }; } + } - public Action<IArguments, object> BuildUp(Plugin plugin) - { - var func = buildUp(plugin); - return (args, raw) => func(args, (T)raw); - } - } + #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs =================================================================== --- trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Construction/ConstructorFunctionBuilder.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -18,20 +19,22 @@ { ConstructorInfo constructor = plugin.GetConstructor(); - var args = Expression.Parameter(typeof (IArguments), "x"); + ParameterExpression args = Expression.Parameter(typeof (IArguments), "x"); - - var arguments = constructor.GetParameters().Select(param => ToParameterValueGetter(args, param.ParameterType, param.Name)); - var ctorCall = Expression.New(constructor, arguments); + IEnumerable<Expression> arguments = + constructor.GetParameters().Select( + param => ToParameterValueGetter(args, param.ParameterType, param.Name)); - var lambda = Expression.Lambda(typeof (Func<IArguments, T>), ctorCall, args); + NewExpression ctorCall = Expression.New(constructor, arguments); + + LambdaExpression lambda = Expression.Lambda(typeof (Func<IArguments, T>), ctorCall, args); return (Func<IArguments, T>) lambda.Compile(); } public static Expression ToParameterValueGetter(ParameterExpression args, Type type, string argName) { - MethodInfo method = typeof(IArguments).GetMethod("Get").MakeGenericMethod(type); + MethodInfo method = typeof (IArguments).GetMethod("Get").MakeGenericMethod(type); return Expression.Call(args, method, Expression.Constant(argName)); } } Modified: trunk/Source/StructureMap/Construction/SetterBuilder.cs =================================================================== --- trunk/Source/StructureMap/Construction/SetterBuilder.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Construction/SetterBuilder.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -14,33 +14,31 @@ public Action<IArguments, T> BuildMandatorySetter(PropertyInfo property) { - var args = Expression.Parameter(typeof (IArguments), "args"); - var target = Expression.Parameter(typeof (T), "target"); + ParameterExpression args = Expression.Parameter(typeof (IArguments), "args"); + ParameterExpression target = Expression.Parameter(typeof (T), "target"); - var getValue = ConstructorFunctionBuilder<T>.ToParameterValueGetter(args, property.PropertyType, property.Name); - var method = property.GetSetMethod(); + Expression getValue = ConstructorFunctionBuilder<T>.ToParameterValueGetter(args, property.PropertyType, + property.Name); + MethodInfo method = property.GetSetMethod(); - var callSetMethod = Expression.Call(target, method, getValue); + MethodCallExpression callSetMethod = Expression.Call(target, method, getValue); - var lambda = Expression.Lambda(typeof (Action<IArguments, T>), callSetMethod, args, target); + LambdaExpression lambda = Expression.Lambda(typeof (Action<IArguments, T>), callSetMethod, args, target); return (Action<IArguments, T>) lambda.Compile(); } public Action<IArguments, T> BuildOptionalSetter(PropertyInfo property) { - var name = property.Name; - var func = BuildMandatorySetter(property); - return (args, target) => - { - if (args.Has(name)) func(args, target); - }; + string name = property.Name; + Action<IArguments, T> func = BuildMandatorySetter(property); + return (args, target) => { if (args.Has(name)) func(args, target); }; } public Action<IArguments, T> BuildOptionalSetter(string propertyName) { - PropertyInfo property = typeof(T).GetProperty(propertyName); + PropertyInfo property = typeof (T).GetProperty(propertyName); return BuildOptionalSetter(property); } } Modified: trunk/Source/StructureMap/Diagnostics/BuildError.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/BuildError.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/BuildError.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -52,20 +52,11 @@ _pluginType = pluginType; } - public List<BuildDependency> Dependencies - { - get { return _dependencies; } - } + public List<BuildDependency> Dependencies { get { return _dependencies; } } - public Instance Instance - { - get { return _instance; } - } + public Instance Instance { get { return _instance; } } - public Type PluginType - { - get { return _pluginType; } - } + public Type PluginType { get { return _pluginType; } } public StructureMapException Exception { get; set; } Modified: trunk/Source/StructureMap/Diagnostics/CharacterWidth.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/CharacterWidth.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/CharacterWidth.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -4,10 +4,7 @@ { private int _width; - internal int Width - { - get { return _width; } - } + internal int Width { get { return _width; } } internal static CharacterWidth[] For(int count) { Modified: trunk/Source/StructureMap/Diagnostics/DoctorRunner.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/DoctorRunner.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/DoctorRunner.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -13,7 +13,10 @@ public DoctorReport RunReport(string bootstrapperTypeName) { - var report = new DoctorReport {Result = DoctorResult.Success}; + var report = new DoctorReport + { + Result = DoctorResult.Success + }; IBootstrapper bootstrapper; Modified: trunk/Source/StructureMap/Diagnostics/Error.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/Error.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/Error.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -24,7 +24,8 @@ _message = ErrorMessages.GetMessage(errorCode, args); } - public Error(int errorCode, Exception ex, params object[] args) : this(errorCode, args) + public Error(int errorCode, Exception ex, params object[] args) + : this(errorCode, args) { _message += "\n\n" + ex.Message; @@ -41,10 +42,7 @@ } - public int Code - { - get { return _code; } - } + public int Code { get { return _code; } } #region IEquatable<Error> Members Modified: trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -21,25 +21,17 @@ { } - public ValidationBuildSession(PluginGraph graph) : base(graph) + public ValidationBuildSession(PluginGraph graph) + : base(graph) { } - public bool Success - { - get { return _errors.BuildErrors.Length == 0 && _validationErrors.Count == 0; } - } + public bool Success { get { return _errors.BuildErrors.Length == 0 && _validationErrors.Count == 0; } } - public BuildError[] BuildErrors - { - get { return _errors.BuildErrors; } - } + public BuildError[] BuildErrors { get { return _errors.BuildErrors; } } - public ValidationError[] ValidationErrors - { - get { return _validationErrors.ToArray(); } - } + public ValidationError[] ValidationErrors { get { return _validationErrors.ToArray(); } } public override object CreateInstance(Type pluginType, Instance instance) { @@ -133,7 +125,6 @@ writer.WriteLine(); - writer.WriteLine("StructureMap Failures: {0} Build/Configuration Failures and {1} Validation Errors", _errors.BuildErrors.Length, _validationErrors.Count); Modified: trunk/Source/StructureMap/Diagnostics/ValidationError.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/ValidationError.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/ValidationError.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,7 +1,6 @@ using System; using System.IO; using System.Reflection; -using StructureMap.Graph; using StructureMap.Pipeline; namespace StructureMap.Diagnostics Modified: trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -67,11 +67,11 @@ { _writer.AddDivider('-'); var contents = new[] - { - "{0} ({1})".ToFormat(pluginType.PluginType.GetName(), pluginType.PluginType.GetFullName()), - string.Empty, - string.Empty - }; + { + "{0} ({1})".ToFormat(pluginType.PluginType.GetName(), pluginType.PluginType.GetFullName()), + string.Empty, + string.Empty + }; if (pluginType.Default != null) { Modified: trunk/Source/StructureMap/Example.cs =================================================================== --- trunk/Source/StructureMap/Example.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Example.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,5 +1,3 @@ -using System; - namespace StructureMap { public interface IMessageSender @@ -21,10 +19,12 @@ return new SendExpression(text, _messageSender); } + #region Nested type: SendExpression + public class SendExpression : ToExpression { + private readonly IMessageSender _messageSender; private readonly string _text; - private readonly IMessageSender _messageSender; private string _sender; public SendExpression(string text, IMessageSender messageSender) @@ -33,22 +33,28 @@ _messageSender = messageSender; } + void ToExpression.To(string receiver) + { + _messageSender.SendMessage(_text, _sender, receiver); + } + public ToExpression From(string sender) { _sender = sender; return this; } - - void ToExpression.To(string receiver) - { - _messageSender.SendMessage(_text, _sender, receiver); - } } + #endregion + + #region Nested type: ToExpression + public interface ToExpression { void To(string receiver); } + + #endregion } public class SendMessageRequest @@ -68,34 +74,34 @@ public class APIConsumer { - // Snippet from a class that uses IMessageSender - public void SendMessage(IMessageSender sender) - { - // Is this right? - sender.SendMessage("the message body", "PARTNER001", "PARTNER002"); + // Snippet from a class that uses IMessageSender + public void SendMessage(IMessageSender sender) + { + // Is this right? + sender.SendMessage("the message body", "PARTNER001", "PARTNER002"); - // or this? - sender.SendMessage("PARTNER001", "the message body", "PARTNER002"); + // or this? + sender.SendMessage("PARTNER001", "the message body", "PARTNER002"); - // or this? - sender.SendMessage("PARTNER001", "PARTNER002", "the message body"); - } + // or this? + sender.SendMessage("PARTNER001", "PARTNER002", "the message body"); + } - public void SendMessageFluently(FluentMessageSender sender) - { - sender - .SendText("the message body") - .From("PARTNER001").To("PARTNER002"); - } + public void SendMessageFluently(FluentMessageSender sender) + { + sender + .SendText("the message body") + .From("PARTNER001").To("PARTNER002"); + } - public void SendMessageAsParameter(ParameterObjectMessageSender sender) - { - sender.Send(new SendMessageRequest() + public void SendMessageAsParameter(ParameterObjectMessageSender sender) { - Text = "the message body", - Receiver = "PARTNER001", - Sender = "PARTNER002" - }); + sender.Send(new SendMessageRequest + { + Text = "the message body", + Receiver = "PARTNER001", + Sender = "PARTNER002" + }); + } } - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Exceptions/InstancePropertyValueException.cs =================================================================== --- trunk/Source/StructureMap/Exceptions/InstancePropertyValueException.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Exceptions/InstancePropertyValueException.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -15,8 +15,9 @@ { } - protected InstancePropertyValueException(SerializationInfo info, StreamingContext context) : - base(info, context) + protected InstancePropertyValueException(SerializationInfo info, StreamingContext context) + : + base(info, context) { } } Modified: trunk/Source/StructureMap/Exceptions/MissingPluginFamilyException.cs =================================================================== --- trunk/Source/StructureMap/Exceptions/MissingPluginFamilyException.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Exceptions/MissingPluginFamilyException.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -13,16 +13,14 @@ _message = string.Format("Type {0} is not a configured PluginFamily", pluginTypeName); } - protected MissingPluginFamilyException(SerializationInfo info, StreamingContext context) : - base(info, context) + protected MissingPluginFamilyException(SerializationInfo info, StreamingContext context) + : + base(info, context) { _message = info.GetString("message"); } - public override string Message - { - get { return _message; } - } + public override string Message { get { return _message; } } public override void GetObjectData(SerializationInfo info, StreamingContext context) { Modified: trunk/Source/StructureMap/Exceptions/StructureMapConfigurationException.cs =================================================================== --- trunk/Source/StructureMap/Exceptions/StructureMapConfigurationException.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Exceptions/StructureMapConfigurationException.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -6,13 +6,15 @@ [Serializable] public class StructureMapConfigurationException : ApplicationException { - public StructureMapConfigurationException(string message) : base(message) + public StructureMapConfigurationException(string message) + : base(message) { } - protected StructureMapConfigurationException(SerializationInfo info, StreamingContext context) : - base(info, context) + protected StructureMapConfigurationException(SerializationInfo info, StreamingContext context) + : + base(info, context) { } } Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -40,9 +40,10 @@ { private readonly List<Assembly> _assemblies = new List<Assembly>(); private readonly CompositeFilter<Type> _filter = new CompositeFilter<Type>(); + private readonly ImplementationMap _implementationMap = new ImplementationMap(); - private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); private readonly List<Action<PluginGraph>> _postScanningActions = new List<Action<PluginGraph>>(); + private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); public AssemblyScanner() { @@ -174,8 +175,6 @@ With(new GenericConnectionScanner(openGenericType)); } - - private readonly ImplementationMap _implementationMap = new ImplementationMap(); public void SingleImplementationsOfInterface() { @@ -232,10 +231,8 @@ internal void ScanForAll(PluginGraph pluginGraph) { - pluginGraph.Types.For(_assemblies, _filter).Each(type => - { - _scanners.Each(x => x.Process(type, pluginGraph)); - }); + pluginGraph.Types.For(_assemblies, _filter).Each( + type => { _scanners.Each(x => x.Process(type, pluginGraph)); }); _postScanningActions.Each(x => x(pluginGraph)); } Modified: trunk/Source/StructureMap/Graph/ImplementationMap.cs =================================================================== --- trunk/Source/StructureMap/Graph/ImplementationMap.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Graph/ImplementationMap.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -10,6 +10,11 @@ { private readonly Cache<Type, List<Type>> _types = new Cache<Type, List<Type>>(t => new List<Type>()); + public void Process(Type type, PluginGraph graph) + { + RegisterType(type); + } + public void Register(Type interfaceType, Type concreteType) { _types[interfaceType].Add(concreteType); @@ -22,11 +27,6 @@ type.GetInterfaces().Where(i => i.IsVisible).Each(i => Register(i, type)); } - public void Process(Type type, PluginGraph graph) - { - RegisterType(type); - } - public void RegisterSingleImplementations(PluginGraph graph) { _types.Each((pluginType, types) => Modified: trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -32,10 +32,7 @@ } } - public int Count - { - get { return _pluginFamilies.Count; } - } + public int Count { get { return _pluginFamilies.Count; } } public IEnumerable<PluginFamily> All { Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -54,7 +54,7 @@ _reference.Target = _builder(); } - return (T)_reference.Target; + return (T) _reference.Target; } } } @@ -72,9 +72,9 @@ private readonly ProfileManager _profileManager = new ProfileManager(); private readonly List<Registry> _registries = new List<Registry>(); private readonly List<AssemblyScanner> _scanners = new List<AssemblyScanner>(); + private readonly WeakReference<TypePool> _types; private GraphLog _log = new GraphLog(); private bool _sealed; - private readonly WeakReference<TypePool> _types; public PluginGraph() @@ -83,13 +83,7 @@ _types = new WeakReference<TypePool>(() => new TypePool(this)); } - public TypePool Types - { - get - { - return _types.Value; - } - } + public TypePool Types { get { return _types.Value; } } public List<Registry> Registries { get { return _registries; } } Modified: trunk/Source/StructureMap/Interceptors/CompoundInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/CompoundInterceptor.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Interceptors/CompoundInterceptor.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -10,10 +10,7 @@ } - public InstanceInterceptor[] Interceptors - { - get { return _interceptors; } - } + public InstanceInterceptor[] Interceptors { get { return _interceptors; } } #region InstanceInterceptor Members Modified: trunk/Source/StructureMap/Interceptors/Interceptors.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/Interceptors.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Interceptors/Interceptors.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,5 +1,6 @@ namespace StructureMap.Interceptors { public delegate object EnrichmentHandler<T>(T target); + public delegate object ContextEnrichmentHandler<T>(IContext context, T target); } \ No newline at end of file Modified: trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -45,6 +45,5 @@ { _interception = interception; } - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using StructureMap.Graph; -using StructureMap.TypeRules; namespace StructureMap.Pipeline { @@ -213,7 +211,7 @@ // _children.Add(name, instance); // } - + // } // protected void setChildArray(string name, Instance[] array) @@ -224,7 +222,7 @@ // { // throw new ApplicationException("There is a null value in the array of child Instances"); // } - + // } // _arrays.Add(name, array); Modified: trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -8,16 +8,6 @@ public static readonly string ITEM_NAME = "STRUCTUREMAP-INSTANCES"; - public static bool HasContext() - { - return HttpContext.Current != null; - } - - public static void DisposeAndClearAll() - { - new HttpContextLifecycle().FindCache().DisposeAndClear(); - } - public void EjectAll() { FindCache().DisposeAndClear(); @@ -33,7 +23,7 @@ { if (!items.Contains(ITEM_NAME)) { - MainObjectCache cache = new MainObjectCache(); + var cache = new MainObjectCache(); items.Add(ITEM_NAME, cache); return cache; @@ -41,10 +31,20 @@ } } - return (IObjectCache)items[ITEM_NAME]; + return (IObjectCache) items[ITEM_NAME]; } + public static bool HasContext() + { + return HttpContext.Current != null; + } + public static void DisposeAndClearAll() + { + new HttpContextLifecycle().FindCache().DisposeAndClear(); + } + + protected virtual IDictionary findHttpDictionary() { return HttpContext.Current.Items; Modified: trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -2,6 +2,5 @@ { public class HybridSessionLifecycle : HttpLifecycleBase<HttpSessionLifecycle, ThreadLocalStorageLifecycle> { - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/InstanceKey.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceKey.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/InstanceKey.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -4,12 +4,8 @@ { public class InstanceKey { - public string Name { get; set; } - public Type PluginType { get; set; } - [Obsolete("Kill!")] - private WeakReference _session; - [Obsolete("Kill!")] - private WeakReference _instance; + [Obsolete("Kill!")] private WeakReference _instance; + [Obsolete("Kill!")] private WeakReference _session; public InstanceKey() { @@ -21,20 +17,16 @@ PluginType = pluginType; } + public string Name { get; set; } + public Type PluginType { get; set; } + [Obsolete("Kill!")] - public BuildSession Session - { - get { return (BuildSession) _session.Target; } - set { _session = new WeakReference(value); } - } + public BuildSession Session { get { return (BuildSession) _session.Target; } set { _session = new WeakReference(value); } } [Obsolete("Kill!")] public Instance Instance { - get - { - return (Instance) _instance.Target; - } + get { return (Instance) _instance.Target; } set { Name = value.Name; @@ -53,15 +45,16 @@ { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != typeof(InstanceKey)) return false; - return Equals((InstanceKey)obj); + if (obj.GetType() != typeof (InstanceKey)) return false; + return Equals((InstanceKey) obj); } public override int GetHashCode() { unchecked { - return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (PluginType != null ? PluginType.GetHashCode() : 0); + return ((Name != null ? Name.GetHashCode() : 0)*397) ^ + (PluginType != null ? PluginType.GetHashCode() : 0); } } } Modified: trunk/Source/StructureMap/Pipeline/Lifecycles.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Lifecycles.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/Lifecycles.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -30,7 +30,7 @@ case InstanceScope.HybridHttpSession: return new HybridSessionLifecycle(); } - + throw new ArgumentOutOfRangeException("scope"); } } Modified: trunk/Source/StructureMap/Pipeline/MainObjectCache.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -5,18 +5,12 @@ { public class MainObjectCache : IObjectCache { - private readonly IDictionary<InstanceKey, object> _objects = new Dictionary<InstanceKey,object>(); private readonly object _locker = new object(); + private readonly IDictionary<InstanceKey, object> _objects = new Dictionary<InstanceKey, object>(); - public object Locker - { - get { return _locker; } - } + public object Locker { get { return _locker; } } - public int Count - { - get { return _objects.Count; } - } + public int Count { get { return _objects.Count; } } public object Get(Type pluginType, Instance instance) { @@ -45,21 +39,23 @@ { lock (Locker) { - foreach (var @object in _objects.Values) + foreach (object @object in _objects.Values) { if (@object is Container) continue; - IDisposable disposable = @object as IDisposable; + var disposable = @object as IDisposable; if (disposable != null) { try { disposable.Dispose(); } - catch (Exception) { } + catch (Exception) + { + } } } - + _objects.Clear(); } } Modified: trunk/Source/StructureMap/Pipeline/SessionWrapper.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SessionWrapper.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/SessionWrapper.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -25,20 +25,11 @@ _session.CopyTo(array, index); } - public int Count - { - get { return _session.Count; } - } + public int Count { get { return _session.Count; } } - public object SyncRoot - { - get { return _session.SyncRoot; } - } + public object SyncRoot { get { return _session.SyncRoot; } } - public bool IsSynchronized - { - get { return _session.IsSynchronized; } - } + public bool IsSynchronized { get { return _session.IsSynchronized; } } public bool Contains(object key) { @@ -65,31 +56,15 @@ _session.Remove(key.ToString()); } - public object this[object key] - { - get { return _session[key.ToString()]; } - set { _session[key.ToString()] = value; } - } + public object this[object key] { get { return _session[key.ToString()]; } set { _session[key.ToString()] = value; } } - public ICollection Keys - { - get { return _session.Keys; } - } + public ICollection Keys { get { return _session.Keys; } } - public ICollection Values - { - get { throw new NotImplementedException(); } - } + public ICollection Values { get { throw new NotImplementedException(); } } - public bool IsReadOnly - { - get { return _session.IsReadOnly; } - } + public bool IsReadOnly { get { return _session.IsReadOnly; } } - public bool IsFixedSize - { - get { return false; } - } + public bool IsFixedSize { get { return false; } } #endregion } Modified: trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,5 +1,3 @@ -using System; - namespace StructureMap.Pipeline { /// <summary> @@ -9,7 +7,6 @@ { public void EjectAll() { - } public IObjectCache FindCache() @@ -18,9 +15,9 @@ } //#region IBuildInterceptor Members - + //private IBuildPolicy _innerPolicy = new BuildPolicy(); - + //public IBuildPolicy InnerPolicy //{ // get { return _innerPolicy; } @@ -52,4 +49,4 @@ //#endregion } -} +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/UserControlInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Pipeline/UserControlInstance.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -1,6 +1,5 @@ using System; using System.Web.UI; -using StructureMap.Graph; using StructureMap.TypeRules; namespace StructureMap.Pipeline @@ -14,16 +13,10 @@ _url = url; } - protected override UserControlInstance thisInstance - { - get { return this; } - } + protected override UserControlInstance thisInstance { get { return this; } } - public string Url - { - get { return _url; } - } + public string Url { get { return _url; } } protected override object build(Type pluginType, BuildSession session) { Modified: trunk/Source/StructureMap/Source/BasicXmlMementoSource.cs =================================================================== --- trunk/Source/StructureMap/Source/BasicXmlMementoSource.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Source/BasicXmlMementoSource.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -15,10 +15,7 @@ _node = Node; } - public override string Description - { - get { return "BasicXmlMementoSource"; } - } + public override string Description { get { return "BasicXmlMementoSource"; } } protected override XmlNode getRootNode() { Modified: trunk/Source/StructureMap/Source/MemoryMementoSource.cs =================================================================== --- trunk/Source/StructureMap/Source/MemoryMementoSource.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/Source/MemoryMementoSource.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -14,10 +14,7 @@ _mementos = new Hashtable(); } - public override string Description - { - get { return "DefaultMementoSource"; } - } + public override string Description { get { return "DefaultMementoSource"; } } protected override InstanceMemento[] fetchInternalMementos() { Modified: trunk/Source/StructureMap/SystemRegistry.cs =================================================================== --- trunk/Source/StructureMap/SystemRegistry.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/SystemRegistry.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -30,7 +30,7 @@ private void AddLifecycleType<T>(InstanceScope scope) where T : ILifecycle { - addExpression(graph => graph.AddType(typeof(ILifecycle), typeof(T), scope.ToString())); + addExpression(graph => graph.AddType(typeof (ILifecycle), typeof (T), scope.ToString())); } private void AddMementoSourceType<T>(string name) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 18:26:08 UTC (rev 299) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 18:39:40 UTC (rev 300) @@ -153,7 +153,7 @@ public static IEnumerable<Type> AllInterfaces(this Type type) { - foreach (var @interface in type.GetInterfaces()) + foreach (Type @interface in type.GetInterfaces()) { yield return @interface; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 19:29:17
|
Revision: 301 http://structuremap.svn.sourceforge.net/structuremap/?rev=301&view=rev Author: jeremydmiller Date: 2009-12-26 19:29:05 +0000 (Sat, 26 Dec 2009) Log Message: ----------- converted the DefaultConventionScanner to be a RegistrationConvention Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/IAssemblyScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -88,6 +88,7 @@ /// </summary> /// <param name="concreteType"></param> /// <returns></returns> + [Obsolete("Switch to Add()")] public ConfiguredInstance AddType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); @@ -97,7 +98,19 @@ return instance; } + /// <summary> + /// Shortcut method to add an additional Instance to this Plugin Type + /// as just a Concrete Type. This will only work if the Concrete Type + /// has no primitive constructor or mandatory Setter arguments. + /// </summary> + /// <param name="concreteType"></param> + /// <returns></returns> + public ConfiguredInstance Add(Type concreteType) + { + return AddType(concreteType); + } + public GenericFamilyExpression Add(Instance instance) { return alterAndContinue(family => family.AddInstance(instance)); Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -8,6 +8,225 @@ namespace StructureMap.Configuration.DSL { + public interface IRegistry + { + /// <summary> + /// Adds the concreteType as an Instance of the pluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="concreteType"></param> + void AddType(Type pluginType, Type concreteType); + + /// <summary> + /// Adds the concreteType as an Instance of the pluginType with a name + /// </summary> + /// <param name="pluginType"></param> + /// <param name="concreteType"></param> + /// <param name="name"></param> + void AddType(Type pluginType, Type concreteType, string name); + + /// <summary> + /// Add the pluggedType as an instance to any configured pluginType where pluggedType + /// could be assigned to the pluginType + /// </summary> + /// <param name="pluggedType"></param> + void AddType(Type pluggedType); + + /// <summary> + /// Imports the configuration from another registry into this registry. + /// </summary> + /// <typeparam name="T"></typeparam> + void IncludeRegistry<T>() where T : Registry, new(); + + /// <summary> + /// Imports the configuration from another registry into this registry. + /// </summary> + /// <param name="registry"></param> + void IncludeRegistry(Registry registry); + + /// <summary> + /// Expression Builder used to define policies for a PluginType including + /// Scoping, the Default Instance, and interception. BuildInstancesOf() + /// and ForRequestedType() are synonyms + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <returns></returns> + [Obsolete("Change to For<T>()")] + CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>(); + + /// <summary> + /// Expression Builder used to define policies for a PluginType including + /// Scoping, the Default Instance, and interception. This method is specifically + /// meant for registering open generic types + /// </summary> + /// <returns></returns> + [Obsolete("Change to For(pluginType)")] + GenericFamilyExpression ForRequestedType(Type pluginType); + + /// <summary> + /// This method is a shortcut for specifying the default constructor and + /// setter arguments for a ConcreteType. ForConcreteType is shorthand for: + /// ForRequestedType[T]().TheDefault.Is.OfConcreteType[T].************** + /// when the PluginType and ConcreteType are the same Type + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + Registry.BuildWithExpression<T> ForConcreteType<T>(); + + /// <summary> + /// Expression Builder used to define policies for a PluginType including + /// Scoping, the Default Instance, and interception. BuildInstancesOf() + /// and ForRequestedType() are synonyms + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <returns></returns> + [Obsolete("Change to For<T>()")] + CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<PLUGINTYPE>(); + + /// <summary> + /// Convenience method. Equivalent of ForRequestedType[PluginType]().AsSingletons() + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <returns></returns> + CreatePluginFamilyExpression<PLUGINTYPE> ForSingletonOf<PLUGINTYPE>(); + + /// <summary> + /// Uses the configuration expressions of this Registry to create a PluginGraph + /// object that could be used to initialize a Container. This method is + /// mostly for internal usage, but might be helpful for diagnostics + /// </summary> + /// <returns></returns> + PluginGraph Build(); + + /// <summary> + /// Adds an additional, non-Default Instance to the PluginType T. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + [Obsolete("Prefer For<T>().Add() instead")] + IsExpression<T> InstanceOf<T>(); + + /// <summary> + /// Adds an additional, non-Default Instance to the designated pluginType + /// This method is mostly meant for open generic types + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + [Obsolete("Prefer For(type).Add() instead")] + GenericIsExpression InstanceOf(Type pluginType); + + /// <summary> + /// Expression Builder to define the defaults for a named Profile. Each call + /// to CreateProfile is additive. + /// </summary> + /// <param name="profileName"></param> + /// <returns></returns> + ProfileExpression CreateProfile(string profileName); + + /// <summary> + /// An alternative way to use CreateProfile that uses ProfileExpression + /// as a Nested Closure. This usage will result in cleaner code for + /// multiple declarations + /// </summary> + /// <param name="profileName"></param> + /// <param name="action"></param> + void CreateProfile(string profileName, Action<ProfileExpression> action); + + /// <summary> + /// Registers a new TypeInterceptor object with the Container + /// </summary> + /// <param name="interceptor"></param> + void RegisterInterceptor(TypeInterceptor interceptor); + + /// <summary> + /// Allows you to define a TypeInterceptor inline with Lambdas or anonymous delegates + /// </summary> + /// <param name="match"></param> + /// <returns></returns> + /// <example> + /// IfTypeMatches( ... ).InterceptWith( o => new ObjectWrapper(o) ); + /// </example> + MatchedTypeInterceptor IfTypeMatches(Predicate<Type> match); + + /// <summary> + /// Designates a policy for scanning assemblies to auto + /// register types + /// </summary> + /// <returns></returns> + void Scan(Action<IAssemblyScanner> action); + + /// <summary> + /// Directs StructureMap to always inject dependencies into any and all public Setter properties + /// of the type PLUGINTYPE. + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <returns></returns> + CreatePluginFamilyExpression<PLUGINTYPE> FillAllPropertiesOfType<PLUGINTYPE>(); + + /// <summary> + /// Creates automatic "policies" for which public setters are considered mandatory + /// properties by StructureMap that will be "setter injected" as part of the + /// construction process. + /// </summary> + /// <param name="action"></param> + void SetAllProperties(Action<SetterConvention> action); + + /// <summary> + /// Use to programmatically select the constructor function of a concrete + /// class. Applies globally to all Containers in a single AppDomain. + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="expression"></param> + void SelectConstructor<T>(Expression<Func<T>> expression); + + /// <summary> + /// All requests For the "TO" types will be filled by fetching the "FROM" + /// type and casting it to "TO" + /// GetInstance(typeof(TO)) basically becomes (TO)GetInstance(typeof(FROM)) + /// </summary> + /// <typeparam name="FROM"></typeparam> + /// <typeparam name="TO"></typeparam> + void Forward<FROM, TO>() where FROM : class where TO : class; + + /// <summary> + /// Syntactic Sugar for saying ForRequestedType().TheDefault.IsThis( @object ) + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <param name="object"></param> + void Register<PLUGINTYPE>(PLUGINTYPE @object); + + /// <summary> + /// Syntactic Sugar for saying ForRequestedType().TheDefault.IsThis( instance ) + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <param name="instance"></param> + void Register<PLUGINTYPE>(Instance instance); + + /// <summary> + /// Shorthand for ForRequestedType<PLUGINTYPE>() + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <returns></returns> + CreatePluginFamilyExpression<PLUGINTYPE> For<PLUGINTYPE>(); + + /// <summary> + /// Shorthand for ForRequestedType(pluginType) + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + GenericFamilyExpression For(Type pluginType); + + /// <summary> + /// Shortcut to make StructureMap return the default object of U casted to T + /// whenever T is requested. I.e.: + /// For<T>().TheDefault.Is.ConstructedBy(c => c.GetInstance<U>() as T); + /// </summary> + /// <typeparam name="T"></typeparam> + /// <typeparam name="U"></typeparam> + /// <returns></returns> + LambdaInstance<T> Redirect<T, U>() where T : class where U : class; + } + /// <summary> /// A Registry class provides methods and grammars for configuring a Container or ObjectFactory. /// Using a Registry subclass is the recommended way of configuring a StructureMap Container. @@ -21,12 +240,43 @@ /// } /// } /// </example> - public class Registry + public class Registry : IRegistry { private readonly List<Action<PluginGraph>> _actions = new List<Action<PluginGraph>>(); private readonly List<Action> _basicActions = new List<Action>(); /// <summary> + /// Adds the concreteType as an Instance of the pluginType + /// </summary> + /// <param name="pluginType"></param> + /// <param name="concreteType"></param> + public void AddType(Type pluginType, Type concreteType) + { + _actions.Add(g => g.AddType(pluginType, concreteType)); + } + + /// <summary> + /// Adds the concreteType as an Instance of the pluginType with a name + /// </summary> + /// <param name="pluginType"></param> + /// <param name="concreteType"></param> + /// <param name="name"></param> + public void AddType(Type pluginType, Type concreteType, string name) + { + _actions.Add(g => g.AddType(pluginType, concreteType, name)); + } + + /// <summary> + /// Add the pluggedType as an instance to any configured pluginType where pluggedType + /// could be assigned to the pluginType + /// </summary> + /// <param name="pluggedType"></param> + public void AddType(Type pluggedType) + { + _actions.Add(g => g.AddType(pluggedType)); + } + + /// <summary> /// Imports the configuration from another registry into this registry. /// </summary> /// <typeparam name="T"></typeparam> @@ -315,6 +565,13 @@ PluginCache.GetPlugin(typeof (T)).UseConstructor(expression); } + /// <summary> + /// All requests For the "TO" types will be filled by fetching the "FROM" + /// type and casting it to "TO" + /// GetInstance(typeof(TO)) basically becomes (TO)GetInstance(typeof(FROM)) + /// </summary> + /// <typeparam name="FROM"></typeparam> + /// <typeparam name="TO"></typeparam> public void Forward<FROM, TO>() where FROM : class where TO : class { For<TO>().AddInstances(x => x.ConstructedBy(c => c.GetInstance<FROM>() as TO)); Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; +using StructureMap.Configuration.DSL; using StructureMap.TypeRules; using StructureMap.Util; @@ -39,6 +40,7 @@ public class AssemblyScanner : IAssemblyScanner { private readonly List<Assembly> _assemblies = new List<Assembly>(); + private readonly List<IRegistrationConvention> _conventions = new List<IRegistrationConvention>(); private readonly CompositeFilter<Type> _filter = new CompositeFilter<Type>(); private readonly ImplementationMap _implementationMap = new ImplementationMap(); @@ -67,18 +69,18 @@ Assembly(AppDomain.CurrentDomain.Load(assemblyName)); } + [Obsolete("Replace ITypeScanner with IRegistrationConvention")] public void With(ITypeScanner scanner) { - if (_scanners.Contains(scanner)) return; - - _scanners.Add(scanner); + _scanners.Fill(scanner); } public void WithDefaultConventions() { - With<DefaultConventionScanner>(); + Convention<DefaultConventionScanner>(); } + [Obsolete("Replace ITypeScanner with IRegistrationConvention")] public void With<T>() where T : ITypeScanner, new() { _scanners.RemoveAll(scanner => scanner is T); @@ -90,6 +92,15 @@ } } + public void Convention<T>() where T : IRegistrationConvention, new() + { + var previous = _conventions.FirstOrDefault(scanner => scanner is T); + if (previous == null) + { + With(new T()); + } + } + public void LookForRegistries() { With<FindRegistriesScanner>(); @@ -206,14 +217,15 @@ public void AssembliesFromPath(string path, Predicate<Assembly> assemblyFilter) { - IEnumerable<string> assemblyPaths = Directory.GetFiles(path).Where(file => - Path.GetExtension(file).Equals( - ".exe", - StringComparison.OrdinalIgnoreCase) - || - Path.GetExtension(file).Equals( - ".dll", - StringComparison.OrdinalIgnoreCase)); + IEnumerable<string> assemblyPaths = Directory.GetFiles(path) + .Where(file => + Path.GetExtension(file).Equals( + ".exe", + StringComparison.OrdinalIgnoreCase) + || + Path.GetExtension(file).Equals( + ".dll", + StringComparison.OrdinalIgnoreCase)); foreach (string assemblyPath in assemblyPaths) { @@ -229,11 +241,23 @@ } } + public void With(IRegistrationConvention convention) + { + _conventions.Fill(convention); + } + internal void ScanForAll(PluginGraph pluginGraph) { + var registry = new Registry(); + pluginGraph.Types.For(_assemblies, _filter).Each( - type => { _scanners.Each(x => x.Process(type, pluginGraph)); }); + type => + { + _scanners.Each(x => x.Process(type, pluginGraph)); + _conventions.Each(c => c.Process(type, registry)); + }); + registry.ConfigurePluginGraph(pluginGraph); _postScanningActions.Each(x => x(pluginGraph)); } Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -53,6 +53,9 @@ private static bool checkGenericType(Type pluggedType, Type pluginType) { + if (pluginType.IsAssignableFrom(pluggedType)) return true; + + // check interfaces foreach (Type type in pluggedType.GetInterfaces()) { Modified: trunk/Source/StructureMap/Graph/IAssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -185,5 +185,18 @@ /// The filters apply /// </summary> void SingleImplementationsOfInterface(); + + /// <summary> + /// Adds a registration convention to be applied to all the types in this + /// logical "scan" operation + /// </summary> + /// <typeparam name="T"></typeparam> + void Convention<T>() where T : IRegistrationConvention, new(); + + /// <summary> + /// Adds a registration convention to be applied to all the types in this + /// logical "scan" operation + /// </summary> + void With(IRegistrationConvention convention); } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -1,30 +1,33 @@ using System; +using StructureMap.Configuration.DSL; using StructureMap.TypeRules; namespace StructureMap.Graph { + [Obsolete("Favor the new IRegistrationConvention")] public interface ITypeScanner { void Process(Type type, PluginGraph graph); } - public class DefaultConventionScanner : ITypeScanner + public interface IRegistrationConvention { - #region ITypeScanner Members + void Process(Type type, Registry registry); + } - public void Process(Type type, PluginGraph graph) + public class DefaultConventionScanner : IRegistrationConvention + { + public void Process(Type type, Registry registry) { if (!type.IsConcrete()) return; Type pluginType = FindPluginType(type); if (pluginType != null && Constructor.HasConstructors(type)) { - graph.AddType(pluginType, type); + registry.For(pluginType).Add(type); } } - #endregion - public virtual Type FindPluginType(Type concreteType) { string interfaceName = "I" + concreteType.Name; Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -172,7 +172,7 @@ return false; } - if (IsOpenGeneric(pluginType)) + if (pluginType.IsOpenGeneric() && pluggedType.IsGenericType) { return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); } Modified: trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -1,5 +1,6 @@ using System; using NUnit.Framework; +using StructureMap.Configuration.DSL; using StructureMap.Graph; namespace StructureMap.Testing.Graph @@ -61,6 +62,14 @@ [TestFixture] public class DefaultConventionScanningTester { + [SetUp] + public void SetUp() + { + PluginCache.ResetAll(); + } + + + [Test] public void FindPluginType() { @@ -77,7 +86,7 @@ registry.Scan(x => { x.TheCallingAssembly(); - x.With<DefaultConventionScanner>(); + x.Convention<DefaultConventionScanner>(); }); }); @@ -105,8 +114,13 @@ { var graph = new PluginGraph(); var scanner = new DefaultConventionScanner(); - scanner.Process(typeof (Convention), graph); + var registry = new Registry(); + + scanner.Process(typeof (Convention), registry); + + registry.ConfigurePluginGraph(graph); + Assert.IsFalse(graph.PluginFamilies.Contains(typeof (IServer))); Assert.IsTrue(graph.PluginFamilies.Contains(typeof (IConvention))); Modified: trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs 2009-12-26 18:39:40 UTC (rev 300) +++ trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs 2009-12-26 19:29:05 UTC (rev 301) @@ -3,6 +3,7 @@ using NUnit.Framework; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Testing.Bugs; using StructureMap.Testing.GenericWidgets; namespace StructureMap.Testing.Graph @@ -30,6 +31,12 @@ } [Test] + public void checking_can_be_cast() + { + assertCanBeCast(typeof(IOpenType<>), typeof(OpenType<>)); + } + + [Test] public void BuildAnInstanceManagerFromTemplatedPluginFamily() { var pluginGraph = new PluginGraph(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-26 20:31:38
|
Revision: 302 http://structuremap.svn.sourceforge.net/structuremap/?rev=302&view=rev Author: jeremydmiller Date: 2009-12-26 20:31:30 +0000 (Sat, 26 Dec 2009) Log Message: ----------- Introduced the new IRegistrationConvention, replacing all ITypeScanner classes Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/FamilyAttributeScanner.cs trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs trunk/Source/StructureMap/Graph/FindRegistriesScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/ImplementationMap.cs trunk/Source/StructureMap/Graph/PluggableAttributeScanner.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -225,6 +225,13 @@ /// <typeparam name="U"></typeparam> /// <returns></returns> LambdaInstance<T> Redirect<T, U>() where T : class where U : class; + + /// <summary> + /// Advanced Usage Only! Skips the Registry and goes right to the inner + /// Semantic Model of StructureMap. Use with care + /// </summary> + /// <param name="configure"></param> + void Configure(Action<PluginGraph> configure); } /// <summary> @@ -641,6 +648,16 @@ }); } + /// <summary> + /// Advanced Usage Only! Skips the Registry and goes right to the inner + /// Semantic Model of StructureMap. Use with care + /// </summary> + /// <param name="configure"></param> + public void Configure(Action<PluginGraph> configure) + { + _actions.Add(configure); + } + #region Nested type: BuildWithExpression /// <summary> Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -49,8 +49,8 @@ public AssemblyScanner() { - With<FamilyAttributeScanner>(); - With<PluggableAttributeScanner>(); + Convention<FamilyAttributeScanner>(); + Convention<PluggableAttributeScanner>(); } public int Count { get { return _assemblies.Count; } } @@ -103,7 +103,7 @@ public void LookForRegistries() { - With<FindRegistriesScanner>(); + Convention<FindRegistriesScanner>(); } public void TheCallingAssembly() @@ -141,8 +141,8 @@ public void IgnoreStructureMapAttributes() { - _scanners.RemoveAll(scanner => scanner is FamilyAttributeScanner); - _scanners.RemoveAll(scanner => scanner is PluggableAttributeScanner); + _conventions.RemoveAll(scanner => scanner is FamilyAttributeScanner); + _conventions.RemoveAll(scanner => scanner is PluggableAttributeScanner); } @@ -189,7 +189,7 @@ public void SingleImplementationsOfInterface() { - _scanners.Fill(_implementationMap); + _conventions.Fill(_implementationMap); _postScanningActions.Add(graph => _implementationMap.RegisterSingleImplementations(graph)); } Modified: trunk/Source/StructureMap/Graph/FamilyAttributeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/FamilyAttributeScanner.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/FamilyAttributeScanner.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -1,19 +1,16 @@ using System; +using StructureMap.Configuration.DSL; namespace StructureMap.Graph { - public class FamilyAttributeScanner : ITypeScanner + public class FamilyAttributeScanner : IRegistrationConvention { - #region ITypeScanner Members - - public void Process(Type type, PluginGraph graph) + public void Process(Type type, Registry registry) { if (PluginFamilyAttribute.MarkedAsPluginFamily(type)) { - graph.CreateFamily(type); + registry.Configure(x => x.FindFamily(type)); } } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/FindAllTypesFilter.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -1,9 +1,10 @@ using System; +using StructureMap.Configuration.DSL; using StructureMap.TypeRules; namespace StructureMap.Graph { - public class FindAllTypesFilter : ITypeScanner + public class FindAllTypesFilter : IRegistrationConvention { private readonly Type _pluginType; private Func<Type, string> _getName = type => PluginCache.GetPlugin(type).ConcreteKey; @@ -13,22 +14,18 @@ _pluginType = pluginType; } - #region ITypeScanner Members + public void NameBy(Func<Type, string> getName) + { + _getName = getName; + } - public void Process(Type type, PluginGraph graph) + public void Process(Type type, Registry registry) { if (type.CanBeCastTo(_pluginType) && Constructor.HasConstructors(type)) { string name = _getName(type); - graph.AddType(_pluginType, type, name); + registry.AddType(_pluginType, type, name); } } - - public void NameBy(Func<Type, string> getName) - { - _getName = getName; - } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/FindRegistriesScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/FindRegistriesScanner.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/FindRegistriesScanner.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -3,26 +3,14 @@ namespace StructureMap.Graph { - public class FindRegistriesScanner : ITypeScanner + public class FindRegistriesScanner : IRegistrationConvention { - #region ITypeScanner Members - - public void Process(Type type, PluginGraph graph) + public void Process(Type type, Registry registry) { - if (!Registry.IsPublicRegistry(type)) return; - - foreach (Registry previous in graph.Registries) + if (Registry.IsPublicRegistry(type)) { - if (previous.GetType().Equals(type)) - { - return; - } + registry.Configure(x => x.ImportRegistry(type)); } - - var registry = (Registry) Activator.CreateInstance(type); - registry.ConfigurePluginGraph(graph); } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -36,7 +36,7 @@ } } - public class GenericConnectionScanner : ITypeScanner + public class GenericConnectionScanner : IRegistrationConvention { private readonly Type _openType; @@ -50,12 +50,12 @@ } } - public void Process(Type type, PluginGraph graph) + public void Process(Type type, Registry registry) { Type interfaceType = type.FindInterfaceThatCloses(_openType); if (interfaceType != null) { - graph.AddType(interfaceType, type); + registry.For(interfaceType).Add(type); } } } Modified: trunk/Source/StructureMap/Graph/ImplementationMap.cs =================================================================== --- trunk/Source/StructureMap/Graph/ImplementationMap.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/ImplementationMap.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -1,20 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; +using StructureMap.Configuration.DSL; using StructureMap.TypeRules; using StructureMap.Util; namespace StructureMap.Graph { - public class ImplementationMap : ITypeScanner + public class ImplementationMap : IRegistrationConvention { private readonly Cache<Type, List<Type>> _types = new Cache<Type, List<Type>>(t => new List<Type>()); - public void Process(Type type, PluginGraph graph) - { - RegisterType(type); - } - public void Register(Type interfaceType, Type concreteType) { _types[interfaceType].Add(concreteType); @@ -37,5 +33,10 @@ } }); } + + public void Process(Type type, Registry registry) + { + RegisterType(type); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluggableAttributeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluggableAttributeScanner.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/PluggableAttributeScanner.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -1,19 +1,16 @@ using System; +using StructureMap.Configuration.DSL; namespace StructureMap.Graph { - public class PluggableAttributeScanner : ITypeScanner + public class PluggableAttributeScanner : IRegistrationConvention { - #region ITypeScanner Members - - public void Process(Type type, PluginGraph graph) + public void Process(Type type, Registry registry) { if (PluggableAttribute.MarkedAsPluggable(type)) { - graph.AddType(type); + registry.AddType(type); } } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -5,6 +5,7 @@ using StructureMap.Diagnostics; using StructureMap.Interceptors; using StructureMap.Pipeline; +using System.Linq; namespace StructureMap.Graph { @@ -218,5 +219,13 @@ registry.ConfigurePluginGraph(this); } + + public void ImportRegistry(Type type) + { + if (Registries.Any(x => x.GetType() == type)) return; + + var registry = (Registry)Activator.CreateInstance(type); + registry.ConfigurePluginGraph(this); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -246,6 +246,8 @@ { return IsChild(type) || IsChildArray(type); } + + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-12-26 19:29:05 UTC (rev 301) +++ trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-12-26 20:31:30 UTC (rev 302) @@ -194,10 +194,19 @@ var scanner = new FamilyAttributeScanner(); var graph = new PluginGraph(); - scanner.Process(typeof (ITypeThatHasAttributeButIsNotInRegistry), graph); + var registry = new Registry(); + + scanner.Process(typeof (ITypeThatHasAttributeButIsNotInRegistry), registry); + registry.ConfigurePluginGraph(graph); + graph.PluginFamilies.Contains(typeof (ITypeThatHasAttributeButIsNotInRegistry)).ShouldBeTrue(); - scanner.Process(GetType(), graph); + graph = new PluginGraph(); + registry = new Registry(); + + scanner.Process(GetType(), registry); + registry.ConfigurePluginGraph(graph); + graph.PluginFamilies.Contains(GetType()).ShouldBeFalse(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-27 15:59:29
|
Revision: 304 http://structuremap.svn.sourceforge.net/structuremap/?rev=304&view=rev Author: jeremydmiller Date: 2009-12-27 15:59:19 +0000 (Sun, 27 Dec 2009) Log Message: ----------- refactored and also extended the IObjectCache, MainObjectCache classes to get ready for extensions to "Model" Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Model.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/IObjectCache.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/Pipeline/MainObjectCache.cs trunk/Source/StructureMap/Pipeline/NulloObjectCache.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/ModelQueryTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/IModel.cs trunk/Source/StructureMap.Testing/Pipeline/MainObjectCacheTester.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Container.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -64,7 +64,7 @@ /// <summary> /// Provides queryable access to the configured PluginType's and Instances of this Container /// </summary> - public IModel Model { get { return new Model(_pipelineGraph); } } + public IModel Model { get { return new Model(_pipelineGraph, this); } } /// <summary> /// Creates or finds the named instance of T Added: trunk/Source/StructureMap/IModel.cs =================================================================== --- trunk/Source/StructureMap/IModel.cs (rev 0) +++ trunk/Source/StructureMap/IModel.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using StructureMap.Pipeline; + +namespace StructureMap +{ + /// <summary> + /// Models the state of a Container or ObjectFactory. Can be used to query for the + /// existence of types registered with StructureMap + /// </summary> + public interface IModel + { + /// <summary> + /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations + /// </summary> + IEnumerable<PluginTypeConfiguration> PluginTypes { get; } + + IEnumerable<IInstance> AllInstances { get; } + + /// <summary> + /// Can StructureMap fulfill a request to ObjectFactory.GetInstance(pluginType) from the + /// current configuration. This does not include concrete classes that could be auto-configured + /// upon demand + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + bool HasDefaultImplementationFor(Type pluginType); + + /// <summary> + /// Can StructureMap fulfill a request to ObjectFactory.GetInstance<T>() from the + /// current configuration. This does not include concrete classes that could be auto-configured + /// upon demand + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + bool HasDefaultImplementationFor<T>(); + + /// <summary> + /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + IEnumerable<IInstance> InstancesOf(Type pluginType); + + /// <summary> + /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// </summary> + /// <returns></returns> + IEnumerable<IInstance> InstancesOf<T>(); + + /// <summary> + /// Does the current container have existing configuration for the "pluginType" + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + bool HasImplementationsFor(Type pluginType); + + /// <summary> + /// Does the current container have existing configuration for the type T + /// </summary> + /// <returns></returns> + bool HasImplementationsFor<T>(); + + /// <summary> + /// Find the concrete type for the default Instance of T. + /// In other words, when I call Container.GetInstance(Type), + /// what do I get? May be indeterminate + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + Type DefaultTypeFor<T>(); + + /// <summary> + /// Find the concrete type for the default Instance of pluginType. + /// In other words, when I call Container.GetInstance(Type), + /// what do I get? May be indeterminate + /// </summary> + /// <returns></returns> + Type DefaultTypeFor(Type pluginType); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Model.cs =================================================================== --- trunk/Source/StructureMap/Model.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Model.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -5,79 +5,28 @@ namespace StructureMap { - /// <summary> - /// Models the state of a Container or ObjectFactory. Can be used to query for the - /// existence of types registered with StructureMap - /// </summary> - public interface IModel - { - /// <summary> - /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations - /// </summary> - IEnumerable<PluginTypeConfiguration> PluginTypes { get; } - - IEnumerable<IInstance> AllInstances { get; } - - /// <summary> - /// Can StructureMap fulfill a request to ObjectFactory.GetInstance(pluginType) from the - /// current configuration. This does not include concrete classes that could be auto-configured - /// upon demand - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - bool HasDefaultImplementationFor(Type pluginType); - - /// <summary> - /// Can StructureMap fulfill a request to ObjectFactory.GetInstance<T>() from the - /// current configuration. This does not include concrete classes that could be auto-configured - /// upon demand - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - bool HasDefaultImplementationFor<T>(); - - /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - IEnumerable<IInstance> InstancesOf(Type pluginType); - - /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType - /// </summary> - /// <returns></returns> - IEnumerable<IInstance> InstancesOf<T>(); - - /// <summary> - /// Does the current container have existing configuration for the "pluginType" - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - bool HasImplementationsFor(Type pluginType); - - /// <summary> - /// Does the current container have existing configuration for the type T - /// </summary> - /// <returns></returns> - bool HasImplementationsFor<T>(); - } - public class Model : IModel { private readonly PipelineGraph _graph; + private readonly IContainer _container; - internal Model(PipelineGraph graph) + internal Model(PipelineGraph graph, IContainer container) { _graph = graph; + _container = container; } #region IModel Members public bool HasDefaultImplementationFor(Type pluginType) { + return findForFamily(pluginType, f => f.Default != null); + } + + private T findForFamily<T>(Type pluginType, Func<PluginTypeConfiguration, T> func) + { PluginTypeConfiguration family = PluginTypes.FirstOrDefault(x => x.PluginType == pluginType); - return family == null ? false : family.Default != null; + return family == null ? default(T) : func(family); } public bool HasDefaultImplementationFor<T>() @@ -85,6 +34,16 @@ return HasDefaultImplementationFor(typeof (T)); } + public Type DefaultTypeFor<T>() + { + return DefaultTypeFor(typeof (T)); + } + + public Type DefaultTypeFor(Type pluginType) + { + return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ConcreteType); + } + public IEnumerable<PluginTypeConfiguration> PluginTypes { get { return _graph.PluginTypes; } } public IEnumerable<IInstance> InstancesOf(Type pluginType) Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -119,11 +119,6 @@ return instance; } - protected override void addTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) - { - throw new NotImplementedException(); - } - protected override sealed string getDescription() { return "Configured Instance of " + _plugin.PluggedType.AssemblyQualifiedName; Modified: trunk/Source/StructureMap/Pipeline/IObjectCache.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IObjectCache.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Pipeline/IObjectCache.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -8,6 +8,11 @@ int Count { get; } + bool Has(Type pluginType, Instance instance); + + void Eject(Type pluginType, Instance instance); + + object Get(Type pluginType, Instance instance); void Set(Type pluginType, Instance instance, object value); void DisposeAndClear(); Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -25,9 +25,6 @@ Instance FindInstanceForProfile(PluginFamily family, string profileName, GraphLog log); InstanceToken CreateToken(); void Preprocess(PluginFamily family); - - [Obsolete("can go away")] - void AddTemplatedInstanceTo(PluginFamily family, Type[] templateTypes); } public abstract class Instance : IDiagnosticInstance @@ -73,12 +70,6 @@ preprocess(family); } - [Obsolete("Can go")] - void IDiagnosticInstance.AddTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) - { - addTemplatedInstanceTo(family, templateTypes); - } - Type IInstance.ConcreteType { get { return getConcreteType(null); } } string IInstance.Description { get { return getDescription(); } } Modified: trunk/Source/StructureMap/Pipeline/MainObjectCache.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -1,21 +1,38 @@ using System; using System.Collections.Generic; +using StructureMap.Util; namespace StructureMap.Pipeline { public class MainObjectCache : IObjectCache { private readonly object _locker = new object(); - private readonly IDictionary<InstanceKey, object> _objects = new Dictionary<InstanceKey, object>(); + private readonly Cache<InstanceKey, object> _objects = new Cache<InstanceKey, object>(); public object Locker { get { return _locker; } } public int Count { get { return _objects.Count; } } + public bool Has(Type pluginType, Instance instance) + { + var key = new InstanceKey(instance, pluginType); + return _objects.Has(key); + } + + public void Eject(Type pluginType, Instance instance) + { + var key = new InstanceKey(instance, pluginType); + if (!_objects.Has(key)) return; + + var disposable = _objects[key] as IDisposable; + _objects.Remove(key); + disposeObject(disposable); + } + public object Get(Type pluginType, Instance instance) { var key = new InstanceKey(instance, pluginType); - return _objects.ContainsKey(key) ? _objects[key] : null; + return _objects.Has(key) ? _objects[key] : null; } public void Set(Type pluginType, Instance instance, object value) @@ -25,7 +42,7 @@ try { var key = new InstanceKey(instance, pluginType); - _objects.Add(key, value); + _objects[key] = value; } catch (ArgumentException e) { @@ -39,25 +56,29 @@ { lock (Locker) { - foreach (object @object in _objects.Values) + _objects.Each(@object => { - if (@object is Container) continue; + if (@object is Container) return; - var disposable = @object as IDisposable; - if (disposable != null) - { - try - { - disposable.Dispose(); - } - catch (Exception) - { - } - } - } + disposeObject(@object as IDisposable); + }); _objects.Clear(); } } + + private void disposeObject(IDisposable disposable) + { + if (disposable != null) + { + try + { + disposable.Dispose(); + } + catch (Exception) + { + } + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/NulloObjectCache.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/NulloObjectCache.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/Pipeline/NulloObjectCache.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -8,6 +8,15 @@ public int Count { get { return 0; } } + public bool Has(Type pluginType, Instance instance) + { + return false; + } + + public void Eject(Type pluginType, Instance instance) + { + } + public object Get(Type pluginType, Instance instance) { return null; Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-27 15:59:19 UTC (rev 304) @@ -367,6 +367,7 @@ <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="IModel.cs" /> <Compile Include="Pipeline\Arguments.cs" /> <Compile Include="Pipeline\ArrayCoercion.cs" /> <Compile Include="Pipeline\ConditionalInstance.cs" /> Modified: trunk/Source/StructureMap.Testing/ModelQueryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -5,6 +5,7 @@ using StructureMap.Graph; using StructureMap.Testing.GenericWidgets; using StructureMap.Testing.Graph; +using StructureMap.Testing.Widget; namespace StructureMap.Testing { @@ -30,7 +31,7 @@ PluginGraph graph = registry.Build(); var pipeline = new PipelineGraph(graph); - _model = new Model(pipeline); + _model = new Container(graph).Model; _container = new Container(graph); } @@ -54,7 +55,7 @@ } - private Model _model; + private IModel _model; private Container _container; @@ -67,6 +68,16 @@ } [Test] + public void the_default_type_for() + { + _model.DefaultTypeFor<ISomething>().ShouldEqual(typeof (SomethingOne)); + _model.DefaultTypeFor<IWidget>().ShouldBeNull(); + + _model.DefaultTypeFor(typeof (IService<>)).ShouldBeNull(); + _model.DefaultTypeFor(typeof(ISomething)).ShouldEqual(typeof(SomethingOne)); + } + + [Test] public void HasImplementationFor_w_container() { _container.Model.HasDefaultImplementationFor(typeof (ISomething)).ShouldBeTrue(); @@ -74,6 +85,8 @@ _container.Model.HasDefaultImplementationFor(typeof (IServiceProvider)).ShouldBeFalse(); } + + [Test] public void HasImplementationsFor() { @@ -109,7 +122,8 @@ [Test] public void Iterate_over_pluginTypes() { - _model.PluginTypes.Count().ShouldEqual(3); + // 3 registered plus the 4th is the IContainer itself + _model.PluginTypes.Count().ShouldEqual(4); } [Test] Added: trunk/Source/StructureMap.Testing/Pipeline/MainObjectCacheTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/MainObjectCacheTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/MainObjectCacheTester.cs 2009-12-27 15:59:19 UTC (rev 304) @@ -0,0 +1,69 @@ +using System; +using NUnit.Framework; +using Rhino.Mocks; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class MainObjectCacheTester + { + private MainObjectCache cache; + + [SetUp] + public void SetUp() + { + cache = new MainObjectCache(); + } + + [Test] + public void has() + { + var widget = new AWidget(); + var instance = new ObjectInstance(widget); + + cache.Has(typeof(IWidget), instance).ShouldBeFalse(); + + cache.Set(typeof(Rule), instance, widget); + + cache.Has(typeof(IWidget), instance).ShouldBeFalse(); + + cache.Set(typeof(IWidget), new ObjectInstance(new AWidget()), widget); + + cache.Has(typeof(IWidget), instance).ShouldBeFalse(); + + cache.Set(typeof(IWidget), instance, widget); + + cache.Has(typeof(IWidget), instance).ShouldBeTrue(); + } + + [Test] + public void eject_a_disposable_object() + { + var disposable = MockRepository.GenerateMock<IDisposable>(); + var instance = new ObjectInstance(disposable); + + cache.Set(typeof(IWidget), instance, disposable); + + cache.Eject(typeof(IWidget), instance); + + cache.Has(typeof(IWidget), instance).ShouldBeFalse(); + + disposable.AssertWasCalled(x => x.Dispose()); + } + + [Test] + public void eject_a_non_disposable_object() + { + var widget = new AWidget(); + var instance = new ObjectInstance(widget); + + cache.Set(typeof(IWidget), instance, widget); + + cache.Eject(typeof(IWidget), instance); + + cache.Has(typeof(IWidget), instance).ShouldBeFalse(); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-26 20:51:05 UTC (rev 303) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-27 15:59:19 UTC (rev 304) @@ -364,6 +364,7 @@ <Compile Include="Pipeline\GenericsHelperExpressionTester.cs" /> <Compile Include="Pipeline\HybridBuildLifecycleTester.cs" /> <Compile Include="Pipeline\InstanceTester.cs" /> + <Compile Include="Pipeline\MainObjectCacheTester.cs" /> <Compile Include="Pipeline\ObjectInstanceTester.cs" /> <Compile Include="Pipeline\MissingInstanceTester.cs" /> <Compile Include="Pipeline\NestedContainerSupportTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-27 16:51:33
|
Revision: 305 http://structuremap.svn.sourceforge.net/structuremap/?rev=305&view=rev Author: jeremydmiller Date: 2009-12-27 16:51:25 +0000 (Sun, 27 Dec 2009) Log Message: ----------- Some refactoring in preparation for redoing the "Model" query system Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IContainer.cs trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/PluginTypeConfiguration.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.DebuggerVisualizers/ContainerVisualizerObjectSource.cs trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs trunk/Source/StructureMap.Testing/ModelQueryTester.cs trunk/Source/StructureMap.Testing/PipelineGraphTester.cs Added Paths: ----------- trunk/Source/StructureMap/Query/ trunk/Source/StructureMap/Query/IModel.cs trunk/Source/StructureMap/Query/Model.cs Removed Paths: ------------- trunk/Source/StructureMap/IModel.cs trunk/Source/StructureMap/Model.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Container.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -8,6 +8,7 @@ using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.TypeRules; namespace StructureMap Modified: trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -6,6 +6,7 @@ using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap.Diagnostics { @@ -14,7 +15,7 @@ private readonly Stack<BuildDependency> _dependencyStack = new Stack<BuildDependency>(); private readonly List<ValidationError> _validationErrors = new List<ValidationError>(); private ErrorCollection _errors; - private List<IInstance> _explicitInstances; + private List<Instance> _explicitInstances; public ValidationBuildSession(PipelineGraph pipelineGraph, InterceptorLibrary interceptorLibrary) : base(pipelineGraph, interceptorLibrary, new NulloObjectCache()) Modified: trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -2,6 +2,7 @@ using System.IO; using System.Text; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.TypeRules; namespace StructureMap.Diagnostics Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -3,6 +3,7 @@ using System.Linq; using StructureMap.Attributes; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.TypeRules; using StructureMap.Util; @@ -324,6 +325,7 @@ }; } + [Obsolete("replace with Instances")] public void ForInstance(string name, Action<Instance> action) { _instances.WithValue(name, action); @@ -338,7 +340,9 @@ public int InstanceCount { get { return _instances.Count; } } + [Obsolete] public IEnumerable<IInstance> Instances { get { return _instances.GetAll(); } } + public Instance MissingInstance { get; set; } /// <summary> Modified: trunk/Source/StructureMap/IContainer.cs =================================================================== --- trunk/Source/StructureMap/IContainer.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/IContainer.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap { Deleted: trunk/Source/StructureMap/IModel.cs =================================================================== --- trunk/Source/StructureMap/IModel.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/IModel.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using StructureMap.Pipeline; - -namespace StructureMap -{ - /// <summary> - /// Models the state of a Container or ObjectFactory. Can be used to query for the - /// existence of types registered with StructureMap - /// </summary> - public interface IModel - { - /// <summary> - /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations - /// </summary> - IEnumerable<PluginTypeConfiguration> PluginTypes { get; } - - IEnumerable<IInstance> AllInstances { get; } - - /// <summary> - /// Can StructureMap fulfill a request to ObjectFactory.GetInstance(pluginType) from the - /// current configuration. This does not include concrete classes that could be auto-configured - /// upon demand - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - bool HasDefaultImplementationFor(Type pluginType); - - /// <summary> - /// Can StructureMap fulfill a request to ObjectFactory.GetInstance<T>() from the - /// current configuration. This does not include concrete classes that could be auto-configured - /// upon demand - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - bool HasDefaultImplementationFor<T>(); - - /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - IEnumerable<IInstance> InstancesOf(Type pluginType); - - /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType - /// </summary> - /// <returns></returns> - IEnumerable<IInstance> InstancesOf<T>(); - - /// <summary> - /// Does the current container have existing configuration for the "pluginType" - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - bool HasImplementationsFor(Type pluginType); - - /// <summary> - /// Does the current container have existing configuration for the type T - /// </summary> - /// <returns></returns> - bool HasImplementationsFor<T>(); - - /// <summary> - /// Find the concrete type for the default Instance of T. - /// In other words, when I call Container.GetInstance(Type), - /// what do I get? May be indeterminate - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - Type DefaultTypeFor<T>(); - - /// <summary> - /// Find the concrete type for the default Instance of pluginType. - /// In other words, when I call Container.GetInstance(Type), - /// what do I get? May be indeterminate - /// </summary> - /// <returns></returns> - Type DefaultTypeFor(Type pluginType); - } -} \ No newline at end of file Deleted: trunk/Source/StructureMap/Model.cs =================================================================== --- trunk/Source/StructureMap/Model.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Model.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -1,85 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using StructureMap.Pipeline; - -namespace StructureMap -{ - public class Model : IModel - { - private readonly PipelineGraph _graph; - private readonly IContainer _container; - - internal Model(PipelineGraph graph, IContainer container) - { - _graph = graph; - _container = container; - } - - #region IModel Members - - public bool HasDefaultImplementationFor(Type pluginType) - { - return findForFamily(pluginType, f => f.Default != null); - } - - private T findForFamily<T>(Type pluginType, Func<PluginTypeConfiguration, T> func) - { - PluginTypeConfiguration family = PluginTypes.FirstOrDefault(x => x.PluginType == pluginType); - return family == null ? default(T) : func(family); - } - - public bool HasDefaultImplementationFor<T>() - { - return HasDefaultImplementationFor(typeof (T)); - } - - public Type DefaultTypeFor<T>() - { - return DefaultTypeFor(typeof (T)); - } - - public Type DefaultTypeFor(Type pluginType) - { - return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ConcreteType); - } - - public IEnumerable<PluginTypeConfiguration> PluginTypes { get { return _graph.PluginTypes; } } - - public IEnumerable<IInstance> InstancesOf(Type pluginType) - { - return _graph.InstancesOf(pluginType); - } - - public IEnumerable<IInstance> InstancesOf<T>() - { - return _graph.InstancesOf(typeof (T)); - } - - public bool HasImplementationsFor(Type pluginType) - { - return _graph.InstancesOf(pluginType).Count() > 0; - } - - public bool HasImplementationsFor<T>() - { - return HasImplementationsFor(typeof (T)); - } - - public IEnumerable<IInstance> AllInstances - { - get - { - foreach (PluginTypeConfiguration pluginType in PluginTypes) - { - foreach (IInstance instance in pluginType.Instances) - { - yield return instance; - } - } - } - } - - #endregion - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/ObjectFactory.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -3,6 +3,7 @@ using System.Collections.Generic; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap { Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -2,23 +2,12 @@ using StructureMap.Diagnostics; using StructureMap.Graph; using StructureMap.Interceptors; +using StructureMap.Query; namespace StructureMap.Pipeline { - public interface IInstance - { - string Name { get; } - /// <summary> - /// The actual concrete type of this Instance. Not every type of IInstance - /// can determine the ConcreteType - /// </summary> - Type ConcreteType { get; } - - string Description { get; } - } - public interface IDiagnosticInstance : IInstance { bool CanBePartOfPluginFamily(PluginFamily family); Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -4,6 +4,7 @@ using StructureMap.Diagnostics; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap { @@ -212,6 +213,7 @@ _profileManager.EjectAllInstancesOf<T>(); } + [Obsolete("Move this to PluginTypeConfiguration")] public IEnumerable<IInstance> InstancesOf(Type pluginType) { if (_genericsGraph.HasFamily(pluginType)) @@ -227,16 +229,9 @@ return new IInstance[0]; } - public List<IInstance> GetAllInstances() + public List<Instance> GetAllInstances() { - var list = new List<IInstance>(); - - foreach (var pair in _factories) - { - list.AddRange(pair.Value.AllInstances); - } - - return list; + return _factories.Values.SelectMany(x => x.AllInstances).ToList(); } public bool HasDefaultForPluginType(Type pluginType) Modified: trunk/Source/StructureMap/PluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/PluginTypeConfiguration.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/PluginTypeConfiguration.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap { @@ -13,7 +14,7 @@ /// <summary> /// The "instance" that will be used when Container.GetInstance(PluginType) is called. - /// See <see cref="StructureMap.Pipeline.IInstance">IInstance</see> for more information + /// See <see cref="IInstance">IInstance</see> for more information /// </summary> public IInstance Default { get; set; } @@ -23,7 +24,7 @@ public ILifecycle Lifecycle { get; set; } /// <summary> - /// All of the <see cref="StructureMap.Pipeline.IInstance">IInstance</see>'s registered + /// All of the <see cref="IInstance">IInstance</see>'s registered /// for this PluginType /// </summary> public IEnumerable<IInstance> Instances { get; set; } Copied: trunk/Source/StructureMap/Query/IModel.cs (from rev 304, trunk/Source/StructureMap/IModel.cs) =================================================================== --- trunk/Source/StructureMap/Query/IModel.cs (rev 0) +++ trunk/Source/StructureMap/Query/IModel.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + + public interface IInstance + { + string Name { get; } + + /// <summary> + /// The actual concrete type of this Instance. Not every type of IInstance + /// can determine the ConcreteType + /// </summary> + Type ConcreteType { get; } + + + string Description { get; } + } + + /// <summary> + /// Models the state of a Container or ObjectFactory. Can be used to query for the + /// existence of types registered with StructureMap + /// </summary> + public interface IModel + { + /// <summary> + /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations + /// </summary> + IEnumerable<PluginTypeConfiguration> PluginTypes { get; } + + IEnumerable<IInstance> AllInstances { get; } + + /// <summary> + /// Can StructureMap fulfill a request to ObjectFactory.GetInstance(pluginType) from the + /// current configuration. This does not include concrete classes that could be auto-configured + /// upon demand + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + bool HasDefaultImplementationFor(Type pluginType); + + /// <summary> + /// Can StructureMap fulfill a request to ObjectFactory.GetInstance<T>() from the + /// current configuration. This does not include concrete classes that could be auto-configured + /// upon demand + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + bool HasDefaultImplementationFor<T>(); + + /// <summary> + /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + IEnumerable<IInstance> InstancesOf(Type pluginType); + + /// <summary> + /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// </summary> + /// <returns></returns> + IEnumerable<IInstance> InstancesOf<T>(); + + /// <summary> + /// Does the current container have existing configuration for the "pluginType" + /// </summary> + /// <param name="pluginType"></param> + /// <returns></returns> + bool HasImplementationsFor(Type pluginType); + + /// <summary> + /// Does the current container have existing configuration for the type T + /// </summary> + /// <returns></returns> + bool HasImplementationsFor<T>(); + + /// <summary> + /// Find the concrete type for the default Instance of T. + /// In other words, when I call Container.GetInstance(Type), + /// what do I get? May be indeterminate + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + Type DefaultTypeFor<T>(); + + /// <summary> + /// Find the concrete type for the default Instance of pluginType. + /// In other words, when I call Container.GetInstance(Type), + /// what do I get? May be indeterminate + /// </summary> + /// <returns></returns> + Type DefaultTypeFor(Type pluginType); + } +} \ No newline at end of file Copied: trunk/Source/StructureMap/Query/Model.cs (from rev 304, trunk/Source/StructureMap/Model.cs) =================================================================== --- trunk/Source/StructureMap/Query/Model.cs (rev 0) +++ trunk/Source/StructureMap/Query/Model.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + public class Model : IModel + { + private readonly PipelineGraph _graph; + private readonly IContainer _container; + + internal Model(PipelineGraph graph, IContainer container) + { + _graph = graph; + _container = container; + } + + #region IModel Members + + public bool HasDefaultImplementationFor(Type pluginType) + { + return findForFamily(pluginType, f => f.Default != null); + } + + private T findForFamily<T>(Type pluginType, Func<PluginTypeConfiguration, T> func) + { + PluginTypeConfiguration family = PluginTypes.FirstOrDefault(x => x.PluginType == pluginType); + return family == null ? default(T) : func(family); + } + + public bool HasDefaultImplementationFor<T>() + { + return HasDefaultImplementationFor(typeof (T)); + } + + public Type DefaultTypeFor<T>() + { + return DefaultTypeFor(typeof (T)); + } + + public Type DefaultTypeFor(Type pluginType) + { + return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ConcreteType); + } + + public IEnumerable<PluginTypeConfiguration> PluginTypes { get { return _graph.PluginTypes; } } + + public IEnumerable<IInstance> InstancesOf(Type pluginType) + { + return _graph.InstancesOf(pluginType); + } + + public IEnumerable<IInstance> InstancesOf<T>() + { + return _graph.InstancesOf(typeof (T)); + } + + public bool HasImplementationsFor(Type pluginType) + { + return _graph.InstancesOf(pluginType).Count() > 0; + } + + public bool HasImplementationsFor<T>() + { + return HasImplementationsFor(typeof (T)); + } + + public IEnumerable<IInstance> AllInstances + { + get + { + foreach (PluginTypeConfiguration pluginType in PluginTypes) + { + foreach (IInstance instance in pluginType.Instances) + { + yield return instance; + } + } + } + } + + #endregion + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-27 16:51:25 UTC (rev 305) @@ -367,7 +367,7 @@ <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> <Compile Include="IContext.cs" /> - <Compile Include="IModel.cs" /> + <Compile Include="Query\IModel.cs" /> <Compile Include="Pipeline\Arguments.cs" /> <Compile Include="Pipeline\ArrayCoercion.cs" /> <Compile Include="Pipeline\ConditionalInstance.cs" /> @@ -392,7 +392,7 @@ <Compile Include="TypeExtensions.cs" /> <Compile Include="IBootstrapper.cs" /> <Compile Include="InitializationExpression.cs" /> - <Compile Include="Model.cs" /> + <Compile Include="Query\Model.cs" /> <Compile Include="Pipeline\BuildFrame.cs" /> <Compile Include="Pipeline\BuildStack.cs" /> <Compile Include="Pipeline\ConfiguredInstanceBase.cs" /> Modified: trunk/Source/StructureMap.DebuggerVisualizers/ContainerVisualizerObjectSource.cs =================================================================== --- trunk/Source/StructureMap.DebuggerVisualizers/ContainerVisualizerObjectSource.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap.DebuggerVisualizers/ContainerVisualizerObjectSource.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -4,6 +4,7 @@ using System.Linq; using Microsoft.VisualStudio.DebuggerVisualizers; using StructureMap.Pipeline; +using StructureMap.Query; namespace StructureMap.DebuggerVisualizers { Modified: trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -6,6 +6,7 @@ using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.Testing.DocumentationExamples; using StructureMap.Testing.Widget; using StructureMap.Testing.Widget3; Modified: trunk/Source/StructureMap.Testing/ModelQueryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -3,6 +3,7 @@ using NUnit.Framework; using StructureMap.Configuration.DSL; using StructureMap.Graph; +using StructureMap.Query; using StructureMap.Testing.GenericWidgets; using StructureMap.Testing.Graph; using StructureMap.Testing.Widget; Modified: trunk/Source/StructureMap.Testing/PipelineGraphTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/PipelineGraphTester.cs 2009-12-27 15:59:19 UTC (rev 304) +++ trunk/Source/StructureMap.Testing/PipelineGraphTester.cs 2009-12-27 16:51:25 UTC (rev 305) @@ -4,6 +4,7 @@ using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.Testing.GenericWidgets; using StructureMap.Testing.Graph; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |