From: <jer...@us...> - 2008-07-27 14:01:52
|
Revision: 128 http://structuremap.svn.sourceforge.net/structuremap/?rev=128&view=rev Author: jeremydmiller Date: 2008-07-27 14:01:47 +0000 (Sun, 27 Jul 2008) Log Message: ----------- refactoring to get ready for "Optional" setter injection Modified Paths: -------------- trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs trunk/Source/StructureMap/Graph/IArgumentVisitor.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/Graph/SetterInjectionEmittingTester.cs trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs Added Paths: ----------- trunk/Source/StructureMap/ReflectionHelper.cs Modified: trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Emitting/ArgumentEmitter.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -22,28 +22,38 @@ #region IArgumentVisitor Members - public void PrimitiveSetter(PropertyInfo property) + public void PrimitiveSetter(PropertyInfo property, bool isMandatory) { + if (!isMandatory) return; + _primitive.Setter(ilgen, property); } - public void StringSetter(PropertyInfo property) + public void StringSetter(PropertyInfo property, bool isMandatory) { + if (!isMandatory) return; + _string.Setter(ilgen, property); } - public void EnumSetter(PropertyInfo property) + public void EnumSetter(PropertyInfo property, bool isMandatory) { + if (!isMandatory) return; + _enum.Setter(ilgen, property); } - public void ChildSetter(PropertyInfo property) + public void ChildSetter(PropertyInfo property, bool isMandatory) { + if (!isMandatory) return; + _child.Setter(ilgen, property); } - public void ChildArraySetter(PropertyInfo property) + public void ChildArraySetter(PropertyInfo property, bool isMandatory) { + if (!isMandatory) return; + _childArray.Setter(ilgen, property); } Modified: trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Emitting/Parameters/PrimitiveParameterEmitter.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -21,7 +21,7 @@ { BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public; MethodInfo parseMethod = - argumentType.GetMethod("Parse", bindingAttr, null, new Type[] {typeof (string)}, null); + argumentType.GetMethod("Parse", bindingAttr, null, new [] {typeof (string)}, null); ilgen.Emit(OpCodes.Call, parseMethod); } Modified: trunk/Source/StructureMap/Graph/IArgumentVisitor.cs =================================================================== --- trunk/Source/StructureMap/Graph/IArgumentVisitor.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Graph/IArgumentVisitor.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -4,11 +4,11 @@ { public interface IArgumentVisitor { - void PrimitiveSetter(PropertyInfo property); - void StringSetter(PropertyInfo property); - void EnumSetter(PropertyInfo property); - void ChildSetter(PropertyInfo property); - void ChildArraySetter(PropertyInfo property); + void PrimitiveSetter(PropertyInfo property, bool isMandatory); + void StringSetter(PropertyInfo property, bool isMandatory); + void EnumSetter(PropertyInfo property, bool isMandatory); + void ChildSetter(PropertyInfo property, bool isMandatory); + void ChildArraySetter(PropertyInfo property, bool isMandatory); void PrimitiveParameter(ParameterInfo parameter); void StringParameter(ParameterInfo parameter); Modified: trunk/Source/StructureMap/Graph/SetterProperty.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterProperty.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Graph/SetterProperty.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -1,5 +1,6 @@ using System; using System.Reflection; +using StructureMap.Attributes; namespace StructureMap.Graph { @@ -10,9 +11,13 @@ { private readonly PropertyInfo _property; + public SetterProperty(PropertyInfo property) : base() { _property = property; + var att = Attribute.GetCustomAttribute(property, typeof (SetterPropertyAttribute)); + + IsMandatory = att != null; } public PropertyInfo Property @@ -25,6 +30,8 @@ get { return _property.Name; } } + public bool IsMandatory { get; set; } + public bool CanBeAutoFilled { get { return IsAutoFillable(_property.PropertyType); } @@ -34,11 +41,11 @@ { Type propertyType = _property.PropertyType; - if (IsPrimitive(propertyType)) visitor.PrimitiveSetter(_property); - if (IsChild(propertyType)) visitor.ChildSetter(_property); - if (IsChildArray(propertyType)) visitor.ChildArraySetter(_property); - if (IsEnum(propertyType)) visitor.EnumSetter(_property); - if (IsString(propertyType)) visitor.StringSetter(_property); + if (IsPrimitive(propertyType)) visitor.PrimitiveSetter(_property, IsMandatory); + if (IsChild(propertyType)) visitor.ChildSetter(_property, IsMandatory); + if (IsChildArray(propertyType)) visitor.ChildArraySetter(_property, IsMandatory); + if (IsEnum(propertyType)) visitor.EnumSetter(_property, IsMandatory); + if (IsString(propertyType)) visitor.StringSetter(_property, IsMandatory); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using System.Reflection; @@ -11,42 +12,40 @@ public class SetterPropertyCollection : IEnumerable<SetterProperty> { private readonly Plugin _plugin; - private Dictionary<string, SetterProperty> _properties; + private List<SetterProperty> _properties; public SetterPropertyCollection(Plugin plugin) { - _properties = new Dictionary<string, SetterProperty>(); + _properties = new List<SetterProperty>(); _plugin = plugin; - PropertyInfo[] properties = SetterPropertyAttribute.FindMarkedProperties(plugin.PluggedType); - foreach (PropertyInfo property in properties) + + foreach (PropertyInfo property in plugin.PluggedType.GetProperties()) { - addSetterProperty(property, property.Name); + if (property.CanWrite) + { + SetterProperty setter = new SetterProperty(property); + _properties.Add(setter); + } } } - public SetterProperty[] Setters + public int MandatoryCount { - get - { - SetterProperty[] returnValue = new SetterProperty[_properties.Count]; - _properties.Values.CopyTo(returnValue, 0); - - return returnValue; - } + get { return _properties.FindAll(p => p.IsMandatory).Count; } } - public int Count + public int OptionalCount { - get { return _properties.Count; } + get { return _properties.FindAll(p => !p.IsMandatory).Count; } } #region IEnumerable<SetterProperty> Members IEnumerator<SetterProperty> IEnumerable<SetterProperty>.GetEnumerator() { - return _properties.Values.GetEnumerator(); + return _properties.GetEnumerator(); } public IEnumerator GetEnumerator() @@ -58,38 +57,35 @@ public SetterProperty Add(string propertyName) { - PropertyInfo property = _plugin.PluggedType.GetProperty(propertyName); - addSetterProperty(property, propertyName); + var setter = _properties.Find(p => p.Property.Name == propertyName); + if (setter == null) + { + throw new StructureMapException(240, propertyName, _plugin.PluggedType); + } - return _properties[propertyName]; + + setter.IsMandatory = true; + + return setter; } - private void addSetterProperty(PropertyInfo property, string propertyName) + + public bool IsMandatory(string propertyName) { + SetterProperty property = _properties.Find(p => p.Name == propertyName); if (property == null) { - throw new StructureMapException(240, propertyName, _plugin.PluggedType); + return false; } - if (property.GetSetMethod() == null) - { - throw new StructureMapException(241, propertyName, _plugin.PluggedType); - } - - SetterProperty setterProperty = new SetterProperty(property); - _properties.Add(propertyName, setterProperty); + return property.IsMandatory; } - public bool Contains(string propertyName) - { - return _properties.ContainsKey(propertyName); - } - public void Merge(SetterPropertyCollection setters) { foreach (SetterProperty setter in setters) { - if (!Contains(setter.Name)) + if (!IsMandatory(setter.Name)) { Add(setter.Name); } @@ -110,7 +106,10 @@ foreach (SetterProperty setterProperty in this) { - returnValue = returnValue && setterProperty.CanBeAutoFilled; + if (setterProperty.IsMandatory) + { + returnValue = returnValue && setterProperty.CanBeAutoFilled; + } } return returnValue; Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/InstanceMemento.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -121,17 +121,14 @@ try { returnValue = getPropertyValue(Key); + } catch (Exception ex) { throw new StructureMapException(205, ex, Key, InstanceKey); } - if (returnValue == string.Empty || returnValue == null) - { - throw new StructureMapException(205, Key, InstanceKey); - } - + if (string.IsNullOrEmpty(returnValue)) return null; if (returnValue.ToUpper() == EMPTY_STRING) { returnValue = string.Empty; Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -187,5 +187,13 @@ return typeName; } } + + public void ForProperty(string propertyName, Action<string> action) + { + if (_properties.ContainsKey(propertyName)) + { + action(_properties[propertyName]); + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -11,5 +11,6 @@ string GetProperty(string propertyName); object GetChild(string propertyName, Type pluginType, IBuildSession buildSession); object Build(Type pluginType, IBuildSession session, InstanceBuilder builder); + void ForProperty(string propertyName, Action<string> action); } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -22,49 +22,49 @@ #region IArgumentVisitor Members - public void PrimitiveSetter(PropertyInfo property) + public void PrimitiveSetter(PropertyInfo property, bool isMandatory) { - copyPrimitive(property.Name); + copyPrimitive(property.Name, isMandatory); } - public void StringSetter(PropertyInfo property) + public void StringSetter(PropertyInfo property, bool isMandatory) { - copyPrimitive(property.Name); + copyPrimitive(property.Name, isMandatory); } - public void EnumSetter(PropertyInfo property) + public void EnumSetter(PropertyInfo property, bool isMandatory) { - copyPrimitive(property.Name); + copyPrimitive(property.Name, isMandatory); } - public void ChildSetter(PropertyInfo property) + public void ChildSetter(PropertyInfo property, bool isMandatory) { - copyChild(property.Name, property.PropertyType); + copyChild(property.Name, property.PropertyType, isMandatory); } - public void ChildArraySetter(PropertyInfo property) + public void ChildArraySetter(PropertyInfo property, bool isMandatory) { copyChildArray(property.Name, property.PropertyType.GetElementType()); } public void PrimitiveParameter(ParameterInfo parameter) { - copyPrimitive(parameter.Name); + copyPrimitive(parameter.Name, true); } public void StringParameter(ParameterInfo parameter) { - copyPrimitive(parameter.Name); + copyPrimitive(parameter.Name, true); } public void EnumParameter(ParameterInfo parameter) { - copyPrimitive(parameter.Name); + copyPrimitive(parameter.Name, true); } public void ChildParameter(ParameterInfo parameter) { - copyChild(parameter.Name, parameter.ParameterType); + copyChild(parameter.Name, parameter.ParameterType, true); } public void ChildArrayParameter(ParameterInfo parameter) @@ -74,16 +74,20 @@ #endregion - private void copyPrimitive(string name) + private void copyPrimitive(string name, bool isMandatory) { - _instance.SetProperty(name, _memento.GetProperty(name)); + string propertyValue = _memento.GetProperty(name); + + if (propertyValue == null && isMandatory) + { + throw new StructureMapException(205, name, _memento.InstanceKey); + } + + _instance.SetProperty(name, propertyValue); } - private void copyChild(string name, Type childType) + private void copyChild(string name, Type childType, bool isMandatory) { - //InstanceMemento child = _memento.GetChildMemento(name); - //Instance childInstance = child == null ? new DefaultInstance() : child.ReadInstance(_pluginGraph, childType); - Instance childInstance = _memento.ReadChildInstance(name, _pluginGraph, childType) ?? new DefaultInstance(); _instance.Child(name).Is(childInstance); Added: trunk/Source/StructureMap/ReflectionHelper.cs =================================================================== --- trunk/Source/StructureMap/ReflectionHelper.cs (rev 0) +++ trunk/Source/StructureMap/ReflectionHelper.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; + +namespace StructureMap +{ + public static class ReflectionHelper + { + public static PropertyInfo GetProperty<MODEL>(Expression<Func<MODEL, object>> expression) + { + MemberExpression memberExpression = getMemberExpression(expression); + return (PropertyInfo)memberExpression.Member; + } + + public static PropertyInfo GetProperty<MODEL, T>(Expression<Func<MODEL, T>> expression) + { + MemberExpression memberExpression = getMemberExpression(expression); + return (PropertyInfo)memberExpression.Member; + } + + private static MemberExpression getMemberExpression<MODEL, T>(Expression<Func<MODEL, T>> expression) + { + MemberExpression memberExpression = null; + if (expression.Body.NodeType == ExpressionType.Convert) + { + var body = (UnaryExpression)expression.Body; + memberExpression = body.Operand as MemberExpression; + } + else if (expression.Body.NodeType == ExpressionType.MemberAccess) + { + memberExpression = expression.Body as MemberExpression; + } + + + + if (memberExpression == null) throw new ArgumentException("Not a member access", "member"); + return memberExpression; + } + + public static MethodInfo GetMethod<T>(Expression<Func<T, object>> expression) + { + MethodCallExpression methodCall = (MethodCallExpression)expression.Body; + return methodCall.Method; + } + + public static MethodInfo GetMethod<T, U>(Expression<Func<T, U>> expression) + { + MethodCallExpression methodCall = (MethodCallExpression)expression.Body; + return methodCall.Method; + } + + public static MethodInfo GetMethod<T, U, V>(Expression<Func<T, U, V>> expression) + { + MethodCallExpression methodCall = (MethodCallExpression)expression.Body; + return methodCall.Method; + } + + } +} Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-07-27 14:01:47 UTC (rev 128) @@ -409,6 +409,7 @@ <Compile Include="Pipeline\ConfiguredInstance.Building.cs" /> <Compile Include="Pipeline\IStructuredInstance.cs" /> <Compile Include="Pipeline\SerializedInstance.cs" /> + <Compile Include="ReflectionHelper.cs" /> <Compile Include="Util\Cache.cs" /> </ItemGroup> <ItemGroup> Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap/StructureMapException.resx 2008-07-27 14:01:47 UTC (rev 128) @@ -173,11 +173,8 @@ <value>Cannot call "FillDependencies" on type {0}. Check that the type is concrete and has no primitive arguments</value> </data> <data name="240" xml:space="preserve"> - <value>Property {0} does not exist on the PluggedType {1}</value> + <value>Property {0} does not exist on the PluggedType {1} or does not have a public setter</value> </data> - <data name="241" xml:space="preserve"> - <value>Property {0} on PluggedType {1} is not writeable!</value> - </data> <data name="150" xml:space="preserve"> <value>The file {0} referenced in an <Include> node cannot be opened</value> </data> Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -111,6 +111,38 @@ Assert.IsInstanceOfType(typeof (Target2), manager.GetInstance<ITarget>()); } + [Test, Explicit] + public void Add_default_instance2() + { + Container manager = + new Container(r => r.ForRequestedType(typeof (IRepository<>)).TheDefaultIsConcreteType(typeof (OnlineRepository<>))); + + Assert.IsInstanceOfType(typeof(Target2), manager.GetInstance<ITarget>()); + + + IRepository<Invoice> repository = + ObjectFactory.GetInstance<IRepository<Invoice>>(); + } + + public interface IRepository<T> + { + void Save(T subject); + } + + public class OnlineRepository<T> : IRepository<T>{ + public void Save(T subject) + { + throw new NotImplementedException(); + } + } + public class OfflineRepository<T> : IRepository<T>{ + public void Save(T subject) + { + throw new NotImplementedException(); + } + } + public class Invoice{} + [Test] public void Add_instance_by_lambda() { Modified: trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Graph/GenericsPluginGraphTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -27,7 +27,19 @@ Assert.IsFalse(GenericsPluginGraph.CanBeCast(pluginType, pluggedType)); } + [Test] + public void Check_the_generic_plugin_family_expression() + { + Container container = new Container(r => + { + r.ForRequestedType(typeof (IGenericService<>)).TheDefaultIsConcreteType(typeof (GenericService<>)); + }); + + container.GetInstance<IGenericService<string>>().ShouldBeOfType(typeof(GenericService<string>)); + } + + [Test] public void BuildAnInstanceManagerFromTemplatedPluginFamily() { PluginGraph pluginGraph = new PluginGraph(); Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -300,11 +300,11 @@ visitor.StringParameter(param("name")); visitor.PrimitiveParameter(param("age")); visitor.EnumParameter(param("breed")); - visitor.StringSetter(prop("LastName")); - visitor.PrimitiveSetter(prop("Income")); - visitor.ChildSetter(prop("Car")); - visitor.ChildArraySetter(prop("Fleet")); - visitor.EnumSetter(prop("OtherBreed")); + visitor.StringSetter(prop("LastName"), true); + visitor.PrimitiveSetter(prop("Income"), true); + visitor.ChildSetter(prop("Car"), true); + visitor.ChildArraySetter(prop("Fleet"), true); + visitor.EnumSetter(prop("OtherBreed"), true); } using (mocks.Playback()) Modified: trunk/Source/StructureMap.Testing/Graph/SetterInjectionEmittingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SetterInjectionEmittingTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Graph/SetterInjectionEmittingTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -23,8 +23,48 @@ private Container buildInstanceManager() { - PluginGraph pluginGraph = DataMother.GetDiagnosticPluginGraph("SetterInjectionTesting.xml"); + PluginGraph pluginGraph = DataMother.BuildPluginGraphFromXml(@" +<StructureMap> + <Assembly Name='StructureMap.Testing.Widget'/> + <Assembly Name='StructureMap.Testing.Widget5'/> + + <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey='Money'> + <Source Type='XmlFile' FilePath='FullTesting.XML' XPath='Widgets' NodeName='Widget'/> + <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotPluggableWidget' ConcreteKey='NotPluggable'/> + </PluginFamily> + <PluginFamily Type='StructureMap.Testing.Widget.Rule' Assembly='StructureMap.Testing.Widget' DefaultKey='Blue'> + <Instance Key='Bigger' Type='GreaterThan'> + <Property Name='Attribute' Value='MyDad' /> + <Property Name='Value' Value='10' /> + </Instance> + <Instance Key='Blue' Type='Color'> + <Property Name='Color' Value='Blue' /> + </Instance> + <Instance Key='Red' Type='Color'> + <Property Name='Color' Value='Red' /> + </Instance> + </PluginFamily> + + <PluginFamily Type='StructureMap.Testing.Widget5.IGridColumn' Assembly='StructureMap.Testing.Widget5' DefaultKey=''> + <Source Type='XmlFile' FilePath='GridColumnInstances.XML' XPath='//GridColumns' NodeName='GridColumn'/> + <Plugin Assembly='StructureMap.Testing.Widget5' Type='StructureMap.Testing.Widget5.OtherGridColumn' ConcreteKey='Other'> + <Setter Name='ColumnName' /> + <Setter Name='FontStyle' /> + <Setter Name='Rules' /> + <Setter Name='Widget' /> + <Setter Name='WrapLines' /> + </Plugin> + </PluginFamily> + + <Instances/> +</StructureMap> + + +"); + + + return new Container(pluginGraph); } Modified: trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Graph/SetterInjectionTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -78,26 +78,73 @@ } [Test] - public void CanFindMarkedProperties() + public void got_the_right_number_of_mandatory_and_optional_properties() { - PropertyInfo[] properties = SetterPropertyAttribute.FindMarkedProperties(typeof (BasicGridColumn)); - Assert.AreEqual(5, properties.Length); + Plugin plugin = new Plugin(typeof(SetterTarget)); + plugin.Setters.IsMandatory("Name1").ShouldBeFalse(); + plugin.Setters.IsMandatory("Name2").ShouldBeTrue(); + plugin.Setters.IsMandatory("Name3").ShouldBeFalse(); + plugin.Setters.IsMandatory("Name4").ShouldBeTrue(); } + [Test] + public void SetterProperty_picks_up_IsMandatory_from_setter_attribute() + { + SetterProperty setter1 = new SetterProperty(ReflectionHelper.GetProperty<SetterTarget>(x => x.Name1)); + setter1.IsMandatory.ShouldBeFalse(); + SetterProperty setter2 = new SetterProperty(ReflectionHelper.GetProperty<SetterTarget>(x => x.Name2)); + setter2.IsMandatory.ShouldBeTrue(); + } + + + public class SetterTarget + { + public string Name1 { get; set; } + + [SetterProperty] + public string Name2 { get; set; } + + public string Name3 { get; set; } + + [SetterProperty] + public string Name4 { get; set; } + } + [Test] + public void SetterPropertyCollection_builds_the_correct_number_of_properties() + { + Plugin plugin = new Plugin(typeof(OtherGridColumn)); + plugin.Setters.OptionalCount.ShouldEqual(7); + + plugin.Setters.Add("Widget"); + plugin.Setters.Add("FontStyle"); + plugin.Setters.Add("ColumnName"); + plugin.Setters.Add("Rules"); + plugin.Setters.Add("WrapLines"); + + Assert.AreEqual(2, plugin.Setters.OptionalCount); + Assert.AreEqual(5, plugin.Setters.MandatoryCount); + } + + + [Test] public void CreateSetterPropertyCollectionFromExplicitPlugin() { PluginGraph pluginGraph = getPluginGraph(); PluginFamily family = pluginGraph.FindFamily(typeof (IGridColumn)); Plugin plugin = family.Plugins["Other"]; - Assert.AreEqual(5, plugin.Setters.Count); - Assert.IsTrue(plugin.Setters.Contains("Widget")); - Assert.IsTrue(plugin.Setters.Contains("FontStyle")); - Assert.IsTrue(plugin.Setters.Contains("ColumnName")); - Assert.IsTrue(plugin.Setters.Contains("Rules")); - Assert.IsTrue(plugin.Setters.Contains("WrapLines")); + Assert.AreEqual(2, plugin.Setters.OptionalCount); + Assert.AreEqual(5, plugin.Setters.MandatoryCount); + + + + Assert.IsTrue(plugin.Setters.IsMandatory("Widget")); + Assert.IsTrue(plugin.Setters.IsMandatory("FontStyle")); + Assert.IsTrue(plugin.Setters.IsMandatory("ColumnName")); + Assert.IsTrue(plugin.Setters.IsMandatory("Rules")); + Assert.IsTrue(plugin.Setters.IsMandatory("WrapLines")); } [Test] @@ -112,13 +159,12 @@ */ Plugin plugin = new Plugin(typeof (BasicGridColumn)); - - Assert.AreEqual(5, plugin.Setters.Count); - Assert.IsTrue(plugin.Setters.Contains("Widget")); - Assert.IsTrue(plugin.Setters.Contains("FontStyle")); - Assert.IsTrue(plugin.Setters.Contains("ColumnName")); - Assert.IsTrue(plugin.Setters.Contains("Rules")); - Assert.IsTrue(plugin.Setters.Contains("WrapLines")); + Assert.AreEqual(5, plugin.Setters.MandatoryCount); + Assert.IsTrue(plugin.Setters.IsMandatory("Widget")); + Assert.IsTrue(plugin.Setters.IsMandatory("FontStyle")); + Assert.IsTrue(plugin.Setters.IsMandatory("ColumnName")); + Assert.IsTrue(plugin.Setters.IsMandatory("Rules")); + Assert.IsTrue(plugin.Setters.IsMandatory("WrapLines")); } [Test] @@ -157,7 +203,7 @@ PluginGraph graph = DataMother.BuildPluginGraphFromXml(errorXml); - graph.Log.AssertHasError(241); + graph.Log.AssertHasError(240); } [Test, ExpectedException(typeof (StructureMapException))] @@ -174,10 +220,5 @@ plugin.Setters.Add("HeaderText"); } - [Test, ExpectedException(typeof (StructureMapException))] - public void TryToCreateAnImplicitPluginWithASetterPropertyThatDoesNotHaveASetMethod() - { - Plugin plugin = new Plugin(typeof (BadSetterClass)); - } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -319,5 +319,23 @@ instance.Build(GetType(), null, null); }); } + + [Test] + public void ForProperty_hit_calls_action() + { + ConfiguredInstance instance = new ConfiguredInstance().WithProperty("age").EqualTo("34"); + string theAge = null; + + instance.ForProperty("age", s => theAge = s); + theAge.ShouldEqual("34"); + } + + [Test] + public void ForProperty_miss_does_not_call_action() + { + ConfiguredInstance instance = new ConfiguredInstance().WithProperty("age").EqualTo("34"); + + instance.ForProperty("NotAge", s => Assert.Fail("Should not be called")); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs =================================================================== --- trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs 2008-07-19 23:43:04 UTC (rev 127) +++ trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs 2008-07-27 14:01:47 UTC (rev 128) @@ -50,4 +50,29 @@ instance.GetProperty("Name")); } } + + public class SetterBuilder : InstanceBuilder + { + public override Type PluggedType + { + get { throw new System.NotImplementedException(); } + } + + public override object BuildInstance(IConfiguredInstance instance, IBuildSession session) + { + SetterTarget target = new SetterTarget(); + instance.ForProperty("Name", x => target.Name = x); + instance.ForProperty("Age", x => target.Age = (int)Convert.ChangeType(x, typeof (int))); + instance.ForProperty("Breed", x => target.Breed = (BreedEnum) Enum.Parse(typeof (BreedEnum), x)); + + return target; + } + } + + public class SetterTarget + { + public string Name { get; set; } + public int Age { get; set; } + public BreedEnum Breed { get; set; } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |