From: <jer...@us...> - 2008-05-27 17:50:53
|
Revision: 102 http://structuremap.svn.sourceforge.net/structuremap/?rev=102&view=rev Author: jeremydmiller Date: 2008-05-27 10:50:51 -0700 (Tue, 27 May 2008) Log Message: ----------- killing off TODO's Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs trunk/Source/StructureMap/Configuration/FamilyParser.cs trunk/Source/StructureMap/Configuration/GraphBuilder.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/InstanceManager.cs trunk/Source/StructureMap/Pipeline/BuildPolicy.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs trunk/Source/StructureMap.Testing/Configuration/DefaultInstanceNodeTester.cs trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs trunk/Source/StructureMap.Testing.Widget/Rule.cs trunk/Source/StructureMap.Testing.Widget5/OtherGridColumn.cs Added Paths: ----------- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Building.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/IStructuredInstance.cs Modified: trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -12,8 +12,7 @@ /// <returns></returns> public static ConfiguredInstance Instance<PLUGGEDTYPE>() { - ConfiguredInstance instance = new ConfiguredInstance(); - instance.PluggedType = typeof (PLUGGEDTYPE); + ConfiguredInstance instance = new ConfiguredInstance(typeof (PLUGGEDTYPE)); return instance; } Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -42,24 +42,11 @@ _builder.ConfigureFamily(pluginTypePath, delegate(PluginFamily family) { - // TODO: there's a little duplication here InstanceScope scope = findScope(element); family.SetScopeTo(scope); - Type pluginType = family.PluginType; - - string name = element.GetAttribute(XmlConstants.NAME); - if (string.IsNullOrEmpty(name)) - { - name = "DefaultInstanceOf" + pluginTypePath.AssemblyQualifiedName; - } - InstanceMemento memento = _mementoCreator.CreateMemento(element); - memento.InstanceKey = name; - - family.DefaultInstanceKey = name; - - family.AddInstance(memento); + family.AddDefaultMemento(memento); }); } Modified: trunk/Source/StructureMap/Configuration/GraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -63,7 +63,6 @@ public void PrepareSystemObjects() { - // TODO: is this a problem here? _systemGraph.Seal(); } Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -286,5 +286,16 @@ return plugin; } } + + public void AddDefaultMemento(InstanceMemento memento) + { + if (string.IsNullOrEmpty(memento.InstanceKey)) + { + memento.InstanceKey = "DefaultInstanceOf" + TypePath.GetAssemblyQualifiedName(PluginType); + } + + AddInstance(memento); + DefaultInstanceKey = memento.InstanceKey; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -68,13 +68,11 @@ { if (property == null) { - // TODO -- log this throw new StructureMapException(240, propertyName, _plugin.PluggedType); } if (property.GetSetMethod() == null) { - // TODO -- log this throw new StructureMapException(241, propertyName, _plugin.PluggedType); } Modified: trunk/Source/StructureMap/InstanceManager.cs =================================================================== --- trunk/Source/StructureMap/InstanceManager.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/InstanceManager.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -96,8 +96,7 @@ public void InjectByName<PLUGINTYPE, CONCRETETYPE>(string instanceKey) { - ConfiguredInstance instance = new ConfiguredInstance(); - instance.PluggedType = typeof(CONCRETETYPE); + ConfiguredInstance instance = new ConfiguredInstance(typeof(CONCRETETYPE)); instance.Name = instanceKey; AddInstance<PLUGINTYPE>(instance); @@ -207,7 +206,6 @@ throw new StructureMapException(230, type.FullName); } - // TODO: Little bit of smelliness here Plugin plugin = new Plugin(type); if (!plugin.CanBeAutoFilled) { Modified: trunk/Source/StructureMap/Pipeline/BuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/BuildPolicy.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/BuildPolicy.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -15,8 +15,14 @@ object builtObject = instance.Build(pluginType, buildSession); - // TODO: error handling around the interception - return buildSession.ApplyInterception(pluginType, builtObject); + try + { + return buildSession.ApplyInterception(pluginType, builtObject); + } + catch (Exception e) + { + throw new StructureMapException(308, e, instance.Name, builtObject.GetType()); + } } public IBuildPolicy Clone() Added: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Building.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Building.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Building.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureMap.Pipeline +{ + public partial class ConfiguredInstance + { + Type IConfiguredInstance.PluggedType + { + get { return _pluggedType; } + } + + Instance[] IConfiguredInstance.GetChildrenArray(string propertyName) + { + return _arrays.ContainsKey(propertyName) ? _arrays[propertyName] : null; + } + + string IConfiguredInstance.GetProperty(string propertyName) + { + if (!_properties.ContainsKey(propertyName)) + { + throw new StructureMapException(205, propertyName, Name); + } + + return _properties[propertyName]; + } + + object IConfiguredInstance.GetChild(string propertyName, Type pluginType, IBuildSession buildSession) + { + return getChild(propertyName, pluginType, buildSession); + } + + string IConfiguredInstance.ConcreteKey + { + get { return _concreteKey; } + set { _concreteKey = value; } + } + + + // Only open for testing + object IConfiguredInstance.Build(Type pluginType, IBuildSession session, InstanceBuilder builder) + { + if (builder == null) + { + throw new StructureMapException( + 201, _concreteKey, Name, pluginType); + } + + + try + { + return builder.BuildInstance(this, session); + } + catch (StructureMapException) + { + throw; + } + catch (InvalidCastException ex) + { + throw new StructureMapException(206, ex, Name); + } + catch (Exception ex) + { + throw new StructureMapException(207, ex, Name, pluginType.FullName); + } + } + + } +} Added: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -0,0 +1,283 @@ +using System; +using System.Configuration; +using StructureMap.Configuration.DSL; +using StructureMap.Graph; + +namespace StructureMap.Pipeline +{ + public partial class ConfiguredInstance + { + /// <summary> + /// Use a named Plugin type denoted by a [Pluggable("Key")] attribute + /// </summary> + /// <param name="concreteKey"></param> + /// <returns></returns> + public ConfiguredInstance UsingConcreteTypeNamed(string concreteKey) + { + _concreteKey = concreteKey; + return this; + } + + /// <summary> + /// Use type T for the concrete type of an instance + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + public ConfiguredInstance UsingConcreteType<T>() + { + _pluggedType = typeof (T); + return this; + } + + public ChildArrayExpression ChildArray<PLUGINTYPE>(string propertyName) + { + validateTypeIsArray<PLUGINTYPE>(); + + ChildArrayExpression expression = + new ChildArrayExpression(this, propertyName, typeof (PLUGINTYPE)); + + return expression; + } + + public ChildArrayExpression ChildArray(string propertyName, Type childType) + { + return new ChildArrayExpression(this, propertyName, childType); + } + + public ChildArrayExpression ChildArray<PLUGINTYPE>() + { + validateTypeIsArray<PLUGINTYPE>(); + + string propertyName = findPropertyName<PLUGINTYPE>(); + return ChildArray<PLUGINTYPE>(propertyName); + } + + /// <summary> + /// Start the definition of a child instance for type CONSTRUCTORARGUMENTTYPE + /// </summary> + /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> + /// <returns></returns> + public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>() + { + string propertyName = findPropertyName<CONSTRUCTORARGUMENTTYPE>(); + + ChildInstanceExpression child = Child(propertyName); + child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); + + return child; + } + + public ChildInstanceExpression Child(string propertyName) + { + return new ChildInstanceExpression(this, propertyName); + } + + + /// <summary> + /// Starts the definition of a child instance specifying the argument name + /// in the case of a constructor function that consumes more than one argument + /// of type T + /// </summary> + /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> + /// <param name="propertyName"></param> + /// <returns></returns> + public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>(string propertyName) + { + ChildInstanceExpression child = new ChildInstanceExpression(this, propertyName); + child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); + + return child; + } + + /// <summary> + /// Start the definition of a primitive argument to a constructor argument + /// </summary> + /// <param name="propertyName"></param> + /// <returns></returns> + public PropertyExpression WithProperty(string propertyName) + { + return new PropertyExpression(this, propertyName); + } + + public ConfiguredInstance WithConcreteKey(string concreteKey) + { + replaceNameIfNotAlreadySet(concreteKey); + _concreteKey = concreteKey; + return this; + } + + + private string findPropertyName<T>() + { + Plugin plugin = new Plugin(_pluggedType); + string propertyName = plugin.FindFirstConstructorArgumentOfType<T>(); + + if (string.IsNullOrEmpty(propertyName)) + { + throw new StructureMapException(305, typeof (T)); + } + + return propertyName; + } + + private static void validateTypeIsArray<PLUGINTYPE>() + { + if (!typeof (PLUGINTYPE).IsArray) + { + throw new StructureMapException(307); + } + } + + #region Nested type: ChildArrayExpression + + public class ChildArrayExpression + { + private readonly Type _childType; + private readonly ConfiguredInstance _instance; + private readonly string _propertyName; + + public ChildArrayExpression(ConfiguredInstance instance, string propertyName, Type childType) + { + _instance = instance; + _propertyName = propertyName; + + _childType = _childType; + } + + public ConfiguredInstance Contains(params Instance[] instances) + { + _instance.setChildArray(_propertyName, instances); + + return _instance; + } + } + + #endregion + + #region Nested type: ChildInstanceExpression + + /// <summary> + /// Part of the Fluent Interface, represents a nonprimitive argument to a + /// constructure function + /// </summary> + public class ChildInstanceExpression + { + private readonly ConfiguredInstance _instance; + private readonly string _propertyName; + private Type _childType; + + + public ChildInstanceExpression(ConfiguredInstance instance, string propertyName) + { + _instance = instance; + _propertyName = propertyName; + } + + public ChildInstanceExpression(ConfiguredInstance instance, string propertyName, + Type childType) + : this(instance, propertyName) + { + _childType = childType; + } + + internal Type ChildType + { + set { _childType = value; } + } + + /// <summary> + /// Use a previously configured and named instance for the child + /// </summary> + /// <param name="instanceKey"></param> + /// <returns></returns> + public ConfiguredInstance IsNamedInstance(string instanceKey) + { + ReferencedInstance instance = new ReferencedInstance(instanceKey); + _instance.setChild(_propertyName, instance); + + return _instance; + } + + /// <summary> + /// Start the definition of a child instance by defining the concrete type + /// </summary> + /// <typeparam name="T"></typeparam> + /// <returns></returns> + public ConfiguredInstance IsConcreteType<T>() + { + Type pluggedType = typeof (T); + ExpressionValidator.ValidatePluggabilityOf(pluggedType).IntoPluginType(_childType); + + ConfiguredInstance childInstance = new ConfiguredInstance(); + childInstance._pluggedType = pluggedType; + _instance.setChild(_propertyName, childInstance); + + return _instance; + } + + + /// <summary> + /// Registers a configured instance to use as the argument to the parent's + /// constructor + /// </summary> + /// <param name="child"></param> + /// <returns></returns> + public ConfiguredInstance Is(Instance child) + { + _instance.setChild(_propertyName, child); + return _instance; + } + + public ConfiguredInstance Is(object value) + { + LiteralInstance instance = new LiteralInstance(value); + return Is(instance); + } + } + + #endregion + + #region Nested type: PropertyExpression + + /// <summary> + /// Defines the value of a primitive argument to a constructur argument + /// </summary> + public class PropertyExpression + { + private readonly ConfiguredInstance _instance; + private readonly string _propertyName; + + public PropertyExpression(ConfiguredInstance instance, string propertyName) + { + _instance = instance; + _propertyName = propertyName; + } + + /// <summary> + /// Sets the value of the constructor argument + /// </summary> + /// <param name="propertyValue"></param> + /// <returns></returns> + public ConfiguredInstance EqualTo(object propertyValue) + { + _instance.SetProperty(_propertyName, propertyValue.ToString()); + return _instance; + } + + /// <summary> + /// Sets the value of the constructor argument to the key/value in the + /// AppSettings + /// </summary> + /// <param name="appSettingKey"></param> + /// <returns></returns> + public ConfiguredInstance EqualToAppSetting(string appSettingKey) + { + string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; + _instance.SetProperty(_propertyName, propertyValue); + return _instance; + } + } + + #endregion + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -1,13 +1,11 @@ using System; using System.Collections.Generic; -using System.Configuration; -using StructureMap.Configuration.DSL; using StructureMap.Graph; namespace StructureMap.Pipeline { - // TODO: Move the FI stuff into a partial class - public class ConfiguredInstance : ExpressedInstance<ConfiguredInstance>, IConfiguredInstance + public partial class ConfiguredInstance : ExpressedInstance<ConfiguredInstance>, IConfiguredInstance, + IStructuredInstance { private readonly Dictionary<string, Instance> _children = new Dictionary<string, Instance>(); private readonly Dictionary<string, string> _properties = new Dictionary<string, string>(); @@ -23,7 +21,7 @@ public ConfiguredInstance(InstanceMemento memento, PluginGraph graph, Type pluginType) { - Read(memento, graph, pluginType); + read(memento, graph, pluginType); } public ConfiguredInstance(string name) @@ -37,55 +35,36 @@ _pluggedType = pluggedType; } - public Type PluggedType - { - get { return _pluggedType; } - set { _pluggedType = value; } - } - protected override ConfiguredInstance thisInstance { get { return this; } } - #region IConfiguredInstance Members + #region IStructuredInstance Members - public string ConcreteKey + Instance IStructuredInstance.GetChild(string name) { - get - { - return _concreteKey; - } - set { _concreteKey = value; } + return _children[name]; } - Instance[] IConfiguredInstance.GetChildrenArray(string propertyName) + Instance[] IStructuredInstance.GetChildArray(string name) { - if (_arrays.ContainsKey(propertyName)) - { - return _arrays[propertyName]; - } - - return null; + return _arrays[name]; } - string IConfiguredInstance.GetProperty(string propertyName) + void IStructuredInstance.RemoveKey(string name) { - if (!_properties.ContainsKey(propertyName)) - { - throw new StructureMapException(205, propertyName, Name); - } - - return _properties[propertyName]; + _properties.Remove(name); } - object IConfiguredInstance.GetChild(string propertyName, Type pluginType, IBuildSession buildSession) + #endregion + + protected void setPluggedType(Type pluggedType) { - return getChild(propertyName, pluginType, buildSession); + _pluggedType = pluggedType; } - #endregion protected void mergeIntoThis(ConfiguredInstance instance) { @@ -114,39 +93,11 @@ protected override object build(Type pluginType, IBuildSession session) { InstanceBuilder builder = session.FindBuilderByType(pluginType, _pluggedType) ?? - session.FindBuilderByConcreteKey(pluginType, ConcreteKey); + session.FindBuilderByConcreteKey(pluginType, _concreteKey); - return Build(pluginType, session, builder); + return ((IConfiguredInstance) this).Build(pluginType, session, builder); } - // Only open for testing - public object Build(Type pluginType, IBuildSession session, InstanceBuilder builder) - { - if (builder == null) - { - throw new StructureMapException( - 201, ConcreteKey, Name, pluginType); - } - - - try - { - return builder.BuildInstance(this, session); - } - catch (StructureMapException) - { - throw; - } - catch (InvalidCastException ex) - { - throw new StructureMapException(206, ex, Name); - } - catch (Exception ex) - { - throw new StructureMapException(207, ex, Name, pluginType.FullName); - } - } - protected virtual object getChild(string propertyName, Type pluginType, IBuildSession buildSession) { Instance childInstance = _children.ContainsKey(propertyName) @@ -166,9 +117,9 @@ return family.Plugins.HasPlugin(_concreteKey); } - if (PluggedType != null) + if (_pluggedType != null) { - return TypeRules.CanBeCast(family.PluginType, PluggedType); + return TypeRules.CanBeCast(family.PluginType, _pluggedType); } return false; @@ -185,12 +136,12 @@ return this; } - public void Read(InstanceMemento memento, PluginGraph graph, Type pluginType) + private void read(InstanceMemento memento, PluginGraph graph, Type pluginType) { PluginFamily family = graph.FindFamily(pluginType); Plugin plugin = memento.FindPlugin(family); - PluggedType = plugin.PluggedType; + _pluggedType = plugin.PluggedType; _concreteKey = plugin.ConcreteKey; InstanceMementoPropertyReader reader = new InstanceMementoPropertyReader(this, memento, graph, pluginType); @@ -198,286 +149,17 @@ } - public void SetChild(string name, Instance instance) + private void setChild(string name, Instance instance) { _children.Add(name, instance); } - public Instance GetChild(string name) + private void setChildArray(string name, Instance[] array) { - return _children[name]; - } - - public void SetChildArray(string name, Instance[] array) - { _arrays.Add(name, array); } - public Instance[] GetChildArray(string name) - { - return _arrays[name]; - } - - public ConfiguredInstance WithConcreteKey(string concreteKey) - { - replaceNameIfNotAlreadySet(concreteKey); - _concreteKey = concreteKey; - return this; - } - - public void RemoveKey(string name) - { - _properties.Remove(name); - } - - /// <summary> - /// Start the definition of a primitive argument to a constructor argument - /// </summary> - /// <param name="propertyName"></param> - /// <returns></returns> - public PropertyExpression WithProperty(string propertyName) - { - return new PropertyExpression(this, propertyName); - } - - /// <summary> - /// Starts the definition of a child instance specifying the argument name - /// in the case of a constructor function that consumes more than one argument - /// of type T - /// </summary> - /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> - /// <param name="propertyName"></param> - /// <returns></returns> - public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>(string propertyName) - { - ChildInstanceExpression child = new ChildInstanceExpression(this, propertyName); - child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); - - return child; - } - - /// <summary> - /// Start the definition of a child instance for type CONSTRUCTORARGUMENTTYPE - /// </summary> - /// <typeparam name="CONSTRUCTORARGUMENTTYPE"></typeparam> - /// <returns></returns> - public ChildInstanceExpression Child<CONSTRUCTORARGUMENTTYPE>() - { - string propertyName = findPropertyName<CONSTRUCTORARGUMENTTYPE>(); - - ChildInstanceExpression child = new ChildInstanceExpression(this, propertyName); - child.ChildType = typeof (CONSTRUCTORARGUMENTTYPE); - return child; - } - - private string findPropertyName<T>() - { - Plugin plugin = new Plugin(_pluggedType); - string propertyName = plugin.FindFirstConstructorArgumentOfType<T>(); - - if (string.IsNullOrEmpty(propertyName)) - { - throw new StructureMapException(305, typeof (T)); - } - - return propertyName; - } - - public ChildArrayExpression<PLUGINTYPE> ChildArray<PLUGINTYPE>() - { - validateTypeIsArray<PLUGINTYPE>(); - - string propertyName = findPropertyName<PLUGINTYPE>(); - return ChildArray<PLUGINTYPE>(propertyName); - } - - public ChildArrayExpression<PLUGINTYPE> ChildArray<PLUGINTYPE>(string propertyName) - { - validateTypeIsArray<PLUGINTYPE>(); - - ChildArrayExpression<PLUGINTYPE> expression = - new ChildArrayExpression<PLUGINTYPE>(this, propertyName); - - return expression; - } - - private static void validateTypeIsArray<PLUGINTYPE>() - { - if (!typeof (PLUGINTYPE).IsArray) - { - throw new StructureMapException(307); - } - } - - /// <summary> - /// Use type T for the concrete type of an instance - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - public ConfiguredInstance UsingConcreteType<T>() - { - _pluggedType = typeof (T); - return this; - } - - /// <summary> - /// Use a named Plugin type denoted by a [Pluggable("Key")] attribute - /// </summary> - /// <param name="concreteKey"></param> - /// <returns></returns> - public ConfiguredInstance UsingConcreteTypeNamed(string concreteKey) - { - _concreteKey = concreteKey; - return this; - } - - #region Nested type: ChildArrayExpression - - public class ChildArrayExpression<PLUGINTYPE> - { - private readonly ConfiguredInstance _instance; - private readonly string _propertyName; - private Type _pluginType = typeof (PLUGINTYPE); - - public ChildArrayExpression(ConfiguredInstance instance, string propertyName) - { - _instance = instance; - _propertyName = propertyName; - - _pluginType = typeof (PLUGINTYPE).GetElementType(); - } - - public ConfiguredInstance Contains(params Instance[] instances) - { - _instance.SetChildArray(_propertyName, instances); - - return _instance; - } - } - - #endregion - - #region Nested type: ChildInstanceExpression - - /// <summary> - /// Part of the Fluent Interface, represents a nonprimitive argument to a - /// constructure function - /// </summary> - public class ChildInstanceExpression - { - private readonly ConfiguredInstance _instance; - private readonly string _propertyName; - private Type _childType; - - - public ChildInstanceExpression(ConfiguredInstance instance, string propertyName) - { - _instance = instance; - _propertyName = propertyName; - } - - public ChildInstanceExpression(ConfiguredInstance instance, string propertyName, - Type childType) - : this(instance, propertyName) - { - _childType = childType; - } - - internal Type ChildType - { - set { _childType = value; } - } - - /// <summary> - /// Use a previously configured and named instance for the child - /// </summary> - /// <param name="instanceKey"></param> - /// <returns></returns> - public ConfiguredInstance IsNamedInstance(string instanceKey) - { - ReferencedInstance instance = new ReferencedInstance(instanceKey); - _instance.SetChild(_propertyName, instance); - - return _instance; - } - - /// <summary> - /// Start the definition of a child instance by defining the concrete type - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - public ConfiguredInstance IsConcreteType<T>() - { - Type pluggedType = typeof (T); - ExpressionValidator.ValidatePluggabilityOf(pluggedType).IntoPluginType(_childType); - - ConfiguredInstance childInstance = new ConfiguredInstance(); - childInstance.PluggedType = pluggedType; - _instance.SetChild(_propertyName, childInstance); - - return _instance; - } - - - /// <summary> - /// Registers a configured instance to use as the argument to the parent's - /// constructor - /// </summary> - /// <param name="child"></param> - /// <returns></returns> - public ConfiguredInstance Is(Instance child) - { - _instance.SetChild(_propertyName, child); - return _instance; - } - } - - #endregion - - #region Nested type: PropertyExpression - - /// <summary> - /// Defines the value of a primitive argument to a constructur argument - /// </summary> - public class PropertyExpression - { - private readonly ConfiguredInstance _instance; - private readonly string _propertyName; - - public PropertyExpression(ConfiguredInstance instance, string propertyName) - { - _instance = instance; - _propertyName = propertyName; - } - - /// <summary> - /// Sets the value of the constructor argument - /// </summary> - /// <param name="propertyValue"></param> - /// <returns></returns> - public ConfiguredInstance EqualTo(object propertyValue) - { - _instance.SetProperty(_propertyName, propertyValue.ToString()); - return _instance; - } - - /// <summary> - /// Sets the value of the constructor argument to the key/value in the - /// AppSettings - /// </summary> - /// <param name="appSettingKey"></param> - /// <returns></returns> - public ConfiguredInstance EqualToAppSetting(string appSettingKey) - { - string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; - _instance.SetProperty(_propertyName, propertyValue); - return _instance; - } - } - - #endregion - protected override void preprocess(PluginFamily family) { if (_pluggedType != null) Modified: trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -47,7 +47,7 @@ foreach (KeyValuePair<string, object> arg in _args) { instance.SetProperty(arg.Key, arg.Value.ToString()); - instance.SetChild(arg.Key, new LiteralInstance(arg.Value)); + instance.Child(arg.Key).Is(arg.Value); } } @@ -78,7 +78,7 @@ } else { - PluggedType = typeof (PLUGINTYPE); + setPluggedType(typeof(PLUGINTYPE)); } } Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -6,8 +6,10 @@ { string Name { get; } string ConcreteKey { get; set; } + Type PluggedType { get; } Instance[] GetChildrenArray(string propertyName); string GetProperty(string propertyName); object GetChild(string propertyName, Type pluginType, IBuildSession buildSession); + object Build(Type pluginType, IBuildSession session, InstanceBuilder builder); } } \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/IStructuredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IStructuredInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/IStructuredInstance.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -0,0 +1,9 @@ +namespace StructureMap.Pipeline +{ + public interface IStructuredInstance + { + Instance GetChild(string name); + Instance[] GetChildArray(string name); + void RemoveKey(string name); + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -81,7 +81,7 @@ public virtual object Build(Type pluginType, IBuildSession session) { object rawValue = createRawObject(pluginType, session); - return applyInterception(rawValue); + return applyInterception(rawValue, pluginType); } private object createRawObject(Type pluginType, IBuildSession session) @@ -100,7 +100,7 @@ } } - private object applyInterception(object rawValue) + private object applyInterception(object rawValue, Type pluginType) { try { @@ -109,8 +109,7 @@ } catch (Exception e) { - throw new StructureMapException(308, e, Name, - TypePath.GetAssemblyQualifiedName(rawValue.GetType())); + throw new StructureMapException(270, e, Name, pluginType); } } Modified: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -84,19 +84,13 @@ InstanceMemento child = _memento.GetChildMemento(name); Instance childInstance = child == null ? new DefaultInstance() : child.ReadInstance(_pluginGraph, childType); - _instance.SetChild(name, childInstance); + _instance.Child(name).Is(childInstance); } private void copyChildArray(string name, Type childType) { - InstanceMemento[] mementoes = _memento.GetChildrenArray(name); + InstanceMemento[] mementoes = _memento.GetChildrenArray(name) ?? new InstanceMemento[0]; - // TODO -- want to default to mementoes == null is all - if (mementoes == null) - { - mementoes = new InstanceMemento[0]; - } - Instance[] children = new Instance[mementoes.Length]; for (int i = 0; i < mementoes.Length; i++) { @@ -104,7 +98,7 @@ children[i] = memento.ReadInstance(_pluginGraph, childType); } - _instance.SetChildArray(name, children); + _instance.ChildArray(name, childType).Contains(children); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 17:50:51 UTC (rev 102) @@ -147,6 +147,7 @@ <Compile Include="Pipeline\BuildPolicy.cs" /> <Compile Include="Pipeline\CacheInterceptor.cs" /> <Compile Include="Pipeline\ConfiguredInstance.cs" /> + <Compile Include="Pipeline\ConfiguredInstance.Expressions.cs" /> <Compile Include="Pipeline\ConstructorInstance.cs" /> <Compile Include="Pipeline\DefaultInstance.cs" /> <Compile Include="Pipeline\HttpContextBuildPolicy.cs" /> @@ -372,6 +373,8 @@ <None Include="..\structuremap.snk"> <Link>Properties\structuremap.snk</Link> </None> + <Compile Include="Pipeline\ConfiguredInstance.Building.cs" /> + <Compile Include="Pipeline\IStructuredInstance.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <PropertyGroup> Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -151,6 +151,11 @@ return _registry.BuildInstancesOf<PLUGINTYPE>(); } + public static GenericFamilyExpression ForRequestedType(Type pluginType) + { + return _registry.ForRequestedType(pluginType); + } + /// <summary> /// Adds a new configured instance of Type T /// </summary> Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap/StructureMapException.resx 2008-05-27 17:50:51 UTC (rev 102) @@ -253,4 +253,7 @@ <data name="196" xml:space="preserve"> <value>Could not find referenced instance named {0} of PluginType {1} for Profile {2}</value> </data> + <data name="270" xml:space="preserve"> + <value>Instance specific interception failed for {0} of PluginType {1}</value> + </data> </root> \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -101,13 +101,18 @@ Assert.AreSame(_lastService, interceptedService); } - [Test, - ExpectedException(typeof (StructureMapException), - ExpectedMessage = "StructureMap Exception Code: 308\nA configured instance interceptor has failed for Instance 'Bad' and concrete type 'StructureMap.Testing.Widget3.ColorService,StructureMap.Testing.Widget3'" - )] + [Test] public void TrapFailureInInterceptor() { - _manager.CreateInstance<IService>("Bad"); + try + { + _manager.CreateInstance<IService>("Bad"); + Assert.Fail("Should have thrown an error"); + } + catch (StructureMapException e) + { + Assert.AreEqual(270, e.ErrorCode); + } } } Modified: trunk/Source/StructureMap.Testing/Configuration/DefaultInstanceNodeTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DefaultInstanceNodeTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Configuration/DefaultInstanceNodeTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -13,7 +13,14 @@ [SetUp] public void SetUp() { - _graph = DataMother.GetPluginGraph("DefaultInstance.xml"); + string xml = @" +<StructureMap MementoStyle='Attribute'> + <DefaultInstance PluginType='StructureMap.Testing.Widget.IWidget,StructureMap.Testing.Widget' PluggedType='StructureMap.Testing.Widget.ColorWidget,StructureMap.Testing.Widget' Color='Red' /> + <DefaultInstance PluginType='StructureMap.Testing.Widget.Rule,StructureMap.Testing.Widget' PluggedType='StructureMap.Testing.Widget.ColorRule,StructureMap.Testing.Widget' Color='Blue' Scope='Singleton' Key='TheBlueOne'/> +</StructureMap> +"; + + _graph = DataMother.BuildPluginGraphFromXml(xml); _manager = new InstanceManager(_graph); } Modified: trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -40,41 +40,11 @@ private BuildSession _session; - [Test] - public void BuildRule1() - { - // TODO: Move to ConfiguredInstanceTester - ConfiguredInstance instance = new ConfiguredInstance().WithConcreteKey("Rule1"); - Rule rule = (Rule) instance.Build(typeof(Rule), _session); - Assert.IsNotNull(rule); - Assert.IsTrue(rule is Rule1); - } - [Test, ExpectedException(typeof (StructureMapException))] - public void BuildRuleWithABadValue() - { - // TODO -- move to ConfiguredInstanceTester - - ConfiguredInstance instance = (ConfiguredInstance) ComplexRule.GetInstance(); - instance.SetProperty("Int", "abc"); - ComplexRule rule = (ComplexRule)instance.Build(typeof(Rule), _session); - } - [Test, ExpectedException(typeof (StructureMapException))] - public void BuildRuleWithAMissingValue() - { - // TODO: Move to ConfiguredInstanceTester - - ConfiguredInstance instance = (ConfiguredInstance) ComplexRule.GetInstance(); - instance.RemoveKey("String"); - - ComplexRule rule = (ComplexRule)instance.Build(typeof(Rule), _session); - } - - [Test, ExpectedException(typeof (StructureMapException))] public void GetInstanceWithInvalidInstanceKey() { _manager.CreateInstance<Rule>("NonExistentRule"); @@ -100,18 +70,8 @@ } - [Test] - public void TestComplexRule() - { - // TODO: Move to ConfiguredInstanceTester - ConfiguredInstance instance = (ConfiguredInstance) ComplexRule.GetInstance(); - Rule rule = (Rule) instance.Build(typeof(Rule), _session); - Assert.IsNotNull(rule); - Assert.IsTrue(rule is ComplexRule); - } - } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Graph/PluginFamilyTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -91,7 +91,7 @@ Assert.AreEqual(Plugin.DEFAULT, family.DefaultInstanceKey); Assert.AreEqual(1, family.PluginCount); Assert.AreEqual(1, family.GetAllInstances().Length); - ConfiguredInstance instance = (ConfiguredInstance) family.GetAllInstances()[0]; + IConfiguredInstance instance = (IConfiguredInstance) family.GetAllInstances()[0]; Assert.AreEqual(Plugin.DEFAULT, instance.Name); Assert.AreEqual(GetType(), instance.PluggedType); } Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -157,7 +157,7 @@ Plugin plugin = new Plugin(typeof (DefaultGateway), "Default"); Assert.IsTrue(plugin.CanBeAutoFilled); - ConfiguredInstance instance = (ConfiguredInstance) plugin.CreateImplicitInstance(); + IConfiguredInstance instance = (ConfiguredInstance) plugin.CreateImplicitInstance(); Assert.AreEqual("Default", instance.Name); Assert.AreEqual(typeof(DefaultGateway), instance.PluggedType); Modified: trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -139,5 +139,45 @@ { Plugin plugin = new Plugin(typeof (BadSetterClass)); } + + + + [Test] + public void Log_240_when_a_designated_setter_does_not_exist() + { + string errorXml = @" + <StructureMap> + <PluginFamily Type='StructureMap.Testing.Widget5.IGridColumn' Assembly='StructureMap.Testing.Widget5' DefaultKey=''> + <Plugin Assembly='StructureMap.Testing.Widget5' Type='StructureMap.Testing.Widget5.OtherGridColumn' ConcreteKey='Other'> + <Setter Name='Does not exist' /> + </Plugin> + </PluginFamily> + </StructureMap> + "; + + + PluginGraph graph = DataMother.BuildPluginGraphFromXml(errorXml); + graph.Log.AssertHasError(240); + } + + + [Test] + public void Log_241_when_a_designated_setter_does_not_exist() + { + string errorXml = @" + <StructureMap> + <PluginFamily Type='StructureMap.Testing.Widget5.IGridColumn' Assembly='StructureMap.Testing.Widget5' DefaultKey=''> + <Plugin Assembly='StructureMap.Testing.Widget5' Type='StructureMap.Testing.Widget5.OtherGridColumn' ConcreteKey='Other'> + <Setter Name='ReadOnly' /> + </Plugin> + </PluginFamily> + </StructureMap> + "; + + + PluginGraph graph = DataMother.BuildPluginGraphFromXml(errorXml); + graph.Log.AssertHasError(241); + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -187,7 +187,7 @@ MemoryInstanceMemento.CreateReferencedInstanceMemento("Dodge"), }); - ConfiguredInstance instance = (ConfiguredInstance)memento.ReadInstance(graph, typeof(Rule)); + IStructuredInstance instance = (IStructuredInstance)memento.ReadInstance(graph, typeof(Rule)); Instance[] instances = instance.GetChildArray("cars"); Assert.AreEqual(3, instances.Length); @@ -216,7 +216,7 @@ MemoryInstanceMemento carMemento = MemoryInstanceMemento.CreateReferencedInstanceMemento("GrandPrix"); memento.AddChild("car", carMemento); - ConfiguredInstance instance = (ConfiguredInstance) memento.ReadInstance(graph, typeof (Rule)); + IStructuredInstance instance = (IStructuredInstance)memento.ReadInstance(graph, typeof(Rule)); ReferencedInstance child = (ReferencedInstance) instance.GetChild("car"); Assert.AreEqual("GrandPrix", child.ReferenceKey); @@ -233,7 +233,7 @@ MemoryInstanceMemento memento = ComplexRule.GetMemento(); memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName); - ConfiguredInstance instance = (ConfiguredInstance) memento.ReadInstance(graph, typeof (Rule)); + IStructuredInstance instance = (IStructuredInstance)memento.ReadInstance(graph, typeof(Rule)); Assert.IsInstanceOfType(typeof (DefaultInstance), instance.GetChild("car")); } Modified: trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -44,7 +44,24 @@ } } + [Test] + public void BuildPolicy_should_throw_270_if_interception_fails() + { + try + { + LiteralInstance instance = new LiteralInstance("something") + .OnCreation<object>(delegate(object o){throw new NotImplementedException();}); + BuildPolicy policy = new BuildPolicy(); + policy.Build(new StubBuildSession(), typeof (string), instance); + } + catch (StructureMapException e) + { + Assert.AreEqual(270, e.ErrorCode); + } + } + + [Test] public void Singleton_build_policy() { Modified: trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2008-05-27 15:36:33 UTC (rev 101) +++ trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2008-05-27 17:50:51 UTC (rev 102) @@ -1,9 +1,11 @@ using System; using NUnit.Framework; using Rhino.Mocks; +using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; using StructureMap.Testing.Widget; +using StructureMap.Testing.Widget2; using StructureMap.Testing.Widget3; namespace StructureMap.Testing.Pipeline @@ -16,12 +18,24 @@ [SetUp] public void SetUp() { + PluginGraph graph = new PluginGraph(); + Registry registry = new Registry(graph); + registry.BuildInstancesOf<Rule>(); + registry.ScanAssemblies() + .IncludeAssembly("StructureMap.Testing.Widget") + .IncludeAssembly("StructureMap.Testing.Widget2"); + + registry.Build(); + + PipelineGraph pipelineGraph = new PipelineGraph(graph); + _session = new BuildSession(pipelineGraph, graph.InterceptorLibrary); instance = new ConfiguredInstance(); } #endregion private ConfiguredInstance instance; + private IBuildSession _session; private void assertActionThrowsErrorCode(int errorCode, Action action) @@ -38,29 +52,17 @@ } } - [Test] - public void Create_description_if_has_pluggedType_and_plugged_type_has_arguments() + public void TestComplexRule() { - ConfiguredInstance instance = new ConfiguredInstance(typeof(ColorService)); - TestUtility.AssertDescriptionIs(instance, "Configured " + TypePath.GetAssemblyQualifiedName(typeof(ColorService))); - } + ConfiguredInstance instance = (ConfiguredInstance)ComplexRule.GetInstance(); - [Test] - public void Create_description_if_has_only_concrete_key() - { - ConfiguredInstance instance = new ConfiguredInstance().WithConcreteKey("Concrete"); - TestUtility.AssertDescriptionIs(instance, "Configured 'Concrete'"); + Rule rule = (Rule)instance.Build(typeof(Rule), _session); + Assert.IsNotNull(rule); + Assert.IsTrue(rule is ComplexRule); } - [Test] - public void Create_description_if_has_plugged_type_and_plugged_type_has_no_arguments() - { - ConfiguredInstance instance = new ConfiguredInstance(GetType()); - TestUtility.AssertDescriptionIs(instance, TypePath.GetAssemblyQualifiedName(GetType())); - } - [Test] public void AttachDependencies_should_find_the_InstanceBuilder_by_ConcreteKey_if_PluggedType_does_not_exists() { @@ -104,29 +106,123 @@ using (mocks.Playback()) { - object actualObject = instance.Build(GetType(), session, builder); + object actualObject = ((IConfiguredInstance)instance).Build(GetType(), session, builder); Assert.AreSame(theObjectBuilt, actualObject); } } [Test] + public void BuildRule1() + { + ConfiguredInstance instance = new ConfiguredInstance().WithConcreteKey("Rule1"); + + Rule rule = (Rule) instance.Build(typeof (Rule), _session); + Assert.IsNotNull(rule); + Assert.IsTrue(rule is Rule1); + } + + + [Test, ExpectedException(typeof (StructureMapException))] + public void BuildRuleWithABadValue() + { + ConfiguredInstance instance = (ConfiguredInstance) ComplexRule.GetInstance(); + + instance.SetProperty("Int", "abc"); + ComplexRule rule = (ComplexRule) instance.Build(typeof (Rule), _session); + } + + [Test, ExpectedException(typeof (StructureMapException))] + public void BuildRuleWithAMissingValue() + { + IStructuredInstance instance = (IStructuredInstance)ComplexRule.GetInstance(); + instance.RemoveKey("String"); + + ComplexRule rule = (ComplexRule) ((Instance)instance).Build(typeof (Rule), _session); + } + + [Test] + public void Can_be_plugged_in_by_concrete_key() + { + ConfiguredInstance instance = new ConfiguredInstance().WithConcreteKey("Color"); + PluginFamily family = new PluginFamily(typeof (IWidget)); + family.AddPlugin(typeof (ColorWidget), "Color"); + + IDiagnosticInstance diagnosticInstance = instance; + Assert.IsTrue(diagnosticInstance.CanBePartOfPluginFamily(family)); + } + + [Test] + public void Can_be_plugged_in_if_there_is_a_plugged_type_and_the_plugged_type_can_be_cast_to_the_plugintype() + { + ConfiguredInstance instance = new ConfiguredInstance().UsingConcreteType<ColorWidget>(); + PluginFamily family = new PluginFamily(typeof (IWidget)); + + IDiagnosticInstance diagnosticInstance = instance; + Assert.IsTrue(diagnosticInstance.CanBePartOfPluginFamily(family)); + } + + [Test] + public void Can_NOT_be_plugged_in_if_no_plugged_type_and_concrete_key_cannot_be_found_in_family() + { + ConfiguredInstance instance = new ConfiguredInstance().WithConcreteKey("SomethingThatDoesNotExist"); + PluginFamily family = new PluginFamily(typeof (IWidget)); + + IDiagnosticInstance diagnosticInstance = instance; + Assert... [truncated message content] |