From: <jer...@us...> - 2008-05-15 16:32:29
|
Revision: 96 http://structuremap.svn.sourceforge.net/structuremap/?rev=96&view=rev Author: jeremydmiller Date: 2008-05-15 09:32:20 -0700 (Thu, 15 May 2008) Log Message: ----------- A LOT OF REFACTORINGS PipelineGraph, BuildSession, cleaning up InstanceFactory & InstanceManager, introduced TypeRules to centralize "Type" testing rules, refactored emitting code Modified Paths: -------------- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Configuration/StructureMapConfigurationSection.cs trunk/Source/StructureMap/Emitting/BuildInstanceMethod.cs trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs trunk/Source/StructureMap/Emitting/Parameters/ChildArrayParameterEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/EnumParameterEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/StringParameterEmitter.cs trunk/Source/StructureMap/Exceptions/StructureMapException.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginCollection.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/Graph/TypePath.cs trunk/Source/StructureMap/IInstanceFactory.cs trunk/Source/StructureMap/InstanceBuilder.cs trunk/Source/StructureMap/InstanceBuilderList.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/InstanceManager.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs trunk/Source/StructureMap/Interceptors/InterceptorLibrary.cs trunk/Source/StructureMap/Interceptors/Interceptors.cs trunk/Source/StructureMap/Interceptors/StartupInterceptor.cs trunk/Source/StructureMap/MementoSource.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/DefaultInstance.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ILocationPolicy.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/Pipeline/LiteralInstance.cs trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs trunk/Source/StructureMap/Pipeline/ReferencedInstance.cs trunk/Source/StructureMap/Pipeline/UserControlInstance.cs trunk/Source/StructureMap/PluginGraphBuilder.cs trunk/Source/StructureMap/Source/BasicXmlMementoSource.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.AutoMocking/AutoMockedInstanceManager.cs trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs trunk/Source/StructureMap.Testing/Configuration/ConfigurationParserTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/FamilyParserTester.cs trunk/Source/StructureMap.Testing/Configuration/NormalGraphBuilderTester.cs trunk/Source/StructureMap.Testing/Container/DynamicInjectionTester.cs trunk/Source/StructureMap.Testing/Container/EmittingTester.cs trunk/Source/StructureMap.Testing/Container/EnumerationTester.cs trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs trunk/Source/StructureMap.Testing/Container/PluginGraphBuilderTester.cs trunk/Source/StructureMap.Testing/Container/SetterInjectionEmittingTester.cs trunk/Source/StructureMap.Testing/Container/TypeFindingTester.cs trunk/Source/StructureMap.Testing/GenericsAcceptanceTester.cs trunk/Source/StructureMap.Testing/GenericsIntegrationTester.cs trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Graph/PluginGraphTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/ImplicitPluginFromPluggedTypeAttributeTester.cs trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs trunk/Source/StructureMap.Testing/ObjectMother.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/DefaultInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/InstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/LiteralInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/PrototypeInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ReferencedInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.Widget/ArrayConstruction.cs trunk/Source/StructureMap.Testing.Widget/Columns.cs trunk/Source/StructureMap.Testing.Widget/Decision.cs trunk/Source/StructureMap.Testing.Widget/Hierarchy.cs trunk/Source/StructureMap.Testing.Widget/IWidget.cs trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs trunk/Source/StructureMap.Testing.Widget3/Gateways.cs trunk/Source/StructureMap.Testing.Widget4/Strategy.cs trunk/Source/StructureMap.Testing.Widget5/BasicGridColumnInstanceBuilder.cs Added Paths: ----------- trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/Configuration/GraphBuilder.cs trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/IArgumentVisitor.cs trunk/Source/StructureMap/Graph/TypeRules.cs trunk/Source/StructureMap/MemoryInstanceMemento.cs trunk/Source/StructureMap/Pipeline/BuildPolicy.cs trunk/Source/StructureMap/Pipeline/CacheInterceptor.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/Pipeline/HybridBuildPolicy.cs trunk/Source/StructureMap/Pipeline/IBuildInterceptor.cs trunk/Source/StructureMap/Pipeline/IBuildPolicy.cs trunk/Source/StructureMap/Pipeline/IBuildSession.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap.Testing/BuildSessionTester.cs trunk/Source/StructureMap.Testing/MementoTester.cs trunk/Source/StructureMap.Testing/Pipeline/StubBuildSession.cs trunk/Source/StructureMap.Testing/Pipeline/TypeRulesTester.cs Removed Paths: ------------- trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs trunk/Source/StructureMap/Emitting/ConstructorEmitter.cs trunk/Source/StructureMap/Pipeline/BuildStrategies.cs trunk/Source/StructureMap.Testing/Debugging.cs trunk/Source/StructureMap.Testing/Pipeline/StubInstanceCreator.cs Added: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs (rev 0) +++ trunk/Source/StructureMap/BuildSession.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Text; +using StructureMap.Interceptors; +using StructureMap.Pipeline; + +namespace StructureMap +{ + public class BuildSession : IBuildSession + { + private readonly PipelineGraph _pipelineGraph; + private readonly InterceptorLibrary _interceptorLibrary; + + public BuildSession(PipelineGraph pipelineGraph, InterceptorLibrary interceptorLibrary) + { + _pipelineGraph = pipelineGraph; + _interceptorLibrary = interceptorLibrary; + } + + private IInstanceFactory forType(Type pluginType) + { + return _pipelineGraph.ForType(pluginType); + } + + public object CreateInstance(Type type, string name) + { + return forType(type).Build(this, name); + } + + public object CreateInstance(Type pluginType, Instance instance) + { + return forType(pluginType).Build(this, instance); + } + + public Array CreateInstanceArray(Type pluginType, Instance[] instances) + { + // TODO -- default to returning all + if (instances == null) + { + throw new StructureMapException(205, pluginType, "UNKNOWN"); + } + + // TODO: 3.5, move this to an extension method of Array? + Array array = Array.CreateInstance(pluginType, instances.Length); + for (int i = 0; i < instances.Length; i++) + { + Instance instance = instances[i]; + + object arrayValue = forType(pluginType).Build(this, instance); + array.SetValue(arrayValue, i); + } + + return array; + } + + public object CreateInstance(Type pluginType) + { + Instance instance = _pipelineGraph.GetDefault(pluginType); + + if (instance == null) + { + throw new StructureMapException(202, pluginType.FullName); + } + + return forType(pluginType).Build(this, instance); + } + + public object ApplyInterception(Type pluginType, object actualValue) + { + return _interceptorLibrary.FindInterceptor(actualValue.GetType()).Process(actualValue); + } + + public InstanceBuilder FindBuilderByType(Type pluginType, Type pluggedType) + { + return forType(pluginType).FindBuilderByType(pluggedType); + } + + public InstanceBuilder FindBuilderByConcreteKey(Type pluginType, string concreteKey) + { + return forType(pluginType).FindBuilderByConcreteKey(concreteKey); + } + } +} Modified: trunk/Source/StructureMap/Configuration/ConfigurationParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/ConfigurationParser.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -48,12 +48,13 @@ includedPath = Path.Combine(folder, fileName); includedDoc.Load(includedPath); - // TODO: get rid of throw, put on PluginGraph here + ConfigurationParser parser = new ConfigurationParser(includedDoc.DocumentElement); list.Add(parser); } catch (Exception ex) { + // TODO: get rid of throw, put on PluginGraph here throw new StructureMapException(150, ex, fileName); } } Modified: trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -70,6 +70,7 @@ } catch (Exception ex) { + // TODO -- needs to log to PluginGraph instead throw new StructureMapException(100, filename, ex); } } Modified: trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/DSL/ExpressionValidator.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -23,7 +23,7 @@ public void IntoPluginType(Type pluginType) { - if (!Plugin.CanBeCast(pluginType, _pluggedType)) + if (!TypeRules.CanBeCast(pluginType, _pluggedType)) { throw new StructureMapException( 303, Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -15,7 +15,7 @@ public class CreatePluginFamilyExpression<PLUGINTYPE> : IExpression { private readonly List<Action<PluginFamily>> _alterations = new List<Action<PluginFamily>>(); - private readonly List<IExpression> _children = new List<IExpression>(); + private readonly List<Action<PluginGraph>> _children = new List<Action<PluginGraph>>(); private readonly Type _pluginType; private readonly InstanceScope _scope = InstanceScope.PerRequest; @@ -31,20 +31,14 @@ PluginFamily family = graph.FindFamily(_pluginType); family.SetScopeTo(_scope); - foreach (IExpression child in _children) - { - child.Configure(graph); - } - - foreach (Action<PluginFamily> alteration in _alterations) - { - alteration(family); - } + // TODO: clean up with 3.5 + _children.ForEach(delegate(Action<PluginGraph> action) { action(graph); }); + _alterations.ForEach(delegate(Action<PluginFamily> action) { action(family); }); } #endregion - // TODO: Try alterAndContinue(f => {}); + // TODO: 3.5, Try alterAndContinue(f => {}); /// <summary> /// Sets the default instance of a Type to the definition represented by builder @@ -122,22 +116,38 @@ } - public CreatePluginFamilyExpression<PLUGINTYPE> OnCreation(StartupHandler<PLUGINTYPE> handler) + public CreatePluginFamilyExpression<PLUGINTYPE> OnCreation(Action<PLUGINTYPE> handler) { - _alterations.Add( - delegate(PluginFamily family) { family.InstanceInterceptor = new StartupInterceptor<PLUGINTYPE>(handler); }); + _children.Add( + delegate(PluginGraph graph) + { + InterceptionFunction function = delegate(object target) + { + handler((PLUGINTYPE) target); + return target; + }; + PluginTypeInterceptor interceptor = new PluginTypeInterceptor(typeof(PLUGINTYPE), function); + graph.InterceptorLibrary.AddInterceptor(interceptor); + }); + return this; } public CreatePluginFamilyExpression<PLUGINTYPE> EnrichWith(EnrichmentHandler<PLUGINTYPE> handler) { - _alterations.Add( - delegate(PluginFamily family) - { - family.InstanceInterceptor = new EnrichmentInterceptor<PLUGINTYPE>(handler); - }); + _children.Add( + delegate(PluginGraph graph) + { + InterceptionFunction function = delegate(object target) + { + return handler((PLUGINTYPE)target); + }; + PluginTypeInterceptor interceptor = new PluginTypeInterceptor(typeof(PLUGINTYPE), function); + graph.InterceptorLibrary.AddInterceptor(interceptor); + }); + return this; } Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -7,8 +7,6 @@ namespace StructureMap.Configuration.DSL { - public delegate object InterceptionDelegate(object target); - public class Registry : IDisposable { private readonly List<IExpression> _expressions = new List<IExpression>(); @@ -284,7 +282,7 @@ public class TypeInterceptorExpression : IExpression, TypeInterceptor { private readonly Predicate<Type> _match; - private InterceptionDelegate _interception; + private InterceptionFunction _interception; internal TypeInterceptorExpression(Predicate<Type> match) { @@ -314,7 +312,7 @@ #endregion - public void InterceptWith(InterceptionDelegate interception) + public void InterceptWith(InterceptionFunction interception) { _interception = interception; } Added: trunk/Source/StructureMap/Configuration/GraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/GraphBuilder.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -0,0 +1,131 @@ +using System; +using System.Reflection; +using StructureMap.Configuration.DSL; +using StructureMap.Graph; +using StructureMap.Pipeline; + +namespace StructureMap.Configuration +{ + public class GraphBuilder : IGraphBuilder + { + private readonly PluginGraph _pluginGraph; + private readonly PluginGraph _systemGraph; + private Profile _profile; + private InstanceManager _systemInstanceManager; + + + public GraphBuilder(Registry[] registries) : this(registries, new PluginGraph()) + { + } + + public GraphBuilder(Registry[] registries, PluginGraph pluginGraph) + { + _pluginGraph = pluginGraph; + foreach (Registry registry in registries) + { + registry.ConfigurePluginGraph(_pluginGraph); + } + + _systemGraph = new PluginGraph(false); + _systemGraph.Assemblies.Add(Assembly.GetExecutingAssembly()); + } + + #region IGraphBuilder Members + + public void FinishFamilies() + { + _pluginGraph.Seal(); + } + + public PluginGraph SystemGraph + { + get { return _systemGraph; } + } + + public PluginGraph PluginGraph + { + get { return _pluginGraph; } + } + + public void AddAssembly(string assemblyName) + { + try + { + Assembly assembly = AppDomain.CurrentDomain.Load(assemblyName); + _pluginGraph.Assemblies.Add(assembly); + _systemGraph.Assemblies.Add(assembly); + } + catch (Exception ex) + { + _pluginGraph.Log.RegisterError(101, ex, assemblyName); + } + } + + public void PrepareSystemObjects() + { + // TODO: is this a problem here? + _systemGraph.Seal(); + } + + public IProfileBuilder GetProfileBuilder() + { + return new ProfileBuilder(_pluginGraph); + } + + public void ConfigureFamily(TypePath pluginTypePath, Action<PluginFamily> action) + { + try + { + Type pluginType = pluginTypePath.FindType(); + PluginFamily family = _pluginGraph.FindFamily(pluginType); + action(family); + } + catch (Exception ex) + { + _pluginGraph.Log.RegisterError(103, ex, pluginTypePath.ClassName, pluginTypePath.AssemblyName); + } + } + + + public void WithSystemObject<T>(InstanceMemento memento, string context, Action<T> action) + { + try + { + T systemObject = (T) buildSystemObject(typeof (T), memento); + action(systemObject); + } + catch (Exception ex) + { + _pluginGraph.Log.RegisterError(130, ex, context); + } + } + + + public void WithType(TypePath path, string context, Action<Type> action) + { + try + { + Type type = path.FindType(); + action(type); + } + catch (Exception ex) + { + _pluginGraph.Log.RegisterError(131, ex, path.AssemblyQualifiedName, context); + } + } + + #endregion + + private object buildSystemObject(Type type, InstanceMemento memento) + { + Instance instance = memento.ReadInstance(_systemGraph, type); + + if (_systemInstanceManager == null) + { + _systemInstanceManager = new InstanceManager(_systemGraph); + } + + return _systemInstanceManager.CreateInstance(type, instance); + } + } +} \ No newline at end of file Deleted: trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -1,131 +0,0 @@ -using System; -using System.Reflection; -using StructureMap.Configuration.DSL; -using StructureMap.Graph; -using StructureMap.Pipeline; - -namespace StructureMap.Configuration -{ - public class NormalGraphBuilder : IGraphBuilder - { - private readonly PluginGraph _pluginGraph; - private readonly PluginGraph _systemGraph; - private Profile _profile; - private InstanceManager _systemInstanceManager; - - - public NormalGraphBuilder(Registry[] registries) : this(registries, new PluginGraph()) - { - } - - public NormalGraphBuilder(Registry[] registries, PluginGraph pluginGraph) - { - _pluginGraph = pluginGraph; - foreach (Registry registry in registries) - { - registry.ConfigurePluginGraph(_pluginGraph); - } - - _systemGraph = new PluginGraph(false); - _systemGraph.Assemblies.Add(Assembly.GetExecutingAssembly()); - } - - #region IGraphBuilder Members - - public void FinishFamilies() - { - _pluginGraph.Seal(); - } - - public PluginGraph SystemGraph - { - get { return _systemGraph; } - } - - public PluginGraph PluginGraph - { - get { return _pluginGraph; } - } - - public void AddAssembly(string assemblyName) - { - try - { - Assembly assembly = AppDomain.CurrentDomain.Load(assemblyName); - _pluginGraph.Assemblies.Add(assembly); - _systemGraph.Assemblies.Add(assembly); - } - catch (Exception ex) - { - _pluginGraph.Log.RegisterError(101, ex, assemblyName); - } - } - - public void PrepareSystemObjects() - { - // TODO: is this a problem here? - _systemGraph.Seal(); - } - - public IProfileBuilder GetProfileBuilder() - { - return new ProfileBuilder(_pluginGraph); - } - - public void ConfigureFamily(TypePath pluginTypePath, Action<PluginFamily> action) - { - try - { - Type pluginType = pluginTypePath.FindType(); - PluginFamily family = _pluginGraph.FindFamily(pluginType); - action(family); - } - catch (Exception ex) - { - _pluginGraph.Log.RegisterError(103, ex, pluginTypePath.ClassName, pluginTypePath.AssemblyName); - } - } - - - public void WithSystemObject<T>(InstanceMemento memento, string context, Action<T> action) - { - try - { - T systemObject = (T) buildSystemObject(typeof (T), memento); - action(systemObject); - } - catch (Exception ex) - { - _pluginGraph.Log.RegisterError(130, ex, context); - } - } - - - public void WithType(TypePath path, string context, Action<Type> action) - { - try - { - Type type = path.FindType(); - action(type); - } - catch (Exception ex) - { - _pluginGraph.Log.RegisterError(131, ex, path.AssemblyQualifiedName, context); - } - } - - #endregion - - private object buildSystemObject(Type type, InstanceMemento memento) - { - Instance instance = memento.ReadInstance(_systemGraph, type); - - if (_systemInstanceManager == null) - { - _systemInstanceManager = new InstanceManager(_systemGraph); - } - - return _systemInstanceManager.CreateInstance(type, instance); - } - } -} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/StructureMapConfigurationSection.cs =================================================================== --- trunk/Source/StructureMap/Configuration/StructureMapConfigurationSection.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Configuration/StructureMapConfigurationSection.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -26,6 +26,7 @@ IList<XmlNode> nodes = ConfigurationSettings.GetConfig(XmlConstants.STRUCTUREMAP) as IList<XmlNode>; if (nodes == null) { + // TODO -- need to get this into PluginGraph instead throw new StructureMapException(105, XmlConstants.STRUCTUREMAP); } return nodes; Added: trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs (rev 0) +++ trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -0,0 +1,77 @@ +using System.Reflection; +using System.Reflection.Emit; +using StructureMap.Emitting.Parameters; +using StructureMap.Graph; + +namespace StructureMap.Emitting +{ + public class ArgumentEmitter : IArgumentVisitor + { + private readonly ChildParameterEmitter _child = new ChildParameterEmitter(); + private readonly ChildArrayParameterEmitter _childArray = new ChildArrayParameterEmitter(); + private readonly EnumParameterEmitter _enum = new EnumParameterEmitter(); + private readonly PrimitiveParameterEmitter _primitive = new PrimitiveParameterEmitter(); + private readonly StringParameterEmitter _string = new StringParameterEmitter(); + private readonly ILGenerator ilgen; + + + public ArgumentEmitter(ILGenerator ilgen) + { + this.ilgen = ilgen; + } + + #region IArgumentVisitor Members + + public void PrimitiveSetter(PropertyInfo property) + { + _primitive.Setter(ilgen, property); + } + + public void StringSetter(PropertyInfo property) + { + _string.Setter(ilgen, property); + } + + public void EnumSetter(PropertyInfo property) + { + _enum.Setter(ilgen, property); + } + + public void ChildSetter(PropertyInfo property) + { + _child.Setter(ilgen, property); + } + + public void ChildArraySetter(PropertyInfo property) + { + _childArray.Setter(ilgen, property); + } + + public void PrimitiveParameter(ParameterInfo parameter) + { + _primitive.Ctor(ilgen, parameter); + } + + public void StringParameter(ParameterInfo parameter) + { + _string.Ctor(ilgen, parameter); + } + + public void EnumParameter(ParameterInfo parameter) + { + _enum.Ctor(ilgen, parameter); + } + + public void ChildParameter(ParameterInfo parameter) + { + _child.Ctor(ilgen, parameter); + } + + public void ChildArrayParameter(ParameterInfo parameter) + { + _childArray.Ctor(ilgen, parameter); + } + + #endregion + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Emitting/BuildInstanceMethod.cs =================================================================== --- trunk/Source/StructureMap/Emitting/BuildInstanceMethod.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/BuildInstanceMethod.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -14,26 +14,26 @@ public class BuildInstanceMethod : Method { private readonly Plugin _plugin; - private readonly ConstructorInfo _constructor; - private readonly ParameterEmitter _parameterEmitter; + //private readonly ConstructorInfo _constructor; + //private readonly ParameterEmitter _parameterEmitter; public BuildInstanceMethod(Plugin plugin) : base() { - _constructor = plugin.GetConstructor(); + //_constructor = plugin.GetConstructor(); - _parameterEmitter = new StringParameterEmitter(); + //_parameterEmitter = new StringParameterEmitter(); - _parameterEmitter.AttachNextSibling(new PrimitiveParameterEmitter()); - _parameterEmitter.AttachNextSibling(new EnumParameterEmitter()); - _parameterEmitter.AttachNextSibling(new ChildParameterEmitter()); - _parameterEmitter.AttachNextSibling(new ChildArrayParameterEmitter()); + //_parameterEmitter.AttachNextSibling(new PrimitiveParameterEmitter()); + //_parameterEmitter.AttachNextSibling(new EnumParameterEmitter()); + //_parameterEmitter.AttachNextSibling(new ChildParameterEmitter()); + //_parameterEmitter.AttachNextSibling(new ChildArrayParameterEmitter()); _plugin = plugin; } public override Type[] ArgumentList { - get { return new Type[] { typeof(IConfiguredInstance), typeof(StructureMap.Pipeline.IInstanceCreator) }; } + get { return new Type[] { typeof(IConfiguredInstance), typeof(StructureMap.Pipeline.IBuildSession) }; } } public override string MethodName @@ -51,20 +51,15 @@ protected override void Generate(ILGenerator ilgen) { ilgen.DeclareLocal(typeof (object)); + ArgumentEmitter arguments = new ArgumentEmitter(ilgen); - foreach (ParameterInfo parameter in _constructor.GetParameters()) - { - _parameterEmitter.Generate(ilgen, parameter); - } + _plugin.VisitConstructor(arguments); - ilgen.Emit(OpCodes.Newobj, _constructor); + ilgen.Emit(OpCodes.Newobj, _plugin.GetConstructor()); Label label = ilgen.DefineLabel(); ilgen.Emit(OpCodes.Stloc_0); - foreach (SetterProperty setter in _plugin.Setters) - { - _parameterEmitter.GenerateSetter(ilgen, setter.Property); - } + _plugin.VisitSetters(arguments); ilgen.Emit(OpCodes.Br_S, label); ilgen.MarkLabel(label); Deleted: trunk/Source/StructureMap/Emitting/ConstructorEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/ConstructorEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/ConstructorEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Reflection.Emit; -using System.Text; -using StructureMap.Graph; -using StructureMap.Pipeline; - -namespace StructureMap.Emitting -{ - public class ConstructorEmitter : IPluginArgumentVisitor - { - private readonly ILGenerator _ilgen; - - public ConstructorEmitter(ILGenerator ilgen) - { - _ilgen = ilgen; - } - - - public void Primitive(string name) - { - throw new NotImplementedException(); - } - - public void Child(string name, Type childType) - { - throw new NotImplementedException(); - } - - public void ChildArray(string name, Type childType) - { - throw new NotImplementedException(); - } - - protected void callInstanceMemento(ILGenerator ilgen, string methodName) - { - MethodInfo _method = typeof(IConfiguredInstance).GetMethod(methodName); - ilgen.Emit(OpCodes.Callvirt, _method); - } - - protected void cast(ILGenerator ilgen, Type parameterType) - { - ilgen.Emit(OpCodes.Castclass, parameterType); - } - } -} Modified: trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs =================================================================== --- trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -16,7 +16,7 @@ public InstanceBuilderAssembly(Type pluginType, IEnumerable<Plugin> plugins) { - string assemblyName = Guid.NewGuid().ToString().Replace(".", "") + "InstanceBuilderAssembly"; + string assemblyName = guidString() + "InstanceBuilderAssembly"; _dynamicAssembly = new DynamicAssembly(assemblyName); _pluginType = pluginType; @@ -26,6 +26,11 @@ } } + private static string guidString() + { + return Guid.NewGuid().ToString().Replace(".", ""); + } + /// <summary> /// Gets a class name for the InstanceBuilder that will be emitted for this Plugin /// </summary> @@ -49,7 +54,7 @@ className = escapeClassName(pluggedType); } - return className + "InstanceBuilder"; + return className + "InstanceBuilder" + guidString(); } private static string escapeClassName(Type type) @@ -61,7 +66,7 @@ private void processPlugin(Plugin plugin) { - if (Plugin.CanBeCast(_pluginType, plugin.PluggedType)) + if (TypeRules.CanBeCast(_pluginType, plugin.PluggedType)) { string className = getInstanceBuilderClassName(plugin.PluggedType); ClassBuilder builderClass = Modified: trunk/Source/StructureMap/Emitting/Parameters/ChildArrayParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/ChildArrayParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/ChildArrayParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -10,20 +10,8 @@ /// </summary> public class ChildArrayParameterEmitter : ParameterEmitter { - protected override bool canProcess(Type parameterType) + public void Ctor(ILGenerator ilgen, ParameterInfo parameter) { - bool returnValue = false; - - if (parameterType.IsArray) - { - returnValue = (!parameterType.GetElementType().IsPrimitive); - } - - return returnValue; - } - - protected override void generate(ILGenerator ilgen, ParameterInfo parameter) - { Type parameterType = parameter.ParameterType; string parameterName = parameter.Name; @@ -34,18 +22,23 @@ private void putChildArrayFromInstanceMementoOntoStack(ILGenerator ilgen, Type argumentType, string argumentName) { ilgen.Emit(OpCodes.Ldarg_2); - ilgen.Emit(OpCodes.Ldstr, argumentType.GetElementType().AssemblyQualifiedName); + + //ilgen.Emit(OpCodes.Ldstr, argumentType.GetElementType().AssemblyQualifiedName); + ilgen.Emit(OpCodes.Ldtoken, argumentType.GetElementType()); + MethodInfo method = typeof(Type).GetMethod("GetTypeFromHandle"); + ilgen.Emit(OpCodes.Call, method); + ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Ldstr, argumentName); callInstanceMemento(ilgen, "GetChildrenArray"); - MethodInfo methodCreateInstanceArray = (typeof (StructureMap.Pipeline.IInstanceCreator).GetMethod("CreateInstanceArray")); + MethodInfo methodCreateInstanceArray = (typeof (StructureMap.Pipeline.IBuildSession).GetMethod("CreateInstanceArray")); ilgen.Emit(OpCodes.Callvirt, methodCreateInstanceArray); cast(ilgen, argumentType); } - protected override void generateSetter(ILGenerator ilgen, PropertyInfo property) + public void Setter(ILGenerator ilgen, PropertyInfo property) { ilgen.Emit(OpCodes.Ldloc_0); putChildArrayFromInstanceMementoOntoStack(ilgen, property.PropertyType, property.Name); Modified: trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -10,13 +10,8 @@ /// </summary> public class ChildParameterEmitter : ParameterEmitter { - protected override bool canProcess(Type parameterType) + public void Ctor(ILGenerator ilgen, ParameterInfo parameter) { - return (!parameterType.IsPrimitive && !parameterType.IsArray); - } - - protected override void generate(ILGenerator ilgen, ParameterInfo parameter) - { Type parameterType = parameter.ParameterType; string parameterName = parameter.Name; @@ -39,7 +34,7 @@ cast(ilgen, parameterType); } - protected override void generateSetter(ILGenerator ilgen, PropertyInfo property) + public void Setter(ILGenerator ilgen, PropertyInfo property) { ilgen.Emit(OpCodes.Ldloc_0); Modified: trunk/Source/StructureMap/Emitting/Parameters/EnumParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/EnumParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/EnumParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -9,13 +9,8 @@ /// </summary> public class EnumParameterEmitter : ParameterEmitter { - protected override bool canProcess(Type parameterType) + public void Ctor(ILGenerator ilgen, ParameterInfo parameter) { - return (parameterType.IsEnum); - } - - protected override void generate(ILGenerator ilgen, ParameterInfo parameter) - { Type parameterType = parameter.ParameterType; string parameterName = parameter.Name; @@ -44,7 +39,7 @@ ilgen.Emit(OpCodes.Ldind_I4); } - protected override void generateSetter(ILGenerator ilgen, PropertyInfo property) + public void Setter(ILGenerator ilgen, PropertyInfo property) { ilgen.Emit(OpCodes.Ldloc_0); putEnumerationValueFromMementoOntoStack(ilgen, property.PropertyType, property.Name); Modified: trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -1,6 +1,7 @@ using System; using System.Reflection; using System.Reflection.Emit; +using StructureMap.Graph; using StructureMap.Pipeline; namespace StructureMap.Emitting.Parameters @@ -12,67 +13,6 @@ /// </summary> public abstract class ParameterEmitter { - private ParameterEmitter _nextSibling; - - protected ParameterEmitter NextSibling - { - set { _nextSibling = value; } - get { return _nextSibling; } - } - - public void Generate(ILGenerator ilgen, ParameterInfo parameter) - { - if (canProcess(parameter.ParameterType)) - { - generate(ilgen, parameter); - } - else if (_nextSibling != null) - { - _nextSibling.Generate(ilgen, parameter); - } - else - { - string msg = - string.Format("Cannot emit constructor injection for type *{0}*", - parameter.ParameterType.AssemblyQualifiedName); - throw new ApplicationException(msg); - } - } - - public void GenerateSetter(ILGenerator ilgen, PropertyInfo property) - { - if (canProcess(property.PropertyType)) - { - generateSetter(ilgen, property); - } - else if (_nextSibling != null) - { - _nextSibling.GenerateSetter(ilgen, property); - } - else - { - string msg = - string.Format("Cannot emit constructor injection for type *{0}*", - property.PropertyType.AssemblyQualifiedName); - throw new ApplicationException(msg); - } - } - - public void AttachNextSibling(ParameterEmitter Sibling) - { - if (NextSibling == null) - { - NextSibling = Sibling; - } - else - { - NextSibling.AttachNextSibling(Sibling); - } - } - - protected abstract bool canProcess(Type parameterType); - protected abstract void generate(ILGenerator ilgen, ParameterInfo parameter); - protected void callInstanceMemento(ILGenerator ilgen, string methodName) { MethodInfo _method = typeof (IConfiguredInstance).GetMethod(methodName); @@ -82,8 +22,6 @@ protected void cast(ILGenerator ilgen, Type parameterType) { ilgen.Emit(OpCodes.Castclass, parameterType); - } - - protected abstract void generateSetter(ILGenerator ilgen, PropertyInfo property); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -9,13 +9,8 @@ /// </summary> public class PrimitiveParameterEmitter : ParameterEmitter { - protected override bool canProcess(Type parameterType) + public void Ctor(ILGenerator ilgen, ParameterInfo parameter) { - return (parameterType.IsPrimitive && !parameterType.Equals(typeof (string))); - } - - protected override void generate(ILGenerator ilgen, ParameterInfo parameter) - { ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Ldstr, parameter.Name); callInstanceMemento(ilgen, "GetProperty"); @@ -31,7 +26,7 @@ } - protected override void generateSetter(ILGenerator ilgen, PropertyInfo property) + public void Setter(ILGenerator ilgen, PropertyInfo property) { ilgen.Emit(OpCodes.Ldloc_0); ilgen.Emit(OpCodes.Ldarg_1); Modified: trunk/Source/StructureMap/Emitting/Parameters/StringParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/StringParameterEmitter.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Emitting/Parameters/StringParameterEmitter.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -9,20 +9,15 @@ /// </summary> public class StringParameterEmitter : ParameterEmitter { - protected override bool canProcess(Type parameterType) + public void Ctor(ILGenerator ilgen, ParameterInfo parameter) { - return (parameterType.Equals(typeof (string))); - } - - protected override void generate(ILGenerator ilgen, ParameterInfo parameter) - { ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Ldstr, parameter.Name); callInstanceMemento(ilgen, "GetProperty"); } - protected override void generateSetter(ILGenerator ilgen, PropertyInfo property) + public void Setter(ILGenerator ilgen, PropertyInfo property) { ilgen.Emit(OpCodes.Ldloc_0); ilgen.Emit(OpCodes.Ldarg_1); Modified: trunk/Source/StructureMap/Exceptions/StructureMapException.cs =================================================================== --- trunk/Source/StructureMap/Exceptions/StructureMapException.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Exceptions/StructureMapException.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -44,11 +44,21 @@ get { return _errorCode; } } + // TODO: Centralize this code somewhere so it isn't duplicated private void initialize(int errorCode, params object[] args) { _errorCode = errorCode; _msg = "StructureMap Exception Code: " + _errorCode + "\n"; + for (int i = 0; i < args.Length; i++) + { + object arg = args[i]; + Type type = arg as Type; + if (type != null) + { + args[i] = type.AssemblyQualifiedName; + } + } string errorMsg = getMessage(ErrorCode); if (errorMsg == null) Added: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs (rev 0) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -0,0 +1,112 @@ +using System; +using System.Reflection; + +namespace StructureMap.Graph +{ + public class Constructor : TypeRules + { + /// <summary> + /// Returns the System.Reflection.ConstructorInfo for the PluggedType. Uses either + /// the "greediest" constructor with the most arguments or the constructor function + /// marked with the [DefaultConstructor] + /// </summary> + /// <returns></returns> + public static ConstructorInfo GetConstructor(Type pluggedType) + { + ConstructorInfo returnValue = DefaultConstructorAttribute.GetConstructor(pluggedType); + + // if no constructor is marked as the "ContainerConstructor", find the greediest constructor + if (returnValue == null) + { + returnValue = GetGreediestConstructor(pluggedType); + } + + if (returnValue == null) + { + throw new StructureMapException(180, pluggedType.Name); + } + + return returnValue; + } + + public static ConstructorInfo GetGreediestConstructor(Type pluggedType) + { + ConstructorInfo returnValue = null; + + foreach (ConstructorInfo constructor in pluggedType.GetConstructors()) + { + if (returnValue == null) + { + returnValue = constructor; + } + else if (constructor.GetParameters().Length > returnValue.GetParameters().Length) + { + returnValue = constructor; + } + } + + return returnValue; + } + + + private readonly Type _pluggedType; + private ConstructorInfo _ctor; + + public Constructor(Type pluggedType) + { + _pluggedType = pluggedType; + _ctor = GetConstructor(pluggedType); + } + + public bool CanBeAutoFilled() + { + foreach (ParameterInfo parameter in _ctor.GetParameters()) + { + if (!IsChild(parameter.ParameterType)) + { + return false; + } + } + + return true; + } + + public string FindFirstConstructorArgumentOfType<T>() + { + foreach (ParameterInfo info in _ctor.GetParameters()) + { + if (info.ParameterType.Equals(typeof(T))) + { + return info.Name; + } + } + + return null; + } + + + public ConstructorInfo Ctor + { + get { return _ctor; } + } + + public void Visit(IArgumentVisitor visitor) + { + foreach (ParameterInfo info in _ctor.GetParameters()) + { + Type parameterType = info.ParameterType; + + visitParameter(info, parameterType, visitor); + } + } + + private void visitParameter(ParameterInfo info, Type parameterType, IArgumentVisitor visitor) + { + if (IsPrimitive(parameterType)) visitor.PrimitiveParameter(info); + if (IsChild(parameterType)) visitor.ChildParameter(info); + if (IsChildArray(parameterType)) visitor.ChildArrayParameter(info); + if (IsEnum(parameterType)) visitor.EnumParameter(info); + if (IsString(parameterType)) visitor.StringParameter(info); + } + } +} Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -99,7 +99,7 @@ // Add Plugins foreach (Plugin plugin in baseFamily.Plugins) { - if (plugin.CanBePluggedIntoGenericType(baseFamily.PluginType, templateTypes)) + if (CanBePluggedIntoGenericType(baseFamily.PluginType, plugin.PluggedType, templateTypes)) { Plugin templatedPlugin = CreateTemplatedClone(plugin, templateTypes); templatedFamily.Plugins.Add(templatedPlugin); @@ -142,5 +142,34 @@ return templatedPlugin; } + + + public static bool CanBePluggedIntoGenericType(Type pluginType, Type pluggedType, params Type[] templateTypes) + { + bool isValid = true; + + Type interfaceType = pluggedType.GetInterface(pluginType.Name); + if (interfaceType == null) + { + interfaceType = pluggedType.BaseType; + } + + Type[] pluginArgs = pluginType.GetGenericArguments(); + Type[] pluggableArgs = interfaceType.GetGenericArguments(); + + if (templateTypes.Length != pluginArgs.Length && + pluginArgs.Length != pluggableArgs.Length) + { + return false; + } + + for (int i = 0; i < templateTypes.Length; i++) + { + isValid &= templateTypes[i] == pluggableArgs[i] || + pluginArgs[i].IsGenericParameter && + pluggableArgs[i].IsGenericParameter; + } + return isValid; + } } } \ No newline at end of file Added: trunk/Source/StructureMap/Graph/IArgumentVisitor.cs =================================================================== --- trunk/Source/StructureMap/Graph/IArgumentVisitor.cs (rev 0) +++ trunk/Source/StructureMap/Graph/IArgumentVisitor.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -0,0 +1,19 @@ +using System.Reflection; + +namespace StructureMap.Graph +{ + public interface IArgumentVisitor + { + void PrimitiveSetter(PropertyInfo property); + void StringSetter(PropertyInfo property); + void EnumSetter(PropertyInfo property); + void ChildSetter(PropertyInfo property); + void ChildArraySetter(PropertyInfo property); + + void PrimitiveParameter(ParameterInfo parameter); + void StringParameter(ParameterInfo parameter); + void EnumParameter(ParameterInfo parameter); + void ChildParameter(ParameterInfo parameter); + void ChildArrayParameter(ParameterInfo parameter); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2008-05-09 15:47:15 UTC (rev 95) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2008-05-15 16:32:20 UTC (rev 96) @@ -1,119 +1,35 @@ using System; using System.Reflection; +using StructureMap.Emitting; +using StructureMap.Pipeline; namespace StructureMap.Graph { - public interface IPluginArgumentVisitor - { - void Primitive(string name); - void Child(string name, Type childType); - void ChildArray(string name, Type childType); - } - - - /// <summary> /// Represents a concrete class that can be built by StructureMap as an instance of the parent /// PluginFamily\x92s PluginType. The properties of a Plugin are the CLR Type of the concrete class, /// and the human-friendly concrete key that StructureMap will use to identify the Type. /// </summary> - public class Plugin + public class Plugin : TypeRules { - #region static + public static readonly string DEFAULT = "DEFAULT"; - - /// <summary> - /// Determines if the PluggedType is a valid Plugin into the - /// PluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool IsExplicitlyMarkedAsPlugin(Type pluginType, Type pluggedType) - { - bool returnValue = false; - - bool markedAsPlugin = PluggableAttribute.MarkedAsPluggable(pluggedType); - if (markedAsPlugin) - { - returnValue = CanBeCast(pluginType, pluggedType); - } - - return returnValue; - } - - - /// <summary> - /// Determines if the pluggedType can be upcast to the pluginType - /// </summary> - /// <param name="pluginType"></param> - /// <param name="pluggedType"></param> - /// <returns></returns> - public static bool CanBeCast(Type pluginType, Type pluggedType) - { - if (pluggedType.IsInterface || pluggedType.IsAbstract) - { - return false; - } - - if (GenericsPluginGraph.CanBeCast(pluginType, pluggedType)) - { - return true; - } - - ConstructorInfo constructor = GetGreediestConstructor(pluggedType); - if (constructor == null) - { - return false; - } - - return pluginType.IsAssignableFrom(pluggedType); - } - - - public static ConstructorInfo GetGreediestConstructor(Type pluggedType) - { - ConstructorInfo returnValue = null; - - foreach (ConstructorInfo constructor in pluggedType.GetConstructors()) - { - if (returnValue == null) - { - returnValue = constructor; - } - else if (constructor.GetParameters().Length > returnValue.GetParameters().Length) - { - returnValue = constructor; - } - } - - return returnValue; - } - - #endregion - private string _concreteKey; - private Type _pluggedType; - private SetterPropertyCollection _setters; + private readonly Type _pluggedType; + private readonly SetterPropertyCollection _setters; + private readonly Constructor _constructor; #region constructors - /// <summary> - /// Creates an Explicit Plugin for the pluggedType with the entered - /// concreteKey - /// </summary> - /// <param name="pluggedType"></param> - /// <param name="concreteKey"></param> - public Plugin(Type pluggedType, string concreteKey) : base() + public Plugin(Type pluggedType, string concreteKey) : this(pluggedType) { if (concreteKey == string.Empty) { + // TODO: Move into PluginFamily and get the exception logged somewhere throw new StructureMapException(112, pluggedType.FullName); } - _pluggedType = pluggedType; _concreteKey = concreteKey; - _setters = new SetterPropertyCollection(this); } public Plugin(Type pluggedType) @@ -123,7 +39,7 @@ _pluggedType = pluggedType; _setters = new SetterPropertyCollection(this); - + _constructor = new Constructor(pluggedType); } #endregion @@ -148,238 +64,74 @@ } /// <summary> - /// Finds any methods on the PluggedType marked with the [ValidationMethod] - /// attributes - /// </summary> - public MethodInfo[] ValidationMethods - { - get { return ValidationMethodAttribute.GetValidationMethods(_pluggedType); } - } - - /// <summary> /// Property's that will be filled by setter injection /// </summary> public SetterPropertyCollection Setters { get { - if (_setters == null) - { - _setters = new SetterPropertyCollection(this); - } - return _setters; } } - - /// <summary> - /// Determines if the concrete class can be autofilled. - /// </summary> public bool CanBeAutoFilled { get { - bool returnValue = true; - - ConstructorInfo ctor = GetConstructor(); - foreach (ParameterInfo parameter in ctor.GetParameters()) - { - returnValue = returnValue ... [truncated message content] |