From: <jer...@us...> - 2009-12-28 01:17:27
|
Revision: 306 http://structuremap.svn.sourceforge.net/structuremap/?rev=306&view=rev Author: jeremydmiller Date: 2009-12-28 01:17:17 +0000 (Mon, 28 Dec 2009) Log Message: ----------- The big "Model" overhaul Modified Paths: -------------- trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs trunk/Source/StructureMap/BuildSession.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/FamilyParser.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/IPluginFamily.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IContainer.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs trunk/Source/StructureMap/Pipeline/HttpLifecycleBase.cs trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs trunk/Source/StructureMap/Pipeline/ILifecycle.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/Pipeline/Lifecycles.cs trunk/Source/StructureMap/Pipeline/ObjectBuilder.cs trunk/Source/StructureMap/Pipeline/SingletonLifecycle.cs trunk/Source/StructureMap/Pipeline/ThreadLocalStorageLifecycle.cs trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/IModel.cs trunk/Source/StructureMap/Query/Model.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/SystemRegistry.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.DebuggerVisualizers/ContainerVisualizerObjectSource.cs trunk/Source/StructureMap.Testing/Attributes/PluginFamilyAttributeTester.cs trunk/Source/StructureMap.Testing/Bugs/HttpSessionNullRefBug.cs trunk/Source/StructureMap.Testing/Bugs/SingletonShouldBeLazy.cs trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeInConfigureTester.cs trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeOnOpenGenericsTester.cs trunk/Source/StructureMap.Testing/BuildSessionTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs trunk/Source/StructureMap.Testing/Configuration/FamilyParserTester.cs trunk/Source/StructureMap.Testing/Examples.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/AssemblyScannerTester.cs trunk/Source/StructureMap.Testing/Graph/ContainerTester.cs trunk/Source/StructureMap.Testing/Graph/FullStackFacadeTester.cs trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs trunk/Source/StructureMap.Testing/Pipeline/HybridBuildLifecycleTester.cs trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs trunk/Source/StructureMap.Testing/Pipeline/ObjectBuilderTester.cs trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStorageLifecycleTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.GenericWidgets/Widgets.cs Added Paths: ----------- trunk/Source/StructureMap/InstanceScope.cs trunk/Source/StructureMap/Pipeline/HybridLifecycle.cs trunk/Source/StructureMap/Query/EmptyConfiguration.cs trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs trunk/Source/StructureMap/Query/IFamily.cs trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceRef.cs trunk/Source/StructureMap.Testing/Query/ trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/InstanceRefTester.cs trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs Removed Paths: ------------- trunk/Source/StructureMap/Attributes/InstanceScope.cs trunk/Source/StructureMap/PluginTypeConfiguration.cs trunk/Source/StructureMap.Testing/PipelineGraphTester.cs Deleted: trunk/Source/StructureMap/Attributes/InstanceScope.cs =================================================================== --- trunk/Source/StructureMap/Attributes/InstanceScope.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Attributes/InstanceScope.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,13 +0,0 @@ -namespace StructureMap.Attributes -{ - public enum InstanceScope - { - PerRequest, - Singleton, - ThreadLocal, - HttpContext, - Hybrid, - HttpSession, - HybridHttpSession - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs =================================================================== --- trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,5 +1,4 @@ using System; -using StructureMap.Attributes; using StructureMap.Graph; namespace StructureMap Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/BuildSession.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -16,9 +16,9 @@ private readonly PipelineGraph _pipelineGraph; protected BuildStack _buildStack = new BuildStack(); - public BuildSession(PipelineGraph pipelineGraph, InterceptorLibrary interceptorLibrary, IObjectCache cache) + public BuildSession(PipelineGraph pipelineGraph, InterceptorLibrary interceptorLibrary) { - _builder = new ObjectBuilder(pipelineGraph, interceptorLibrary, cache); + _builder = new ObjectBuilder(pipelineGraph, interceptorLibrary); _pipelineGraph = pipelineGraph; _defaults = new Cache<Type, Func<object>>(t => @@ -35,7 +35,7 @@ } public BuildSession(PluginGraph graph) - : this(new PipelineGraph(graph), graph.InterceptorLibrary, new NulloObjectCache()) + : this(new PipelineGraph(graph), graph.InterceptorLibrary) { } Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using StructureMap.Attributes; using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; @@ -358,5 +357,18 @@ { return LifecycleIs(new UniquePerRequestLifecycle()); } + + /// <summary> + /// Adds the object to to the PLUGINTYPE + /// </summary> + /// <param name="object"></param> + /// <returns></returns> + public ObjectInstance Add(PLUGINTYPE @object) + { + var instance = new ObjectInstance(@object); + _alterations.Add(f => f.AddInstance(instance)); + + return instance; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,5 +1,4 @@ using System; -using StructureMap.Attributes; using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -103,7 +103,7 @@ /// </summary> /// <typeparam name="PLUGGEDTYPE"></typeparam> /// <returns></returns> - [Obsolete("Favor For<ISomething>().Use<Something>()")] + [Obsolete("Favor For<ISomething>().Use<Something>() or For<ISomething>().Add<Something>()")] SmartInstance<PLUGGEDTYPE> OfConcreteType<PLUGGEDTYPE>() where PLUGGEDTYPE : T; /// <summary> Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,6 +1,5 @@ using System; using System.Xml; -using StructureMap.Attributes; using StructureMap.Graph; using StructureMap.Pipeline; using StructureMap.Source; Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Container.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -18,7 +18,6 @@ private InterceptorLibrary _interceptorLibrary; private PipelineGraph _pipelineGraph; private PluginGraph _pluginGraph; - private IObjectCache _transientCache = new NulloObjectCache(); public Container(Action<ConfigurationExpression> action) { @@ -65,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, this); } } + public IModel Model { get { return new Model(_pipelineGraph.GetPluginTypes(this)); } } /// <summary> /// Creates or finds the named instance of T @@ -442,8 +441,7 @@ var container = new Container { _interceptorLibrary = _interceptorLibrary, - _pipelineGraph = _pipelineGraph.Clone(), - _transientCache = new MainObjectCache() + _pipelineGraph = _pipelineGraph.ToNestedGraph(), }; // Fixes a mild bug. The child container should inject itself @@ -467,61 +465,12 @@ public void Dispose() { - _transientCache.DisposeAndClear(); _pipelineGraph.Dispose(); } #endregion - /// <summary> - /// Injects the given object into a Container by name for the designated - /// pluginType. Mostly used for temporarily setting up return values of the Container - /// to introduce mocks or stubs during automated testing scenarios - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="name"></param> - /// <param name="object"></param> - public void Inject<T>(string name, T @object) - { - ObjectInstance instance = new ObjectInstance(@object).WithName(name); - _transientCache.Set(typeof (T), instance, @object); - _pipelineGraph.AddInstance<T>(instance); - } - /// <summary> - /// Injects the given object into a Container as the default for the designated - /// pluginType. Mostly used for temporarily setting up return values of the Container - /// to introduce mocks or stubs during automated testing scenarios - /// </summary> - /// <param name="pluginType"></param> - /// <param name="object"></param> - public void Inject(Type pluginType, object @object) - { - if (!@object.GetType().CanBeCastTo(pluginType)) - { - throw new StructureMapException(220, pluginType.FullName, - @object.GetType().FullName); - } - - - var instance = new ObjectInstance(@object); - _transientCache.Set(pluginType, instance, @object); - _pipelineGraph.SetDefault(pluginType, instance); - } - - /// <summary> - /// Injects the given object into a Container as the default for the designated - /// PLUGINTYPE. Mostly used for temporarily setting up return values of the Container - /// to introduce mocks or stubs during automated testing scenarios - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - /// <param name="object"></param> - public void Inject<PLUGINTYPE>(PLUGINTYPE @object) - { - Instance instance = _pipelineGraph.Inject(@object); - _transientCache.Set(typeof (PLUGINTYPE), instance, @object); - } - private object buildInstanceWithArgs(Type pluginType, Instance defaultInstance, ExplicitArguments args, string requestedName) { @@ -561,11 +510,16 @@ } _pluginGraph = pluginGraph; + + var thisInstance = new ObjectInstance(this); + _pluginGraph.FindFamily(typeof(IContainer)).AddInstance(thisInstance); + _pluginGraph.ProfileManager.SetDefault(typeof(IContainer), thisInstance); + pluginGraph.Log.AssertFailures(); _pipelineGraph = new PipelineGraph(pluginGraph); - _pipelineGraph.Inject<IContainer>(this); + } [Obsolete("delegate to something cleaner in BuildSession")] @@ -583,7 +537,7 @@ private BuildSession withNewSession(string name) { - return new BuildSession(_pipelineGraph, _interceptorLibrary, _transientCache) + return new BuildSession(_pipelineGraph, _interceptorLibrary) { RequestedName = name }; @@ -644,5 +598,31 @@ } #endregion + + /// <summary> + /// Injects the given object into a Container as the default for the designated + /// PLUGINTYPE. Mostly used for temporarily setting up return values of the Container + /// to introduce mocks or stubs during automated testing scenarios + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <param name="instance"></param> + public void Inject<PLUGINTYPE>(PLUGINTYPE instance) + { + Configure(x => x.For<PLUGINTYPE>().Use(instance)); + } + + /// <summary> + /// Injects the given object into a Container as the default for the designated + /// pluginType. Mostly used for temporarily setting up return values of the Container + /// to introduce mocks or stubs during automated testing scenarios + /// </summary> + /// <param name="pluginType"></param> + /// <param name="object"></param> + public void Inject(Type pluginType, object @object) + { + Configure(x => x.For(pluginType).Use(@object)); + } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Diagnostics/ValidationBuildSession.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -18,7 +18,7 @@ private List<Instance> _explicitInstances; public ValidationBuildSession(PipelineGraph pipelineGraph, InterceptorLibrary interceptorLibrary) - : base(pipelineGraph, interceptorLibrary, new NulloObjectCache()) + : base(pipelineGraph, interceptorLibrary) { } @@ -104,14 +104,11 @@ _explicitInstances = pipelineGraph.GetAllInstances(); _errors = new ErrorCollection(); - foreach (PluginTypeConfiguration pluginType in pipelineGraph.PluginTypes) + pipelineGraph.EachInstance((t, i) => { - foreach (Instance instance in pluginType.Instances) - { - _buildStack = new BuildStack(); - validateInstance(pluginType.PluginType, instance); - } - } + _buildStack = new BuildStack(); + validateInstance(t, i); + }); } public string BuildErrorMessages() Modified: trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -10,7 +10,7 @@ public class WhatDoIHaveWriter { private readonly PipelineGraph _graph; - private List<IInstance> _instances; + private List<InstanceRef> _instances; private TextReportWriter _writer; public WhatDoIHaveWriter(PipelineGraph graph) @@ -32,12 +32,12 @@ private void writeContentsOfPluginTypes(StringWriter stringWriter) { _writer = new TextReportWriter(3); - _instances = new List<IInstance>(); + _instances = new List<InstanceRef>(); _writer.AddDivider('='); _writer.AddText("PluginType", "Name", "Description"); - foreach (PluginTypeConfiguration pluginType in _graph.PluginTypes) + foreach (IPluginTypeConfiguration pluginType in _graph.GetPluginTypes(null)) { writePluginType(pluginType); } @@ -64,7 +64,7 @@ writer.WriteLine(); } - private void writePluginType(PluginTypeConfiguration pluginType) + private void writePluginType(IPluginTypeConfiguration pluginType) { _writer.AddDivider('-'); var contents = new[] @@ -90,13 +90,13 @@ _writer.AddContent("Scoped as: PerRequest"); } - foreach (IInstance instance in pluginType.Instances) + foreach (InstanceRef instance in pluginType.Instances) { writeInstance(instance); } } - private void writeInstance(IInstance instance) + private void writeInstance(InstanceRef instance) { if (_instances.Contains(instance)) { @@ -110,7 +110,7 @@ } - private void setContents(string[] contents, IInstance instance) + private void setContents(string[] contents, InstanceRef instance) { contents[1] = instance.Name; contents[2] = instance.Description; Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.Util; namespace StructureMap.Graph @@ -16,14 +17,11 @@ public int FamilyCount { get { return _families.Count; } } - public IEnumerable<PluginTypeConfiguration> Families + public IEnumerable<PluginFamily> Families { get { - foreach (PluginFamily family in _families.GetAll()) - { - yield return family.GetConfiguration(); - } + return _families.GetAll(); } } Modified: trunk/Source/StructureMap/Graph/IPluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/IPluginFamily.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Graph/IPluginFamily.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,5 +1,4 @@ using System; -using StructureMap.Attributes; using StructureMap.Pipeline; namespace StructureMap.Graph Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using StructureMap.Attributes; using StructureMap.Pipeline; using StructureMap.Query; using StructureMap.TypeRules; @@ -57,6 +56,12 @@ public void SetScopeTo(InstanceScope scope) { + if (scope == InstanceScope.Transient) + { + _lifecycle = null; + return; + } + _lifecycle = Lifecycles.GetLifecycle(scope); } @@ -79,7 +84,13 @@ _instances[instance.Name] = instance; } + public void SetDefault(Instance instance) + { + AddInstance(instance); + DefaultInstanceKey = instance.Name; + } + // For testing public InstanceMemento GetMemento(string instanceKey) { @@ -156,9 +167,13 @@ return _instances.Exists(instance => instance.Matches(plugin)); } - public void EachInstance(Action<Instance> action) + + public IEnumerable<Instance> Instances { - _instances.Each(action); + get + { + return _instances.GetAll(); + } } public Instance GetInstance(string name) @@ -243,7 +258,8 @@ public void ImportFrom(PluginFamily source) { - source.EachInstance(instance => _instances.Fill(instance.Name, instance)); + source.Instances.Each(instance => _instances.Fill(instance.Name, instance)); + source._pluggedTypes.Each((key, plugin) => _pluggedTypes.Fill(key, plugin)); if (source.MissingInstance != null) @@ -314,18 +330,6 @@ } } - public PluginTypeConfiguration GetConfiguration() - { - return new PluginTypeConfiguration - { - Default = GetDefaultInstance(), - PluginType = PluginType, - Lifecycle = _lifecycle, - Instances = Instances - }; - } - - [Obsolete("replace with Instances")] public void ForInstance(string name, Action<Instance> action) { _instances.WithValue(name, action); @@ -340,9 +344,6 @@ public int InstanceCount { get { return _instances.Count; } } - [Obsolete] - public IEnumerable<IInstance> Instances { get { return _instances.GetAll(); } } - public Instance MissingInstance { get; set; } /// <summary> @@ -353,6 +354,7 @@ /// <summary> /// The InstanceKey of the default instance of the PluginFamily /// </summary> + [Obsolete] public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion Modified: trunk/Source/StructureMap/IContainer.cs =================================================================== --- trunk/Source/StructureMap/IContainer.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/IContainer.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -135,15 +135,6 @@ /// <param name="object"></param> void Inject(Type pluginType, object @object); - /// <summary> - /// Injects the given object into a Container by name for the designated - /// pluginType. Mostly used for temporarily setting up return values of the Container - /// to introduce mocks or stubs during automated testing scenarios - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="name"></param> - /// <param name="instance"></param> - void Inject<T>(string name, T instance); /// <summary> /// Sets the default instance for all PluginType's to the designated Profile. Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/InstanceFactory.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -42,7 +42,7 @@ _pluginType = family.PluginType; MissingInstance = family.MissingInstance; - family.EachInstance(AddInstance); + family.Instances.Each(AddInstance); } catch (StructureMapException) { @@ -105,8 +105,9 @@ _lifecycle = family.Lifecycle; } - family.EachInstance(instance => _instances.Fill(instance.Name, instance)); + family.Instances.Each(instance => _instances.Fill(instance.Name, instance)); + if (family.MissingInstance != null) { MissingInstance = family.MissingInstance; Copied: trunk/Source/StructureMap/InstanceScope.cs (from rev 302, trunk/Source/StructureMap/Attributes/InstanceScope.cs) =================================================================== --- trunk/Source/StructureMap/InstanceScope.cs (rev 0) +++ trunk/Source/StructureMap/InstanceScope.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,15 @@ +namespace StructureMap +{ + public enum InstanceScope + { + PerRequest, + Singleton, + ThreadLocal, + HttpContext, + Hybrid, + HttpSession, + HybridHttpSession, + Unique, + Transient + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/ObjectFactory.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -63,24 +63,6 @@ } - [Obsolete("Please use GetInstance(Type) instead")] - public static object FillDependencies(Type type) - { - return container.FillDependencies(type); - } - - [Obsolete("Please use GetInstance<T>() instead")] - public static T FillDependencies<T>() - { - return (T) container.FillDependencies(typeof (T)); - } - - [Obsolete("Please use Inject(Type, object) instead.")] - public static void InjectStub(Type pluginType, object stub) - { - Inject(pluginType, stub); - } - /// <summary> /// Injects the given object into a Container as the default for the designated /// pluginType. Mostly used for temporarily setting up return values of the Container @@ -93,12 +75,6 @@ container.Inject(pluginType, instance); } - [Obsolete("Please use Inject() instead.")] - public static void InjectStub<PLUGINTYPE>(PLUGINTYPE stub) - { - Inject(stub); - } - /// <summary> /// Injects the given object into a Container as the default for the designated /// PLUGINTYPE. Mostly used for temporarily setting up return values of the Container @@ -111,25 +87,7 @@ container.Inject(instance); } - /// <summary> - /// Injects the given object into a Container by name for the designated - /// pluginType. Mostly used for temporarily setting up return values of the Container - /// to introduce mocks or stubs during automated testing scenarios - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - /// <param name="name"></param> - /// <param name="instance"></param> - public static void Inject<PLUGINTYPE>(string name, PLUGINTYPE instance) - { - container.Inject(name, instance); - } - [Obsolete("Please use Inject<PLUGINTYPE>(name) instead.")] - public static void InjectStub<PLUGINTYPE>(string name, PLUGINTYPE stub) - { - Inject(name, stub); - } - /// <summary> /// Returns a report detailing the complete configuration of all PluginTypes and Instances /// </summary> Modified: trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/HttpContextLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Web; @@ -34,6 +35,11 @@ return (IObjectCache) items[ITEM_NAME]; } + public string Scope + { + get { return InstanceScope.HttpContext.ToString(); } + } + public static bool HasContext() { return HttpContext.Current != null; Modified: trunk/Source/StructureMap/Pipeline/HttpLifecycleBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HttpLifecycleBase.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/HttpLifecycleBase.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { public abstract class HttpLifecycleBase<HTTP, NONHTTP> : ILifecycle @@ -25,9 +27,7 @@ ? _http.FindCache() : _nonHttp.FindCache(); } - } - public class HybridLifecycle : HttpLifecycleBase<HttpContextLifecycle, ThreadLocalStorageLifecycle> - { + public abstract string Scope { get;} } } \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/HybridLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HybridLifecycle.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/HybridLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,10 @@ +namespace StructureMap.Pipeline +{ + public class HybridLifecycle : HttpLifecycleBase<HttpContextLifecycle, ThreadLocalStorageLifecycle> + { + public override string Scope + { + get { return InstanceScope.Hybrid.ToString(); } + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/HybridSessionLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { public class HybridSessionLifecycle : HttpLifecycleBase<HttpSessionLifecycle, ThreadLocalStorageLifecycle> @@ -2,2 +4,6 @@ { + public override string Scope + { + get { return InstanceScope.HybridHttpSession.ToString(); } + } } Modified: trunk/Source/StructureMap/Pipeline/ILifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ILifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/ILifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,5 @@ +using StructureMap.Attributes; + namespace StructureMap.Pipeline { public interface ILifecycle @@ -4,5 +6,6 @@ { void EjectAll(); IObjectCache FindCache(); + string Scope { get; } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -2,13 +2,10 @@ using StructureMap.Diagnostics; using StructureMap.Graph; using StructureMap.Interceptors; -using StructureMap.Query; namespace StructureMap.Pipeline { - - - public interface IDiagnosticInstance : IInstance + public interface IDiagnosticInstance { bool CanBePartOfPluginFamily(PluginFamily family); Instance FindInstanceForProfile(PluginFamily family, string profileName, GraphLog log); @@ -38,7 +35,10 @@ #region IDiagnosticInstance Members public string Name { get { return _name; } set { _name = value; } } + internal Type ConcreteType { get { return getConcreteType(null); } } + internal string Description { get { return getDescription(); } } + bool IDiagnosticInstance.CanBePartOfPluginFamily(PluginFamily family) { return canBePartOfPluginFamily(family); @@ -59,10 +59,6 @@ preprocess(family); } - Type IInstance.ConcreteType { get { return getConcreteType(null); } } - - string IInstance.Description { get { return getDescription(); } } - #endregion public virtual object Build(Type pluginType, BuildSession session) Modified: trunk/Source/StructureMap/Pipeline/Lifecycles.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Lifecycles.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/Lifecycles.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,5 +1,4 @@ using System; -using StructureMap.Attributes; namespace StructureMap.Pipeline { @@ -29,6 +28,9 @@ case InstanceScope.HybridHttpSession: return new HybridSessionLifecycle(); + + case InstanceScope.Unique: + return new UniquePerRequestLifecycle(); } throw new ArgumentOutOfRangeException("scope"); Modified: trunk/Source/StructureMap/Pipeline/ObjectBuilder.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ObjectBuilder.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/ObjectBuilder.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -5,17 +5,15 @@ { public class ObjectBuilder { - private readonly IObjectCache _defaultCache; private readonly InterceptorLibrary _library; private readonly PipelineGraph _pipeline; - public ObjectBuilder(PipelineGraph pipeline, InterceptorLibrary library, IObjectCache defaultCache) + public ObjectBuilder(PipelineGraph pipeline, InterceptorLibrary library) { if (pipeline == null) throw new ArgumentNullException("pipeline"); _pipeline = pipeline; _library = library; - _defaultCache = defaultCache; } public object Resolve(Type pluginType, Instance instance, BuildSession session) @@ -59,10 +57,7 @@ public IObjectCache FindCache(Type pluginType, Instance instance, BuildSession session) { - ILifecycle lifecycle = _pipeline.ForType(pluginType).Lifecycle; - return lifecycle == null - ? _defaultCache - : lifecycle.FindCache(); + return _pipeline.FindCache(pluginType); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/SingletonLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SingletonLifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/SingletonLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { public class SingletonLifecycle : ILifecycle @@ -13,5 +15,10 @@ { return _cache; } + + public string Scope + { + get { return InstanceScope.Singleton.ToString(); } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ThreadLocalStorageLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ThreadLocalStorageLifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/ThreadLocalStorageLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -18,6 +18,11 @@ return _cache; } + public string Scope + { + get { return InstanceScope.ThreadLocal.ToString(); } + } + private void guaranteeHashExists() { if (_cache == null) Modified: trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Pipeline/UniquePerRequestLifecycle.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { /// <summary> @@ -14,39 +16,10 @@ return new NulloObjectCache(); } - //#region IBuildInterceptor Members + public string Scope + { + get { return InstanceScope.Unique.ToString(); } } - //private IBuildPolicy _innerPolicy = new BuildPolicy(); - //public IBuildPolicy InnerPolicy - //{ - // get { return _innerPolicy; } - // set { _innerPolicy = value; } - //} - - //#endregion - - //#region IBuildPolicy Members - - //public object Build(BuildSession buildSession, Type pluginType, Instance instance) - //{ - // //insert a default object creator - // buildSession.RegisterDefault(pluginType, () => InnerPolicy.Build(buildSession, pluginType, instance)); - - // //build this object for the first time - // return buildSession.CreateInstance(pluginType); - //} - - //public IBuildPolicy Clone() - //{ - // return new UniquePerRequestInterceptor(); - //} - - //public void EjectAll() - //{ - // InnerPolicy.EjectAll(); - //} - - //#endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -19,12 +19,14 @@ private readonly GenericsPluginGraph _genericsGraph = new GenericsPluginGraph(); private readonly GraphLog _log; private readonly ProfileManager _profileManager; + private readonly IObjectCache _transientCache; private MissingFactoryFunction _missingFactory = (pluginType, profileManager) => null; public PipelineGraph(PluginGraph graph) { + _transientCache = new NulloObjectCache(); _profileManager = graph.ProfileManager; _log = graph.Log; @@ -47,6 +49,7 @@ _profileManager = profileManager; _genericsGraph = genericsGraph; _log = log; + _transientCache = new MainObjectCache(); } public GraphLog Log { get { return _log; } } @@ -55,28 +58,16 @@ public string CurrentProfile { get { return _profileManager.CurrentProfile; } set { _profileManager.CurrentProfile = value; } } - public IEnumerable<PluginTypeConfiguration> PluginTypes + public IEnumerable<IPluginTypeConfiguration> GetPluginTypes(IContainer container) { - get + foreach (PluginFamily family in _genericsGraph.Families) { - foreach (PluginTypeConfiguration configuration in _genericsGraph.Families) - { - yield return configuration; - } + yield return new GenericFamilyConfiguration(family); + } - var factories = new IInstanceFactory[_factories.Count]; - _factories.Values.CopyTo(factories, 0); - - foreach (IInstanceFactory factory in factories) - { - yield return new PluginTypeConfiguration - { - Default = _profileManager.GetDefault(factory.PluginType), - PluginType = factory.PluginType, - Lifecycle = factory.Lifecycle, - Instances = factory.AllInstances - }; - } + foreach (IInstanceFactory factory in _factories.Values.ToArray()) + { + yield return new InstanceFactoryTypeConfiguration(factory.PluginType, container, this); } } @@ -101,9 +92,11 @@ _factories.Clear(); _profileManager.Dispose(); _genericsGraph.ClearAll(); + + _transientCache.DisposeAndClear(); } - public PipelineGraph Clone() + public PipelineGraph ToNestedGraph() { var clone = new PipelineGraph(_profileManager.Clone(), _genericsGraph.Clone(), _log) { @@ -198,37 +191,13 @@ ForType(typeof (T)).AddInstance(instance); } - public Instance Inject<PLUGINTYPE>(PLUGINTYPE instance) - { - var literalInstance = new ObjectInstance(instance); - ForType(typeof (PLUGINTYPE)).AddInstance(literalInstance); - SetDefault(typeof (PLUGINTYPE), literalInstance); - - return literalInstance; - } - public void EjectAllInstancesOf<T>() { ForType(typeof (T)).EjectAllInstances(); _profileManager.EjectAllInstancesOf<T>(); } - [Obsolete("Move this to PluginTypeConfiguration")] - public IEnumerable<IInstance> InstancesOf(Type pluginType) - { - if (_genericsGraph.HasFamily(pluginType)) - { - return _genericsGraph.FindFamily(pluginType).Instances; - } - if (_factories.ContainsKey(pluginType)) - { - return _factories[pluginType].AllInstances; - } - - return new IInstance[0]; - } - public List<Instance> GetAllInstances() { return _factories.Values.SelectMany(x => x.AllInstances).ToList(); @@ -249,5 +218,21 @@ { return ForType(pluginType).FindInstance(instanceKey) != null; } + + public void EachInstance(Action<Type, Instance> action) + { + _factories.Values.Each(f => + { + f.AllInstances.Each(i => action(f.PluginType, i)); + }); + } + + public IObjectCache FindCache(Type pluginType) + { + ILifecycle lifecycle = ForType(pluginType).Lifecycle; + return lifecycle == null + ? _transientCache + : lifecycle.FindCache(); + } } } \ No newline at end of file Deleted: trunk/Source/StructureMap/PluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/PluginTypeConfiguration.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/PluginTypeConfiguration.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using StructureMap.Pipeline; -using StructureMap.Query; - -namespace StructureMap -{ - /// <summary> - /// Metadata describing the registration of a PluginType - /// </summary> - public class PluginTypeConfiguration - { - public Type PluginType { get; set; } - - /// <summary> - /// The "instance" that will be used when Container.GetInstance(PluginType) is called. - /// See <see cref="IInstance">IInstance</see> for more information - /// </summary> - public IInstance Default { get; set; } - - /// <summary> - /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods - /// </summary> - public ILifecycle Lifecycle { get; set; } - - /// <summary> - /// All of the <see cref="IInstance">IInstance</see>'s registered - /// for this PluginType - /// </summary> - public IEnumerable<IInstance> Instances { get; set; } - } -} \ No newline at end of file Added: trunk/Source/StructureMap/Query/EmptyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/EmptyConfiguration.cs (rev 0) +++ trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Query +{ + public class EmptyConfiguration : IPluginTypeConfiguration + { + private readonly Type _pluginType; + + public EmptyConfiguration(Type pluginType) + { + _pluginType = pluginType; + } + + public Type PluginType + { + get { return _pluginType; } + } + + /// <summary> + /// The "instance" that will be used when Container.GetInstance(PluginType) is called. + /// See <see cref="InstanceRef">InstanceRef</see> for more information + /// </summary> + public InstanceRef Default + { + get { return null; } } + + /// <summary> + /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods + /// </summary> + public string Lifecycle + { + get { return null; } } + + /// <summary> + /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered + /// for this PluginType + /// </summary> + public IEnumerable<InstanceRef> Instances + { + get { return new InstanceRef[0]; } } + + /// <summary> + /// Simply query to see if there are any implementations registered + /// </summary> + /// <returns></returns> + public bool HasImplementations() + { + return false; + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs (rev 0) +++ trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StructureMap.Graph; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + public class GenericFamilyConfiguration : IPluginTypeConfiguration, IFamily + { + private readonly PluginFamily _family; + + public GenericFamilyConfiguration(PluginFamily family) + { + _family = family; + } + + void IFamily.Eject(Instance instance) + { + } + + object IFamily.Build(Instance instance) + { + return null; + } + + bool IFamily.HasBeenCreated(Instance instance) + { + return false; + } + + public Type PluginType { get { return _family.PluginType; } } + + /// <summary> + /// The "instance" that will be used when Container.GetInstance(PluginType) is called. + /// See <see cref="InstanceRef">InstanceRef</see> for more information + /// </summary> + public InstanceRef Default + { + get + { + Instance defaultInstance = _family.GetDefaultInstance(); + return defaultInstance == null ? null : new InstanceRef(defaultInstance, this); + } + } + + /// <summary> + /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods + /// </summary> + public string Lifecycle + { + get + { + return _family.Lifecycle.ToName(); + } + } + + /// <summary> + /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered + /// for this PluginType + /// </summary> + public IEnumerable<InstanceRef> Instances + { + get + { + return _family.Instances.Select(x => new InstanceRef(x, this)); + } + } + + /// <summary> + /// Simply query to see if there are any implementations registered + /// </summary> + /// <returns></returns> + public bool HasImplementations() + { + return _family.InstanceCount > 0; + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Query/IFamily.cs =================================================================== --- trunk/Source/StructureMap/Query/IFamily.cs (rev 0) +++ trunk/Source/StructureMap/Query/IFamily.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,13 @@ +using System; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + public interface IFamily + { + Type PluginType { get; } + void Eject(Instance instance); + object Build(Instance instance); + bool HasBeenCreated(Instance instance); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IModel.cs =================================================================== --- trunk/Source/StructureMap/Query/IModel.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Query/IModel.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -5,20 +5,8 @@ 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 @@ -28,9 +16,9 @@ /// <summary> /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations /// </summary> - IEnumerable<PluginTypeConfiguration> PluginTypes { get; } + IEnumerable<IPluginTypeConfiguration> PluginTypes { get; } - IEnumerable<IInstance> AllInstances { get; } + IEnumerable<InstanceRef> AllInstances { get; } /// <summary> /// Can StructureMap fulfill a request to ObjectFactory.GetInstance(pluginType) from the @@ -55,13 +43,13 @@ /// </summary> /// <param name="pluginType"></param> /// <returns></returns> - IEnumerable<IInstance> InstancesOf(Type pluginType); + IEnumerable<InstanceRef> 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>(); + IEnumerable<InstanceRef> InstancesOf<T>(); /// <summary> /// Does the current container have existing configuration for the "pluginType" @@ -92,5 +80,19 @@ /// </summary> /// <returns></returns> Type DefaultTypeFor(Type pluginType); + + /// <summary> + /// Retrieves the configuration for the given type + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + IPluginTypeConfiguration For<T>(); + + /// <summary> + /// Retrieves the configuration for the given type + /// </summary> + /// <param name="type"></param> + /// <returns></returns> + IPluginTypeConfiguration For(Type type); } } \ No newline at end of file Added: trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs (rev 0) +++ trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Query +{ + public interface IPluginTypeConfiguration + { + Type PluginType { get; } + + /// <summary> + /// The "instance" that will be used when Container.GetInstance(PluginType) is called. + /// See <see cref="InstanceRef">InstanceRef</see> for more information + /// </summary> + InstanceRef Default { get; } + + /// <summary> + /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods + /// </summary> + string Lifecycle { get; } + + /// <summary> + /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered + /// for this PluginType + /// </summary> + IEnumerable<InstanceRef> Instances { get; } + + /// <summary> + /// Simply query to see if there are any implementations registered + /// </summary> + /// <returns></returns> + bool HasImplementations(); + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs (rev 0) +++ trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + public class InstanceFactoryTypeConfiguration : IPluginTypeConfiguration, IFamily + { + private readonly IContainer _container; + private readonly PipelineGraph _graph; + private readonly Type _pluginType; + + public InstanceFactoryTypeConfiguration(Type pluginType, IContainer container, PipelineGraph graph) + { + _pluginType = pluginType; + _container = container; + _graph = graph; + } + + private IObjectCache cache { get { return _graph.FindCache(_pluginType); } } + private IInstanceFactory factory { get { return _graph.ForType(_pluginType); } } + + void IFamily.Eject(Instance instance) + { + cache.Eject(_pluginType, instance); + } + + object IFamily.Build(Instance instance) + { + // It's important to use this overload to get it to + // respect the lifecycle. I think. It works, so leave + // it alone. + return _container.GetInstance(_pluginType, instance.Name); + } + + bool IFamily.HasBeenCreated(Instance instance) + { + return cache.Has(_pluginType, instance); + } + + public Type PluginType { get { return _pluginType; } } + + /// <summary> + /// The "instance" that will be used when Container.GetInstance(PluginType) is called. + /// See <see cref="InstanceRef">InstanceRef</see> for more information + /// </summary> + public InstanceRef Default + { + get + { + Instance instance = _graph.GetDefault(_pluginType); + return toRef(instance); + } + } + + /// <summary> + /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods + /// </summary> + public string Lifecycle { get { return factory.Lifecycle.ToName(); } } + + /// <summary> + /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered + /// for this PluginType + /// </summary> + public IEnumerable<InstanceRef> Instances { get { return factory.AllInstances.Select(x => toRef(x)); } } + + /// <summary> + /// Simply query to see if there are any implementations registered + /// </summary> + /// <returns></returns> + public bool HasImplementations() + { + return factory.AllInstances.Any(); + } + + private InstanceRef toRef(Instance instance) + { + if (instance == null) return null; + + return new InstanceRef(instance, this); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Query/InstanceRef.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceRef.cs (rev 0) +++ trunk/Source/StructureMap/Query/InstanceRef.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -0,0 +1,45 @@ +using System; +using StructureMap.Pipeline; + +namespace StructureMap.Query +{ + public class InstanceRef + { + private readonly IFamily _family; + private readonly Instance _instance; + + public InstanceRef(Instance instance, IFamily family) + { + _instance = instance; + _family = family; + } + + + public string Name { get { return _instance.Name; } } + + /// <summary> + /// The actual concrete type of this Instance. Not every type of IInstance + /// can determine the ConcreteType + /// </summary> + public Type ConcreteType { get { return _instance.ConcreteType; } } + + + public string Description { get { return _instance.Description; } } + public Type PluginType { get { return _family.PluginType; } } + + public void EjectObject() + { + _family.Eject(_instance); + } + + public T Get<T>() where T : class + { + return _family.Build(_instance) as T; + } + + public bool ObjectHasBeenCreated() + { + return _family.HasBeenCreated(_instance); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Query/Model.cs =================================================================== --- trunk/Source/StructureMap/Query/Model.cs 2009-12-27 16:51:25 UTC (rev 305) +++ trunk/Source/StructureMap/Query/Model.cs 2009-12-28 01:17:17 UTC (rev 306) @@ -1,19 +1,16 @@ 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; + private readonly IEnumerable<IPluginTypeConf... [truncated message content] |
From: <jer...@us...> - 2009-12-28 02:57:31
|
Revision: 308 http://structuremap.svn.sourceforge.net/structuremap/?rev=308&view=rev Author: jeremydmiller Date: 2009-12-28 02:57:22 +0000 (Mon, 28 Dec 2009) Log Message: ----------- enhanced the Model a bit to start being able to remove types. Ripped out that old silly Machine specific default crap. Modified Paths: -------------- trunk/Source/StructureMap/Configuration/IGraphBuilder.cs trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs trunk/Source/StructureMap/Configuration/ProfileBuilder.cs trunk/Source/StructureMap/Configuration/XmlConstants.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IInstanceFactory.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/Pipeline/Profile.cs trunk/Source/StructureMap/Pipeline/ProfileManager.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/EmptyConfiguration.cs trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceRef.cs trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs Modified: trunk/Source/StructureMap/Configuration/IGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -7,8 +7,6 @@ { void AddProfile(string profileName); void OverrideProfile(TypePath typePath, string instanceKey); - void AddMachine(string machineName, string profileName); - void OverrideMachine(TypePath typePath, string instanceKey); void SetDefaultProfileName(string profileName); } Modified: trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -25,6 +25,11 @@ _structureMapNode.ForAttributeValue(DEFAULT_PROFILE, profileName => _profileBuilder.SetDefaultProfileName(profileName)); + readProfileNodes(); + } + + private void readProfileNodes() + { forEachNode(PROFILE_NODE).Do(element => { string profileName = element.GetAttribute(NAME); @@ -34,19 +39,6 @@ (fullName, defaultKey) => _profileBuilder.OverrideProfile(new TypePath(fullName), defaultKey), profileName); }); - - - forEachNode(MACHINE_NODE).Do(element => - { - string machineName = element.GetAttribute(NAME); - string profileName = element.GetAttribute(PROFILE_NODE); - - _profileBuilder.AddMachine(machineName, profileName); - - writeOverrides(element, - (fullName, defaultKey) => - _profileBuilder.OverrideMachine(new TypePath(fullName), defaultKey), machineName); - }); } Modified: trunk/Source/StructureMap/Configuration/ProfileBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -6,28 +6,17 @@ { public class ProfileBuilder : IProfileBuilder { - private static string _overriden_machine_name; - - private readonly string _machineName; private readonly PluginGraph _pluginGraph; private readonly ProfileManager _profileManager; private string _lastProfile; - private bool _useMachineOverrides; - public ProfileBuilder(PluginGraph pluginGraph, string machineName) + public ProfileBuilder(PluginGraph pluginGraph) { _pluginGraph = pluginGraph; _profileManager = pluginGraph.ProfileManager; - _machineName = machineName; } - - public ProfileBuilder(PluginGraph pluginGraph) - : this(pluginGraph, GetMachineName()) - { - } - #region IProfileBuilder Members public void AddProfile(string profileName) @@ -44,32 +33,6 @@ }); } - public void AddMachine(string machineName, string profileName) - { - _useMachineOverrides = machineName == _machineName; - - if (_useMachineOverrides) - { - _profileManager.DefaultMachineProfileName = profileName; - } - } - - public void OverrideMachine(TypePath typePath, string instanceKey) - { - if (!_useMachineOverrides) - { - return; - } - - _pluginGraph.Log.WithType(typePath, - "trying to configure a Machine Override", - pluginType => - { - var instance = new ReferencedInstance(instanceKey); - _profileManager.SetMachineDefault(pluginType, instance); - }); - } - public void SetDefaultProfileName(string profileName) { _profileManager.DefaultProfileName = profileName; @@ -77,33 +40,5 @@ #endregion - public static void OverrideMachineName(string machineName) - { - _overriden_machine_name = machineName; - } - - public static void ResetMachineName() - { - _overriden_machine_name = string.Empty; - } - - public static string GetMachineName() - { - if (!string.IsNullOrEmpty(_overriden_machine_name)) - { - return _overriden_machine_name; - } - - string machineName = string.Empty; - try - { - machineName = Environment.MachineName.ToUpper(); - } - finally - { - } - - return machineName; - } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/XmlConstants.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -18,7 +18,6 @@ public const string INSTANCE_NODE = "Instance"; public const string INTERCEPTORS_NODE = "Interceptors"; public const string KEY_ATTRIBUTE = "Key"; - public const string MACHINE_NODE = "Machine"; public const string MEMENTO_SOURCE_NODE = "Source"; public const string MEMENTO_STYLE = "MementoStyle"; public const string NAME = "Name"; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.TypeRules; using StructureMap.Util; @@ -350,5 +351,15 @@ public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion + + public void RemoveInstance(Instance instance) + { + _instances.Remove(instance.Name); + if (_defaultKey == instance.Name) + { + _defaultKey = null; + } + + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/IInstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/IInstanceFactory.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/IInstanceFactory.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -27,5 +27,6 @@ void EjectAllInstances(); IInstanceFactory Clone(); + void RemoveInstance(Instance instance); } } \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/InstanceFactory.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -133,6 +133,11 @@ return factory; } + public void RemoveInstance(Instance instance) + { + _instances.Remove(instance.Name); + } + #endregion public void Dispose() Modified: trunk/Source/StructureMap/Pipeline/Profile.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -114,5 +114,15 @@ { _instances.Clear(); } + + public void Remove(Type pluginType, Instance instance) + { + if (!_instances.ContainsKey(pluginType)) return; + + if (_instances[pluginType] == instance) + { + _instances.Remove(pluginType); + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ProfileManager.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -9,14 +9,10 @@ { private readonly Profile _default = new Profile(""); private readonly object _locker = new object(); - private readonly Profile _machineProfile = new Profile("MACHINE"); private readonly Dictionary<string, Profile> _profiles = new Dictionary<string, Profile>(); private Profile _currentProfile = new Profile(string.Empty); - private string _defaultMachineProfileName; private string _defaultProfileName; - public string DefaultMachineProfileName { get { return _defaultMachineProfileName; } set { _defaultMachineProfileName = value; } } - public string DefaultProfileName { get { return _defaultProfileName; } set { _defaultProfileName = value; } } public string CurrentProfile @@ -38,18 +34,25 @@ } } - public void Dispose() + private IEnumerable<Profile> profiles { - _currentProfile.Clear(); - _machineProfile.Clear(); - _default.Clear(); - - foreach (var profile in _profiles) + get { - profile.Value.Clear(); + yield return _currentProfile; + yield return _default; + + foreach (Profile profile in _profiles.Values) + { + yield return profile; + } } } + public void Dispose() + { + profiles.Each(p => p.Clear()); + } + private void setProfile(Profile profile) { lock (_locker) @@ -83,7 +86,7 @@ { Profile profile = getProfile(profileName); profile.SetDefault(pluginType, instance); - if (profileName == _defaultProfileName || profileName == _defaultMachineProfileName) + if (profileName == _defaultProfileName) { _default.FillTypeInto(pluginType, instance); _currentProfile.FillTypeInto(pluginType, instance); @@ -101,16 +104,6 @@ return _profiles[profileName]; } - public Instance GetMachineDefault(Type pluginType) - { - return _machineProfile.GetDefault(pluginType); - } - - public void SetMachineDefault(Type pluginType, Instance instance) - { - _machineProfile.SetDefault(pluginType, instance); - } - public void SetDefault(Type pluginType, Instance instance) { _currentProfile.SetDefault(pluginType, instance); @@ -128,8 +121,6 @@ setProfileDefaults(graph.Log); - processMachineDefaults(graph); - findFamilyDefaults(graph); backfillProfiles(); @@ -171,28 +162,13 @@ private void findMasterInstances(PluginGraph graph) { - _machineProfile.FindMasterInstances(graph); foreach (var pair in _profiles) { pair.Value.FindMasterInstances(graph); } } - private void processMachineDefaults(PluginGraph graph) - { - _machineProfile.FillAllTypesInto(_default); - if (!string.IsNullOrEmpty(_defaultMachineProfileName)) - { - if (!_profiles.ContainsKey(_defaultMachineProfileName)) - { - graph.Log.RegisterError(280, _defaultMachineProfileName); - } - Profile profile = getProfile(_defaultMachineProfileName); - profile.FillAllTypesInto(_default); - } - } - public void CopyDefaults(Type basicType, Type templatedType, PluginFamily family) { _default.CopyDefault(basicType, templatedType, family); @@ -232,7 +208,6 @@ { var clone = new ProfileManager { - DefaultMachineProfileName = DefaultMachineProfileName, DefaultProfileName = DefaultProfileName }; @@ -249,5 +224,10 @@ pair.Value.Remove<T>(); } } + + public void RemoveInstance(Type pluginType, Instance instance) + { + profiles.Each(x => x.Remove(pluginType, instance)); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -30,18 +30,13 @@ _profileManager = graph.ProfileManager; _log = graph.Log; - foreach (PluginFamily family in graph.PluginFamilies) + + graph.PluginFamilies.Where(x => x.IsGenericTemplate).Each(_genericsGraph.AddFamily); + graph.PluginFamilies.Where(x => !x.IsGenericTemplate).Each(family => { - if (family.IsGenericTemplate) - { - _genericsGraph.AddFamily(family); - } - else - { - var factory = new InstanceFactory(family); - _factories.Add(family.PluginType, factory); - } - } + var factory = new InstanceFactory(family); + _factories.Add(family.PluginType, factory); + }); } private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph, GraphLog log) @@ -231,5 +226,11 @@ ? _transientCache : lifecycle.FindCache(); } + + public void Remove(Type pluginType, Instance instance) + { + ForType(pluginType).RemoveInstance(instance); + _profileManager.RemoveInstance(pluginType, instance); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/EmptyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -39,5 +39,10 @@ { return false; } + + public void EjectAndRemove(InstanceRef instance) + { + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -53,7 +53,7 @@ /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered /// for this PluginType /// </summary> - public IEnumerable<InstanceRef> Instances { get { return _family.Instances.Select(x => new InstanceRef(x, this)); } } + public IEnumerable<InstanceRef> Instances { get { return _family.Instances.Select(x => new InstanceRef(x, this)).ToArray(); } } /// <summary> /// Simply query to see if there are any implementations registered @@ -63,5 +63,13 @@ { return _family.InstanceCount > 0; } + + public void EjectAndRemove(InstanceRef instance) + { + if (instance == null) return; + _family.RemoveInstance(instance.Instance); + } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace StructureMap.Query { @@ -29,5 +30,42 @@ /// </summary> /// <returns></returns> bool HasImplementations(); + + /// <summary> + /// Ejects any instances of this instance from the current container + /// and permanently removes the instance from the container configuration + /// </summary> + /// <param name="instance"></param> + void EjectAndRemove(InstanceRef instance); + } + + public static class PluginTypeConfigurationExtensions + { + public static InstanceRef Find(this IPluginTypeConfiguration configuration, string instanceName) + { + return configuration.Instances.FirstOrDefault(x => x.Name == instanceName); + } + + /// <summary> + /// Ejects and removes all objects and the configuration for the named instance from the + /// container + /// </summary> + /// <param name="configuration"></param> + /// <param name="instanceName"></param> + public static void EjectAndRemove(this IPluginTypeConfiguration configuration, string instanceName) + { + configuration.EjectAndRemove(configuration.Find(instanceName)); + } + + /// <summary> + /// Ejects and removes all objects and configuration for the instances that match the filter + /// </summary> + /// <param name="configuration"></param> + /// <param name="filter"></param> + public static void EjectAndRemove(this IPluginTypeConfiguration configuration, Func<InstanceRef, bool> filter) + { + configuration.Instances.Where(filter).Each(configuration.EjectAndRemove); + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -63,7 +63,7 @@ /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered /// for this PluginType /// </summary> - public IEnumerable<InstanceRef> Instances { get { return factory.AllInstances.Select(x => toRef(x)); } } + public IEnumerable<InstanceRef> Instances { get { return factory.AllInstances.Select(x => toRef(x)).ToArray(); } } /// <summary> /// Simply query to see if there are any implementations registered @@ -74,6 +74,13 @@ return factory.AllInstances.Any(); } + public void EjectAndRemove(InstanceRef instance) + { + instance.EjectObject(); + _graph.Remove(_pluginType, instance.Instance); + } + + private InstanceRef toRef(Instance instance) { if (instance == null) return null; Modified: trunk/Source/StructureMap/Query/InstanceRef.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceRef.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/InstanceRef.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -14,6 +14,7 @@ _family = family; } + internal Instance Instance { get { return _instance; } } public string Name { get { return _instance.Name; } } Modified: trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -53,7 +53,6 @@ public void TearDown() { - ProfileBuilder.ResetMachineName(); } [Test] @@ -75,26 +74,6 @@ } [Test] - public void GettingTheRightInstanceKeyWhenUsingAMAchineOverrideInCombinationWithProfile() - { - ProfileBuilder.OverrideMachineName("GREEN-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance<IWidget>(); - Assert.AreEqual("Green", widget.Color); - } - - [Test] - public void GotTheInstanceForTheMachineOverride() - { - ProfileBuilder.OverrideMachineName("ORANGE-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance<IWidget>(); - Assert.AreEqual("Orange", widget.Color); - } - - [Test] public void HasADefaultInstanceKey() { var manager = new Container(graph); @@ -118,28 +97,7 @@ Assert.AreEqual("Blue", widget.Color); } - [Test] - public void InlineMachine1() - { - ProfileBuilder.OverrideMachineName("ORANGE-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance(typeof (IWidget)); - Assert.AreEqual("Orange", widget.Color); - } - - [Test] - public void InlineMachine2() - { - ProfileBuilder.OverrideMachineName("GREEN-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance(typeof (IWidget)); - Assert.AreEqual("Green", widget.Color); - } - - [Test] public void SetTheProfile() { var manager = new Container(graph); Modified: trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -14,44 +14,15 @@ public void SetUp() { _graph = new PluginGraph(); - _builder = new ProfileBuilder(_graph, THE_MACHINE_NAME); + _builder = new ProfileBuilder(_graph); } #endregion private PluginGraph _graph; private ProfileBuilder _builder; - private const string THE_MACHINE_NAME = "TheMachineName"; [Test] - public void Do_not_register_a_machine_override_if_it_is_NOT_the_matching_machine() - { - _builder.AddMachine("Some other machine", "TheProfile"); - _builder.OverrideMachine(new TypePath(GetType()), "Purple"); - - Assert.IsNull(_graph.ProfileManager.GetMachineDefault(GetType())); - } - - [Test] - public void Ignore_any_information_from_a_different_machine() - { - _builder.AddMachine("DifferentMachine", "TheProfile"); - Assert.IsTrue(string.IsNullOrEmpty(_graph.ProfileManager.DefaultMachineProfileName)); - } - - [Test] - public void Log_131_if_trying_to_register_override_for_a_machine_when_the_PluginType_cannot_be_found() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - - _graph.Log.AssertHasNoError(131); - - _builder.OverrideMachine(new TypePath("not a real type"), "Purple"); - - _graph.Log.AssertHasError(131); - } - - [Test] public void Log_131_if_trying_to_register_override_for_a_profile_when_the_PluginType_cannot_be_found() { _builder.AddProfile("TheProfile"); @@ -80,16 +51,6 @@ } [Test] - public void Register_a_machine_override_if_it_is_the_matching_machine() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - _builder.OverrideMachine(new TypePath(GetType()), "Purple"); - - var instance = new ReferencedInstance("Purple"); - Assert.AreEqual(instance, _graph.ProfileManager.GetMachineDefault(GetType())); - } - - [Test] public void SetDefaultProfileName() { string theProfileName = "some profile name"; @@ -99,12 +60,6 @@ } [Test] - public void smoke_test_get_machine_name() - { - ProfileBuilder.GetMachineName().ShouldNotBeEmpty(); - } - - [Test] public void Throw_280_if_requesting_an_invalid_profile() { try @@ -120,11 +75,5 @@ } } - [Test] - public void Use_the_machine_profile_name_if_the_machine_name_matches() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - Assert.AreEqual("TheProfile", _graph.ProfileManager.DefaultMachineProfileName); - } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -38,14 +38,7 @@ family.AddInstance(new ObjectInstance(0).WithName(name)); } - private void addDefaultToMachine<T>(string name) - { - ObjectInstance instance = new ObjectInstance(0).WithName(name); - PluginFamily family = _pluginGraph.FindFamily(typeof (T)); - family.AddInstance(instance); - _manager.SetMachineDefault(typeof (T), new ReferencedInstance(name)); - } private void assertNoDefaultForType<T>() { @@ -85,59 +78,6 @@ [Test] - public void DefaultMachineProfileNotSet_and_pickUp_default_from_family_machine() - { - addDefaultToProfile<ISomething>("Machine", "Red"); - addDefaultToPluginFamily<ISomething>("Blue"); - _manager.DefaultMachineProfileName = null; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - - [Test] - public void Got_machine_default_machine_profile_and_family_default_the_machine_wins() - { - addDefaultToProfile<ISomething>("MachineProfile", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultMachineProfileName = "MachineProfile"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Machine"); - } - - - [Test] - public void Got_profile_and_family_and_machine_and_machine_profile_so_profile_wins() - { - addDefaultToProfile<ISomething>("TheProfile", "Profile"); - addDefaultToProfile<ISomething>("TheMachineProfile", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultProfileName = "TheProfile"; - _manager.DefaultMachineProfileName = "TheMachineProfile"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Profile"); - } - - [Test] - public void Got_profile_and_family_and_machine_so_profile_wins() - { - addDefaultToProfile<ISomething>("TheProfile", "Profile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultProfileName = "TheProfile"; - seal(); - - assertDefaultInstanceNameIs<ISomething>("Profile"); - } - - [Test] public void Got_profile_and_family_so_profile_wins() { addDefaultToProfile<ISomething>("TheProfile", "Profile"); @@ -158,39 +98,8 @@ assertDefaultInstanceNameIs<ISomething>("Profile"); } - [Test] - public void Have_a_machine_default_and_a_base_default_the_machine_wins() - { - addDefaultToPluginFamily<ISomething>("Red"); - addDefaultToMachine<ISomething>("Blue"); - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - [Test] - public void Have_a_machine_default_but_not_any_other_default_for_a_type() - { - addDefaultToMachine<ISomething>("Blue"); - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - - [Test] - public void Have_machine_profile_and_default_from_family_machine_profile_wins() - { - addDefaultToProfile<ISomething>("Machine", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - _manager.DefaultMachineProfileName = "Machine"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("MachineProfile"); - } - - [Test] public void If_the_profile_is_set_and_there_is_a_default_in_that_profile_use_the_profile_default() { addDefaultToProfile<ISomething>("TheProfile", "Profile"); @@ -215,18 +124,6 @@ } [Test] - public void Log_280_if_the_machine_default_profile_cannot_be_found() - { - var manager = new ProfileManager(); - manager.DefaultMachineProfileName = "something that doesn't exist"; - - var graph = new PluginGraph(); - manager.Seal(graph); - - graph.Log.AssertHasError(280); - } - - [Test] public void Only_programmatic_override_so_use_the_programmatic_override() { _manager.SetDefault(typeof (ISomething), new ConfiguredInstance(typeof (SomethingOne)).WithName("Red")); Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -78,6 +78,40 @@ } [Test] + public void remove_if_the_instance_does_not_match() + { + var profile = new Profile("something"); + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + profile.SetDefault(typeof(ISomething), instance); + + profile.Remove(typeof(ISomething), new NullInstance()); + + profile.GetDefault(typeof (ISomething)).ShouldBeTheSameAs(instance); + } + + [Test] + public void remove_if_the_instance_does_match() + { + var profile = new Profile("something"); + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + profile.SetDefault(typeof(ISomething), instance); + + profile.Remove(typeof(ISomething), instance); + + profile.GetDefault(typeof(ISomething)).ShouldBeNull(); + } + + [Test] + public void do_not_blow_up_if_the_profile_does_not_have_the_plugin_type_being_removed() + { + var profile = new Profile("something"); + + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + + profile.Remove(typeof(ISomething), instance); + } + + [Test] public void A_call_to_fill_sets_the_default_for_a_plugin_type_if_no_previous_default_is_known() { var profile = new Profile("something"); Modified: trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -26,6 +26,37 @@ private GenericFamilyConfiguration configuration; [Test] + public void eject_and_remove_an_instance() + { + var instance = new ConfiguredInstance(typeof(Service<>)); + family.AddInstance(instance); + family.AddInstance(new ConfiguredInstance(typeof(Service2<>))); + + var iRef = configuration.Instances.FirstOrDefault(x => x.Name == instance.Name); + + configuration.EjectAndRemove(iRef); + + family.InstanceCount.ShouldEqual(1); + configuration.Instances.Count().ShouldEqual(1); + + configuration.Instances.Any(x => x.Name == instance.Name).ShouldBeFalse(); + } + + [Test] + public void eject_and_remove_the_default_value() + { + var instance = new ConfiguredInstance(typeof(Service<>)); + family.SetDefault(instance); + family.AddInstance(new ConfiguredInstance(typeof(Service2<>))); + + var iRef = configuration.Instances.FirstOrDefault(x => x.Name == instance.Name); + + configuration.EjectAndRemove(iRef); + + family.GetDefaultInstance().ShouldBeNull(); + } + + [Test] public void build_should_return_null() { configuration.As<IFamily>().Build(null).ShouldBeNull(); Modified: trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -1,5 +1,6 @@ using System.Linq; using NUnit.Framework; +using StructureMap.Query; using StructureMap.Testing.Configuration.DSL; using StructureMap.Testing.Graph; using StructureMap.Testing.Widget; @@ -36,6 +37,46 @@ private Container container; [Test] + public void eject_and_remove_an_instance_should_remove_it_from_the_model() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(iRef); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + + [Test] + public void eject_and_remove_an_instance_should_remove_it_from_the_model_by_name() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(iRef.Name); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + [Test] + public void eject_and_remove_an_instance_by_filter_should_remove_it_from_the_model() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(x => x.Name == iRef.Name); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + [Test] public void build_when_the_cast_does_not_work() { container.Model.For<IWidget>().Default.Get<Rule>().ShouldBeNull(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-28 04:07:29
|
Revision: 309 http://structuremap.svn.sourceforge.net/structuremap/?rev=309&view=rev Author: jeremydmiller Date: 2009-12-28 04:07:20 +0000 (Mon, 28 Dec 2009) Log Message: ----------- Finished the work on the new "Model" subsystem Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Pipeline/Profile.cs trunk/Source/StructureMap/Pipeline/ProfileManager.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/EmptyConfiguration.cs trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs trunk/Source/StructureMap/Query/IModel.cs trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs trunk/Source/StructureMap/Query/Model.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Container.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -64,7 +64,13 @@ /// <summary> /// Provides queryable access to the configured PluginType's and Instances of this Container /// </summary> - public IModel Model { get { return new Model(_pipelineGraph.GetPluginTypes(this)); } } + public IModel Model + { + get + { + return new Model(_pipelineGraph, this); + } + } /// <summary> /// Creates or finds the named instance of T Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -160,5 +160,10 @@ { _families.Clear(); } + + public void Remove(Type pluginType) + { + _families.Remove(pluginType); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -361,5 +361,12 @@ } } + + public void RemoveAll() + { + _instances.Clear(); + _mementoList.Clear(); + _defaultKey = null; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/Profile.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -105,9 +105,9 @@ } - public void Remove<T>() + public void Remove(Type pluginType) { - _instances.Remove(typeof (T)); + _instances.Remove(pluginType); } public void Clear() Modified: trunk/Source/StructureMap/Pipeline/ProfileManager.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -216,13 +216,9 @@ return clone; } - public void EjectAllInstancesOf<T>() + public void EjectAllInstancesOf(Type pluginType) { - _currentProfile.Remove<T>(); - foreach (var pair in _profiles) - { - pair.Value.Remove<T>(); - } + profiles.Each(x => x.Remove(pluginType)); } public void RemoveInstance(Type pluginType, Instance instance) Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -188,11 +188,29 @@ public void EjectAllInstancesOf<T>() { - ForType(typeof (T)).EjectAllInstances(); - _profileManager.EjectAllInstancesOf<T>(); + EjectAllInstancesOf(typeof(T)); } + public void EjectAllInstancesOf(Type pluginType) + { + ForType(pluginType).EjectAllInstances(); + _profileManager.EjectAllInstancesOf(pluginType); + } + public void Remove(Func<Type, bool> filter) + { + _genericsGraph.Families.Where(x => filter(x.PluginType)).ToArray().Each(x => Remove(x.PluginType)); + _factories.Values.Where(x => filter(x.PluginType)).ToArray().Each(x => Remove(x.PluginType)); + } + + public void Remove(Type pluginType) + { + EjectAllInstancesOf(pluginType); + _factories.Remove(pluginType); + _genericsGraph.Remove(pluginType); + } + + public List<Instance> GetAllInstances() { return _factories.Values.SelectMany(x => x.AllInstances).ToList(); @@ -232,5 +250,8 @@ ForType(pluginType).RemoveInstance(instance); _profileManager.RemoveInstance(pluginType, instance); } + + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/EmptyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -44,5 +44,8 @@ { } + public void EjectAndRemoveAll() + { + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -70,6 +70,9 @@ _family.RemoveInstance(instance.Instance); } - + public void EjectAndRemoveAll() + { + _family.RemoveAll(); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IModel.cs =================================================================== --- trunk/Source/StructureMap/Query/IModel.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/IModel.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -90,5 +90,32 @@ /// <param name="type"></param> /// <returns></returns> IPluginTypeConfiguration For(Type type); + + /// <summary> + /// Eject all objects, configuration, and Plugin Types matching this filter + /// </summary> + /// <param name="filter"></param> + void EjectAndRemoveTypes(Func<Type, bool> filter); + + /// <summary> + /// Eject all objects and configuration for any Plugin Type that matches this filter + /// </summary> + /// <param name="filter"></param> + void EjectAndRemovePluginTypes(Func<Type, bool> filter); + + /// <summary> + /// Eject all objects and Instance configuration for this PluginType + /// </summary> + /// <param name="pluginType"></param> + void EjectAndRemove(Type pluginType); + + + /// <summary> + /// Get each and every configured instance that could possibly + /// be cast to T + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + IEnumerable<T> GetAllPossible<T>() where T : class; } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -38,6 +38,11 @@ /// <param name="instance"></param> void EjectAndRemove(InstanceRef instance); + /// <summary> + /// Eject all instances of this PluginType from the current container, + /// but leaves the lifecycle behavior + /// </summary> + void EjectAndRemoveAll(); } public static class PluginTypeConfigurationExtensions Modified: trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -80,7 +80,12 @@ _graph.Remove(_pluginType, instance.Instance); } - + public void EjectAndRemoveAll() + { + _graph.EjectAllInstancesOf(_pluginType); + } + + private InstanceRef toRef(Instance instance) { if (instance == null) return null; Modified: trunk/Source/StructureMap/Query/Model.cs =================================================================== --- trunk/Source/StructureMap/Query/Model.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/Query/Model.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -1,16 +1,19 @@ using System; using System.Collections.Generic; using System.Linq; +using StructureMap.TypeRules; namespace StructureMap.Query { public class Model : IModel { - private readonly IEnumerable<IPluginTypeConfiguration> _pluginTypes; + private PipelineGraph _graph; + private IContainer _container; - internal Model(IEnumerable<IPluginTypeConfiguration> pluginTypes) + internal Model(PipelineGraph graph, IContainer container) { - _pluginTypes = pluginTypes; + _graph = graph; + _container = container; } #region IModel Members @@ -35,8 +38,16 @@ return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ConcreteType); } - public IEnumerable<IPluginTypeConfiguration> PluginTypes { get { return _pluginTypes; } } + private IEnumerable<IPluginTypeConfiguration> pluginTypes + { + get + { + return _graph.GetPluginTypes(_container); + } + } + public IEnumerable<IPluginTypeConfiguration> PluginTypes { get { return pluginTypes; } } + /// <summary> /// Retrieves the configuration for the given type /// </summary> @@ -54,9 +65,58 @@ /// <returns></returns> public IPluginTypeConfiguration For(Type type) { - return _pluginTypes.FirstOrDefault(x => x.PluginType == type) ?? new EmptyConfiguration(type); + return pluginTypes.FirstOrDefault(x => x.PluginType == type) ?? new EmptyConfiguration(type); } + /// <summary> + /// Eject all objects, configuration, and Plugin Types matching this filter + /// </summary> + /// <param name="filter"></param> + public void EjectAndRemoveTypes(Func<Type, bool> filter) + { + // first pass hits plugin types + EjectAndRemovePluginTypes(filter); + + // second pass to hit instances + pluginTypes.Each(x => + { + x.EjectAndRemove(i => filter(i.ConcreteType)); + }); + } + + /// <summary> + /// Eject all objects and configuration for any Plugin Type that matches this filter + /// </summary> + /// <param name="filter"></param> + public void EjectAndRemovePluginTypes(Func<Type, bool> filter) + { + _graph.Remove(filter); + } + + /// <summary> + /// Eject all objects and Instance configuration for this PluginType + /// </summary> + /// <param name="pluginType"></param> + public void EjectAndRemove(Type pluginType) + { + _graph.Remove(pluginType); + } + + /// <summary> + /// Get each and every configured instance that could possibly + /// be cast to T + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + public IEnumerable<T> GetAllPossible<T>() where T : class + { + Type targetType = typeof (T); + return AllInstances + .Where(x => x.ConcreteType.CanBeCastTo(targetType)) + .Select(x => x.Get<T>()) + .Where(x => x != null); + } + public IEnumerable<InstanceRef> InstancesOf(Type pluginType) { return findForFamily(pluginType, x => x.Instances, new InstanceRef[0]); Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -173,6 +173,8 @@ /// <returns></returns> public static bool CanBeCastTo(this Type pluggedType, Type pluginType) { + if (pluggedType == null) return false; + if (pluggedType.IsInterface || pluggedType.IsAbstract) { return false; Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -208,6 +208,24 @@ } [Test] + public void remove_all_clears_the_defaul_and_removes_all_plugins_instances() + { + var family = new PluginFamily(typeof(IServiceProvider)); + var instance = new SmartInstance<DataSet>(); + + family.SetDefault(instance); + + family.AddInstance(new NullInstance()); + family.AddType(typeof(DataSet)); + + family.RemoveAll(); + + family.DefaultInstanceKey.ShouldBeNull(); + + family.InstanceCount.ShouldEqual(0); + } + + [Test] public void set_the_scope_to_session() { var family = new PluginFamily(typeof (IServiceProvider)); Modified: trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2009-12-28 02:57:22 UTC (rev 308) +++ trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2009-12-28 04:07:20 UTC (rev 309) @@ -32,6 +32,10 @@ }); x.For<IEngine>().Use<PushrodEngine>(); + + x.For<Startable1>().Singleton().Use<Startable1>(); + x.For<Startable2>().Use<Startable2>(); + x.For<Startable3>().Use<Startable3>(); }); } @@ -40,10 +44,63 @@ private Container container; [Test] + public void remove_types_based_on_a_filter() + { + container.GetAllInstances<Rule>().Any(x => x is ARule).ShouldBeTrue(); + container.Model.HasImplementationsFor<IWidget>().ShouldBeTrue(); + + container.Model.EjectAndRemoveTypes(t => t == typeof(IWidget) || t == typeof(ARule)); + + container.GetAllInstances<Rule>().Any(x => x is ARule).ShouldBeFalse(); + container.Model.HasImplementationsFor<IWidget>().ShouldBeFalse(); + } + + [Test] + public void remove_an_entire_closed_type() + { + container.Model.EjectAndRemove(typeof(Rule)); + container.Model.HasImplementationsFor<Rule>().ShouldBeFalse(); + + container.TryGetInstance<Rule>().ShouldBeNull(); + container.GetAllInstances<Rule>().Count.ShouldEqual(0); + + } + + [Test] + public void remove_an_entire_closed_type_with_the_filter() + { + container.Model.EjectAndRemovePluginTypes(t => t == typeof(Rule) || t == typeof(IWidget)); + + container.Model.HasImplementationsFor<IWidget>().ShouldBeFalse(); + container.Model.HasImplementationsFor<Rule>().ShouldBeFalse(); + container.Model.HasImplementationsFor<IEngine>().ShouldBeTrue(); + } + + [Test] + public void remove_an_open_type() + { + container.Model.EjectAndRemove(typeof(IService<>)); + + container.Model.HasImplementationsFor(typeof (IService<>)); + + container.TryGetInstance<IService<string>>().ShouldBeNull(); + } + + [Test] + public void remove_an_open_type_with_a_filter() + { + container.Model.EjectAndRemovePluginTypes(t => t == typeof(IService<>)); + + container.Model.HasImplementationsFor(typeof(IService<>)); + + container.TryGetInstance<IService<string>>().ShouldBeNull(); + } + + [Test] public void can_iterate_through_families_including_both_generics_and_normal() { // +1 for "IContainer" itself - container.Model.PluginTypes.Count().ShouldEqual(5); + container.Model.PluginTypes.Count().ShouldEqual(8); container.Model.PluginTypes.Each(x => Debug.WriteLine(x.PluginType.FullName)); } @@ -82,7 +139,7 @@ [Test] public void get_all_instances_from_the_top() { - container.Model.AllInstances.Count().ShouldEqual(8); + container.Model.AllInstances.Count().ShouldEqual(11); } [Test] @@ -105,5 +162,42 @@ { container.Model.For<ISomething>().HasImplementations().ShouldBeFalse(); } + + [Test] + public void get_all_possibles() + { + // Startable1 is a singleton + + var startable1 = container.GetInstance<Startable1>(); + startable1.WasStarted.ShouldBeFalse(); + + container.Model.GetAllPossible<IStartable>() + .ToArray() + .Each(x => x.Start()) + .Each(x => x.WasStarted.ShouldBeTrue()); + + startable1.WasStarted.ShouldBeTrue(); + } } + + + public interface IStartable + { + void Start(); + bool WasStarted { get; } + } + + public class Startable : IStartable + { + public void Start() + { + WasStarted = true; + } + + public bool WasStarted { get; private set; } + } + + public class Startable1 : Startable{} + public class Startable2 : Startable{} + public class Startable3 : Startable{} } \ 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-28 05:05:01
|
Revision: 311 http://structuremap.svn.sourceforge.net/structuremap/?rev=311&view=rev Author: jeremydmiller Date: 2009-12-28 05:04:44 +0000 (Mon, 28 Dec 2009) Log Message: ----------- added the FirstInterfaceConvention (I think it's a bad idea, but other people wanted it), deleted some obsolete code hiding out leftover from the data access crap Modified Paths: -------------- trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/IAssemblyScanner.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs trunk/Source/StructureMap.Testing/SpecificationExtensions.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs Removed Paths: ------------- trunk/Source/StructureMap.Testing/DataAccess/ Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -80,6 +80,11 @@ Convention<DefaultConventionScanner>(); } + public void RegisterConcreteTypesAgainstTheFirstInterface() + { + Convention<FirstInterfaceConvention>(); + } + [Obsolete("Replace ITypeScanner with IRegistrationConvention")] public void With<T>() where T : ITypeScanner, new() { Added: trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs =================================================================== --- trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs (rev 0) +++ trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -0,0 +1,24 @@ +using System; +using System.Diagnostics; +using StructureMap.Configuration.DSL; +using StructureMap.TypeRules; +using System.Linq; + +namespace StructureMap.Graph +{ + public class FirstInterfaceConvention : IRegistrationConvention + { + public void Process(Type type, Registry registry) + { + if (!type.IsConcrete() || !type.CanBeCreated()) return; + + + var interfaceType = type.AllInterfaces().FirstOrDefault(); + if (interfaceType != null) + { + Debug.WriteLine("Plugging {0} into {1}".ToFormat(type.Name, interfaceType.Name)); + registry.AddType(interfaceType, type); + } + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/IAssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -83,6 +83,13 @@ void WithDefaultConventions(); /// <summary> + /// Automatically registers all concrete types without primitive arguments + /// against its first interface, if any + /// </summary> + void RegisterConcreteTypesAgainstTheFirstInterface(); + + + /// <summary> /// Creates and adds a new ITypeScanner of type T to this scanning operation /// </summary> /// <typeparam name="T"></typeparam> Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -348,7 +348,11 @@ /// The InstanceKey of the default instance of the PluginFamily /// </summary> [Obsolete] - public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } + public string DefaultInstanceKey + { + get { return _defaultKey; } + set { _defaultKey = value ?? string.Empty; } + } #endregion Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-28 05:04:44 UTC (rev 311) @@ -361,6 +361,7 @@ <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> + <Compile Include="Graph\FirstInterfaceConvention.cs" /> <Compile Include="Graph\IAssemblyScanner.cs" /> <Compile Include="Graph\ImplementationMap.cs" /> <Compile Include="Graph\ITypeScanner.cs" /> Modified: trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -58,7 +58,6 @@ { } - [TestFixture] public class DefaultConventionScanningTester { Added: trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -0,0 +1,79 @@ +using System.Diagnostics; +using NUnit.Framework; +using System.Linq; +using StructureMap.Testing.Widget; +using StructureMap.Testing.Widget3; +using StructureMap.TypeRules; + +namespace StructureMap.Testing.Graph +{ + [TestFixture] + public class FirstInterfaceConventionTester + { + private Container container; + + [SetUp] + public void SetUp() + { + container = new Container(x => + { + x.Scan(o => + { + o.TheCallingAssembly(); + o.RegisterConcreteTypesAgainstTheFirstInterface(); + + o.Exclude(t => t.CanBeCastTo(typeof(IGateway))); + }); + }); + } + + [Test] + public void simple_case() + { + container.Model.For<I1>().Instances.Select(x => x.ConcreteType).ShouldHaveTheSameElementsAs(typeof(C1), typeof(C2)); + container.Model.For<I2>().Instances.Select(x => x.ConcreteType).Any().ShouldBeFalse(); + } + + [Test] + public void do_not_register_type_if_there_are_primitive_arguments() + { + container.Model.HasImplementationsFor<I3>().ShouldBeFalse(); + } + + + public interface I1 { } + public interface I2 { } + public interface I3 { } + + public class C1 : I1 { } + public class C2 : C1, I2 { } + public class C3 : C2, I3 + { + public C3(string name) + { + } + } + + public class C4 + { + + } + + public class C5 : I1 + { + private C5(){} + } + + public interface I1<T> { } + public interface I2<T> { } + public interface I3<T> { } + + public class C1<T> : I1<T> { } + public class C2<T> : C1<T>, I2<T> { } + public class C3<T> : C2<T>, I3<T> { } + + } + + +} + Modified: trunk/Source/StructureMap.Testing/SpecificationExtensions.cs =================================================================== --- trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-12-28 05:04:44 UTC (rev 311) @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using NUnit.Framework; @@ -31,11 +32,23 @@ actual.ShouldNotBeNull(); expected.ShouldNotBeNull(); - actual.Count.ShouldEqual(expected.Count); + try + { + actual.Count.ShouldEqual(expected.Count); - for (int i = 0; i < actual.Count; i++) + for (int i = 0; i < actual.Count; i++) + { + actual[i].ShouldEqual(expected[i]); + } + } + catch (Exception) { - actual[i].ShouldEqual(expected[i]); + Debug.WriteLine("ACTUAL:"); + foreach (var o in actual) + { + Debug.WriteLine(o); + } + throw; } } Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-28 04:19:45 UTC (rev 310) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-28 05:04:44 UTC (rev 311) @@ -257,6 +257,7 @@ <Compile Include="Graph\ExceptionHandling\StructureMapExceptionTester.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Graph\FirstInterfaceConventionTester.cs" /> <Compile Include="Graph\SingleImplementationScannerTester.cs" /> <Compile Include="Graph\TestExplicitArguments.cs" /> <Compile Include="Graph\FillDependenciesTester.cs"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-28 05:14:51
|
Revision: 312 http://structuremap.svn.sourceforge.net/structuremap/?rev=312&view=rev Author: jeremydmiller Date: 2009-12-28 05:14:41 +0000 (Mon, 28 Dec 2009) Log Message: ----------- quickie code cleanup Modified Paths: -------------- trunk/Source/StructureMap/Configuration/ProfileBuilder.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/Model.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/SpecificationExtensions.cs Modified: trunk/Source/StructureMap/Configuration/ProfileBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -1,4 +1,3 @@ -using System; using StructureMap.Graph; using StructureMap.Pipeline; @@ -39,6 +38,5 @@ } #endregion - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/Container.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -64,13 +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, this); - } - } + public IModel Model { get { return new Model(_pipelineGraph, this); } } /// <summary> /// Creates or finds the named instance of T Modified: trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs =================================================================== --- trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -1,8 +1,8 @@ using System; using System.Diagnostics; +using System.Linq; using StructureMap.Configuration.DSL; using StructureMap.TypeRules; -using System.Linq; namespace StructureMap.Graph { @@ -11,9 +11,9 @@ public void Process(Type type, Registry registry) { if (!type.IsConcrete() || !type.CanBeCreated()) return; - - - var interfaceType = type.AllInterfaces().FirstOrDefault(); + + + Type interfaceType = type.AllInterfaces().FirstOrDefault(); if (interfaceType != null) { Debug.WriteLine("Plugging {0} into {1}".ToFormat(type.Name, interfaceType.Name)); Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using StructureMap.Pipeline; -using StructureMap.Query; using StructureMap.TypeRules; using StructureMap.Util; @@ -328,6 +327,22 @@ _instances.WithValue(name, action); } + public void RemoveInstance(Instance instance) + { + _instances.Remove(instance.Name); + if (_defaultKey == instance.Name) + { + _defaultKey = null; + } + } + + public void RemoveAll() + { + _instances.Clear(); + _mementoList.Clear(); + _defaultKey = null; + } + #region properties public bool IsGenericTemplate { get { return _pluginType.IsGenericTypeDefinition || _pluginType.ContainsGenericParameters; } } @@ -348,29 +363,8 @@ /// The InstanceKey of the default instance of the PluginFamily /// </summary> [Obsolete] - public string DefaultInstanceKey - { - get { return _defaultKey; } - set { _defaultKey = value ?? string.Empty; } - } + public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion - - public void RemoveInstance(Instance instance) - { - _instances.Remove(instance.Name); - if (_defaultKey == instance.Name) - { - _defaultKey = null; - } - - } - - public void RemoveAll() - { - _instances.Clear(); - _mementoList.Clear(); - _defaultKey = null; - } } } \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -188,7 +188,7 @@ public void EjectAllInstancesOf<T>() { - EjectAllInstancesOf(typeof(T)); + EjectAllInstancesOf(typeof (T)); } public void EjectAllInstancesOf(Type pluginType) @@ -250,8 +250,5 @@ ForType(pluginType).RemoveInstance(instance); _profileManager.RemoveInstance(pluginType, instance); } - - - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/Model.cs =================================================================== --- trunk/Source/StructureMap/Query/Model.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap/Query/Model.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -7,8 +7,8 @@ { public class Model : IModel { - private PipelineGraph _graph; - private IContainer _container; + private readonly IContainer _container; + private readonly PipelineGraph _graph; internal Model(PipelineGraph graph, IContainer container) { @@ -18,6 +18,8 @@ #region IModel Members + private IEnumerable<IPluginTypeConfiguration> pluginTypes { get { return _graph.GetPluginTypes(_container); } } + public bool HasDefaultImplementationFor(Type pluginType) { return findForFamily(pluginType, f => f.Default != null); @@ -38,14 +40,6 @@ return findForFamily(pluginType, f => f.Default == null ? null : f.Default.ConcreteType); } - private IEnumerable<IPluginTypeConfiguration> pluginTypes - { - get - { - return _graph.GetPluginTypes(_container); - } - } - public IEnumerable<IPluginTypeConfiguration> PluginTypes { get { return pluginTypes; } } /// <summary> @@ -78,10 +72,7 @@ EjectAndRemovePluginTypes(filter); // second pass to hit instances - pluginTypes.Each(x => - { - x.EjectAndRemove(i => filter(i.ConcreteType)); - }); + pluginTypes.Each(x => { x.EjectAndRemove(i => filter(i.ConcreteType)); }); } /// <summary> Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -23,7 +23,8 @@ { public RedGreenRegistry() { - InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Red").WithName("Red"); + InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Red").WithName( + "Red"); InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Green").WithName( "Green"); } @@ -35,7 +36,8 @@ { InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Yellow").WithName( "Yellow"); - InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithProperty("color").EqualTo("Blue").WithName("Blue"); + InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithProperty("color").EqualTo("Blue").WithName( + "Blue"); } } @@ -47,22 +49,7 @@ } } - [Test] - public void include_a_registry() - { - var registry = new Registry(); - registry.IncludeRegistry<YellowBlueRegistry>(); - registry.IncludeRegistry<RedGreenRegistry>(); - registry.IncludeRegistry<PurpleRegistry>(); - var container = new Container(registry); - - container.GetInstance<IWidget>().ShouldBeOfType<AWidget>(); - - container.GetAllInstances<IWidget>().Count.ShouldEqual(5); - } - - [Test] public void Can_add_an_instance_for_concrete_class_with_no_constructors() { @@ -91,6 +78,21 @@ } [Test] + public void include_a_registry() + { + var registry = new Registry(); + registry.IncludeRegistry<YellowBlueRegistry>(); + registry.IncludeRegistry<RedGreenRegistry>(); + registry.IncludeRegistry<PurpleRegistry>(); + + var container = new Container(registry); + + container.GetInstance<IWidget>().ShouldBeOfType<AWidget>(); + + container.GetAllInstances<IWidget>().Count.ShouldEqual(5); + } + + [Test] public void Latch_on_a_PluginGraph() { var registry2 = new TestRegistry2(); Modified: trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -1,5 +1,4 @@ using NUnit.Framework; -using StructureMap.Configuration; using StructureMap.Graph; using StructureMap.Testing.TestData; using StructureMap.Testing.Widget; Modified: trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -74,6 +74,5 @@ Assert.AreEqual(280, ex.ErrorCode); } } - } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -1,7 +1,5 @@ -using System.Diagnostics; +using System.Linq; using NUnit.Framework; -using System.Linq; -using StructureMap.Testing.Widget; using StructureMap.Testing.Widget3; using StructureMap.TypeRules; @@ -10,7 +8,7 @@ [TestFixture] public class FirstInterfaceConventionTester { - private Container container; + #region Setup/Teardown [SetUp] public void SetUp() @@ -22,31 +20,36 @@ o.TheCallingAssembly(); o.RegisterConcreteTypesAgainstTheFirstInterface(); - o.Exclude(t => t.CanBeCastTo(typeof(IGateway))); + o.Exclude(t => t.CanBeCastTo(typeof (IGateway))); }); }); } - [Test] - public void simple_case() + #endregion + + private Container container; + + + public interface I1 { - container.Model.For<I1>().Instances.Select(x => x.ConcreteType).ShouldHaveTheSameElementsAs(typeof(C1), typeof(C2)); - container.Model.For<I2>().Instances.Select(x => x.ConcreteType).Any().ShouldBeFalse(); } - [Test] - public void do_not_register_type_if_there_are_primitive_arguments() + public interface I2 { - container.Model.HasImplementationsFor<I3>().ShouldBeFalse(); } + public interface I3 + { + } - public interface I1 { } - public interface I2 { } - public interface I3 { } + public class C1 : I1 + { + } - public class C1 : I1 { } - public class C2 : C1, I2 { } + public class C2 : C1, I2 + { + } + public class C3 : C2, I3 { public C3(string name) @@ -56,24 +59,51 @@ public class C4 { - } public class C5 : I1 { - private C5(){} + private C5() + { + } } - public interface I1<T> { } - public interface I2<T> { } - public interface I3<T> { } + public interface I1<T> + { + } - public class C1<T> : I1<T> { } - public class C2<T> : C1<T>, I2<T> { } - public class C3<T> : C2<T>, I3<T> { } + public interface I2<T> + { + } - } + public interface I3<T> + { + } + public class C1<T> : I1<T> + { + } -} + public class C2<T> : C1<T>, I2<T> + { + } + public class C3<T> : C2<T>, I3<T> + { + } + + [Test] + public void do_not_register_type_if_there_are_primitive_arguments() + { + container.Model.HasImplementationsFor<I3>().ShouldBeFalse(); + } + + [Test] + public void simple_case() + { + container.Model.For<I1>().Instances.Select(x => x.ConcreteType).ShouldHaveTheSameElementsAs(typeof (C1), + typeof (C2)); + container.Model.For<I2>().Instances.Select(x => x.ConcreteType).Any().ShouldBeFalse(); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -196,33 +196,33 @@ } [Test] - public void set_default() + public void remove_all_clears_the_defaul_and_removes_all_plugins_instances() { var family = new PluginFamily(typeof (IServiceProvider)); var instance = new SmartInstance<DataSet>(); family.SetDefault(instance); - family.GetDefaultInstance().ShouldBeTheSameAs(instance); - family.DefaultInstanceKey.ShouldEqual(instance.Name); + family.AddInstance(new NullInstance()); + family.AddType(typeof (DataSet)); + + family.RemoveAll(); + + family.DefaultInstanceKey.ShouldBeNull(); + + family.InstanceCount.ShouldEqual(0); } [Test] - public void remove_all_clears_the_defaul_and_removes_all_plugins_instances() + public void set_default() { - var family = new PluginFamily(typeof(IServiceProvider)); + var family = new PluginFamily(typeof (IServiceProvider)); var instance = new SmartInstance<DataSet>(); family.SetDefault(instance); - family.AddInstance(new NullInstance()); - family.AddType(typeof(DataSet)); - - family.RemoveAll(); - - family.DefaultInstanceKey.ShouldBeNull(); - - family.InstanceCount.ShouldEqual(0); + family.GetDefaultInstance().ShouldBeTheSameAs(instance); + family.DefaultInstanceKey.ShouldEqual(instance.Name); } [Test] Modified: trunk/Source/StructureMap.Testing/SpecificationExtensions.cs =================================================================== --- trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-12-28 05:04:44 UTC (rev 311) +++ trunk/Source/StructureMap.Testing/SpecificationExtensions.cs 2009-12-28 05:14:41 UTC (rev 312) @@ -44,7 +44,7 @@ catch (Exception) { Debug.WriteLine("ACTUAL:"); - foreach (var o in actual) + foreach (object o in actual) { Debug.WriteLine(o); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-28 05:21:36
|
Revision: 313 http://structuremap.svn.sourceforge.net/structuremap/?rev=313&view=rev Author: jeremydmiller Date: 2009-12-28 05:21:25 +0000 (Mon, 28 Dec 2009) Log Message: ----------- ding dong, the witch is dead. StructureMapConfiguration is gone. Modified Paths: -------------- trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs Removed Paths: ------------- trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap.Testing/StructureMapConfigurationDefensiveChecksTester.cs Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap/ObjectFactory.cs 2009-12-28 05:21:25 UTC (rev 313) @@ -35,8 +35,6 @@ { lock (_lockObject) { - StructureMapConfiguration.Unseal(); - _container = null; _profile = string.Empty; @@ -55,7 +53,6 @@ action(expression); PluginGraph graph = expression.BuildGraph(); - StructureMapConfiguration.Seal(); _container = new Container(graph); Profile = expression.DefaultProfileName; @@ -336,7 +333,7 @@ { if (_container == null) { - _container = buildManager(); + _container = new Container(); } } } @@ -404,17 +401,7 @@ } - private static Container buildManager() - { - PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); - StructureMapConfiguration.Seal(); - var container = new Container(graph); - container.SetDefaultsToProfile(_profile); - - return container; - } - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-28 05:21:25 UTC (rev 313) @@ -323,7 +323,6 @@ <Compile Include="Source\XmlTemplater.cs"> <SubType>Code</SubType> </Compile> - <Compile Include="StructureMapConfiguration.cs" /> <Compile Include="Diagnostics\ValidationBuildSession.cs" /> <EmbeddedResource Include="StructureMapException.resx"> <SubType>Designer</SubType> Deleted: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2009-12-28 05:21:25 UTC (rev 313) @@ -1,208 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Xml; -using StructureMap.Configuration; -using StructureMap.Configuration.DSL; -using StructureMap.Configuration.DSL.Expressions; -using StructureMap.Diagnostics; -using StructureMap.Graph; -using StructureMap.Interceptors; - -namespace StructureMap -{ - [Obsolete( - "Please use the ObjectFactory.Initialize() method for configuring the container and put configuration into Registry classes" - )] - public static class StructureMapConfiguration - { - private static GraphLog _log; - private static ConfigurationParserBuilder _parserBuilder; - private static List<Registry> _registries; - private static Registry _registry; - private static bool _sealed; - - - static StructureMapConfiguration() - { - ResetAll(); - } - - private static IConfigurationParserBuilder parserBuilder - { - get - { - assertIsNotSealed(); - return _parserBuilder; - } - } - - private static Registry registry - { - get - { - assertIsNotSealed(); - return _registry; - } - } - - /// <summary> - /// Flag to enable or disable the usage of the default StructureMap.config - /// If set to false, StructureMap will not look for a StructureMap.config file - /// </summary> - public static bool UseDefaultStructureMapConfigFile { get { return parserBuilder.UseAndEnforceExistenceOfDefaultFile; } set { parserBuilder.UseAndEnforceExistenceOfDefaultFile = value; } } - - - public static bool IgnoreStructureMapConfig { get { return parserBuilder.IgnoreDefaultFile; } set { parserBuilder.IgnoreDefaultFile = value; } } - - public static bool PullConfigurationFromAppConfig { get { return parserBuilder.PullConfigurationFromAppConfig; } set { parserBuilder.PullConfigurationFromAppConfig = value; } } - - private static void assertIsNotSealed() - { - if (_sealed) - { - throw new StructureMapException(50); - } - } - - /// <summary> - /// Programmatically adds a <StructureMap> node containing Xml configuration - /// </summary> - /// <param name="node"></param> - /// <param name="description">A description of this node source for troubleshooting purposes</param> - public static void IncludeConfigurationFromNode(XmlNode node, string description) - { - parserBuilder.IncludeNode(node, string.Empty); - } - - - /// <summary> - /// Returns the path to the StructureMap.config file - /// </summary> - /// <returns></returns> - [Obsolete("Use ConfigurationParserBuilder.GetStructureMapConfigurationPath() instead.")] - public static string GetStructureMapConfigurationPath() - { - return ConfigurationParserBuilder.GetStructureMapConfigurationPath(); - } - - /// <summary> - /// Clears StructureMapConfiguration of all configuration options. Returns StructureMap - /// to only using the default StructureMap.config file for configuration. - /// </summary> - public static void ResetAll() - { - _sealed = false; - - _log = new GraphLog(); - _parserBuilder = new ConfigurationParserBuilder(_log); - _registry = new Registry(); - _registries = new List<Registry>(); - _registries.Add(_registry); - UseDefaultStructureMapConfigFile = false; - IgnoreStructureMapConfig = false; - - ObjectFactory.Reset(); - } - - public static void RegisterInterceptor(TypeInterceptor interceptor) - { - registry.RegisterInterceptor(interceptor); - } - - /// <summary> - /// Builds a PluginGraph object for the current configuration. Used by ObjectFactory. - /// </summary> - /// <returns></returns> - internal static PluginGraph GetPluginGraph() - { - _sealed = true; - - ConfigurationParser[] parsers = _parserBuilder.GetParsers(); - - var pluginGraphBuilder = new PluginGraphBuilder(parsers, _registries.ToArray(), _log); - return pluginGraphBuilder.Build(); - } - - /// <summary> - /// Directs StructureMap to include Xml configuration information from a separate file - /// </summary> - /// <param name="filename"></param> - public static void IncludeConfigurationFromFile(string filename) - { - parserBuilder.IncludeFile(filename); - } - - - /// <summary> - /// Programmatically determine Assembly's to be scanned for attribute configuration - /// </summary> - /// <returns></returns> - public static void Scan(Action<IAssemblyScanner> action) - { - registry.Scan(action); - } - - /// <summary> - /// Direct StructureMap to create instances of Type T - /// </summary> - /// <typeparam name="PLUGINTYPE">The Type to build</typeparam> - /// <returns></returns> - public static CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>() - { - return registry.BuildInstancesOf<PLUGINTYPE>(); - } - - /// <summary> - /// Direct StructureMap to create instances of Type T - /// </summary> - /// <typeparam name="PLUGINTYPE">The Type to build</typeparam> - /// <returns></returns> - public static CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<PLUGINTYPE>() - { - return registry.BuildInstancesOf<PLUGINTYPE>(); - } - - public static GenericFamilyExpression ForRequestedType(Type pluginType) - { - return registry.ForRequestedType(pluginType); - } - - - /// <summary> - /// Starts the definition of a configuration Profile. - /// </summary> - /// <param name="profileName"></param> - /// <returns></returns> - public static ProfileExpression CreateProfile(string profileName) - { - return registry.CreateProfile(profileName); - } - - /// <summary> - /// Directs StructureMap to use a Registry class to construct the - /// PluginGraph - /// </summary> - /// <param name="registry"></param> - public static void AddRegistry(Registry registry) - { - assertIsNotSealed(); - _registries.Add(registry); - } - - public static void TheDefaultProfileIs(string profileName) - { - registry.addExpression(graph => graph.ProfileManager.DefaultProfileName = profileName); - } - - - internal static void Seal() - { - _sealed = true; - } - - internal static void Unseal() - { - _sealed = false; - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-12-28 05:21:25 UTC (rev 313) @@ -394,7 +394,6 @@ <Compile Include="ReadingRegistriesFromXmlTester.cs" /> <Compile Include="SpecificationExtensions.cs" /> <Compile Include="StructureMapConfigCreator.cs" /> - <Compile Include="StructureMapConfigurationDefensiveChecksTester.cs" /> <Compile Include="StructureMapConfigurationTester.cs" /> <Compile Include="TestData\DataMother.cs"> <SubType>Code</SubType> Deleted: trunk/Source/StructureMap.Testing/StructureMapConfigurationDefensiveChecksTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/StructureMapConfigurationDefensiveChecksTester.cs 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap.Testing/StructureMapConfigurationDefensiveChecksTester.cs 2009-12-28 05:21:25 UTC (rev 313) @@ -1,43 +0,0 @@ -using System; -using NUnit.Framework; -using StructureMap.Testing.Widget3; - -namespace StructureMap.Testing -{ - [TestFixture] - public class StructureMapConfigurationDefensiveChecksTester - { - private void shouldThrowExceptionWhenSealed(Action action) - { - StructureMapConfiguration.ResetAll(); - StructureMapConfiguration.Seal(); - - try - { - action(); - Assert.Fail("Should have thrown exception"); - } - catch (StructureMapException ex) - { - ex.ErrorCode.ShouldEqual(50); - } - } - - [Test] - public void Ensure_defensive_check_is_always_thrown_when_StructureMapConfiguration_is_sealed() - { - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.AddRegistry(null)); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.BuildInstancesOf<IGateway>()); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.CreateProfile("something")); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.ForRequestedType(typeof (IGateway))); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.IgnoreStructureMapConfig = true); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.IncludeConfigurationFromFile("something")); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.IncludeConfigurationFromNode(null, null)); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.PullConfigurationFromAppConfig = true); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.RegisterInterceptor(null)); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.Scan(x => { })); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.TheDefaultProfileIs("something")); - shouldThrowExceptionWhenSealed(() => StructureMapConfiguration.UseDefaultStructureMapConfigFile = true); - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2009-12-28 05:14:41 UTC (rev 312) +++ trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2009-12-28 05:21:25 UTC (rev 313) @@ -20,15 +20,8 @@ public void SetUp() { DataMother.RestoreStructureMapConfig(); - StructureMapConfiguration.ResetAll(); } - [TearDown] - public void TearDown() - { - StructureMapConfiguration.ResetAll(); - } - #endregion private static XmlNode createNodeFromText(string outerXml) @@ -39,19 +32,6 @@ } - public static void Bootstrap() - { - StructureMapConfiguration.UseDefaultStructureMapConfigFile = false; - StructureMapConfiguration.PullConfigurationFromAppConfig = true; - - StructureMapConfiguration.AddRegistry(new CoreRegistry()); - StructureMapConfiguration.AddRegistry(new WebRegistry()); - - StructureMapConfiguration.ForRequestedType<IGateway>().TheDefaultIsConcreteType<DefaultGateway>(); - - var gateway = ObjectFactory.GetInstance<IGateway>(); - } - public class WebRegistry : Registry { } @@ -61,20 +41,6 @@ } [Test] - public void BuildPluginGraph() - { - PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); - Assert.IsNotNull(graph); - } - - [Test] - public void Ignore_the_StructureMap_config_file_even_if_it_exists() - { - StructureMapConfiguration.IgnoreStructureMapConfig = true; - StructureMapConfiguration.GetPluginGraph().FamilyCount.ShouldEqual(0); - } - - [Test] public void PullConfigurationFromTheAppConfig() { ObjectFactory.Initialize(x => @@ -115,7 +81,7 @@ { DataMother.RemoveStructureMapConfig(); - StructureMapConfiguration.GetPluginGraph().ShouldNotBeNull(); + ObjectFactory.Initialize(x => { }); } [Test( @@ -124,11 +90,16 @@ )] public void TheDefaultInstance_has_a_dependency_upon_a_Guid_NewGuid_lambda_generated_instance() { - StructureMapConfiguration.IgnoreStructureMapConfig = true; + ObjectFactory.Initialize(x => + { + x.IgnoreStructureMapConfig = true; - StructureMapConfiguration.ForRequestedType<Guid>().TheDefault.Is.ConstructedBy(() => Guid.NewGuid()); - StructureMapConfiguration.ForRequestedType<IFoo>().TheDefaultIsConcreteType<Foo>(); + x.ForRequestedType<Guid>().TheDefault.Is.ConstructedBy(() => Guid.NewGuid()); + x.ForRequestedType<IFoo>().TheDefaultIsConcreteType<Foo>(); + }); + + Assert.That(ObjectFactory.GetInstance<IFoo>().SomeGuid != Guid.Empty); } @@ -138,31 +109,18 @@ )] public void TheDefaultInstanceIsALambdaForGuidNewGuid() { - StructureMapConfiguration.IgnoreStructureMapConfig = true; - StructureMapConfiguration.ForRequestedType<Guid>().TheDefault.Is.ConstructedBy(() => Guid.NewGuid()); + ObjectFactory.Initialize(x => + { + x.IgnoreStructureMapConfig = true; - Assert.That(ObjectFactory.GetInstance<Guid>() != Guid.Empty); - } + x.ForRequestedType<Guid>().TheDefault.Is.ConstructedBy(() => Guid.NewGuid()); + }); - [Test] - public void TheDefaultNameIs_should_set_the_default_profile_name() - { - StructureMapConfiguration.IgnoreStructureMapConfig = true; - string theDefaultProfileName = "the default profile"; - StructureMapConfiguration.TheDefaultProfileIs(theDefaultProfileName); - - PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); - graph.ProfileManager.DefaultProfileName.ShouldEqual(theDefaultProfileName); + Assert.That(ObjectFactory.GetInstance<Guid>() != Guid.Empty); } - [Test] - public void Use_the_StructureMap_config_file_if_it_exists() - { - DataMother.RestoreStructureMapConfig(); - StructureMapConfiguration.GetPluginGraph().FamilyCount.ShouldBeGreaterThan(0); - } } public interface IFoo This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-29 02:55:20
|
Revision: 314 http://structuremap.svn.sourceforge.net/structuremap/?rev=314&view=rev Author: jeremydmiller Date: 2009-12-29 02:55:12 +0000 (Tue, 29 Dec 2009) Log Message: ----------- API cleanup Part 1 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/Configuration/DSL/Expressions/ProfileExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/InitializationExpression.cs trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs trunk/Source/StructureMap.Testing/Examples.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/ContainerTester.cs trunk/Source/StructureMap.Testing/ObjectFactoryInitializeTester.cs trunk/Source/StructureMap.Testing/Pipeline/NestedContainerSupportTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs Added Paths: ----------- trunk/Source/StructureMap/Configuration/DSL/IRegistry.cs Removed Paths: ------------- trunk/Source/StructureMap.Testing/Graph/ExplicitArgumentTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -32,6 +32,7 @@ /// <summary> /// Define the Default Instance for this PluginType /// </summary> + [Obsolete("Prefer the Use() methods")] public IsExpression<PLUGINTYPE> TheDefault { get { return new InstanceExpression<PLUGINTYPE>(i => registerDefault(i)); } } public InstanceExpression<PLUGINTYPE> MissingNamedInstanceIs { get { return new InstanceExpression<PLUGINTYPE>(i => _alterations.Add(family => family.MissingInstance = i)); } } @@ -135,6 +136,7 @@ /// </summary> /// <param name="scope"></param> /// <returns></returns> + [Obsolete("Change to LifecycleIs(scope)")] public CreatePluginFamilyExpression<PLUGINTYPE> CacheBy(InstanceScope scope) { return alterAndContinue(family => family.SetScopeTo(scope)); @@ -296,6 +298,7 @@ /// </summary> /// <typeparam name="PLUGGEDTYPE"></typeparam> /// <returns></returns> + [Obsolete("Change to Add<T>()")] public CreatePluginFamilyExpression<PLUGINTYPE> AddConcreteType<PLUGGEDTYPE>() { if (!PluginCache.GetPlugin(typeof (PLUGGEDTYPE)).CanBeAutoFilled) @@ -366,9 +369,28 @@ public ObjectInstance Add(PLUGINTYPE @object) { var instance = new ObjectInstance(@object); - _alterations.Add(f => f.AddInstance(instance)); + Add(instance); return instance; } + + /// <summary> + /// Add an Instance to this type created by a Lambda + /// </summary> + /// <param name="func"></param> + /// <returns></returns> + public LambdaInstance<PLUGINTYPE> Add(Func<IContext, PLUGINTYPE> func) + { + var instance = new LambdaInstance<PLUGINTYPE>(func); + Add(instance); + + return instance; + } + + + public void Add(Instance instance) + { + _alterations.Add(f => f.AddInstance(instance)); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -39,6 +39,7 @@ /// </summary> /// <param name="concreteType"></param> /// <returns></returns> + [Obsolete("Change to Use(concreteType)")] public ConfiguredInstance TheDefaultIsConcreteType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); @@ -47,6 +48,10 @@ return instance; } + /// <summary> + /// Use this configured Instance as is + /// </summary> + /// <param name="instance"></param> public void Use(Instance instance) { alterAndContinue(family => @@ -131,10 +136,18 @@ { return alterAndContinue(family => family.AddInstance(instance)); } + + /// <summary> + /// Configure this type as the supplied value + /// </summary> + /// <param name="instance"></param> + /// <returns></returns> + public ObjectInstance Add(object @object) + { + var instance = new ObjectInstance(@object); + Add(instance); - public GenericFamilyExpression Add(object instance) - { - return Add(new ObjectInstance(instance)); + return instance; } @@ -144,6 +157,7 @@ /// </summary> /// <param name="scope"></param> /// <returns></returns> + [Obsolete("Change to LifecycleIs()")] public GenericFamilyExpression CacheBy(InstanceScope scope) { return alterAndContinue(family => family.SetScopeTo(scope)); @@ -261,6 +275,7 @@ /// </summary> /// <param name="concreteType"></param> /// <returns></returns> + [Obsolete("Change to Add(Type)")] public GenericFamilyExpression AddConcreteType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); @@ -276,6 +291,7 @@ /// <param name="concreteType"></param> /// <param name="instanceName"></param> /// <returns></returns> + [Obsolete("Change to Add(Type, [name)]")] public GenericFamilyExpression AddConcreteType(Type concreteType, string instanceName) { return Add(new ConfiguredInstance(concreteType).WithName(instanceName)); Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -106,7 +106,16 @@ [Obsolete("Favor For<ISomething>().Use<Something>() or For<ISomething>().Add<Something>()")] SmartInstance<PLUGGEDTYPE> OfConcreteType<PLUGGEDTYPE>() where PLUGGEDTYPE : T; + /// <summary> + /// Build the Instance with the constructor function and setter arguments. Starts + /// the definition of a <see cref="SmartInstance{T}">SmartInstance</see> + /// </summary> + /// <typeparam name="PLUGGEDTYPE"></typeparam> + /// <returns></returns> + SmartInstance<PLUGGEDTYPE> Type<PLUGGEDTYPE>(); + + /// <summary> /// Build the Instance with the constructor function and setter arguments. Use this /// method for open generic types, and favor the generic version of OfConcreteType /// for all other types @@ -117,12 +126,20 @@ ConfiguredInstance OfConcreteType(Type type); /// <summary> + /// Build the Instance with the constructor function and setter arguments. Use this + /// method for open generic types, and favor the generic version of OfConcreteType + /// for all other types + /// </summary> + /// <param name="type"></param> + /// <returns></returns> + ConfiguredInstance Type(Type type); + + /// <summary> /// Create an Instance that builds an object by calling a Lambda or /// an anonymous delegate with no arguments /// </summary> /// <param name="func"></param> /// <returns></returns> - [Obsolete("Prefer For<T>().Use(Func<T> func) or For<T>().Add(Func<T> func)")] LambdaInstance<T> ConstructedBy(Func<T> func); /// <summary> @@ -218,11 +235,21 @@ return returnInstance(new SmartInstance<PLUGGEDTYPE>()); } + public SmartInstance<PLUGGEDTYPE> Type<PLUGGEDTYPE>() + { + return returnInstance(new SmartInstance<PLUGGEDTYPE>()); + } + public ConfiguredInstance OfConcreteType(Type type) { return returnInstance(new ConfiguredInstance(type)); } + public ConfiguredInstance Type(Type type) + { + return returnInstance(new ConfiguredInstance(type)); + } + public ObjectInstance Object(T theObject) { return returnInstance(new ObjectInstance(theObject)); Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -35,6 +35,7 @@ /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> + [Obsolete("Prefer the For<T>().**** methods")] public IsExpression<T> Type<T>() { return @@ -76,22 +77,37 @@ /// </summary> /// <param name="concreteType"></param> /// <returns></returns> - public ProfileExpression UseConcreteType(Type concreteType) + [Obsolete("Change to Use(type)")] + public ConfiguredInstance UseConcreteType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); - return Use(instance); + Use(instance); + + return instance; } /// <summary> + /// Use this concreteType for the Instance of this Profile for the PluginType + /// </summary> + /// <param name="concreteType"></param> + /// <returns></returns> + [Obsolete("Change to Use(type)")] + public ConfiguredInstance Use(Type concreteType) + { + var instance = new ConfiguredInstance(concreteType); + Use(instance); + + return instance; + } + + /// <summary> /// Use this Instance for the Profile Instance of this Plugin Type /// </summary> /// <param name="instance"></param> /// <returns></returns> - public ProfileExpression Use(Instance instance) + public void Use(Instance instance) { _registry.addExpression(graph => graph.SetDefault(_parent._profileName, _pluginType, instance)); - - return _parent; } @@ -100,11 +116,38 @@ /// </summary> /// <param name="name"></param> /// <returns></returns> - public ProfileExpression UseNamedInstance(string name) + [Obsolete("Change to Use([name])")] + public void UseNamedInstance(string name) { var instance = new ReferencedInstance(name); - return Use(instance); + Use(instance); } + + /// <summary> + /// Use the named Instance as the Profile Instance for this PluginType + /// </summary> + /// <param name="name"></param> + /// <returns></returns> + public ReferencedInstance Use(string name) + { + var instance = new ReferencedInstance(name); + Use(instance); + + return instance; + } + + /// <summary> + /// For this type and profile, build the object with this Lambda + /// </summary> + /// <param name="func"></param> + /// <returns></returns> + public LambdaInstance<object> Use(Func<IContext, object> func) + { + var instance = new LambdaInstance<object>(func); + Use(instance); + + return instance; + } } #endregion @@ -133,6 +176,7 @@ /// </summary> /// <param name="instanceKey"></param> /// <returns></returns> + [Obsolete("Change to For<T>().Use([name])")] public ProfileExpression UseNamedInstance(string instanceKey) { _registry.addExpression( @@ -142,17 +186,39 @@ } /// <summary> + /// Use a named, preconfigured instance as the default instance for this profile + /// </summary> + /// <param name="instanceKey"></param> + /// <returns></returns> + public void Use(string instanceKey) + { + _registry.addExpression( + graph => graph.SetDefault(_profileName, typeof(T), new ReferencedInstance(instanceKey))); + } + + /// <summary> /// Define the default instance of the PluginType for the containing Profile /// </summary> /// <param name="instance"></param> /// <returns></returns> - public ProfileExpression Use(Instance instance) + public void Use(Instance instance) { instance.Name = "Default Instance for Profile " + _profileName; _registry.addExpression(graph => graph.SetDefault(_profileName, typeof (T), instance)); + } - return _parent; + /// <summary> + /// For this Profile, use an Instance with this Func + /// </summary> + /// <param name="func"></param> + /// <returns></returns> + public LambdaInstance<T> Use(Func<T> func) + { + var instance = new LambdaInstance<T>(func); + Use(instance); + + return instance; } /// <summary> @@ -160,10 +226,12 @@ /// </summary> /// <param name="func"></param> /// <returns></returns> - public ProfileExpression Use(Func<T> func) + public LambdaInstance<T> Use(Func<IContext, T> func) { var instance = new LambdaInstance<T>(func); - return Use(instance); + Use(instance); + + return instance; } /// <summary> @@ -171,10 +239,12 @@ /// </summary> /// <param name="t"></param> /// <returns></returns> - public ProfileExpression Use(T t) + public ObjectInstance Use(T t) { var instance = new ObjectInstance(t); - return Use(instance); + Use(instance); + + return instance; } /// <summary> @@ -182,11 +252,25 @@ /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> /// <returns></returns> - public ProfileExpression UseConcreteType<CONCRETETYPE>() + [Obsolete("Change to For<T>().Use<CONCRETETYPE>()")] + public void UseConcreteType<CONCRETETYPE>() { var instance = new ConfiguredInstance(typeof (CONCRETETYPE)); - return Use(instance); + Use(instance); } + + /// <summary> + /// For this profile, use this concrete type + /// </summary> + /// <typeparam name="CONCRETETYPE"></typeparam> + /// <returns></returns> + public SmartInstance<CONCRETETYPE> Use<CONCRETETYPE>() + { + var instance = new SmartInstance<CONCRETETYPE>(); + Use(instance); + + return instance; + } } #endregion Added: trunk/Source/StructureMap/Configuration/DSL/IRegistry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/IRegistry.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/IRegistry.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -0,0 +1,235 @@ +using System; +using System.Linq.Expressions; +using StructureMap.Configuration.DSL.Expressions; +using StructureMap.Graph; +using StructureMap.Interceptors; +using StructureMap.Pipeline; + +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 Profile(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 Profile(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> + /// 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); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -8,232 +8,6 @@ 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> - /// 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> /// 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. @@ -253,7 +27,8 @@ private readonly List<Action> _basicActions = new List<Action>(); /// <summary> - /// Adds the concreteType as an Instance of the pluginType + /// Adds the concreteType as an Instance of the pluginType. Mostly useful + /// for conventions /// </summary> /// <param name="pluginType"></param> /// <param name="concreteType"></param> @@ -263,7 +38,8 @@ } /// <summary> - /// Adds the concreteType as an Instance of the pluginType with a name + /// Adds the concreteType as an Instance of the pluginType with a name. Mostly + /// useful for conventions /// </summary> /// <param name="pluginType"></param> /// <param name="concreteType"></param> @@ -274,8 +50,8 @@ } /// <summary> - /// Add the pluggedType as an instance to any configured pluginType where pluggedType - /// could be assigned to the pluginType + /// Add the pluggedType as an instance to any configured pluginType where pluggedType. + /// Mostly useful for conventions /// </summary> /// <param name="pluggedType"></param> public void AddType(Type pluggedType) @@ -330,7 +106,7 @@ /// <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].************** + /// For[T]().Use[T].************** /// when the PluginType and ConcreteType are the same Type /// </summary> /// <typeparam name="T"></typeparam> @@ -355,7 +131,7 @@ } /// <summary> - /// Convenience method. Equivalent of ForRequestedType[PluginType]().AsSingletons() + /// Convenience method. Equivalent of ForRequestedType[PluginType]().Singletons() /// </summary> /// <typeparam name="PLUGINTYPE"></typeparam> /// <returns></returns> @@ -414,7 +190,8 @@ /// </summary> /// <param name="profileName"></param> /// <returns></returns> - public ProfileExpression CreateProfile(string profileName) + [Obsolete("Change to Profile( [name], Action<ProfileExpression> )")] + public ProfileExpression Profile(string profileName) { var expression = new ProfileExpression(profileName, this); @@ -428,7 +205,7 @@ /// </summary> /// <param name="profileName"></param> /// <param name="action"></param> - public void CreateProfile(string profileName, Action<ProfileExpression> action) + public void Profile(string profileName, Action<ProfileExpression> action) { var expression = new ProfileExpression(profileName, this); action(expression); @@ -525,6 +302,7 @@ /// </summary> /// <typeparam name="PLUGINTYPE"></typeparam> /// <param name="object"></param> + [Obsolete("Prefer For<T>().Use(value)")] public void Register<PLUGINTYPE>(PLUGINTYPE @object) { ForRequestedType<PLUGINTYPE>().TheDefault.IsThis(@object); @@ -535,13 +313,16 @@ /// </summary> /// <typeparam name="PLUGINTYPE"></typeparam> /// <param name="instance"></param> + [Obsolete("Prefer For<T>().Use(instance)")] public void Register<PLUGINTYPE>(Instance instance) { ForRequestedType<PLUGINTYPE>().TheDefault.IsThis(instance); } /// <summary> - /// Shorthand for ForRequestedType<PLUGINTYPE>() + /// 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> @@ -551,9 +332,10 @@ } /// <summary> - /// Shorthand for ForRequestedType(pluginType) + /// 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> - /// <param name="pluginType"></param> /// <returns></returns> public GenericFamilyExpression For(Type pluginType) { Modified: trunk/Source/StructureMap/InitializationExpression.cs =================================================================== --- trunk/Source/StructureMap/InitializationExpression.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/InitializationExpression.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -119,7 +119,7 @@ /// </summary> /// <param name="profileName"></param> /// <returns></returns> - ProfileExpression CreateProfile(string profileName); + ProfileExpression Profile(string profileName); /// <summary> /// An alternative way to use CreateProfile that uses ProfileExpression @@ -128,7 +128,7 @@ /// </summary> /// <param name="profileName"></param> /// <param name="action"></param> - void CreateProfile(string profileName, Action<ProfileExpression> action); + void Profile(string profileName, Action<ProfileExpression> action); /// <summary> /// Registers a new TypeInterceptor object with the Container Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/ObjectFactory.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -400,8 +400,6 @@ } } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -190,12 +190,19 @@ /// </summary> /// <param name="instanceKey"></param> /// <returns></returns> + [Obsolete("Change to Named")] public T WithName(string instanceKey) { Name = instanceKey; return thisInstance; } + public T Named(string instanceKey) + { + Name = instanceKey; + return thisInstance; + } + /// <summary> /// Register an Action to perform on the object created by this Instance /// before it is returned to the caller Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap/StructureMap.csproj 2009-12-29 02:55:12 UTC (rev 314) @@ -337,6 +337,7 @@ <Compile Include="ConfigurationExpression.cs" /> <Compile Include="Configuration\DictionaryReader.cs" /> <Compile Include="Configuration\DSL\Expressions\InstanceExpression.cs" /> + <Compile Include="Configuration\DSL\IRegistry.cs" /> <Compile Include="Configuration\DSL\SetterConvention.cs" /> <Compile Include="Configuration\ITypeReader.cs" /> <Compile Include="Configuration\PrimitiveArrayReader.cs" /> Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/ProfileExpressionTester.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -24,14 +24,23 @@ { string theProfileName = "something"; - IContainer manager = new Container(registry => registry.CreateProfile(theProfileName) - .For<IWidget>().Use(delegate { return new AWidget(); }) - .For<Rule>().Use(delegate { return new DefaultRule(); })); + IContainer container = new Container(r => + { + r.Profile(theProfileName, x => + { + x.For<IWidget>().Use(() => new AWidget()); + x.For<Rule>().Use(() => new DefaultRule()); + }); - manager.SetDefaultsToProfile(theProfileName); - Assert.IsInstanceOfType(typeof (AWidget), manager.GetInstance<IWidget>()); - Assert.IsInstanceOfType(typeof (DefaultRule), manager.GetInstance<Rule>()); + + + }); + + container.SetDefaultsToProfile(theProfileName); + + Assert.IsInstanceOfType(typeof (AWidget), container.GetInstance<IWidget>()); + Assert.IsInstanceOfType(typeof (DefaultRule), container.GetInstance<Rule>()); } @@ -42,7 +51,7 @@ IContainer container = new Container(registry => { - registry.CreateProfile(theProfileName, x => + registry.Profile(theProfileName, x => { x.Type<IWidget>().Is.ConstructedBy(() => new AWidget()); x.Type<Rule>().Is.ConstructedBy(() => new DefaultRule()); @@ -61,9 +70,15 @@ { string theProfileName = "something"; - IContainer manager = new Container(registry => registry.CreateProfile(theProfileName) - .For<IWidget>().UseConcreteType<AWidget>() - .For<Rule>().UseConcreteType<DefaultRule>()); + IContainer manager = new Container(registry => + { + registry.Profile(theProfileName, p => + { + p.For<IWidget>().UseConcreteType<AWidget>(); + p.For<Rule>().UseConcreteType<DefaultRule>(); + }); + + }); manager.SetDefaultsToProfile(theProfileName); Assert.IsInstanceOfType(typeof (AWidget), manager.GetInstance<IWidget>()); @@ -77,7 +92,7 @@ var theWidget = new AWidget(); string theProfileName = "something"; - registry.CreateProfile(theProfileName) + registry.Profile(theProfileName) .For<IWidget>().Use(theWidget); PluginGraph graph = registry.Build(); @@ -93,7 +108,7 @@ string theDefaultName = "TheDefaultName"; var registry = new Registry(); - registry.CreateProfile(theProfileName) + registry.Profile(theProfileName) .For<IWidget>().UseNamedInstance(theDefaultName) .For<Rule>().UseNamedInstance("DefaultRule"); @@ -110,7 +125,7 @@ string theProfileName = "TheProfile"; var registry = new Registry(); - registry.CreateProfile(theProfileName) + registry.Profile(theProfileName) .For<IWidget>().UseConcreteType<AWidget>(); PluginGraph graph = registry.Build(); @@ -118,7 +133,7 @@ ProfileManager profileManager = graph.ProfileManager; Instance defaultInstance = profileManager.GetDefault(typeof (IWidget), theProfileName); - Assert.AreEqual(Profile.InstanceKeyForProfile(theProfileName), defaultInstance.Name); + Assert.AreEqual(StructureMap.Pipeline.Profile.InstanceKeyForProfile(theProfileName), defaultInstance.Name); var manager = new Container(graph); manager.SetDefaultsToProfile(theProfileName); Modified: trunk/Source/StructureMap.Testing/Examples.cs =================================================================== --- trunk/Source/StructureMap.Testing/Examples.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap.Testing/Examples.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -430,7 +430,7 @@ // Use the InstanceExpression to define the default Instance // of a PluginType within a Profile - CreateProfile("Connected", x => { x.Type<IService>().Is.OfConcreteType<RemoteService>(); }); + Profile("Connected", x => { x.Type<IService>().Is.OfConcreteType<RemoteService>(); }); } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -161,8 +161,8 @@ { var container = new Container(registry => { - registry.CreateProfile("1").For(typeof (IService<>)).UseConcreteType(typeof (Service<>)); - registry.CreateProfile("2").For(typeof (IService<>)).UseConcreteType(typeof (Service2<>)); + registry.Profile("1").For(typeof (IService<>)).UseConcreteType(typeof (Service<>)); + registry.Profile("2").For(typeof (IService<>)).UseConcreteType(typeof (Service2<>)); }); container.SetDefaultsToProfile("1"); @@ -182,8 +182,8 @@ r.InstanceOf(typeof (IService<>)).Is(typeof (Service<>)).WithName("Service1"); r.InstanceOf(typeof (IService<>)).Is(typeof (Service2<>)).WithName("Service2"); - r.CreateProfile("1").For(typeof (IService<>)).UseNamedInstance("Service1"); - r.CreateProfile("2").For(typeof (IService<>)).UseNamedInstance("Service2"); + r.Profile("1").For(typeof (IService<>)).UseNamedInstance("Service1"); + r.Profile("2").For(typeof (IService<>)).UseNamedInstance("Service2"); }); manager.SetDefaultsToProfile("1"); Modified: trunk/Source/StructureMap.Testing/Graph/ContainerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/ContainerTester.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap.Testing/Graph/ContainerTester.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -89,8 +89,8 @@ x.OfConcreteType<ColorService>().WithName("Green").WithProperty("color").EqualTo("Green"); }); - r.CreateProfile("Red").For<IService>().UseNamedInstance("Red"); - r.CreateProfile("Blue").For<IService>().UseNamedInstance("Blue"); + r.Profile("Red").For<IService>().UseNamedInstance("Red"); + r.Profile("Blue").For<IService>().UseNamedInstance("Blue"); }); assertColorIs(container, "Orange"); Deleted: trunk/Source/StructureMap.Testing/Graph/ExplicitArgumentTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/ExplicitArgumentTester.cs 2009-12-28 05:21:25 UTC (rev 313) +++ trunk/Source/StructureMap.Testing/Graph/ExplicitArgumentTester.cs 2009-12-29 02:55:12 UTC (rev 314) @@ -1,419 +0,0 @@ -using System.Collections.Generic; -using NUnit.Framework; -using StructureMap.Pipeline; -using StructureMap.Testing.Pipeline; - -namespace StructureMap.Testing.Graph -{ - [TestFixture] - public class ExplicitArgumentTester - { - #region Setup/Teardown - - [SetUp] - public void SetUp() - { - ObjectFactory.Initialize(x => { x.UseDefaultStructureMapConfigFile = false; }); - } - - #endregion - - public interface IExplicitTarget - { - } - - public class RedTarget : IExplicitTarget - { - } - - public class GreenTarget : IExplicitTarget - { - } - - public class ExplicitTarget : IExplicitTarget - { - private readonly string _name; - private readonly IProvider _provider; - - public ExplicitTarget(string name, IProvider provider) - { - _name = name; - _provider = provider; - } - - - public string Name - { - get { return _name; } - } - - public IProvider Provider - { - get { return _provider; } - } - } - - public interface IProvider - { - } - - public class RedProvider : IProvider - { - } - - public class BlueProvider : IProvider - { - } - - [Test] - public void Example() - { - IContainer container = new Container(); - var theTrade = new Trade(); - - var view = container.With(theTrade).GetInstance<TradeView>(); - - view.Trade.ShouldBeTheSameAs(theTrade); - } - - [Test] - public void Explicit_services_are_used_throughout_the_object_graph() - { - var theTrade = new Trade(); - - IContainer container = new Container(r => - { - r.ForRequestedType<IView>().TheDefaultIsConcreteType<TradeView>(); - r.ForRequestedType<Node>().TheDefaultIsConcreteType<TradeNode>(); - }); - - var command = container.With(theTrade).GetInstance<Command>(); - - command.Trade.ShouldBeTheSameAs(theTrade); - command.Node.IsType<TradeNode>().Trade.ShouldBeTheSameAs(theTrade); - command.View.IsType<TradeView>().Trade.ShouldBeTheSameAs(theTrade); - } - - [Test] - public void ExplicitArguments_can_return_child_by_name() - { - var args = new ExplicitArguments(); - var theNode = new Node(); - args.SetArg("node", theNode); - - IConfiguredInstance instance = new ExplicitInstance(typeof (Command), args, null); - - Assert.AreSame(theNode, instance.GetChild("node", typeof (Node), new StubBuildSession())); - } - - [Test] - public void Fill_in_argument_by_name() - { - var container = new Container(x => - { - x.ForRequestedType<IView>().TheDefaultIsConcreteType<View>(); - }); - - var theNode = new Node(); - var theTrade = new Trade(); - - var command = container - .With("node").EqualTo(theNode) - .With(theTrade) - .GetInstance<Command>(); - - Assert.IsInstanceOfType(typeof (View), command.View); - Assert.AreSame(theNode, command.Node); - Assert.AreSame(theTrade, command.Trade); - } - - [Test] - public void NowDoItWithObjectFactoryItself() - { - ObjectFactory.Initialize(x => - { - x.ForConcreteType<ExplicitTarget>().Configure - .CtorDependency<IProvider>().Is<RedProvider>() - .WithCtorArg("name").EqualTo("Jeremy"); - }); - - // Get the ExplicitTarget without setting an explicit arg for IProvider - var firstTarget = ObjectFactory.GetInstance<ExplicitTarget>(); - Assert.IsInstanceOfType(typeof (RedProvider), firstTarget.Provider); - - // Now, set the explicit arg for IProvider - var theBlueProvider = new BlueProvider(); - var secondTarget = ObjectFactory.With<IProvider>(theBlueProvider).GetInstance<ExplicitTarget>(); - Assert.AreSame(theBlueProvider, secondTarget.Provider); - } - - [Test] - public void NowDoItWithObjectFactoryItself_with_new_API() - { - ObjectFactory.Initialize(x => - { - x.ForRequestedType<ExplicitTarget>().TheDefault.Is.OfConcreteType<ExplicitTarget>() - .CtorDependency<IProvider>().Is(child => child.OfConcreteType<RedProvider>()) - .WithCtorArg("name").EqualTo("Jeremy"); - }); - - // Get the ExplicitTarget without setting an explicit arg for IProvider - ObjectFactory.GetInstance<ExplicitTarget>().Provider.IsType<RedProvider>(); - - // Now, set the explicit arg for IProvider - var theBlueProvider = new BlueProvider(); - ObjectFactory.With<IProvider>(theBlueProvider).GetInstance<ExplicitTarget>() - .Provider.ShouldBeTheSameAs(theBlueProvider); - } - - [Test] - public void OverrideAPrimitiveWithObjectFactory() - { - ObjectFactory.Initialize(x => - { - x.ForConcreteType<ExplicitTarget>().Configure - .CtorDependency<IProvider>().Is<RedProvider>() - .WithCtorArg("name").EqualTo("Jeremy"); - }); - - // Get the ExplicitTarget without setting an explicit arg for IProvider - var firstTarget = ObjectFactory.GetInstance<ExplicitTarget>(); - Assert.AreEqual("Jeremy", firstTarget.Name); - - // Now, set the explicit arg for IProvider - var secondTarget = ObjectFactory.With("name").EqualTo("Julia").GetInstance<ExplicitTarget>(); - Assert.AreEqual("Julia", secondTarget.Name); - } - - [Test] - public void pass_explicit_service_into_all_instances() - { - // The Container is constructed with 2 instances - // of TradeView - var container = new Container(r => - { - r.ForRequestedType<TradeView>() - .TheDefaultIsConcreteType<TradeView>() - .AddConcreteType<SecuredTradeView>(); - }); - - var theTrade = new Trade(); - - IList<TradeView> views = container.With(theTrade).GetAllInstances<TradeView>(); - - views[0].Trade.ShouldBeTheSameAs(theTrade); - views[1].Trade.ShouldBeTheSameAs(theTrade); - } - - [Test] - public void Pass_in_arguments_as_dictionary() - { - var container = new Container(x => - { - x.ForRequestedType<IView>().TheDefaultIsConcreteType<View>(); - }); - - var theNode = new Node(); - var theTrade = new Trade(); - - var args = new ExplicitArguments(); - args.Set(theNode); - args.SetArg("trade", theTrade); - - var command = container.GetInstance<Command>(args); - - Assert.IsInstanceOfType(typeof (View), command.View); - Assert... [truncated message content] |
From: <jer...@us...> - 2009-12-29 03:50:22
|
Revision: 315 http://structuremap.svn.sourceforge.net/structuremap/?rev=315&view=rev Author: jeremydmiller Date: 2009-12-29 03:50:13 +0000 (Tue, 29 Dec 2009) Log Message: ----------- marking more things [Obsolete]. Depending on what happens with the migration to other projects, this might be the 2.5.4 release Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Extensions.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap/Pipeline/SmartInstance.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/ObjectFactoryInitializeTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -374,6 +374,14 @@ return instance; } + public SmartInstance<PLUGGEDTYPE> Add<PLUGGEDTYPE>() + { + var instance = new SmartInstance<PLUGGEDTYPE>(); + Add(instance); + + return instance; + } + /// <summary> /// Add an Instance to this type created by a Lambda /// </summary> Modified: trunk/Source/StructureMap/Extensions.cs =================================================================== --- trunk/Source/StructureMap/Extensions.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Extensions.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -15,6 +15,16 @@ return enumerable; } + public static bool IsEmpty(this string value) + { + return string.IsNullOrEmpty(value); + } + + public static bool IsNotEmpty(this string value) + { + return !value.IsEmpty(); + } + 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-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -1,8 +1,10 @@ using System; +using System.Collections.Generic; using System.Linq.Expressions; using System.Reflection; using StructureMap.Pipeline; using StructureMap.TypeRules; +using System.Linq; namespace StructureMap.Graph { @@ -75,11 +77,21 @@ return FindArgumentNameForType(typeof (T)); } + public string FindArgumentNameForEnumerableOf(Type type) + { + var enumerableTypes = EnumerableInstance.OpenEnumerableTypes.Select(x => x.MakeGenericType(type)).Union(new []{type.MakeArrayType()}); + return enumerableTypes.Select(t => + { + return _constructor.FindFirstConstructorArgumentOfType(t) ?? + _setters.FindFirstWriteablePropertyOfType(t); + }).FirstOrDefault(x => x != null); + } + public string FindArgumentNameForType(Type type) { string returnValue = _constructor.FindFirstConstructorArgumentOfType(type) ?? - _setters.FindFirstConstructorArgumentOfType(type); + _setters.FindFirstWriteablePropertyOfType(type); if (returnValue == null) { Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -107,7 +107,7 @@ return returnValue; } - public string FindFirstConstructorArgumentOfType(Type type) + public string FindFirstWriteablePropertyOfType(Type type) { foreach (SetterProperty setterProperty in this) { Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -6,12 +6,19 @@ { public partial class ConfiguredInstance { + [Obsolete("Change to Named()")] public ConfiguredInstance WithName(string instanceKey) { Name = instanceKey; return this; } + public ConfiguredInstance Named(string instanceKey) + { + Name = instanceKey; + return this; + } + /// <summary> /// Register an Action to perform on the object created by this Instance /// before it is returned to the caller @@ -202,6 +209,7 @@ /// </summary> /// <param name="propertyName"></param> /// <returns></returns> + [Obsolete("Change to DependencyExpression<CTORTYPE> instead")] public PropertyExpression<ConfiguredInstance> WithCtorArg(string propertyName) { return new PropertyExpression<ConfiguredInstance>(this, propertyName); Modified: trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -13,6 +13,14 @@ typeof (List<>) }; + public static IEnumerable<Type> OpenEnumerableTypes + { + get + { + return _enumerableTypes; + } + } + private readonly IEnumerable<Instance> _children; private readonly IEnumerableCoercion _coercion; private readonly string _description; @@ -83,5 +91,7 @@ return type.IsGenericType && type.GetGenericTypeDefinition().IsIn(_enumerableTypes); } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -6,6 +6,7 @@ /// <summary> /// Defines the value of a primitive argument to a constructur argument /// </summary> + [Obsolete("Change to DependencyExpression<CTORTYPE> instead")] public class PropertyExpression<T> where T : ConstructorInstance { private readonly ConstructorInstance _instance; Modified: trunk/Source/StructureMap/Pipeline/SmartInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.Linq.Expressions; using StructureMap.Configuration.DSL.Expressions; @@ -26,6 +27,7 @@ /// </summary> /// <param name="instanceKey"></param> /// <returns></returns> + [Obsolete("Please change to Named(instanceKey)")] public SmartInstance<T> WithName(string instanceKey) { Name = instanceKey; @@ -33,6 +35,17 @@ } /// <summary> + /// Sets the name of this Instance + /// </summary> + /// <param name="instanceKey"></param> + /// <returns></returns> + public SmartInstance<T> Named(string instanceKey) + { + Name = instanceKey; + return this; + } + + /// <summary> /// Register an Action to perform on the object created by this Instance /// before it is returned to the caller /// </summary> @@ -132,6 +145,7 @@ /// </summary> /// <param name="argumentName"></param> /// <returns></returns> + [Obsolete("Use CtorDependency() instead")] public PropertyExpression<SmartInstance<T>> WithCtorArg(string argumentName) { return new PropertyExpression<SmartInstance<T>>(this, argumentName); @@ -187,12 +201,25 @@ /// </summary> /// <typeparam name="CTORTYPE"></typeparam> /// <returns></returns> + [Obsolete("Change to Ctor<>()")] public DependencyExpression<CTORTYPE> CtorDependency<CTORTYPE>() { string constructorArg = getArgumentNameForType<CTORTYPE>(); return CtorDependency<CTORTYPE>(constructorArg); } + /// <summary> + /// Inline definition of a constructor dependency. Select the constructor argument by type. Do not + /// use this method if there is more than one constructor arguments of the same type + /// </summary> + /// <typeparam name="CTORTYPE"></typeparam> + /// <returns></returns> + public DependencyExpression<CTORTYPE> Ctor<CTORTYPE>() + { + string constructorArg = getArgumentNameForType<CTORTYPE>(); + return CtorDependency<CTORTYPE>(constructorArg); + } + private string getArgumentNameForType<CTORTYPE>() { Plugin plugin = PluginCache.GetPlugin(getConcreteType(null)); @@ -206,17 +233,31 @@ /// <typeparam name="CTORTYPE"></typeparam> /// <param name="constructorArg"></param> /// <returns></returns> + [Obsolete("Use Ctor<CTORTYPE>(constructorArg)")] public DependencyExpression<CTORTYPE> CtorDependency<CTORTYPE>(string constructorArg) { return new DependencyExpression<CTORTYPE>(this, constructorArg); } /// <summary> + /// Inline definition of a constructor dependency. Select the constructor argument by type and constructor name. + /// Use this method if there is more than one constructor arguments of the same type + /// </summary> + /// <typeparam name="CTORTYPE"></typeparam> + /// <param name="constructorArg"></param> + /// <returns></returns> + public DependencyExpression<CTORTYPE> Ctor<CTORTYPE>(string constructorArg) + { + return new DependencyExpression<CTORTYPE>(this, constructorArg); + } + + /// <summary> /// Inline definition of a setter dependency. The property name is specified with an Expression /// </summary> /// <typeparam name="SETTERTYPE"></typeparam> /// <param name="expression"></param> /// <returns></returns> + [Obsolete("Use Setter()")] public DependencyExpression<SETTERTYPE> SetterDependency<SETTERTYPE>( Expression<Func<T, SETTERTYPE>> expression) { @@ -224,23 +265,51 @@ return new DependencyExpression<SETTERTYPE>(this, propertyName); } + /// <summary> + /// Inline definition of a setter dependency. The property name is specified with an Expression + /// </summary> + /// <typeparam name="SETTERTYPE"></typeparam> + /// <param name="expression"></param> + /// <returns></returns> + public DependencyExpression<SETTERTYPE> Setter<SETTERTYPE>( + Expression<Func<T, SETTERTYPE>> expression) + { + string propertyName = ReflectionHelper.GetProperty(expression).Name; + return new DependencyExpression<SETTERTYPE>(this, propertyName); + } + + /// <summary> /// Inline definition of a setter dependency. Only use this method if there /// is only a single property of the SETTERTYPE /// </summary> /// <typeparam name="SETTERTYPE"></typeparam> /// <returns></returns> + [Obsolete("Use Setter<>()")] public DependencyExpression<SETTERTYPE> SetterDependency<SETTERTYPE>() { return CtorDependency<SETTERTYPE>(); } + /// <summary> + /// Inline definition of a setter dependency. Only use this method if there + /// is only a single property of the SETTERTYPE + /// </summary> + /// <typeparam name="SETTERTYPE"></typeparam> + /// <returns></returns> + public DependencyExpression<SETTERTYPE> Setter<SETTERTYPE>() + { + return CtorDependency<SETTERTYPE>(); + } + + /// <summary> /// Inline definition of a dependency on an Array of the CHILD type. I.e. CHILD[]. /// This method can be used for either constructor arguments or setter properties /// </summary> /// <typeparam name="CHILD"></typeparam> /// <returns></returns> + [Obsolete("Use EnumerableOf<>")] public ArrayDefinitionExpression<CHILD> TheArrayOf<CHILD>() { if (typeof (CHILD).IsArray) @@ -249,8 +318,8 @@ } Plugin plugin = PluginCache.GetPlugin(typeof (T)); - string propertyName = plugin.FindArgumentNameForType(typeof (CHILD).MakeArrayType()); - Debug.WriteLine("Property Name: " + propertyName); + string propertyName = plugin.FindArgumentNameForEnumerableOf(typeof (CHILD)); + return TheArrayOf<CHILD>(propertyName); } @@ -261,11 +330,53 @@ /// <typeparam name="CHILD"></typeparam> /// <param name="ctorOrPropertyName"></param> /// <returns></returns> + [Obsolete("Use EnumerableOf<>")] public ArrayDefinitionExpression<CHILD> TheArrayOf<CHILD>(string ctorOrPropertyName) { + if (ctorOrPropertyName.IsEmpty()) + { + throw new StructureMapException(302, typeof(CHILD).FullName, typeof(T).FullName); + } return new ArrayDefinitionExpression<CHILD>(this, ctorOrPropertyName); } + + /// <summary> + /// Inline definition of a dependency on an Array of the CHILD type. I.e. CHILD[]. + /// This method can be used for either constructor arguments or setter properties + /// </summary> + /// <typeparam name="CHILD"></typeparam> + /// <returns></returns> + public ArrayDefinitionExpression<CHILD> EnumerableOf<CHILD>() + { + if (typeof(CHILD).IsArray) + { + throw new ApplicationException("Please specify the element type in the call to TheArrayOf"); + } + + Plugin plugin = PluginCache.GetPlugin(typeof(T)); + string propertyName = plugin.FindArgumentNameForEnumerableOf(typeof(CHILD)); + + return TheArrayOf<CHILD>(propertyName); + } + + /// <summary> + /// Inline definition of a dependency on an Array of the CHILD type and the specified setter property or constructor argument name. I.e. CHILD[]. + /// This method can be used for either constructor arguments or setter properties + /// </summary> + /// <typeparam name="CHILD"></typeparam> + /// <param name="ctorOrPropertyName"></param> + /// <returns></returns> + public ArrayDefinitionExpression<CHILD> EnumerableOf<CHILD>(string ctorOrPropertyName) + { + if (ctorOrPropertyName.IsEmpty()) + { + throw new StructureMapException(302, typeof(CHILD).FullName, typeof(T).FullName); + } + return new ArrayDefinitionExpression<CHILD>(this, ctorOrPropertyName); + } + + #region Nested type: ArrayDefinitionExpression /// <summary> @@ -333,6 +444,32 @@ } /// <summary> + /// Sets the value of the constructor argument to the key/value in the + /// AppSettings + /// </summary> + /// <param name="appSettingKey">The key in appSettings for the value to use.</param> + /// <returns></returns> + public SmartInstance<T> EqualToAppSetting(string appSettingKey) + { + return EqualToAppSetting(appSettingKey, null); + } + + /// <summary> + /// Sets the value of the constructor argument to the key/value in the + /// AppSettings when it exists. Otherwise uses the provided default value. + /// </summary> + /// <param name="appSettingKey">The key in appSettings for the value to use.</param> + /// <param name="defaultValue">The value to use if an entry for <paramref name="appSettingKey"/> does not exist in the appSettings section.</param> + /// <returns></returns> + public SmartInstance<T> EqualToAppSetting(string appSettingKey, string defaultValue) + { + string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; + if (propertyValue == null) propertyValue = defaultValue; + _instance.SetValue(_propertyName, propertyValue); + return _instance; + } + + /// <summary> /// Nested Closure to define a child dependency inline /// </summary> /// <param name="action"></param> @@ -345,6 +482,12 @@ return _instance; } + public SmartInstance<T> Is(Func<IContext, CHILD> func) + { + var child = new LambdaInstance<CHILD>(func); + return Is(child); + } + /// <summary> /// Shortcut to set an inline dependency to an Instance /// </summary> @@ -363,10 +506,24 @@ /// <returns></returns> public SmartInstance<T> Is(CHILD value) { - return Is(new ObjectInstance(value)); + _instance.SetValue(_propertyName, value); + return _instance; } + /// <summary> + /// Shortcut to set an inline dependency to a designated object + /// </summary> + /// <param name="value"></param> + /// <returns></returns> + [Obsolete("Change to Is()")] + public SmartInstance<T> EqualTo(CHILD value) + { + _instance.SetValue(_propertyName, value); + return _instance; + } + + /// <summary> /// Set an Inline dependency to the Default Instance of the Property type /// Used mostly to force an optional Setter property to be filled by /// StructureMap @@ -386,6 +543,20 @@ { return Is(new SmartInstance<CONCRETETYPE>()); } + + + /// <summary> + /// Shortcut method to define a child dependency inline and configure + /// the child dependency + /// </summary> + /// <typeparam name="CONCRETETYPE"></typeparam> + /// <returns></returns> + public SmartInstance<T> Is<CONCRETETYPE>(Action<SmartInstance<CONCRETETYPE>> configure) where CONCRETETYPE : CHILD + { + var instance = new SmartInstance<CONCRETETYPE>(); + configure(instance); + return Is(instance); + } } #endregion Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -45,6 +45,7 @@ { public static class TypeExtensions { + public static bool Closes(this Type type, Type openType) { Type baseType = type.BaseType; Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -1,5 +1,7 @@ +using System.Collections.Generic; using NUnit.Framework; using StructureMap.Pipeline; +using System.Linq; namespace StructureMap.Testing.Configuration.DSL { @@ -33,6 +35,24 @@ public string Name { get { return _name; } } } + public class ProcessorWithList + { + private readonly IList<IHandler> _handlers; + private readonly string _name; + + public ProcessorWithList(IList<IHandler> handlers, string name) + { + _handlers = handlers; + _name = name; + } + + + public IList<IHandler> Handlers { get { return _handlers; } } + + + public string Name { get { return _name; } } + } + public class Processor2 { private readonly IHandler[] _first; @@ -86,6 +106,24 @@ } [Test] + public void get_a_configured_list() + { + var container = new Container(x => + { + x.ForRequestedType<Processor>().TheDefault.Is + .OfConcreteType<Processor>() + .TheArrayOf<IHandler>().Contains( + new SmartInstance<Handler1>(), + new SmartInstance<Handler2>(), + new SmartInstance<Handler3>() + ) + .WithCtorArg("name").EqualTo("Jeremy"); + }); + + container.GetInstance<Processor>().Handlers.Select(x => x.GetType()).ShouldHaveTheSameElementsAs(typeof(Handler1), typeof(Handler2), typeof(Handler3)); + } + + [Test] public void InjectPropertiesByName() { var container = new Container(r => Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using NUnit.Framework; @@ -405,6 +406,64 @@ } } + + [TestFixture] + public class when_finding_property_name_of_enumerable_type + { + private Plugin plugin; + + + + [SetUp] + public void SetUp() + { + plugin = new Plugin(typeof (ClassWithEnumerables)); + } + + [Test] + public void smoke_test() + { + new Plugin(typeof (ClassThatUsesValidators)).FindArgumentNameForEnumerableOf(typeof (IValidator)). + ShouldEqual("validators"); + } + + [Test] + public void array_in_ctor() + { + plugin.FindArgumentNameForEnumerableOf(typeof (IEngine)).ShouldEqual("engines"); + } + + [Test] + public void enumerable_in_ctor() + { + plugin.FindArgumentNameForEnumerableOf(typeof (IAutomobile)).ShouldEqual("autos"); + } + + [Test] + public void ilist_as_setter() + { + plugin.FindArgumentNameForEnumerableOf(typeof (IWidget)).ShouldEqual("Widgets"); + } + + [Test] + public void list_as_setter() + { + plugin.FindArgumentNameForEnumerableOf(typeof (Rule)).ShouldEqual("Rules"); + } + } + + + public class ClassWithEnumerables + { + public ClassWithEnumerables(IEngine[] engines, IEnumerable<IAutomobile> autos) + { + + } + + public IList<IWidget> Widgets { get; set; } + public List<Rule> Rules { get; set; } + } + public class LotsOfStuff { public LotsOfStuff(IEngine engine, IEngine[] engines, string name, int age, BreedEnum breed) @@ -432,6 +491,8 @@ { } + + [Pluggable("Pushrod")] public class PushrodEngine : IEngine { Modified: trunk/Source/StructureMap.Testing/ObjectFactoryInitializeTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ObjectFactoryInitializeTester.cs 2009-12-29 02:55:12 UTC (rev 314) +++ trunk/Source/StructureMap.Testing/ObjectFactoryInitializeTester.cs 2009-12-29 03:50:13 UTC (rev 315) @@ -11,8 +11,9 @@ { public InitializeRegistry() { - InstanceOf<IWidget>().Is.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("Green").WithName( - "Green"); + For<IWidget>().Add<ColorWidget>() + .Ctor<string>("color").Is("Green") + .Named("Green"); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-29 04:51:24
|
Revision: 316 http://structuremap.svn.sourceforge.net/structuremap/?rev=316&view=rev Author: jeremydmiller Date: 2009-12-29 04:51:11 +0000 (Tue, 29 Dec 2009) Log Message: ----------- fix to the explicit args functionality introduced in the recent changes Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs trunk/Source/StructureMap.Testing/Examples/Interception.cs trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -120,6 +120,11 @@ return TheDefault.Is.ConstructedBy(func); } + public void Use(Instance instance) + { + TheDefault.IsThis(instance); + } + /// <summary> /// Shorthand to say TheDefault.IsThis(@object) /// </summary> Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -74,7 +74,7 @@ public string FindArgumentNameForType<T>() { - return FindArgumentNameForType(typeof (T)); + return FindArgumentNameForType(typeof (T), CannotFindProperty.ThrowException); } public string FindArgumentNameForEnumerableOf(Type type) @@ -87,13 +87,13 @@ }).FirstOrDefault(x => x != null); } - public string FindArgumentNameForType(Type type) + public string FindArgumentNameForType(Type type, CannotFindProperty cannotFind) { string returnValue = _constructor.FindFirstConstructorArgumentOfType(type) ?? _setters.FindFirstWriteablePropertyOfType(type); - if (returnValue == null) + if (returnValue == null && cannotFind == CannotFindProperty.ThrowException) { throw new StructureMapException(302, type.FullName, _pluggedType.FullName); } Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -47,10 +47,10 @@ SetChild(name, instance); } - public void SetValue(Type type, object value) + public void SetValue(Type type, object value, CannotFindProperty cannotFind) { - string name = _plugin.FindArgumentNameForType(type); - SetValue(name, value); + string name = _plugin.FindArgumentNameForType(type, cannotFind); + if (name != null) SetValue(name, value); } void IConfiguredInstance.SetValue(string name, object value) @@ -175,7 +175,7 @@ protected string findPropertyName(Type dependencyType) { - string propertyName = _plugin.FindArgumentNameForType(dependencyType); + string propertyName = _plugin.FindArgumentNameForType(dependencyType, CannotFindProperty.ThrowException); if (string.IsNullOrEmpty(propertyName)) { Modified: trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -52,7 +52,11 @@ { _args.Each(pair => { instance.SetValue(pair.Key, pair.Value); }); - _children.Each(pair => { instance.SetValue(pair.Key, pair.Value); }); + _children.Each(pair => + { + instance.SetValue(pair.Key, pair.Value, CannotFindProperty.Ignore); + + }); } public bool Has(Type type) Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -3,6 +3,12 @@ namespace StructureMap.Pipeline { + public enum CannotFindProperty + { + ThrowException, + Ignore + } + public interface IConfiguredInstance { string Name { get; } @@ -15,7 +21,7 @@ bool HasProperty(string propertyName, BuildSession session); void SetChild(string name, Instance instance); - void SetValue(Type type, object value); + void SetValue(Type type, object value, CannotFindProperty cannotFind); void SetValue(string name, object value); void SetCollection(string name, IEnumerable<Instance> children); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -117,21 +117,7 @@ container.GetInstance<ITarget>().ShouldBeOfType<Target2>(); } - [Test, Explicit] - public void Add_default_instance2() - { - var container = - new Container( - r => { r.ForRequestedType(typeof (IRepository<>)).TheDefaultIsConcreteType(typeof (OnlineRepository<>)); }); - Assert.IsInstanceOfType(typeof (Target2), container.GetInstance<ITarget>()); - - - var repository = - ObjectFactory.GetInstance<IRepository<Invoice>>(); - } - - [Test] public void Add_instance_directly() { Modified: trunk/Source/StructureMap.Testing/Examples/Interception.cs =================================================================== --- trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap.Testing/Examples/Interception.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -16,7 +16,6 @@ { public void StartConnection() { - throw new NotImplementedException(); } public void Start() @@ -96,19 +95,6 @@ } } - [TestFixture, Explicit] - public class InterceptionRegistryInAction - { - [Test] - public void see_the_enrichment_with_a_decorator_in_action() - { - var container = new Container(new InterceptionRegistry()); - container.GetInstance<IConnectionListener>() - .ShouldBeOfType<LoggingDecorator>() - .Inner.ShouldBeOfType<ClassThatNeedsSomeBootstrapping>(); - } - } - public class CustomInterceptor : InstanceInterceptor { public object Process(object target, IContext context) Modified: trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs 2009-12-29 03:50:13 UTC (rev 315) +++ trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs 2009-12-29 04:51:11 UTC (rev 316) @@ -6,7 +6,7 @@ namespace StructureMap.Testing { - [TestFixture, Ignore("Putting right back on")] + [TestFixture] public class GenericsIntegrationTester { #region Setup/Teardown @@ -49,7 +49,7 @@ private Container manager; - [Test] + [Test, Ignore("not sure I want this behavior anyway")] public void AllTypesWithSpecificImplementation() { IList objectConcepts = manager.GetAllInstances(typeof (IConcept<object>)); @@ -136,7 +136,7 @@ Assert.AreNotSame(object1, object4); } - [Test] + [Test, Ignore("not sure we want this behavior anyway")] public void SpecificImplementation() { var concept = (IConcept<object>) manager.GetInstance(typeof (IConcept<object>), "Specific"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2009-12-29 14:40:53
|
Revision: 317 http://structuremap.svn.sourceforge.net/structuremap/?rev=317&view=rev Author: jeremydmiller Date: 2009-12-29 14:40:45 +0000 (Tue, 29 Dec 2009) Log Message: ----------- last minute fixes to scanning found in regression test runs Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IContainer.cs trunk/Source/StructureMap/InitializationExpression.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap/Util/Cache.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Container.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -482,6 +482,11 @@ Configure(x => x.For<PLUGINTYPE>().Use(instance)); } + public void Inject<PLUGINTYPE>(string name, PLUGINTYPE value) + { + Configure(x => x.For<PLUGINTYPE>().Use(value).Named(name)); + } + /// <summary> /// Injects the given object into a Container as the default for the designated /// pluginType. Mostly used for temporarily setting up return values of the Container Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -24,7 +24,7 @@ Type pluginType = FindPluginType(type); if (pluginType != null && Constructor.HasConstructors(type)) { - registry.For(pluginType).Add(type); + registry.AddType(pluginType, type); } } Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -34,14 +34,6 @@ PluginFamilyAttribute.ConfigureFamily(this); - //if (IsConcrete(pluginType)) - //{ - // Plugin plugin = PluginCache.GetPlugin(pluginType); - // if (plugin.CanBeCreated()) - // { - // AddPlugin(pluginType, Plugin.DEFAULT); - // } - //} } public PluginGraph Parent { get { return _parent; } set { _parent = value; } } @@ -362,7 +354,6 @@ /// <summary> /// The InstanceKey of the default instance of the PluginFamily /// </summary> - [Obsolete] public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion Modified: trunk/Source/StructureMap/IContainer.cs =================================================================== --- trunk/Source/StructureMap/IContainer.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/IContainer.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -126,6 +126,9 @@ /// <param name="instance"></param> void Inject<PLUGINTYPE>(PLUGINTYPE instance); + void Inject<PLUGINTYPE>(string name, PLUGINTYPE value); + + /// <summary> /// Injects the given object into a Container as the default for the designated /// pluginType. Mostly used for temporarily setting up return values of the Container Modified: trunk/Source/StructureMap/InitializationExpression.cs =================================================================== --- trunk/Source/StructureMap/InitializationExpression.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/InitializationExpression.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -7,7 +7,7 @@ namespace StructureMap { - public interface IInitializationExpression + public interface IInitializationExpression : IRegistry { /// <summary> /// If true, makes the existence of the StructureMap.config mandatory. @@ -61,118 +61,6 @@ /// <param name="registry"></param> void AddRegistry(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> - CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>(); - - /// <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> - CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<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> - 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> - /// Adds an additional, non-Default Instance to the PluginType T. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - 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> - 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 Profile(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 Profile(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> - /// Expresses a single "Scanning" action - /// </summary> - /// <param name="action"></param> - /// <example> - /// Scan(x => { - /// x.Assembly("Foo.Services"); - /// x.WithDefaultConventions(); - /// }); - /// </example> - 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); } public class InitializationExpression : ConfigurationExpression, IInitializationExpression Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -234,7 +234,10 @@ public void EachInstance(Action<Type, Instance> action) { - _factories.Values.Each(f => { f.AllInstances.Each(i => action(f.PluginType, i)); }); + _factories.Values.ToArray().Each(f => + { + f.AllInstances.ToArray().Each(i => action(f.PluginType, i)); + }); } public IObjectCache FindCache(Type pluginType) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -5,7 +5,7 @@ namespace StructureMap { - public static class BasicExtensions + internal static class BasicExtensions { public static string ToName(this ILifecycle lifecycle) { @@ -30,7 +30,7 @@ } } - public static T As<T>(this object target) where T : class + internal static T As<T>(this object target) where T : class { return target as T; } Modified: trunk/Source/StructureMap/Util/Cache.cs =================================================================== --- trunk/Source/StructureMap/Util/Cache.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Util/Cache.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -1,127 +1,147 @@ using System; +using System.Collections; using System.Collections.Generic; -using System.Threading; +using StructureMap.Graph; +using StructureMap.Pipeline; namespace StructureMap.Util { - [Serializable] - public class Cache<TKey, TValue> where TValue : class + public class Cache<KEY, VALUE> : IEnumerable<VALUE> where VALUE : class { - private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly IDictionary<TKey, TValue> _values; - private Func<TValue, TKey> _getKey = delegate { throw new NotImplementedException(); }; + private readonly object _locker = new object(); + private readonly IDictionary<KEY, VALUE> _values; - private Action<TValue> _onAddition = x => { }; + private Func<VALUE, KEY> _getKey = delegate { throw new NotImplementedException(); }; - private Func<TKey, TValue> _onMissing = delegate(TKey key) + private Func<KEY, VALUE> _onMissing = delegate(KEY key) { string message = string.Format("Key '{0}' could not be found", key); throw new KeyNotFoundException(message); }; public Cache() - : this(new Dictionary<TKey, TValue>()) + : this(new Dictionary<KEY, VALUE>()) { } - public Cache(Func<TKey, TValue> onMissing) - : this(new Dictionary<TKey, TValue>(), onMissing) + public Cache(Func<KEY, VALUE> onMissing) + : this(new Dictionary<KEY, VALUE>(), onMissing) { } - public Cache(IDictionary<TKey, TValue> dictionary, Func<TKey, TValue> onMissing) + public Cache(IDictionary<KEY, VALUE> dictionary, Func<KEY, VALUE> onMissing) : this(dictionary) { _onMissing = onMissing; } - public Cache(IDictionary<TKey, TValue> dictionary) + public Cache(IDictionary<KEY, VALUE> dictionary) { _values = dictionary; } - public Func<TKey, TValue> OnMissing { set { _onMissing = value; } } + public Func<KEY, VALUE> OnMissing + { + set { _onMissing = value; } + } - public Action<TValue> OnAddition { set { _onAddition = value; } } + public Func<VALUE, KEY> GetKey + { + get { return _getKey; } + set { _getKey = value; } + } - public Func<TValue, TKey> GetKey { get { return _getKey; } set { _getKey = value; } } - public int Count { - get - { - using (ReadLock()) - return _values.Count; - } + get { return _values.Count; } } - public TValue First + public VALUE First { get { - using (ReadLock()) - foreach (var pair in _values) - { - return pair.Value; - } + foreach (var pair in _values) + { + return pair.Value; + } return null; } } - public TValue this[TKey key] + public VALUE this[KEY key] { get { - using (ReadLockToken @lock = ReadLock()) + if (!_values.ContainsKey(key)) { - if (!_values.ContainsKey(key)) + lock (_locker) { - @lock.Upgrade(); if (!_values.ContainsKey(key)) { - TValue value = _onMissing(key); - _values.Add(key, value); + VALUE value = _onMissing(key); + //Check to make sure that the onMissing didn't cache this already + if(!_values.ContainsKey(key)) + _values.Add(key, value); } } - - return _values[key]; } + + return _values[key]; } set { - _onAddition(value); - - using (WriteLockToken @lock = WriteLock()) + if (_values.ContainsKey(key)) { - if (_values.ContainsKey(key)) - { - _values[key] = value; - } - else - { - _values.Add(key, value); - } + _values[key] = value; } + else + { + _values.Add(key, value); + } } } - public void Fill(TKey key, TValue value) + #region IEnumerable<VALUE> Members + + IEnumerator IEnumerable.GetEnumerator() { - using (WriteLock()) + return ((IEnumerable<VALUE>)this).GetEnumerator(); + } + + public IEnumerator<VALUE> GetEnumerator() + { + return _values.Values.GetEnumerator(); + } + + #endregion + + public void Fill(KEY key, VALUE value) + { + if (_values.ContainsKey(key)) { - if (_values.ContainsKey(key)) - { - return; - } + return; + } - _values.Add(key, value); + _values.Add(key, value); + } + + public bool TryRetrieve(KEY key, out VALUE value) + { + value = default(VALUE); + + if (_values.ContainsKey(key)) + { + value = _values[key]; + return true; } + + return false; } - public void Each(Action<TValue> action) + public void Each(Action<VALUE> action) { - using (ReadLock()) + lock (_locker) { foreach (var pair in _values) { @@ -130,159 +150,76 @@ } } - public void Each(Action<TKey, TValue> action) + public void Each(Action<KEY, VALUE> action) { - using (ReadLock()) + foreach (var pair in _values) { - foreach (var pair in _values) - { - action(pair.Key, pair.Value); - } + action(pair.Key, pair.Value); } } - public bool Has(TKey key) + public bool Has(KEY key) { - using (ReadLock()) - return _values.ContainsKey(key); + return _values.ContainsKey(key); } - public bool Exists(Predicate<TValue> predicate) + public bool Exists(Predicate<VALUE> predicate) { bool returnValue = false; - Each(delegate(TValue value) { returnValue |= predicate(value); }); + Each(delegate(VALUE value) { returnValue |= predicate(value); }); return returnValue; } - public TValue Find(Predicate<TValue> predicate) + public VALUE Find(Predicate<VALUE> predicate) { - using (ReadLock()) - foreach (var pair in _values) + foreach (var pair in _values) + { + if (predicate(pair.Value)) { - if (predicate(pair.Value)) - { - return pair.Value; - } + return pair.Value; } + } return null; } - public TKey Find(TValue value) + public VALUE[] GetAll() { - using (ReadLock()) - foreach (var pair in _values) - { - if (pair.Value == value) - { - return pair.Key; - } - } + var returnValue = new VALUE[Count]; + _values.Values.CopyTo(returnValue, 0); - return default(TKey); - } - - public TValue[] GetAll() - { - var returnValue = new TValue[Count]; - using (ReadLock()) - _values.Values.CopyTo(returnValue, 0); - return returnValue; } - public void Remove(TKey key) + public void Remove(KEY key) { - using (ReadLock()) - if (_values.ContainsKey(key)) - { - _values.Remove(key); - } + if (_values.ContainsKey(key)) + { + _values.Remove(key); + } } public void Clear() { - using (WriteLock()) - _values.Clear(); + _values.Clear(); } - public void WithValue(TKey key, Action<TValue> callback) + public Cache<KEY, VALUE> Clone() { - using (ReadLock()) - _values.TryGet(key, callback); - } + var clone = new Cache<KEY, VALUE>(_onMissing); + _values.Each(pair => clone[pair.Key] = pair.Value); - private ReadLockToken ReadLock() - { - return new ReadLockToken(_lock); + return clone; } - private WriteLockToken WriteLock() + public void WithValue(KEY key, Action<VALUE> action) { - return new WriteLockToken(_lock); - } - - public Cache<TKey, TValue> Clone() - { - var cache = new Cache<TKey, TValue>(); - cache._onMissing = _onMissing; - - Each((key, value) => cache[key] = value); - - return cache; - } - - #region Nested type: ReadLockToken - - private class ReadLockToken : IDisposable - { - private readonly ReaderWriterLockSlim _lock; - private bool upgraded; - - public ReadLockToken(ReaderWriterLockSlim @lock) + if (_values.ContainsKey(key)) { - _lock = @lock; - _lock.EnterReadLock(); + action(this[key]); } - - public void Dispose() - { - if (upgraded) - _lock.ExitWriteLock(); - else - _lock.ExitReadLock(); - } - - public void Upgrade() - { - _lock.ExitReadLock(); - _lock.EnterWriteLock(); - upgraded = true; - } } - - #endregion - - #region Nested type: WriteLockToken - - private class WriteLockToken : IDisposable - { - private readonly ReaderWriterLockSlim _lock; - - public WriteLockToken(ReaderWriterLockSlim @lock) - { - _lock = @lock; - _lock.EnterWriteLock(); - } - - public void Dispose() - { - _lock.EnterWriteLock(); - } - } - - #endregion } -} \ 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-29 17:15:05
|
Revision: 318 http://structuremap.svn.sourceforge.net/structuremap/?rev=318&view=rev Author: jeremydmiller Date: 2009-12-29 17:14:57 +0000 (Tue, 29 Dec 2009) Log Message: ----------- last minute fixes for scanning, ObjectInstance.ConcreteType, PluginFamily.AddType() Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Pipeline/ObjectInstance.cs trunk/Source/StructureMap/Query/IModel.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-29 14:40:45 UTC (rev 317) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2009-12-29 17:14:57 UTC (rev 318) @@ -140,7 +140,6 @@ /// <summary> /// Configure this type as the supplied value /// </summary> - /// <param name="instance"></param> /// <returns></returns> public ObjectInstance Add(object @object) { Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 14:40:45 UTC (rev 317) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 17:14:57 UTC (rev 318) @@ -294,11 +294,16 @@ return templatedFamily; } + private bool hasType(Type concreteType) + { + return FindPlugin(concreteType) != null || _instances.Any(x => x.ConcreteType == concreteType); + } + public void AddType(Type concreteType) { if (!concreteType.CanBeCastTo(_pluginType)) return; - if (FindPlugin(concreteType) == null) + if (!hasType(concreteType)) { AddPlugin(concreteType); } @@ -308,7 +313,7 @@ { if (!concreteType.CanBeCastTo(_pluginType)) return; - if (FindPlugin(name) == null) + if (!hasType(concreteType)) { AddPlugin(concreteType, name); } Modified: trunk/Source/StructureMap/Pipeline/ObjectInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2009-12-29 14:40:45 UTC (rev 317) +++ trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2009-12-29 17:14:57 UTC (rev 318) @@ -63,5 +63,10 @@ { return string.Format("LiteralInstance: {0}", _object); } + + protected override Type getConcreteType(Type pluginType) + { + return _object.GetType(); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IModel.cs =================================================================== --- trunk/Source/StructureMap/Query/IModel.cs 2009-12-29 14:40:45 UTC (rev 317) +++ trunk/Source/StructureMap/Query/IModel.cs 2009-12-29 17:14:57 UTC (rev 318) @@ -10,7 +10,7 @@ public interface IModel { /// <summary> - /// Access to all the <seealso cref="PluginTypeConfiguration">Plugin Type</seealso> registrations + /// Access to all the <seealso cref="IPluginTypeConfiguration">Plugin Type</seealso> registrations /// </summary> IEnumerable<IPluginTypeConfiguration> PluginTypes { get; } @@ -35,14 +35,14 @@ bool HasDefaultImplementationFor<T>(); /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// Queryable access to all of the <see cref="InstanceRef">InstanceRef</see> for a given PluginType /// </summary> /// <param name="pluginType"></param> /// <returns></returns> IEnumerable<InstanceRef> InstancesOf(Type pluginType); /// <summary> - /// Queryable access to all of the <see cref="IInstance">IInstance</see> for a given PluginType + /// Queryable access to all of the <see cref="InstanceRef">InstanceRef</see> for a given PluginType /// </summary> /// <returns></returns> IEnumerable<InstanceRef> InstancesOf<T>(); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs 2009-12-29 14:40:45 UTC (rev 317) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs 2009-12-29 17:14:57 UTC (rev 318) @@ -14,8 +14,12 @@ { ObjectFactory.Initialize(x => { + x.IgnoreStructureMapConfig = true; + x.Scan(s => { + + s.AssemblyContainingType<RedGreenRegistry>(); s.LookForRegistries(); }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-01-04 18:56:27
|
Revision: 320 http://structuremap.svn.sourceforge.net/structuremap/?rev=320&view=rev Author: jeremydmiller Date: 2010-01-04 18:56:18 +0000 (Mon, 04 Jan 2010) Log Message: ----------- Eliminated the FillDependencies methods Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/IContainer.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs trunk/Source/StructureMap.Testing/Graph/FillDependenciesTester.cs Added Paths: ----------- trunk/Source/StructureMap/Pipeline/BasicInstance.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-29 19:22:20 UTC (rev 319) +++ trunk/Source/StructureMap/Container.cs 2010-01-04 18:56:18 UTC (rev 320) @@ -155,11 +155,6 @@ return (T) GetInstance(typeof (T)); } - [Obsolete("Please use GetInstance<T>() instead.")] - public T FillDependencies<T>() - { - return (T) FillDependencies(typeof (T)); - } /// <summary> @@ -282,24 +277,7 @@ _pipelineGraph.SetDefault(pluginType, instance); } - [Obsolete("Please use GetInstance(Type) instead")] - public object FillDependencies(Type type) - { - if (!type.IsConcrete()) - { - throw new StructureMapException(230, type.FullName); - } - var plugin = new Plugin(type); - if (!plugin.CanBeAutoFilled) - { - throw new StructureMapException(230, type.FullName); - } - - return GetInstance(type); - } - - /// <summary> /// Creates or resolves all registered instances of the pluginType /// </summary> Modified: trunk/Source/StructureMap/IContainer.cs =================================================================== --- trunk/Source/StructureMap/IContainer.cs 2009-12-29 19:22:20 UTC (rev 319) +++ trunk/Source/StructureMap/IContainer.cs 2010-01-04 18:56:18 UTC (rev 320) @@ -104,13 +104,6 @@ /// <returns></returns> T TryGetInstance<T>(string instanceKey); - - [Obsolete("Please use GetInstance<T>() instead.")] - T FillDependencies<T>(); - - [Obsolete("Please use GetInstance(Type) instead")] - object FillDependencies(Type type); - /// <summary> /// Used to add additional configuration to a Container *after* the initialization. /// </summary> Added: trunk/Source/StructureMap/Pipeline/BasicInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/BasicInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/BasicInstance.cs 2010-01-04 18:56:18 UTC (rev 320) @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Pipeline +{ + public interface BasicInstance + { + Type PluggedType { get; } + + + Dictionary<string, string> Properties { get; } + Dictionary<string, Instance> Children { get; } + Dictionary<string, Instance[]> Arrays { get; } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2009-12-29 19:22:20 UTC (rev 319) +++ trunk/Source/StructureMap/StructureMap.csproj 2010-01-04 18:56:18 UTC (rev 320) @@ -368,6 +368,7 @@ <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="Pipeline\BasicInstance.cs" /> <Compile Include="Pipeline\HybridLifecycle.cs" /> <Compile Include="Query\EmptyConfiguration.cs" /> <Compile Include="Query\GenericFamilyConfiguration.cs" /> @@ -403,7 +404,6 @@ <Compile Include="Query\Model.cs" /> <Compile Include="Pipeline\BuildFrame.cs" /> <Compile Include="Pipeline\BuildStack.cs" /> - <Compile Include="Pipeline\ConfiguredInstanceBase.cs" /> <Compile Include="Pipeline\InstanceKey.cs" /> <Compile Include="Pipeline\IStructuredInstance.cs" /> <Compile Include="Pipeline\PropertyExpression.cs" /> Modified: trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs 2009-12-29 19:22:20 UTC (rev 319) +++ trunk/Source/StructureMap.Testing/AutoMocking/RhinoAutoMockerTester.cs 2010-01-04 18:56:18 UTC (rev 320) @@ -45,7 +45,7 @@ var service2 = _container.GetInstance<IMockedService2>(); var service3 = _container.GetInstance<IMockedService3>(); - var concreteClass = _container.FillDependencies<ConcreteClass>(); + var concreteClass = _container.GetInstance<ConcreteClass>(); Assert.AreSame(service, concreteClass.Service); Assert.AreSame(service2, concreteClass.Service2); Modified: trunk/Source/StructureMap.Testing/Graph/FillDependenciesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/FillDependenciesTester.cs 2009-12-29 19:22:20 UTC (rev 319) +++ trunk/Source/StructureMap.Testing/Graph/FillDependenciesTester.cs 2010-01-04 18:56:18 UTC (rev 320) @@ -17,7 +17,7 @@ }); var concreteClass = - (FilledConcreteClass) container.FillDependencies(typeof (FilledConcreteClass)); + (FilledConcreteClass) container.GetInstance(typeof (FilledConcreteClass)); Assert.IsNotNull(concreteClass.Widget); Assert.IsNotNull(concreteClass.Strategy); @@ -27,7 +27,7 @@ public void TryToFillDependenciesOnAbstractClassThrowsException() { var manager = new Container(); - manager.FillDependencies(typeof (AbstractClass)); + manager.GetInstance(typeof (AbstractClass)); } @@ -35,7 +35,7 @@ public void TryToFillDependenciesOnClassWithPrimitiveArgumentsThrowsException() { var manager = new Container(); - manager.FillDependencies(typeof (CannotBeFilledConcreteClass)); + manager.GetInstance(typeof (CannotBeFilledConcreteClass)); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2010-01-30 19:14:06
|
Revision: 326 http://structuremap.svn.sourceforge.net/structuremap/?rev=326&view=rev Author: flimflan Date: 2010-01-30 19:13:59 +0000 (Sat, 30 Jan 2010) Log Message: ----------- When wiring up the built-in registration conventions, can now call OnAddedPluginTypes() to configure options (scope, enrichment, etc) for discovered plugin types - pulled registration of built-in conventions off IAssemblyScanner and moved to AssemblyScannerExtensions Modified Paths: -------------- trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs trunk/Source/StructureMap/Graph/IAssemblyScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/ImplementationMap.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/AssemblyScannerExtensions.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/ConfigureConventionExpression.cs trunk/Source/StructureMap/Graph/IRegistrationConvention.cs trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs Added: trunk/Source/StructureMap/AssemblyScannerExtensions.cs =================================================================== --- trunk/Source/StructureMap/AssemblyScannerExtensions.cs (rev 0) +++ trunk/Source/StructureMap/AssemblyScannerExtensions.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -0,0 +1,63 @@ +using System; +using StructureMap.Configuration.DSL.Expressions; +using StructureMap.Graph; + +namespace StructureMap +{ + /// <summary> + /// Extend the assembly scanning DSL to support the built-in registration conventions + /// </summary> + public static class AssemblyScannerExtensions + { + /// <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> + public static ConfigureConventionExpression WithDefaultConventions(this IAssemblyScanner assemblyScanner) + { + var convention = new DefaultConventionScanner(); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Scans for PluginType's and Concrete Types that close the given open generic type + /// </summary> + /// <example> + /// + /// </example> + /// <param name="openGenericType"></param> + public static ConfigureConventionExpression ConnectImplementationsToTypesClosing(this IAssemblyScanner assemblyScanner, Type openGenericType) + { + var convention = new GenericConnectionScanner(openGenericType); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Automatically registers all concrete types without primitive arguments + /// against its first interface, if any + /// </summary> + public static ConfigureConventionExpression RegisterConcreteTypesAgainstTheFirstInterface(this IAssemblyScanner assemblyScanner) + { + var convention = new FirstInterfaceConvention(); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Directs the scanning to automatically register any type that is the single + /// implementation of an interface against that interface. + /// The filters apply + /// </summary> + public static ConfigureConventionExpression SingleImplementationsOfInterface(this IAssemblyScanner assemblyScanner) + { + var convention = new ImplementationMap(); + assemblyScanner.With(convention); + assemblyScanner.ModifyGraphAfterScan(convention.RegisterSingleImplementations); + return new ConfigureConventionExpression(convention); + } + + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/DSL/Expressions/ConfigureConventionExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ConfigureConventionExpression.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ConfigureConventionExpression.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -0,0 +1,21 @@ +using System; +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL.Expressions +{ + public class ConfigureConventionExpression + { + private readonly ConfigurableRegistrationConvention _convention; + + internal ConfigureConventionExpression(ConfigurableRegistrationConvention convention) + { + _convention = convention; + } + + public ConfigureConventionExpression OnAddedPluginTypes(Action<GenericFamilyExpression> configurePluginType) + { + _convention.SetFamilyConfigurationAction(configurePluginType); + return this; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -42,7 +42,6 @@ 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(); private readonly List<Action<PluginGraph>> _postScanningActions = new List<Action<PluginGraph>>(); private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); @@ -75,16 +74,6 @@ _scanners.Fill(scanner); } - public void WithDefaultConventions() - { - Convention<DefaultConventionScanner>(); - } - - public void RegisterConcreteTypesAgainstTheFirstInterface() - { - Convention<FirstInterfaceConvention>(); - } - [Obsolete("Replace ITypeScanner with IRegistrationConvention")] public void With<T>() where T : ITypeScanner, new() { @@ -186,18 +175,11 @@ Exclude(type => type == typeof (T)); } - public void ConnectImplementationsToTypesClosing(Type openGenericType) + public void ModifyGraphAfterScan(Action<PluginGraph> modifyGraph) { - With(new GenericConnectionScanner(openGenericType)); + _postScanningActions.Add(modifyGraph); } - - public void SingleImplementationsOfInterface() - { - _conventions.Fill(_implementationMap); - _postScanningActions.Add(graph => _implementationMap.RegisterSingleImplementations(graph)); - } - public void AssembliesFromApplicationBaseDirectory() { AssembliesFromApplicationBaseDirectory(a => true); Modified: trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs =================================================================== --- trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/Graph/FirstInterfaceConvention.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -6,9 +6,9 @@ namespace StructureMap.Graph { - public class FirstInterfaceConvention : IRegistrationConvention + public class FirstInterfaceConvention : ConfigurableRegistrationConvention { - public void Process(Type type, Registry registry) + public override void Process(Type type, Registry registry) { if (!type.IsConcrete() || !type.CanBeCreated()) return; @@ -18,6 +18,7 @@ { Debug.WriteLine("Plugging {0} into {1}".ToFormat(type.Name, interfaceType.Name)); registry.AddType(interfaceType, type); + ConfigureFamily(registry.For(interfaceType)); } } } Modified: trunk/Source/StructureMap/Graph/IAssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/Graph/IAssemblyScanner.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -1,5 +1,6 @@ using System; using System.Reflection; +using StructureMap.Configuration.DSL.Expressions; namespace StructureMap.Graph { @@ -76,20 +77,6 @@ void With(ITypeScanner scanner); /// <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> - /// Automatically registers all concrete types without primitive arguments - /// against its first interface, if any - /// </summary> - void RegisterConcreteTypesAgainstTheFirstInterface(); - - - /// <summary> /// Creates and adds a new ITypeScanner of type T to this scanning operation /// </summary> /// <typeparam name="T"></typeparam> @@ -178,22 +165,6 @@ // ... 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); - - /// <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(); - - /// <summary> /// Adds a registration convention to be applied to all the types in this /// logical "scan" operation /// </summary> @@ -205,5 +176,7 @@ /// logical "scan" operation /// </summary> void With(IRegistrationConvention convention); + + void ModifyGraphAfterScan(Action<PluginGraph> modifyGraph); } } \ No newline at end of file Added: trunk/Source/StructureMap/Graph/IRegistrationConvention.cs =================================================================== --- trunk/Source/StructureMap/Graph/IRegistrationConvention.cs (rev 0) +++ trunk/Source/StructureMap/Graph/IRegistrationConvention.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -0,0 +1,31 @@ +using System; +using StructureMap.Configuration.DSL; +using StructureMap.Configuration.DSL.Expressions; + +namespace StructureMap.Graph +{ + public interface IRegistrationConvention + { + void Process(Type type, Registry registry); + } + + /// <summary> + /// Allows built-in registration conventions to be configurable through the assembly scanning DSL + /// </summary> + /// <remarks> + /// Intended for StructureMap internal use only. + /// Custom registration convention instances can be directly configured + /// before being passed to IAssemblyScanner.With(IRegistrationConvention). + /// </remarks> + public abstract class ConfigurableRegistrationConvention : IRegistrationConvention + { + protected Action<GenericFamilyExpression> ConfigureFamily = x => { }; + + public void SetFamilyConfigurationAction(Action<GenericFamilyExpression> configureFamily) + { + ConfigureFamily = configureFamily; + } + + public abstract void Process(Type type, Registry registry); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -10,14 +10,9 @@ void Process(Type type, PluginGraph graph); } - public interface IRegistrationConvention + public class DefaultConventionScanner : ConfigurableRegistrationConvention { - void Process(Type type, Registry registry); - } - - public class DefaultConventionScanner : IRegistrationConvention - { - public void Process(Type type, Registry registry) + public override void Process(Type type, Registry registry) { if (!type.IsConcrete()) return; @@ -25,6 +20,7 @@ if (pluginType != null && Constructor.HasConstructors(type)) { registry.AddType(pluginType, type); + ConfigureFamily(registry.For(pluginType)); } } @@ -36,7 +32,7 @@ } } - public class GenericConnectionScanner : IRegistrationConvention + public class GenericConnectionScanner : ConfigurableRegistrationConvention { private readonly Type _openType; @@ -50,12 +46,14 @@ } } - public void Process(Type type, Registry registry) + public override void Process(Type type, Registry registry) { Type interfaceType = type.FindInterfaceThatCloses(_openType); if (interfaceType != null) { - registry.For(interfaceType).Add(type); + var family = registry.For(interfaceType); + ConfigureFamily(family); + family.Add(type); } } } Modified: trunk/Source/StructureMap/Graph/ImplementationMap.cs =================================================================== --- trunk/Source/StructureMap/Graph/ImplementationMap.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/Graph/ImplementationMap.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -7,11 +7,11 @@ namespace StructureMap.Graph { - public class ImplementationMap : IRegistrationConvention + public class ImplementationMap : ConfigurableRegistrationConvention { private readonly Cache<Type, List<Type>> _types = new Cache<Type, List<Type>>(t => new List<Type>()); - public void Process(Type type, Registry registry) + public override void Process(Type type, Registry registry) { RegisterType(type); } @@ -30,13 +30,22 @@ public void RegisterSingleImplementations(PluginGraph graph) { + var singleImplementationRegistry = new SingleImplementationRegistry(); _types.Each((pluginType, types) => { if (types.Count == 1) { - graph.AddType(pluginType, types[0]); + singleImplementationRegistry.AddType(pluginType, types[0]); + ConfigureFamily(singleImplementationRegistry.For(pluginType)); } }); + singleImplementationRegistry.ConfigurePluginGraph(graph); } } + + internal class SingleImplementationRegistry : Registry + { + // This type created just to make the output clearer in WhatDoIHave() + // might consider adding a Description property to Registry instead + } } \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap/StructureMap.csproj 2010-01-30 19:13:59 UTC (rev 326) @@ -336,6 +336,7 @@ <Compile Include="CloseGenericTypeExpression.cs" /> <Compile Include="ConfigurationExpression.cs" /> <Compile Include="Configuration\DictionaryReader.cs" /> + <Compile Include="AssemblyScannerExtensions.cs" /> <Compile Include="Configuration\DSL\Expressions\InstanceExpression.cs" /> <Compile Include="Configuration\DSL\IRegistry.cs" /> <Compile Include="Configuration\DSL\SetterConvention.cs" /> @@ -358,12 +359,14 @@ <Compile Include="Extensions.cs" /> <Compile Include="Graph\CompositeFilter.cs" /> <Compile Include="Graph\CompositePredicate.cs" /> + <Compile Include="Configuration\DSL\Expressions\ConfigureConventionExpression.cs" /> <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> <Compile Include="Graph\FirstInterfaceConvention.cs" /> <Compile Include="Graph\IAssemblyScanner.cs" /> <Compile Include="Graph\ImplementationMap.cs" /> + <Compile Include="Graph\IRegistrationConvention.cs" /> <Compile Include="Graph\ITypeScanner.cs" /> <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> Modified: trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -6,71 +6,14 @@ namespace StructureMap.Testing.Graph { [TestFixture] - public class GenericConnectionScannerTester - { - #region Setup/Teardown - - [SetUp] - public void SetUp() - { - container = new Container(x => - { - x.Scan(o => - { - o.TheCallingAssembly(); - o.ConnectImplementationsToTypesClosing(typeof (IFinder<>)); - }); - }); - } - - #endregion - - private Container container; - - [Test] - public void can_find_the_closed_finders() - { - container.GetInstance<IFinder<string>>().ShouldBeOfType<StringFinder>(); - container.GetInstance<IFinder<int>>().ShouldBeOfType<IntFinder>(); - container.GetInstance<IFinder<double>>().ShouldBeOfType<DoubleFinder>(); - } - - [Test, ExpectedException(typeof (ApplicationException))] - public void fails_on_closed_type() - { - new GenericConnectionScanner(typeof (double)); - } - } - - public interface IFinder<T> - { - } - - public class StringFinder : IFinder<string> - { - } - - public class IntFinder : IFinder<int> - { - } - - public class DoubleFinder : IFinder<double> - { - } - - [TestFixture] public class DefaultConventionScanningTester { - #region Setup/Teardown - [SetUp] public void SetUp() { PluginCache.ResetAll(); } - #endregion - [Test] public void FindPluginType() { @@ -129,6 +72,21 @@ family.Seal(); Assert.AreEqual(1, family.InstanceCount); } + + [Test] + public void can_configure_plugin_families_via_dsl() + { + var container = new Container(registry => registry.Scan(x => + { + x.TheCallingAssembly(); + x.WithDefaultConventions().OnAddedPluginTypes(t => t.Singleton()); + })); + + var firstInstance = container.GetInstance<IConvention>(); + var secondInstance = container.GetInstance<IConvention>(); + secondInstance.ShouldBeTheSameAs(firstInstance); + } + } public interface IConvention Added: trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -0,0 +1,63 @@ +using System; +using NUnit.Framework; +using StructureMap.Graph; + +namespace StructureMap.Testing.Graph +{ + [TestFixture] + public class GenericConnectionScannerTester + { + [Test] + public void can_find_the_closed_finders() + { + var container = new Container(x => x.Scan(o => + { + o.TheCallingAssembly(); + o.ConnectImplementationsToTypesClosing(typeof(IFinder<>)); + })); + container.GetInstance<IFinder<string>>().ShouldBeOfType<StringFinder>(); + container.GetInstance<IFinder<int>>().ShouldBeOfType<IntFinder>(); + container.GetInstance<IFinder<double>>().ShouldBeOfType<DoubleFinder>(); + } + + [Test, ExpectedException(typeof (ApplicationException))] + public void fails_on_closed_type() + { + new GenericConnectionScanner(typeof (double)); + } + + [Test] + public void can_configure_plugin_families_via_dsl() + { + var container = new Container(registry => registry.Scan(x => + { + x.TheCallingAssembly(); + x.ConnectImplementationsToTypesClosing(typeof(IFinder<>)).OnAddedPluginTypes(t => t.Singleton()); + })); + + var firstStringFinder = container.GetInstance<IFinder<string>>().ShouldBeOfType<StringFinder>(); + var secondStringFinder = container.GetInstance<IFinder<string>>().ShouldBeOfType<StringFinder>(); + secondStringFinder.ShouldBeTheSameAs(firstStringFinder); + + var firstIntFinder = container.GetInstance<IFinder<int>>().ShouldBeOfType<IntFinder>(); + var secondIntFinder = container.GetInstance<IFinder<int>>().ShouldBeOfType<IntFinder>(); + secondIntFinder.ShouldBeTheSameAs(firstIntFinder); + } + } + + public interface IFinder<T> + { + } + + public class StringFinder : IFinder<string> + { + } + + public class IntFinder : IFinder<int> + { + } + + public class DoubleFinder : IFinder<double> + { + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2010-01-30 19:13:59 UTC (rev 326) @@ -6,28 +6,48 @@ [TestFixture] public class SingleImplementationScannerTester { - [Test] - public void other() + private Container _container; + + [SetUp] + public void Setup() { - typeof (Type).IsValueType.ShouldBeFalse(); + _container = new Container(registry => registry.Scan(x => + { + x.TheCallingAssembly(); + x.IncludeNamespaceContainingType<SingleImplementationScannerTester>(); + x.SingleImplementationsOfInterface(); + })); } [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.SingleImplementationsOfInterface(); - }); - }); - container.GetInstance<IOnlyHaveASingleConcreteImplementation>() + _container.GetInstance<IOnlyHaveASingleConcreteImplementation>() .ShouldBeOfType<MyNameIsNotConventionallyRelatedToMyInterface>(); } + + [Test] + public void should_not_automatically_register_plugins_that_have_multiple_implementations() + { + _container.TryGetInstance<IHaveMultipleConcreteImplementations>().ShouldBeNull(); + } + + [Test] + public void can_configure_plugin_families_via_dsl() + { + var differentContainer = new Container(registry => registry.Scan(x => + { + x.TheCallingAssembly(); + x.IncludeNamespaceContainingType<SingleImplementationScannerTester>(); + x.SingleImplementationsOfInterface().OnAddedPluginTypes(t => t.Singleton()); + })); + + var firstInstance = differentContainer.GetInstance<IOnlyHaveASingleConcreteImplementation>(); + var secondInstance = differentContainer.GetInstance<IOnlyHaveASingleConcreteImplementation>(); + secondInstance.ShouldBeTheSameAs(firstInstance); + } + } @@ -38,4 +58,8 @@ public class MyNameIsNotConventionallyRelatedToMyInterface : IOnlyHaveASingleConcreteImplementation { } -} \ No newline at end of file + + public interface IHaveMultipleConcreteImplementations { } + public class FirstConcreteImplementation : IHaveMultipleConcreteImplementations { } + public class SecondConcreteImplementation : IHaveMultipleConcreteImplementations { } +} Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-01-28 01:27:28 UTC (rev 325) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-01-30 19:13:59 UTC (rev 326) @@ -258,6 +258,7 @@ <SubType>Code</SubType> </Compile> <Compile Include="Graph\FirstInterfaceConventionTester.cs" /> + <Compile Include="Graph\GenericConnectionScannerTester.cs" /> <Compile Include="Graph\SingleImplementationScannerTester.cs" /> <Compile Include="Graph\TestExplicitArguments.cs" /> <Compile Include="Graph\FillDependenciesTester.cs"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 02:35:13
|
Revision: 333 http://structuremap.svn.sourceforge.net/structuremap/?rev=333&view=rev Author: jeremydmiller Date: 2010-02-04 01:24:52 +0000 (Thu, 04 Feb 2010) Log Message: ----------- finally fixed the issue w/ the lifecycle being displayed incorrectly in WhatDoIHave() Modified Paths: -------------- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs trunk/Source/StructureMap.Testing/Graph/ConventionBasedSetterInjectionTester.cs Modified: trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2010-02-04 00:56:59 UTC (rev 332) +++ trunk/Source/StructureMap/Diagnostics/WhatDoIHaveWriter.cs 2010-02-04 01:24:52 UTC (rev 333) @@ -82,11 +82,11 @@ if (pluginType.Lifecycle != null) { - _writer.AddContent("Scoped as: " + pluginType.Lifecycle.GetType().Name); + _writer.AddContent("Scoped as: " + pluginType.Lifecycle); } else { - _writer.AddContent("Scoped as: PerRequest"); + _writer.AddContent("Scoped as: PerRequest/Transient"); } foreach (InstanceRef instance in pluginType.Instances) Modified: trunk/Source/StructureMap.Testing/Graph/ConventionBasedSetterInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/ConventionBasedSetterInjectionTester.cs 2010-02-04 00:56:59 UTC (rev 332) +++ trunk/Source/StructureMap.Testing/Graph/ConventionBasedSetterInjectionTester.cs 2010-02-04 01:24:52 UTC (rev 333) @@ -40,6 +40,9 @@ plugin.Setters.IsMandatory("LastName").ShouldBeTrue(); plugin.Setters.IsMandatory("Gateway").ShouldBeFalse(); plugin.Setters.IsMandatory("Service").ShouldBeFalse(); + + + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 02:35:31
|
Revision: 332 http://structuremap.svn.sourceforge.net/structuremap/?rev=332&view=rev Author: jeremydmiller Date: 2010-02-04 00:56:59 +0000 (Thu, 04 Feb 2010) Log Message: ----------- patch from Kyle Malloy that adds IContext.BuildUp() and fixes a bug where Lifecycle was not correctly set from a Configure() block Modified Paths: -------------- trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IContext.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2010-02-04 00:52:26 UTC (rev 331) +++ trunk/Source/StructureMap/BuildSession.cs 2010-02-04 00:56:59 UTC (rev 332) @@ -5,6 +5,7 @@ using StructureMap.Interceptors; using StructureMap.Pipeline; using StructureMap.Util; +using StructureMap.Construction; namespace StructureMap { @@ -61,6 +62,17 @@ } } + public void BuildUp(object target) + { + Type pluggedType = target.GetType(); + IConfiguredInstance instance = _pipelineGraph.GetDefault(pluggedType) as IConfiguredInstance + ?? new ConfiguredInstance(pluggedType); + + IInstanceBuilder builder = PluginCache.FindBuilder(pluggedType); + var arguments = new Arguments(instance, this); + builder.BuildUp(arguments, target); + } + public T GetInstance<T>() { return (T) CreateInstance(typeof (T)); @@ -186,4 +198,4 @@ return _pipelineGraph.ForType(pluginType); } } -} \ No newline at end of file +} Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2010-02-04 00:52:26 UTC (rev 331) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2010-02-04 00:56:59 UTC (rev 332) @@ -242,6 +242,11 @@ public void ImportFrom(PluginFamily source) { + if (source.Lifecycle != null) + { + SetScopeTo(source.Lifecycle); + } + source.Instances.Each(instance => _instances.Fill(instance.Name, instance)); source._pluggedTypes.Each((key, plugin) => _pluggedTypes.Fill(key, plugin)); Modified: trunk/Source/StructureMap/IContext.cs =================================================================== --- trunk/Source/StructureMap/IContext.cs 2010-02-04 00:52:26 UTC (rev 331) +++ trunk/Source/StructureMap/IContext.cs 2010-02-04 00:56:59 UTC (rev 332) @@ -27,6 +27,14 @@ string RequestedName { get; } /// <summary> + /// The "BuildUp" method takes in an already constructed object + /// and uses Setter Injection to push in configured dependencies + /// of that object + /// </summary> + /// <param name="target"></param> + void BuildUp(object target); + + /// <summary> /// Get the object of type T that is valid for this build session. /// </summary> /// <typeparam name="T"></typeparam> @@ -81,4 +89,4 @@ /// <returns></returns> IEnumerable<T> GetAllInstances<T>(); } -} \ No newline at end of file +} Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2010-02-04 00:52:26 UTC (rev 331) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2010-02-04 00:56:59 UTC (rev 332) @@ -281,6 +281,18 @@ family.SetScopeTo(InstanceScope.ThreadLocal); Assert.IsInstanceOfType(typeof (ThreadLocalStorageLifecycle), family.Lifecycle); } + + [Test] + public void Lifecycle_is_imported_from_the_source_when_merging_PluginFamilies() + { + var source = new PluginFamily(typeof(GenericType<>)); + source.SetScopeTo(InstanceScope.Unique); + var importInto = new PluginFamily(typeof(GenericType<>)); + + importInto.ImportFrom(source); + + importInto.Lifecycle.ShouldBeOfType(source.Lifecycle.GetType()); + } } @@ -313,4 +325,6 @@ public interface IDevice { } -} \ No newline at end of file + + public class GenericType<T> { } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 02:35:32
|
Revision: 330 http://structuremap.svn.sourceforge.net/structuremap/?rev=330&view=rev Author: jeremydmiller Date: 2010-02-04 00:51:33 +0000 (Thu, 04 Feb 2010) Log Message: ----------- committed a patch from Frank Quednau related to open generics types Modified Paths: -------------- trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2010-02-04 00:33:40 UTC (rev 329) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2010-02-04 00:51:33 UTC (rev 330) @@ -288,9 +288,15 @@ return clone; }).Where(x => x != null).Each(templatedFamily.AddInstance); + //Are there instances that close the templatedtype straight away? + _instances.GetAll() + .Where(x => x.ConcreteType.CanBeCastTo(templatedType)) + .Each(templatedFamily.AddInstance); + // Need to attach the new PluginFamily to the old PluginGraph Parent.PluginFamilies.Add(templatedFamily); + return templatedFamily; } @@ -363,4 +369,4 @@ #endregion } -} \ No newline at end of file +} Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2010-02-04 00:33:40 UTC (rev 329) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2010-02-04 00:51:33 UTC (rev 330) @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Linq; using StructureMap.Construction; using StructureMap.Graph; @@ -198,7 +199,7 @@ if (value.GetType() == dependencyType) return new ObjectInstance(value); TypeConverter converter = TypeDescriptor.GetConverter(dependencyType); - object convertedValue = converter.ConvertFrom(value); + object convertedValue = converter.ConvertFrom(null, CultureInfo.InvariantCulture, value); return new ObjectInstance(convertedValue); } catch (Exception e) @@ -276,4 +277,4 @@ return "'{0}' -> {1}".ToFormat(Name, _plugin.PluggedType.FullName); } } -} \ No newline at end of file +} Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2010-02-04 00:33:40 UTC (rev 329) +++ trunk/Source/StructureMap/TypeExtensions.cs 2010-02-04 00:51:33 UTC (rev 330) @@ -181,7 +181,7 @@ return false; } - if (pluginType.IsOpenGeneric() && pluggedType.IsGenericType) + if (pluginType.IsOpenGeneric()) { return GenericsPluginGraph.CanBeCast(pluginType, pluggedType); } @@ -257,4 +257,4 @@ } } } -} \ No newline at end of file +} Modified: trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2010-02-04 00:33:40 UTC (rev 329) +++ trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs 2010-02-04 00:51:33 UTC (rev 330) @@ -157,6 +157,13 @@ } [Test] + public void CanPlugConcreteNonGenericClassIntoGenericInterface() + { + typeof(NotSoGenericService).CanBeCastTo(typeof(IGenericService<>)) + .ShouldBeTrue(); + } + + [Test] public void Define_profile_with_generics_and_concrete_type() { var container = new Container(registry => @@ -306,4 +313,9 @@ return typeof (T); } } -} \ No newline at end of file + + public class NotSoGenericService : IGenericService<string> + { + public void DoSomething(string thing) { } + } +} Modified: trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2010-02-04 00:33:40 UTC (rev 329) +++ trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2010-02-04 00:51:33 UTC (rev 330) @@ -37,7 +37,15 @@ public class Service3<T> : IService<T> { } + + public interface IOtherService<T> + { + } + public class Service4 : IOtherService<string> + { + } + [PluginFamily("Default")] public interface IThingy { @@ -134,6 +142,22 @@ } [Test] + public void Add_an_assembly_on_the_fly_and_pick_up_plugins4() + { + var container = new Container(); + container.Configure( + registry => registry.Scan( + x => + { + x.AssemblyContainingType(typeof (IOtherService<>)); + x.AddAllTypesOf(typeof (IOtherService<>)); + })); + + var instances = container.GetAllInstances<IOtherService<string>>(); + instances.Any(s=> s is Service4).ShouldBeTrue(); + } + + [Test] public void Add_generic_stuff_in_configure() { var container = new Container(); @@ -331,4 +355,4 @@ throw new NotImplementedException(); } } -} \ 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...> - 2010-02-04 03:17:14
|
Revision: 336 http://structuremap.svn.sourceforge.net/structuremap/?rev=336&view=rev Author: jeremydmiller Date: 2010-02-04 03:17:07 +0000 (Thu, 04 Feb 2010) Log Message: ----------- Made disposing a container much, much more robust Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/Pipeline/MainObjectCache.cs trunk/Source/StructureMap/Pipeline/ObjectInstance.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/Container.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -442,6 +442,7 @@ { _interceptorLibrary = _interceptorLibrary, _pipelineGraph = _pipelineGraph.ToNestedGraph(), + _onDispose = nestedDispose }; // Fixes a mild bug. The child container should inject itself @@ -463,11 +464,25 @@ return container; } + + private Action<Container> _onDispose = fullDispose; public void Dispose() { - _pipelineGraph.Dispose(); + _onDispose(this); } + private static void fullDispose(Container c) + { + c.Model.AllInstances.Each(i => i.EjectObject()); + + nestedDispose(c); + } + + private static void nestedDispose(Container c) + { + c._pipelineGraph.Dispose(); + } + #endregion /// <summary> Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/InstanceFactory.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -142,6 +142,7 @@ public void Dispose() { + _instances.GetAll().Each(i => i.SafeDispose()); _instances.Clear(); } } Modified: trunk/Source/StructureMap/Pipeline/MainObjectCache.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/Pipeline/MainObjectCache.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -25,7 +25,7 @@ var disposable = _objects[key] as IDisposable; _objects.Remove(key); - disposeObject(disposable); + disposable.SafeDispose(); } public object Get(Type pluginType, Instance instance) @@ -59,25 +59,12 @@ { if (@object is Container) return; - disposeObject(@object as IDisposable); + @object.SafeDispose(); }); _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/ObjectInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -40,6 +40,12 @@ public void Dispose() { + bool isContainer = _object is IContainer; + if (!isContainer) + { + _object.SafeDispose(); + } + _object = null; } Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -55,17 +55,17 @@ public void Dispose() { - if (_factories.ContainsKey(typeof (IContainer))) - { - foreach (Instance instance in _factories[typeof (IContainer)].AllInstances) - { - var disposable = instance as IDisposable; - if (disposable != null) - { - disposable.Dispose(); - } - } - } + //if (_factories.ContainsKey(typeof (IContainer))) + //{ + // foreach (Instance instance in _factories[typeof (IContainer)].AllInstances) + // { + // var disposable = instance as IDisposable; + // if (disposable != null) + // { + // disposable.Dispose(); + // } + // } + //} foreach (var factory in _factories) { Modified: trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -24,6 +24,7 @@ void IFamily.Eject(Instance instance) { cache.Eject(_pluginType, instance); + instance.SafeDispose(); } object IFamily.Build(Instance instance) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap/TypeExtensions.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -19,7 +19,21 @@ list.Add(value); } + public static void SafeDispose(this object target) + { + var disposable = target as IDisposable; + if (disposable == null) return; + try + { + disposable.Dispose(); + } + catch (Exception) + { + } + } + + public static void TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Action<TValue> action) { Modified: trunk/Source/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs 2010-02-04 02:32:53 UTC (rev 335) +++ trunk/Source/StructureMap.Testing/Pipeline/ContainerDisposalTester.cs 2010-02-04 03:17:07 UTC (rev 336) @@ -16,6 +16,35 @@ #endregion [Test] + public void disposing_a_main_container_will_dispose_an_object_injected_into_the_container() + { + var disposable = new C2Yes(); + var container = new Container(x => x.For<C2Yes>().Use(disposable)); + + container.Dispose(); + + disposable.WasDisposed.ShouldBeTrue(); + } + + [Test] + public void main_container_should_dispose_singletons() + { + var container = new Container(x => + { + x.ForSingletonOf<C1Yes>().Use<C1Yes>(); + }); + + var single = container.GetInstance<C1Yes>(); + + container.Dispose(); + + single.WasDisposed.ShouldBeTrue(); + } + + + + + [Test] public void disposing_a_nested_container_does_not_try_to_dispose_objects_created_by_the_parent() { var container = new Container(x => { x.ForSingletonOf<I1>().Use<C1No>(); }); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 03:48:00
|
Revision: 337 http://structuremap.svn.sourceforge.net/structuremap/?rev=337&view=rev Author: jeremydmiller Date: 2010-02-04 03:47:54 +0000 (Thu, 04 Feb 2010) Log Message: ----------- convenience methods to get at all the weird Instance types Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs trunk/Source/StructureMap/PipelineGraph.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2010-02-04 03:17:07 UTC (rev 336) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2010-02-04 03:47:54 UTC (rev 337) @@ -59,6 +59,48 @@ } + /// <summary> + /// Conditional binding of instances + /// </summary> + /// <param name="configuration"></param> + /// <returns></returns> + public ConditionalInstance<PLUGINTYPE> ConditionallyUse( + Action<ConditionalInstance<PLUGINTYPE>.ConditionalInstanceExpression> configuration) + { + var instance = new ConditionalInstance<PLUGINTYPE>(configuration); + Use(instance); + + return instance; + } + + /// <summary> + /// Access to all of the uncommon Instance types + /// </summary> + /// <param name="configure"></param> + /// <returns></returns> + public CreatePluginFamilyExpression<PLUGINTYPE> Use(Action<IInstanceExpression<PLUGINTYPE>> configure) + { + var expression = new InstanceExpression<PLUGINTYPE>(i => Use(i)); + configure(expression); + + return this; + } + + + /// <summary> + /// Access to all of the uncommon Instance types + /// </summary> + /// <param name="configure"></param> + /// <returns></returns> + public CreatePluginFamilyExpression<PLUGINTYPE> Add(Action<IInstanceExpression<PLUGINTYPE>> configure) + { + var expression = new InstanceExpression<PLUGINTYPE>(i => Add(i)); + configure(expression); + + return this; + } + + private CreatePluginFamilyExpression<PLUGINTYPE> alterAndContinue(Action<PluginFamily> action) { _alterations.Add(action); Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2010-02-04 03:17:07 UTC (rev 336) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2010-02-04 03:47:54 UTC (rev 337) @@ -110,6 +110,7 @@ _registry.addExpression(graph => graph.SetDefault(_parent._profileName, _pluginType, instance)); } + /// <summary> /// Use the named Instance as the Profile Instance for this PluginType @@ -248,6 +249,17 @@ } /// <summary> + /// Access to the uncommon types of Instance + /// </summary> + /// <param name="configure"></param> + public void Use(Action<IInstanceExpression<T>> configure) + { + var expression = new InstanceExpression<T>(i => Use(i)); + configure(expression); + } + + + /// <summary> /// For this Profile, use the Concrete Type /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 03:17:07 UTC (rev 336) +++ trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 03:47:54 UTC (rev 337) @@ -55,18 +55,6 @@ public void Dispose() { - //if (_factories.ContainsKey(typeof (IContainer))) - //{ - // foreach (Instance instance in _factories[typeof (IContainer)].AllInstances) - // { - // var disposable = instance as IDisposable; - // if (disposable != null) - // { - // disposable.Dispose(); - // } - // } - //} - foreach (var factory in _factories) { factory.Value.Dispose(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 13:50:24
|
Revision: 340 http://structuremap.svn.sourceforge.net/structuremap/?rev=340&view=rev Author: jeremydmiller Date: 2010-02-04 13:50:17 +0000 (Thu, 04 Feb 2010) Log Message: ----------- pushed the AssemblyScannerExtensions to the same namespace as Registry Modified Paths: -------------- trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs Added Paths: ----------- trunk/Source/StructureMap/Configuration/DSL/AssemblyScannerExtensions.cs Removed Paths: ------------- trunk/Source/StructureMap/AssemblyScannerExtensions.cs Deleted: trunk/Source/StructureMap/AssemblyScannerExtensions.cs =================================================================== --- trunk/Source/StructureMap/AssemblyScannerExtensions.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap/AssemblyScannerExtensions.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,63 +0,0 @@ -using System; -using StructureMap.Configuration.DSL.Expressions; -using StructureMap.Graph; - -namespace StructureMap -{ - /// <summary> - /// Extend the assembly scanning DSL to support the built-in registration conventions - /// </summary> - public static class AssemblyScannerExtensions - { - /// <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> - public static ConfigureConventionExpression WithDefaultConventions(this IAssemblyScanner assemblyScanner) - { - var convention = new DefaultConventionScanner(); - assemblyScanner.With(convention); - return new ConfigureConventionExpression(convention); - } - - /// <summary> - /// Scans for PluginType's and Concrete Types that close the given open generic type - /// </summary> - /// <example> - /// - /// </example> - /// <param name="openGenericType"></param> - public static ConfigureConventionExpression ConnectImplementationsToTypesClosing(this IAssemblyScanner assemblyScanner, Type openGenericType) - { - var convention = new GenericConnectionScanner(openGenericType); - assemblyScanner.With(convention); - return new ConfigureConventionExpression(convention); - } - - /// <summary> - /// Automatically registers all concrete types without primitive arguments - /// against its first interface, if any - /// </summary> - public static ConfigureConventionExpression RegisterConcreteTypesAgainstTheFirstInterface(this IAssemblyScanner assemblyScanner) - { - var convention = new FirstInterfaceConvention(); - assemblyScanner.With(convention); - return new ConfigureConventionExpression(convention); - } - - /// <summary> - /// Directs the scanning to automatically register any type that is the single - /// implementation of an interface against that interface. - /// The filters apply - /// </summary> - public static ConfigureConventionExpression SingleImplementationsOfInterface(this IAssemblyScanner assemblyScanner) - { - var convention = new ImplementationMap(); - assemblyScanner.With(convention); - assemblyScanner.ModifyGraphAfterScan(convention.RegisterSingleImplementations); - return new ConfigureConventionExpression(convention); - } - - } -} \ No newline at end of file Copied: trunk/Source/StructureMap/Configuration/DSL/AssemblyScannerExtensions.cs (from rev 338, trunk/Source/StructureMap/AssemblyScannerExtensions.cs) =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/AssemblyScannerExtensions.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/AssemblyScannerExtensions.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -0,0 +1,63 @@ +using System; +using StructureMap.Configuration.DSL.Expressions; +using StructureMap.Graph; + +namespace StructureMap.Configuration.DSL +{ + /// <summary> + /// Extend the assembly scanning DSL to support the built-in registration conventions + /// </summary> + public static class AssemblyScannerExtensions + { + /// <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> + public static ConfigureConventionExpression WithDefaultConventions(this IAssemblyScanner assemblyScanner) + { + var convention = new DefaultConventionScanner(); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Scans for PluginType's and Concrete Types that close the given open generic type + /// </summary> + /// <example> + /// + /// </example> + /// <param name="openGenericType"></param> + public static ConfigureConventionExpression ConnectImplementationsToTypesClosing(this IAssemblyScanner assemblyScanner, Type openGenericType) + { + var convention = new GenericConnectionScanner(openGenericType); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Automatically registers all concrete types without primitive arguments + /// against its first interface, if any + /// </summary> + public static ConfigureConventionExpression RegisterConcreteTypesAgainstTheFirstInterface(this IAssemblyScanner assemblyScanner) + { + var convention = new FirstInterfaceConvention(); + assemblyScanner.With(convention); + return new ConfigureConventionExpression(convention); + } + + /// <summary> + /// Directs the scanning to automatically register any type that is the single + /// implementation of an interface against that interface. + /// The filters apply + /// </summary> + public static ConfigureConventionExpression SingleImplementationsOfInterface(this IAssemblyScanner assemblyScanner) + { + var convention = new ImplementationMap(); + assemblyScanner.With(convention); + assemblyScanner.ModifyGraphAfterScan(convention.RegisterSingleImplementations); + return new ConfigureConventionExpression(convention); + } + + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 13:50:17 UTC (rev 340) @@ -336,7 +336,7 @@ <Compile Include="CloseGenericTypeExpression.cs" /> <Compile Include="ConfigurationExpression.cs" /> <Compile Include="Configuration\DictionaryReader.cs" /> - <Compile Include="AssemblyScannerExtensions.cs" /> + <Compile Include="Configuration\DSL\AssemblyScannerExtensions.cs" /> <Compile Include="Configuration\DSL\Expressions\InstanceExpression.cs" /> <Compile Include="Configuration\DSL\IRegistry.cs" /> <Compile Include="Configuration\DSL\SetterConvention.cs" /> Modified: trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs =================================================================== --- trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap.DebuggerVisualizers.Testing/Program.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,5 +1,6 @@ using System.Diagnostics; using System.Windows.Forms; +using StructureMap.Configuration.DSL; namespace StructureMap.DebuggerVisualizers.Testing { Modified: trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap.Testing/Bugs/TryGetInstanceWithOpenGenericsBugTester.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,5 +1,6 @@ using System.Diagnostics; using NUnit.Framework; +using StructureMap.Configuration.DSL; using StructureMap.TypeRules; namespace StructureMap.Testing.Bugs Modified: trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap.Testing/Graph/FirstInterfaceConventionTester.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,5 +1,6 @@ using System.Linq; using NUnit.Framework; +using StructureMap.Configuration.DSL; using StructureMap.Testing.Widget3; using StructureMap.TypeRules; Modified: trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap.Testing/Graph/GenericConnectionScannerTester.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,5 +1,6 @@ using System; using NUnit.Framework; +using StructureMap.Configuration.DSL; using StructureMap.Graph; namespace StructureMap.Testing.Graph Modified: trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2010-02-04 13:40:53 UTC (rev 339) +++ trunk/Source/StructureMap.Testing/Graph/SingleImplementationScannerTester.cs 2010-02-04 13:50:17 UTC (rev 340) @@ -1,5 +1,6 @@ using System; using NUnit.Framework; +using StructureMap.Configuration.DSL; namespace StructureMap.Testing.Graph { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 14:09:08
|
Revision: 339 http://structuremap.svn.sourceforge.net/structuremap/?rev=339&view=rev Author: jeremydmiller Date: 2010-02-04 13:40:53 +0000 (Thu, 04 Feb 2010) Log Message: ----------- Added the Func<> lazy evaluation strategy Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/ModelQueryTester.cs trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Pipeline/AdapterClasses.cs trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/Container.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -558,9 +558,17 @@ _pluginGraph.FindFamily(typeof (IContainer)).AddInstance(thisInstance); _pluginGraph.ProfileManager.SetDefault(typeof (IContainer), thisInstance); + var funcInstance = new FactoryTemplate(typeof (LazyInstance<>)); + _pluginGraph.FindFamily(typeof(Func<>)).AddInstance(funcInstance); + _pluginGraph.ProfileManager.SetDefault(typeof(Func<>), funcInstance); + + + pluginGraph.Log.AssertFailures(); _pipelineGraph = new PipelineGraph(pluginGraph); + + } [Obsolete("delegate to something cleaner in BuildSession")] Added: trunk/Source/StructureMap/Pipeline/AdapterClasses.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/AdapterClasses.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/AdapterClasses.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -0,0 +1,79 @@ +using System; + +namespace StructureMap.Pipeline +{ + public class FactoryTemplate : Instance + { + private readonly Type _openInstanceType; + + public FactoryTemplate(Type openInstanceType) + { + _openInstanceType = openInstanceType; + } + + protected override string getDescription() + { + return string.Empty; + } + + protected override object build(Type pluginType, BuildSession session) + { + throw new NotImplementedException(); + } + + public override Instance CloseType(Type[] types) + { + var instanceType = _openInstanceType.MakeGenericType(types); + return (Instance) Activator.CreateInstance(instanceType); + } + } + + + public class LazyInstance<T> : Instance + { + protected override string getDescription() + { + return "Lazy construction of " + typeof (T).FullName; + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<T> func = () => container.GetInstance<T>(); + + return func; + } + } + + public class FactoryInstance<T> : Instance + { + protected override string getDescription() + { + return "Lazy factory of " + typeof (T).FullName; + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<string, T> func = name => container.GetInstance<T>(name); + + return func; + } + } + + public class FactoryInstance<T, T1> : Instance + { + protected override string getDescription() + { + return "Lazy construction of {0} using {1}".ToFormat(typeof(T1).FullName, typeof(T).FullName); + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<T, T1> func = key => container.With(key).GetInstance<T1>(); + + return func; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -37,6 +37,8 @@ var factory = new InstanceFactory(family); _factories.Add(family.PluginType, factory); }); + + } private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph, GraphLog log) Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 13:40:53 UTC (rev 339) @@ -371,6 +371,7 @@ <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="Pipeline\AdapterClasses.cs" /> <Compile Include="Pipeline\HybridLifecycle.cs" /> <Compile Include="Query\EmptyConfiguration.cs" /> <Compile Include="Query\GenericFamilyConfiguration.cs" /> Modified: trunk/Source/StructureMap.Testing/ModelQueryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -112,15 +112,15 @@ [Test] public void Iterate_over_pluginTypes() { - // 3 registered plus the 4th is the IContainer itself - _model.PluginTypes.Count().ShouldEqual(4); + // 3 registered plus the 4th is the IContainer itself + Func + _model.PluginTypes.Count().ShouldEqual(5); } [Test] public void Iterate_over_pluginTypes_w_container() { - // IContainer is always added to the Container - _container.Model.PluginTypes.Count().ShouldEqual(4); + // IContainer is always added to the Container + Func<> + _container.Model.PluginTypes.Count().ShouldEqual(5); } [Test] Added: trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -0,0 +1,78 @@ +using System; +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class LazyFuncTester + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void FactoryTemplateTester() + { + var container = new Container(x => + { + x.For(typeof (Func<>)).Use(new FactoryTemplate(typeof (LazyInstance<>))); + }); + + //container.GetInstance<Func<ConcreteClass>>()().ShouldNotBeNull(); + } + + [Test] + public void build_a_func_for_a_concrete_class() + { + var container = new Container(); + var func = container.GetInstance<Func<ConcreteClass>>(); + + func().ShouldNotBeNull(); + } + + [Test] + public void build_a_func_that_returns_a_transient() + { + var container = new Container(x => + { + x.For<IWidget>().Use<ColorWidget>().Ctor<string>("color").Is("green"); + }); + + var func = container.GetInstance<Func<IWidget>>(); + var w1 = func(); + var w2 = func(); + var w3 = func(); + + w1.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("green"); + + w1.ShouldNotBeTheSameAs(w2); + w1.ShouldNotBeTheSameAs(w3); + w2.ShouldNotBeTheSameAs(w3); + } + + [Test] + public void build_a_func_that_returns_a_singleton() + { + var container = new Container(x => + { + x.ForSingletonOf<IWidget>().Use<ColorWidget>().Ctor<string>("color").Is("green"); + }); + + var func = container.GetInstance<Func<IWidget>>(); + var w1 = func(); + var w2 = func(); + var w3 = func(); + + w1.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("green"); + + w1.ShouldBeTheSameAs(w2); + w1.ShouldBeTheSameAs(w3); + w2.ShouldBeTheSameAs(w3); + } + + public class ConcreteClass{} + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -46,8 +46,8 @@ [Test] public void can_iterate_through_families_including_both_generics_and_normal() { - // +1 for "IContainer" itself - container.Model.PluginTypes.Count().ShouldEqual(8); + // +1 for "IContainer" itself + Func + container.Model.PluginTypes.Count().ShouldEqual(9); container.Model.PluginTypes.Each(x => Debug.WriteLine(x.PluginType.FullName)); } @@ -86,7 +86,7 @@ [Test] public void get_all_instances_from_the_top() { - container.Model.AllInstances.Count().ShouldEqual(11); + container.Model.AllInstances.Count().ShouldEqual(12); } [Test] Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-02-04 13:40:53 UTC (rev 339) @@ -364,6 +364,7 @@ <Compile Include="Pipeline\GenericsHelperExpressionTester.cs" /> <Compile Include="Pipeline\HybridBuildLifecycleTester.cs" /> <Compile Include="Pipeline\InstanceTester.cs" /> + <Compile Include="Pipeline\LazyFuncTester.cs" /> <Compile Include="Pipeline\MainObjectCacheTester.cs" /> <Compile Include="Pipeline\ObjectInstanceTester.cs" /> <Compile Include="Pipeline\MissingInstanceTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jer...@us...> - 2010-02-04 23:30:26
|
Revision: 341 http://structuremap.svn.sourceforge.net/structuremap/?rev=341&view=rev Author: jeremydmiller Date: 2010-02-04 23:30:20 +0000 (Thu, 04 Feb 2010) Log Message: ----------- Fixing a bug with the Dispose() method on PipelineGraph that only comes into play on a nested container Modified Paths: -------------- trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/PipelineGraph.cs Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2010-02-04 13:50:17 UTC (rev 340) +++ trunk/Source/StructureMap/InstanceFactory.cs 2010-02-04 23:30:20 UTC (rev 341) @@ -142,8 +142,12 @@ public void Dispose() { - _instances.GetAll().Each(i => i.SafeDispose()); _instances.Clear(); } + + public void DisposeInstances() + { + _instances.GetAll().Each(i => i.SafeDispose()); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 13:50:17 UTC (rev 340) +++ trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 23:30:20 UTC (rev 341) @@ -57,10 +57,16 @@ public void Dispose() { + if (_factories.ContainsKey(typeof(IContainer))) + { + _factories[typeof (IContainer)].AllInstances.Each(x => x.SafeDispose()); + } + foreach (var factory in _factories) { factory.Value.Dispose(); } + _factories.Clear(); _profileManager.Dispose(); _genericsGraph.ClearAll(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fli...@us...> - 2010-02-10 01:10:36
|
Revision: 346 http://structuremap.svn.sourceforge.net/structuremap/?rev=346&view=rev Author: flimflan Date: 2010-02-10 00:14:42 +0000 (Wed, 10 Feb 2010) Log Message: ----------- Fix bug in Registry equality. Two different instances of Registry are not considered equal. Two different instances of the same Registry derived type are considered equal. Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2010-02-05 03:06:42 UTC (rev 345) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2010-02-10 00:14:42 UTC (rev 346) @@ -417,31 +417,27 @@ return (type.GetConstructor(new Type[0]) != null); } - public bool Equals(Registry obj) + public bool Equals(Registry other) { - if (ReferenceEquals(null, obj)) return false; - return GetType().Equals(obj.GetType()); + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + if(other.GetType() == typeof(Registry) && GetType() == typeof(Registry)) return false; + return Equals(other.GetType(), GetType()); } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; - - if (obj is Registry) return false; - - - if (obj.GetType() != typeof (Registry)) return false; + if (!typeof (Registry).IsAssignableFrom(obj.GetType())) return false; return Equals((Registry) obj); } public override int GetHashCode() { - return 0; + return GetType().GetHashCode(); } - #region Nested type: BuildWithExpression - /// <summary> /// Define the constructor and setter arguments for the default T /// </summary> @@ -457,7 +453,5 @@ public SmartInstance<T> Configure { get { return _instance; } } } - - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2010-02-05 03:06:42 UTC (rev 345) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2010-02-10 00:14:42 UTC (rev 346) @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using NUnit.Framework; using StructureMap.Configuration.DSL; using StructureMap.Graph; @@ -63,20 +64,37 @@ } [Test] - public void Equals_check_true() + public void an_instance_of_the_base_registry_is_equal_to_itself() { + var registry1 = new Registry(); + + registry1.Equals((object)registry1).ShouldBeTrue(); + } + + [Test] + public void two_instances_of_the_base_registry_type_are_not_considered_equal() + { + var registry1 = new Registry(); + var registry2 = new Registry(); + + registry1.Equals((object)registry2).ShouldBeFalse(); + } + + [Test] + public void two_instances_of_a_derived_registry_type_are_considered_equal() + { var registry1 = new TestRegistry(); var registry2 = new TestRegistry(); var registry3 = new TestRegistry2(); var registry4 = new TestRegistry2(); - registry1.Equals(registry1).ShouldBeTrue(); - registry1.Equals(registry2).ShouldBeTrue(); - registry2.Equals(registry1).ShouldBeTrue(); - registry3.Equals(registry4).ShouldBeTrue(); + registry1.Equals((object)registry1).ShouldBeTrue(); + registry1.Equals((object)registry2).ShouldBeTrue(); + registry2.Equals((object)registry1).ShouldBeTrue(); + registry3.Equals((object)registry4).ShouldBeTrue(); - registry1.Equals(registry3).ShouldBeFalse(); - registry3.Equals(registry1).ShouldBeFalse(); + registry1.Equals((object)registry3).ShouldBeFalse(); + registry3.Equals((object)registry1).ShouldBeFalse(); } [Test] @@ -94,7 +112,46 @@ container.GetAllInstances<IWidget>().Count.ShouldEqual(5); } + public class MutatedWidget : IWidget + { + public void DoSomething() { } + } + + public class MutatingRegistry : Registry + { + private static int count = 0; + + public MutatingRegistry() + { + For<IWidget>().Use<AWidget>(); + + if(count++ >= 1) + { + For<IWidget>().Use<MutatedWidget>(); + } + } + } + [Test] + public void include_an_existing_registry_should_not_reevaluate_the_registry() + { + var registry1 = new Registry(); + registry1.IncludeRegistry<MutatingRegistry>(); + + var registry2 = new Registry(); + registry2.IncludeRegistry<MutatingRegistry>(); + + var container = new Container(config => + { + config.AddRegistry(registry1); + config.AddRegistry(registry2); + }); + + container.GetInstance<IWidget>().ShouldBeOfType<AWidget>(); + } + + + [Test] public void Latch_on_a_PluginGraph() { var registry2 = new TestRegistry2(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |