From: <jer...@us...> - 2009-12-26 05:36:13
|
Revision: 293 http://structuremap.svn.sourceforge.net/structuremap/?rev=293&view=rev Author: jeremydmiller Date: 2009-12-26 05:36:06 +0000 (Sat, 26 Dec 2009) Log Message: ----------- The big, big, big switch from IL generation to dynamic Func's. Ding dong, the witch is dead. Modified Paths: -------------- trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/DefaultInstance.cs trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/Pipeline/ListCoercion.cs trunk/Source/StructureMap/Pipeline/ObjectInstance.cs trunk/Source/StructureMap/Pipeline/Profile.cs trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap/Pipeline/ReferencedInstance.cs trunk/Source/StructureMap/Pipeline/SmartInstance.cs Added Paths: ----------- trunk/Source/StructureMap/Pipeline/Arguments.cs Added: trunk/Source/StructureMap/Pipeline/Arguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Arguments.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/Arguments.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -0,0 +1,26 @@ +using StructureMap.Construction; + +namespace StructureMap.Pipeline +{ + public class Arguments : IArguments + { + private readonly IConfiguredInstance _instance; + private readonly BuildSession _session; + + public Arguments(IConfiguredInstance instance, BuildSession session) + { + _instance = instance; + _session = session; + } + + public T Get<T>(string propertyName) + { + return _instance.Get<T>(propertyName, _session); + } + + public bool Has(string propertyName) + { + return _instance.HasProperty(propertyName, _session); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ArrayCoercion.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -9,5 +10,7 @@ { return enumerable.Select(x => x as T).ToArray(); } + + public Type ElementType { get { return typeof (T); } } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -78,8 +78,6 @@ /// <returns></returns> public ChildArrayExpression ChildArray<PLUGINTYPE>(string propertyName) { - validateTypeIsArray<PLUGINTYPE>(); - var expression = new ChildArrayExpression(this, propertyName); @@ -98,8 +96,6 @@ public ChildArrayExpression ChildArray(Type pluginType) { - //TODO: add validation check - validateTypeIsArray(pluginType); var propertyName = findPropertyName(pluginType); return ChildArray(propertyName); } @@ -231,7 +227,7 @@ /// <returns></returns> public ConfiguredInstance Contains(params Instance[] instances) { - _instance.setChildArray(_propertyName, instances); + _instance.SetCollection(_propertyName, instances); return _instance; } @@ -278,7 +274,7 @@ public ConfiguredInstance IsNamedInstance(string instanceKey) { var instance = new ReferencedInstance(instanceKey); - _instance.setChild(_propertyName, instance); + _instance.SetChild(_propertyName, instance); return _instance; } @@ -303,7 +299,7 @@ ExpressionValidator.ValidatePluggabilityOf(pluggedType).IntoPluginType(_childType); var childInstance = new ConfiguredInstance(pluggedType); - _instance.setChild(_propertyName, childInstance); + _instance.SetChild(_propertyName, childInstance); return _instance; } @@ -316,7 +312,7 @@ /// <returns></returns> public ConfiguredInstance Is(Instance child) { - _instance.setChild(_propertyName, child); + _instance.SetChild(_propertyName, child); return _instance; } Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -9,69 +9,31 @@ /// and filling setter properties. ConfiguredInstance should only be used for open generic types. /// Favor <see cref="SmartInstance{T}">SmartInstance{T}</see> for all other usages. /// </summary> - public partial class ConfiguredInstance : ConfiguredInstanceBase<ConfiguredInstance> + public partial class ConfiguredInstance : ConstructorInstance { - public ConfiguredInstance(InstanceMemento memento, PluginGraph graph, Type pluginType) - : base(memento, graph, pluginType) + public ConfiguredInstance(InstanceMemento memento, PluginGraph graph, Type pluginType) : base(memento.FindPlugin(graph.FindFamily(pluginType))) { + read(memento, graph, pluginType); } - public ConfiguredInstance(Type pluggedType, string name) : base(pluggedType, name) + private void read(InstanceMemento memento, PluginGraph graph, Type pluginType) { + var reader = new InstanceMementoPropertyReader(this, memento, graph, pluginType); + plugin.VisitArguments(reader); } - - public ConfiguredInstance(Type pluggedType) : base(pluggedType) + public ConfiguredInstance(Type pluggedType, string name) : base(pluggedType, name) { } - public ConfiguredInstance(Type templateType, params Type[] types) : base(GetGenericType(templateType, types)) - { - } - public static Type GetGenericType(Type templateType, params Type[] types) + public ConfiguredInstance(Type pluggedType) : base(pluggedType) { - return templateType.MakeGenericType(types); - } - protected void setPluggedType(Type pluggedType) - { - _pluggedType = pluggedType; } - - protected override void preprocess(PluginFamily family) + public ConfiguredInstance(Type templateType, params Type[] types) : base(templateType.MakeGenericType(types)) { } - - protected override string getDescription() - { - string typeName = _pluggedType.AssemblyQualifiedName; - var ctor = new Constructor(_pluggedType); - if (ctor.HasArguments()) - { - return "Configured " + typeName; - } - else - { - return typeName; - } - } - - protected override void addTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) - { - Type specificType = _pluggedType.IsGenericTypeDefinition - ? _pluggedType.MakeGenericType(templateTypes) - : _pluggedType; - if (specificType.CanBeCastTo(family.PluginType)) - { - var instance = new ConfiguredInstance(specificType); - instance._arrays = _arrays; - instance._children = _children; - instance._properties = _properties; - instance.Name = Name; - family.AddInstance(instance); - } - } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstanceBase.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -8,291 +8,293 @@ public interface BasicInstance { Type PluggedType { get; } + + Dictionary<string, string> Properties { get; } Dictionary<string, Instance> Children { get; } Dictionary<string, Instance[]> Arrays { get; } } - public abstract class ConfiguredInstanceBase<T> : Instance, IConfiguredInstance, IStructuredInstance, BasicInstance - { - protected Dictionary<string, Instance[]> _arrays = new Dictionary<string, Instance[]>(); - protected Dictionary<string, Instance> _children = new Dictionary<string, Instance>(); - protected Type _pluggedType; - protected Dictionary<string, string> _properties = new Dictionary<string, string>(); + //public abstract class ConfiguredInstanceBase<T> : Instance, IConfiguredInstance, IStructuredInstance, BasicInstance + //{ + // protected Dictionary<string, Instance[]> _arrays = new Dictionary<string, Instance[]>(); + // protected Dictionary<string, Instance> _children = new Dictionary<string, Instance>(); + // protected Type _pluggedType; + // protected Dictionary<string, string> _properties = new Dictionary<string, string>(); - protected ConfiguredInstanceBase(InstanceMemento memento, PluginGraph graph, Type pluginType) - { - read(memento, graph, pluginType); - } + // protected ConfiguredInstanceBase(InstanceMemento memento, PluginGraph graph, Type pluginType) + // { + // read(memento, graph, pluginType); + // } - protected ConfiguredInstanceBase(Type pluggedType, string name) - { - _pluggedType = pluggedType; - Name = name; - } + // protected ConfiguredInstanceBase(Type pluggedType, string name) + // { + // _pluggedType = pluggedType; + // Name = name; + // } - protected ConfiguredInstanceBase(Type pluggedType) : this(pluggedType, Guid.NewGuid().ToString()) - { - if (pluggedType != null) - if (pluggedType.IsAbstract || pluggedType.IsInterface) - { - throw new StructureMapException(25); - } - } + // protected ConfiguredInstanceBase(Type pluggedType) : this(pluggedType, Guid.NewGuid().ToString()) + // { + // if (pluggedType != null) + // if (pluggedType.IsAbstract || pluggedType.IsInterface) + // { + // throw new StructureMapException(25); + // } + // } - #region Copyable Members + // #region Copyable Members - Type BasicInstance.PluggedType - { - get { return _pluggedType; } - } + // Type BasicInstance.PluggedType + // { + // get { return _pluggedType; } + // } - Dictionary<string, string> BasicInstance.Properties - { - get { return _properties; } - } + // Dictionary<string, string> BasicInstance.Properties + // { + // get { return _properties; } + // } - Dictionary<string, Instance> BasicInstance.Children - { - get { return _children; } - } + // Dictionary<string, Instance> BasicInstance.Children + // { + // get { return _children; } + // } - Dictionary<string, Instance[]> BasicInstance.Arrays - { - get { return _arrays; } - } + // Dictionary<string, Instance[]> BasicInstance.Arrays + // { + // get { return _arrays; } + // } - #endregion + // #endregion - #region IConfiguredInstance Members + // //#region IConfiguredInstance Members - Type IConfiguredInstance.PluggedType - { - get { return _pluggedType; } - } + // //Type IConfiguredInstance.PluggedType + // //{ + // // get { return _pluggedType; } + // //} - Instance[] IConfiguredInstance.GetChildrenArray(string propertyName) - { - return _arrays.ContainsKey(propertyName) ? _arrays[propertyName] : null; - } + // //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); - } + // //string IConfiguredInstance.GetProperty(string propertyName) + // //{ + // // if (!_properties.ContainsKey(propertyName)) + // // { + // // throw new StructureMapException(205, propertyName, Name); + // // } - return _properties[propertyName]; - } + // // return _properties[propertyName]; + // //} - object IConfiguredInstance.Get(string propertyName, Type pluginType, BuildSession buildSession) - { - return getChild(propertyName, pluginType, buildSession); - } + // //object IConfiguredInstance.Get(string propertyName, Type pluginType, BuildSession buildSession) + // //{ + // // return getChild(propertyName, pluginType, buildSession); + // //} - object IConfiguredInstance.Build(Type pluginType, BuildSession session, InstanceBuilder builder) - { - if (builder == null) - { - throw new StructureMapException( - 201, _pluggedType.FullName, Name, pluginType); - } + // //object IConfiguredInstance.Build(Type pluginType, BuildSession session, InstanceBuilder builder) + // //{ + // // if (builder == null) + // // { + // // throw new StructureMapException( + // // 201, _pluggedType.FullName, 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); - } - } + // // 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); + // // } + // //} - bool IConfiguredInstance.HasProperty(string propertyName) - { - return _properties.ContainsKey(propertyName) || _children.ContainsKey(propertyName) || - _arrays.ContainsKey(propertyName); - } + // //bool IConfiguredInstance.HasProperty(string propertyName, BuildSession session) + // //{ + // // return _properties.ContainsKey(propertyName) || _children.ContainsKey(propertyName) || + // // _arrays.ContainsKey(propertyName); + // //} - void IConfiguredInstance.SetProperty(string propertyName, string propertyValue) - { - setProperty(propertyName, propertyValue); - } + // //void IConfiguredInstance.SetProperty(string propertyName, string propertyValue) + // //{ + // // setProperty(propertyName, propertyValue); + // //} - void IConfiguredInstance.Set(string name, Instance instance) - { - setChild(name, instance); - } + // //void IConfiguredInstance.SetChild(string name, Instance instance) + // //{ + // // setChild(name, instance); + // //} - void IConfiguredInstance.SetChildArray(string name, Type type, Instance[] children) - { - setChildArray(name, children); - } + // //void IConfiguredInstance.SetChildArray(string name, Type type, Instance[] children) + // //{ + // // setChildArray(name, children); + // //} - #endregion + // //#endregion - #region IStructuredInstance Members + // #region IStructuredInstance Members - Instance IStructuredInstance.GetChild(string name) - { - return _children[name]; - } + // Instance IStructuredInstance.GetChild(string name) + // { + // return _children[name]; + // } - Instance[] IStructuredInstance.GetChildArray(string name) - { - return _arrays[name]; - } + // Instance[] IStructuredInstance.GetChildArray(string name) + // { + // return _arrays[name]; + // } - void IStructuredInstance.RemoveKey(string name) - { - _properties.Remove(name); - } + // void IStructuredInstance.RemoveKey(string name) + // { + // _properties.Remove(name); + // } - #endregion + // #endregion - protected override object build(Type pluginType, BuildSession session) - { - InstanceBuilder builder = PluginCache.FindBuilder(_pluggedType); - return ((IConfiguredInstance) this).Build(pluginType, session, builder); - } + // protected override object build(Type pluginType, BuildSession session) + // { + // InstanceBuilder builder = PluginCache.FindBuilder(_pluggedType); + // return ((IConfiguredInstance) this).Build(pluginType, session, builder); + // } - protected virtual object getChild(string propertyName, Type pluginType, BuildSession buildSession) - { - Instance childInstance = _children.ContainsKey(propertyName) - ? _children[propertyName] - : new DefaultInstance(); + // protected virtual object getChild(string propertyName, Type pluginType, BuildSession buildSession) + // { + // Instance childInstance = _children.ContainsKey(propertyName) + // ? _children[propertyName] + // : new DefaultInstance(); - return childInstance.Build(pluginType, buildSession); - } + // return childInstance.Build(pluginType, buildSession); + // } - protected override bool canBePartOfPluginFamily(PluginFamily family) - { - return _pluggedType.CanBeCastTo(family.PluginType) && Constructor.HasConstructors(_pluggedType); - } + // protected override bool canBePartOfPluginFamily(PluginFamily family) + // { + // return _pluggedType.CanBeCastTo(family.PluginType) && Constructor.HasConstructors(_pluggedType); + // } - internal override bool Matches(Plugin plugin) - { - return plugin.PluggedType == _pluggedType; - } + // internal override bool Matches(Plugin plugin) + // { + // return plugin.PluggedType == _pluggedType; + // } - private void read(InstanceMemento memento, PluginGraph graph, Type pluginType) - { - PluginFamily family = graph.FindFamily(pluginType); - Plugin plugin = memento.FindPlugin(family); + // 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; - var reader = new InstanceMementoPropertyReader(this, memento, graph, pluginType); - plugin.VisitArguments(reader); - } + // var reader = new InstanceMementoPropertyReader(this, memento, graph, pluginType); + // plugin.VisitArguments(reader); + // } - protected void setChild(string name, Instance instance) - { - if (instance == null) return; + // protected void setChild(string name, Instance instance) + // { + // if (instance == null) return; - if (_children.ContainsKey(name)) - { - _children[name] = instance; - } - else - { - _children.Add(name, instance); - } + // if (_children.ContainsKey(name)) + // { + // _children[name] = instance; + // } + // else + // { + // _children.Add(name, instance); + // } - } + // } - protected void setChildArray(string name, Instance[] array) - { - for (int i = 0; i < array.Length; i++) - { - if (array[i] == null) - { - throw new ApplicationException("There is a null value in the array of child Instances"); - } + // protected void setChildArray(string name, Instance[] array) + // { + // for (int i = 0; i < array.Length; i++) + // { + // if (array[i] == null) + // { + // throw new ApplicationException("There is a null value in the array of child Instances"); + // } - } + // } - _arrays.Add(name, array); - } + // _arrays.Add(name, array); + // } - protected override Type getConcreteType(Type pluginType) - { - return _pluggedType; - } + // protected override Type getConcreteType(Type pluginType) + // { + // return _pluggedType; + // } - protected string findPropertyName<PLUGINTYPE>() - { - Type dependencyType = typeof(PLUGINTYPE); + // protected string findPropertyName<PLUGINTYPE>() + // { + // Type dependencyType = typeof(PLUGINTYPE); - return findPropertyName(dependencyType); - } + // return findPropertyName(dependencyType); + // } - protected string findPropertyName(Type dependencyType) - { - var plugin = new Plugin(_pluggedType); - string propertyName = plugin.FindArgumentNameForType(dependencyType); + // protected string findPropertyName(Type dependencyType) + // { + // var plugin = new Plugin(_pluggedType); + // string propertyName = plugin.FindArgumentNameForType(dependencyType); - if (string.IsNullOrEmpty(propertyName)) - { - throw new StructureMapException(305, dependencyType); - } + // if (string.IsNullOrEmpty(propertyName)) + // { + // throw new StructureMapException(305, dependencyType); + // } - return propertyName; - } + // return propertyName; + // } - protected static void validateTypeIsArray<PLUGINTYPE>() - { - validateTypeIsArray(typeof(PLUGINTYPE)); - } + // protected static void validateTypeIsArray<PLUGINTYPE>() + // { + // validateTypeIsArray(typeof(PLUGINTYPE)); + // } - protected static void validateTypeIsArray(Type pluginType) - { - if (!pluginType.IsArray) - { - throw new StructureMapException(307); - } - } + // protected static void validateTypeIsArray(Type pluginType) + // { + // if (!pluginType.IsArray) + // { + // throw new StructureMapException(307); + // } + // } - protected void setProperty(string propertyName, string propertyValue) - { - if (string.IsNullOrEmpty(propertyValue)) return; - _properties[propertyName] = propertyValue; - } + // protected void setProperty(string propertyName, string propertyValue) + // { + // if (string.IsNullOrEmpty(propertyValue)) return; + // _properties[propertyName] = propertyValue; + // } - protected void mergeIntoThis(BasicInstance instance) - { - _pluggedType = instance.PluggedType; + // protected void mergeIntoThis(BasicInstance instance) + // { + // _pluggedType = instance.PluggedType; - foreach (var pair in instance.Properties) - { - if (!_properties.ContainsKey(pair.Key)) - { - _properties.Add(pair.Key, pair.Value); - } - } + // foreach (var pair in instance.Properties) + // { + // if (!_properties.ContainsKey(pair.Key)) + // { + // _properties.Add(pair.Key, pair.Value); + // } + // } - foreach (var pair in instance.Children) - { - if (!_children.ContainsKey(pair.Key)) - { - _children.Add(pair.Key, pair.Value); - } - } + // foreach (var pair in instance.Children) + // { + // if (!_children.ContainsKey(pair.Key)) + // { + // _children.Add(pair.Key, pair.Value); + // } + // } - _arrays = instance.Arrays; - } - } + // _arrays = instance.Arrays; + // } + //} } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,56 +1,179 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using StructureMap.Construction; using StructureMap.Graph; using StructureMap.TypeRules; using StructureMap.Util; +using System.Linq; namespace StructureMap.Pipeline { - public class ConstructorInstance : Instance + + + public class ConstructorInstance : Instance, IConfiguredInstance, IStructuredInstance { private readonly Cache<string, Instance> _dependencies = new Cache<string, Instance>(); private readonly Plugin _plugin; - public ConstructorInstance(Type pluggedType) + public ConstructorInstance(Type pluggedType) : this(PluginCache.GetPlugin(pluggedType)) { - _plugin = PluginCache.GetPlugin(pluggedType); + } - protected override string getDescription() + protected override bool canBePartOfPluginFamily(PluginFamily family) { + return _plugin.PluggedType.CanBeCastTo(family.PluginType); + } + + public ConstructorInstance(Plugin plugin) + { + _plugin = plugin; + + _dependencies.OnMissing = key => + { + if (_plugin.FindArgumentType(key).IsSimple()) + { + throw new StructureMapException(205, key, Name); + } + + return new DefaultInstance(); + }; + } + + public ConstructorInstance Override(ExplicitArguments arguments) + { + var instance = new ConstructorInstance(_plugin); + _dependencies.Each((key, i) => instance.SetChild(key, i)); + + arguments.Configure(instance); + + return instance; + } + + public ConstructorInstance(Type pluggedType, string name) : this(pluggedType) + { + Name = name; + } + + protected override void addTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) + { + throw new NotImplementedException(); + } + + protected Plugin plugin { get { return _plugin; } } + + protected sealed override string getDescription() + { return "Configured Instance of " + _plugin.PluggedType.AssemblyQualifiedName; } - public void SetChild(string name, Instance instance) + protected sealed override Type getConcreteType(Type pluginType) { + return _plugin.PluggedType; + } + + void IConfiguredInstance.SetChild(string name, Instance instance) + { + SetChild(name, instance); + } + + public void SetValue(Type type, object value) + { + var name = _plugin.FindArgumentNameForType(type); + SetValue(name, value); + } + + void IConfiguredInstance.SetValue(string name, object value) + { + SetValue(name, value); + } + + void IConfiguredInstance.SetCollection(string name, IEnumerable<Instance> children) + { + SetCollection(name, children); + } + + public string GetProperty(string propertyName) + { + return _dependencies[propertyName].As<ObjectInstance>().Object.ToString(); + } + + internal void SetChild(string name, Instance instance) + { + if (instance == null) + { + throw new ArgumentNullException("instance", "Instance for {0} was null".ToFormat(name)); + } + _dependencies[name] = instance; } - public void SetValue(string name, object value) + internal void SetValue(string name, object value) { - var dependencyType = _plugin.FindArgumentType(name); + Type dependencyType = getDependencyType(name); + var instance = buildInstanceForType(dependencyType, value); - _dependencies[name] = instance; + SetChild(name, instance); } - public void SetCollection(string name, IEnumerable<Instance> children) + private Type getDependencyType(string name) { var dependencyType = _plugin.FindArgumentType(name); + if (dependencyType == null) + { + throw new ArgumentOutOfRangeException("name", + "Could not find a constructor parameter or property for {0} named {1}" + .ToFormat(_plugin.PluggedType.AssemblyQualifiedName, name)); + } + return dependencyType; + } + + internal void SetCollection(string name, IEnumerable<Instance> children) + { + Type dependencyType = getDependencyType(name); var instance = new EnumerableInstance(dependencyType, children); - _dependencies[name] = instance; + SetChild(name, instance); } - private static Instance buildInstanceForType(Type dependencyType, object value) + protected string findPropertyName<PLUGINTYPE>() { + Type dependencyType = typeof(PLUGINTYPE); + + return findPropertyName(dependencyType); + } + + protected string findPropertyName(Type dependencyType) + { + string propertyName = _plugin.FindArgumentNameForType(dependencyType); + + if (string.IsNullOrEmpty(propertyName)) + { + throw new StructureMapException(305, dependencyType); + } + + return propertyName; + } + + private Instance buildInstanceForType(Type dependencyType, object value) + { if (value == null) return new NullInstance(); if (dependencyType.IsSimple() || dependencyType.IsNullable() || dependencyType == typeof(Guid) || dependencyType == typeof(DateTime)) { - var converter = TypeDescriptor.GetConverter(dependencyType); - var convertedValue = converter.ConvertFrom(value); - return new ObjectInstance(convertedValue); + try + { + if (value.GetType() == dependencyType) return new ObjectInstance(value); + + var converter = TypeDescriptor.GetConverter(dependencyType); + var convertedValue = converter.ConvertFrom(value); + return new ObjectInstance(convertedValue); + } + catch (Exception e) + { + throw new StructureMapException(206, e, Name); + } } @@ -62,19 +185,103 @@ return _dependencies[propertyName].Build(pluginType, session); } + public T Get<T>(string propertyName, BuildSession session) + { + object o = Get(propertyName, typeof (T), session); + if (o == null) return default(T); + + return (T)o; + } + protected override object build(Type pluginType, BuildSession session) { - throw new NotImplementedException(); + IInstanceBuilder builder = PluginCache.FindBuilder(_plugin.PluggedType); + return Build(pluginType, session, builder); } - public object Build(Type pluginType, BuildSession session, InstanceBuilder builder) + public Type PluggedType { - throw new NotImplementedException(); + get { return _plugin.PluggedType; } } + public bool HasProperty(string propertyName, BuildSession session) + { + // TODO -- richer behavior + return _dependencies.Has(propertyName); + } + + public object Build(Type pluginType, BuildSession session, IInstanceBuilder builder) + { + if (builder == null) + { + throw new StructureMapException( + 201, _plugin.PluggedType.FullName, Name, pluginType); + } + + + try + { + var args = new Arguments(this, session); + return builder.BuildInstance(args); + } + catch (StructureMapException) + { + throw; + } + catch (InvalidCastException ex) + { + throw new StructureMapException(206, ex, Name); + } + catch (Exception ex) + { + throw new StructureMapException(207, ex, Name, pluginType.FullName); + } + } + public static ConstructorInstance For<T>() { return new ConstructorInstance(typeof(T)); } + + Instance IStructuredInstance.GetChild(string name) + { + return _dependencies[name]; + } + + Instance[] IStructuredInstance.GetChildArray(string name) + { + return _dependencies[name].As<EnumerableInstance>().Children.ToArray(); + } + + void IStructuredInstance.RemoveKey(string name) + { + _dependencies.Remove(name); + } + + public override Instance CloseType(Type[] types) + { + if (_plugin.PluggedType.IsOpenGeneric()) + { + var closedType = _plugin.PluggedType.MakeGenericType(types); + var closedInstance = new ConstructorInstance(closedType); + + _dependencies.Each((key, i) => + { + if (i.CopyAsIsWhenClosingInstance) + { + closedInstance.SetChild(key, i); + } + }); + + return closedInstance; + } + + return null; + } + + public override string ToString() + { + return "'{0}' -> {1}".ToFormat(Name, _plugin.PluggedType.FullName); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/DefaultInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/DefaultInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/DefaultInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -4,6 +4,11 @@ { public class DefaultInstance : Instance { + public DefaultInstance() + { + CopyAsIsWhenClosingInstance = true; + } + protected override bool doesRecordOnTheStack { get { return false; } @@ -11,6 +16,12 @@ protected override object build(Type pluginType, BuildSession session) { + if (EnumerableInstance.IsEnumerable(pluginType)) + { + var enumerable = new EnumerableInstance(pluginType, null); + return enumerable.Build(pluginType, session); + } + return session.CreateInstance(pluginType); } Modified: trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/EnumerableInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; @@ -44,6 +45,8 @@ throw new ArgumentException("Only IEnumerable<T> types can be passed to this constructor. {0} is invalid".ToFormat(propertyType.AssemblyQualifiedName)); } + public IEnumerable<Instance> Children { get { return _children; } } + protected override string getDescription() { return _description; @@ -51,8 +54,27 @@ protected override object build(Type pluginType, BuildSession session) { - var objects = _children.Select(x => x.Build(pluginType, session)); + var elementType = _coercion.ElementType; + + var objects = buildObjects(elementType, session); return _coercion.Convert(objects); } + + private IEnumerable<object> buildObjects(Type elementType, BuildSession session) + { + if (_children == null) + { + return session.GetAllInstances(elementType); + } + + return _children.Select(x => x.Build(elementType, session)); + } + + public static bool IsEnumerable(Type type) + { + if (type.IsArray) return true; + + return type.IsGenericType && type.GetGenericTypeDefinition().IsIn(_enumerableTypes); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -49,13 +49,15 @@ public void Configure(IConfiguredInstance instance) { - foreach (var arg in _args) + _args.Each(pair => { - if (arg.Value == null) continue; + instance.SetValue(pair.Key, pair.Value); + }); - instance.SetProperty(arg.Key, arg.Value.ToString()); - instance.Set(arg.Key, new ObjectInstance(arg.Value)); - } + _children.Each(pair => + { + instance.SetValue(pair.Key, pair.Value); + }); } public bool Has(Type type) @@ -77,31 +79,4 @@ } } - public class ExplicitInstance : ConfiguredInstance - { - private readonly ExplicitArguments _args; - - public ExplicitInstance(Type pluginType, ExplicitArguments args, BasicInstance defaultInstance) : base(null) - { - args.Configure(this); - _args = args; - mergeIntoThis(defaultInstance); - } - - - protected override object getChild(string propertyName, Type pluginType, BuildSession buildSession) - { - if (_args.Has(pluginType)) - { - return _args.Get(pluginType); - } - - if (_args.Has(propertyName)) - { - return _args.GetArg(propertyName); - } - - return base.getChild(propertyName, pluginType, buildSession); - } - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace StructureMap.Pipeline { @@ -7,28 +8,17 @@ string Name { get; } Type PluggedType { get; } - - - [Obsolete] - Instance[] GetChildrenArray(string propertyName); - - [Obsolete] - string GetProperty(string propertyName); - - object Get(string propertyName, Type pluginType, BuildSession buildSession); - object Build(Type pluginType, BuildSession session, InstanceBuilder builder); + T Get<T>(string propertyName, BuildSession session); - bool HasProperty(string propertyName); + bool HasProperty(string propertyName, BuildSession session); - [Obsolete] - void SetProperty(string name, string value); - - - void Set(string name, Instance instance); + void SetChild(string name, Instance instance); + void SetValue(Type type, object value); + void SetValue(string name, object value); + void SetCollection(string name, IEnumerable<Instance> children); - [Obsolete] - void SetChildArray(string name, Type type, Instance[] children); + string GetProperty(string propertyName); } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/IEnumerableCoercion.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,3 +1,5 @@ +using System; +using System.Collections; using System.Collections.Generic; namespace StructureMap.Pipeline @@ -5,5 +7,6 @@ public interface IEnumerableCoercion { object Convert(IEnumerable<object> enumerable); + Type ElementType { get; } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -27,6 +27,8 @@ Instance FindInstanceForProfile(PluginFamily family, string profileName, GraphLog log); InstanceToken CreateToken(); void Preprocess(PluginFamily family); + + [Obsolete("can go away")] void AddTemplatedInstanceTo(PluginFamily family, Type[] templateTypes); } @@ -53,6 +55,9 @@ set { _interceptor = value; } } + internal bool IsReference { get; set; } + internal bool CopyAsIsWhenClosingInstance { get; set; } + #region IDiagnosticInstance Members public string Name @@ -81,6 +86,7 @@ preprocess(family); } + [Obsolete("Can go")] void IDiagnosticInstance.AddTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) { addTemplatedInstanceTo(family, templateTypes); @@ -150,6 +156,7 @@ return pluginType; } + [Obsolete("Can go")] protected virtual void addTemplatedInstanceTo(PluginFamily family, Type[] templateTypes) { if (canBePartOfPluginFamily(family)) @@ -204,6 +211,11 @@ { return false; } + + public virtual Instance CloseType(Type[] types) + { + return null; + } } /// <summary> Modified: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -6,12 +6,12 @@ { public class InstanceMementoPropertyReader : IArgumentVisitor { - private readonly IConfiguredInstance _instance; + private readonly ConfiguredInstance _instance; private readonly InstanceMemento _memento; private readonly PluginGraph _pluginGraph; private readonly Type _pluginType; - public InstanceMementoPropertyReader(IConfiguredInstance instance, InstanceMemento memento, + public InstanceMementoPropertyReader(ConfiguredInstance instance, InstanceMemento memento, PluginGraph pluginGraph, Type pluginType) { _instance = instance; @@ -39,7 +39,7 @@ public void ChildSetter(PropertyInfo property, bool isMandatory) { - copyChild(property.Name, property.PropertyType, isMandatory); + copyChild(property.Name, property.PropertyType); } public void ChildArraySetter(PropertyInfo property, bool isMandatory) @@ -64,7 +64,7 @@ public void ChildParameter(ParameterInfo parameter) { - copyChild(parameter.Name, parameter.ParameterType, true); + copyChild(parameter.Name, parameter.ParameterType); } public void ChildArrayParameter(ParameterInfo parameter) @@ -83,14 +83,15 @@ throw new StructureMapException(205, name, _memento.InstanceKey); } - _instance.SetProperty(name, propertyValue); + _instance.SetValue(name, propertyValue); } - private void copyChild(string name, Type childType, bool isMandatory) + private void copyChild(string name, Type childType) { Instance childInstance = _memento.ReadChildInstance(name, _pluginGraph, childType); + if (childInstance == null) return; - _instance.Set(name, childInstance); + _instance.SetChild(name, childInstance); } private void copyChildArray(string name, Type childType) @@ -104,7 +105,7 @@ children[i] = memento.ReadInstance(_pluginGraph, childType); } - _instance.SetChildArray(name, childType, children); + _instance.SetCollection(name, children); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ListCoercion.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ListCoercion.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ListCoercion.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -9,5 +10,7 @@ { return enumerable.Select(x => x as T).ToList(); } + + public Type ElementType { get { return typeof (T); } } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ObjectInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ObjectInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -23,6 +23,8 @@ public ObjectInstance(object anObject) { + CopyAsIsWhenClosingInstance = true; + if (anObject == null) { throw new ArgumentNullException("anObject"); Modified: trunk/Source/StructureMap/Pipeline/Profile.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -94,15 +94,23 @@ public void CopyDefault(Type sourceType, Type destinationType, PluginFamily family) { - if (_instances.ContainsKey(sourceType)) + if (!_instances.ContainsKey(sourceType)) return; + + var sourceInstance = _instances[sourceType]; + if (sourceInstance.IsReference) { - Instance sourceInstance = _instances[sourceType]; - Instance destinationInstance = family.GetInstance(sourceInstance.Name); - if (destinationInstance != null) + _instances.Add(destinationType, sourceInstance); + } + else + { + family.ForInstance(sourceInstance.Name, x => { - _instances.Add(destinationType, destinationInstance); - } + _instances.Add(destinationType, x); + }); } + + + } Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,3 +1,4 @@ +using System; using System.Configuration; using StructureMap.TypeRules; @@ -6,12 +7,12 @@ /// <summary> /// Defines the value of a primitive argument to a constructur argument /// </summary> - public class PropertyExpression<T> where T : IConfiguredInstance + public class PropertyExpression<T> where T : ConstructorInstance { - private readonly IConfiguredInstance _instance; + private readonly ConstructorInstance _instance; private readonly string _propertyName; - public PropertyExpression(IConfiguredInstance instance, string propertyName) + public PropertyExpression(ConstructorInstance instance, string propertyName) { _instance = instance; _propertyName = propertyName; @@ -24,12 +25,8 @@ /// <returns></returns> public T EqualTo(object propertyValue) { - if(propertyValue.GetType().IsSimple()) - _instance.SetProperty(_propertyName, propertyValue.ToString()); - else - { - _instance.Set(_propertyName,new ObjectInstance(propertyValue)); - } + _instance.SetValue(_propertyName, propertyValue); + return (T) _instance; } @@ -51,11 +48,12 @@ /// <param name="appSettingKey">The key in appSettings for the value to use.</param> /// <param name="defaultValue">The value to use if an entry for <paramref name="appSettingKey"/> does not exist in the appSettings section.</param> /// <returns></returns> + [Obsolete("Change to using a func to get this")] public T EqualToAppSetting(string appSettingKey, string defaultValue) { string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; if (propertyValue == null) propertyValue = defaultValue; - _instance.SetProperty(_propertyName, propertyValue); + _instance.SetValue(_propertyName, propertyValue); return (T)_instance; } } Modified: trunk/Source/StructureMap/Pipeline/ReferencedInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ReferencedInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/ReferencedInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -11,6 +11,9 @@ public ReferencedInstance(string referenceKey) { + IsReference = true; + CopyAsIsWhenClosingInstance = true; + if (string.IsNullOrEmpty(referenceKey)) { throw new ArgumentNullException("referenceKey"); Modified: trunk/Source/StructureMap/Pipeline/SmartInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-26 05:34:50 UTC (rev 292) +++ trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2009-12-26 05:36:06 UTC (rev 293) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq.Expressions; using StructureMap.Configuration.DSL.Expressions; using StructureMap.Graph; @@ -11,11 +12,11 @@ /// Instance that builds objects with by calling constructor functions and using setter properties /// </summary> /// <typeparam name="T">The concrete type constructed by SmartInstance</typeparam> - public class SmartInstance<T> : ConfiguredInstanceBase<SmartInstance<T>> + public class SmartInstance<T> : ConstructorInstance { private readonly List<Action<T>> _actions = new List<Action<T>>(); - public SmartInstance() : base(typeof (T)) + public SmartInstance() : base(typeof(T)) { } @@ -125,11 +126,6 @@ return this; } - protected override string getDescription() - { - return "Smart Instance for " + getConcreteType(null).FullName; - } - /// <summary> /// Define a primitive constructor argument /// </summary> @@ -253,7 +249,7 @@ Plugin plugin = PluginCache.GetPlugin(typeof (T)); string propertyName = plugin.FindArgumentNameForType(typeof (CHILD).MakeArrayType()); - + Debug.WriteLine("Property Name: " + propertyName); return TheArrayOf<CHILD>(propertyName); } @@ -298,7 +294,7 @@ var child = new InstanceExpression<ARRAY>(list.Add); action(child); - _instance.setChildArray(_propertyName, list.ToArray()); + _instance.SetCollection(_propertyName, list); return _instance; } @@ -306,11 +302,11 @@ /// <summary> /// Specify an array of Instance objects directly for an Array dependency /// </summary> - /// <param name="arrayInstances"></param> + /// <param name="children"></param> /// <returns></returns> - public SmartInstance<T> Contains(params Instance[] arrayInstances) + public SmartInstance<T> Contains(params Instance[] children) { - _instance.setChildArray(_propertyName, arrayInstances); + _instance.SetCollection(_propertyName, children); return _instance; } @@ -342,7 +338,7 @@ /// <returns></returns> public SmartInstance<T> Is(Action<IInstanceExpression<CHILD>> action) { - var expression = new InstanceExpression<CHILD>(i => _instance.setChild(_propertyName, i)); + var expression = new InstanceExpression<CHILD>(i => _instance.SetChild(_propertyName, i)); action(expression); return _instance; @@ -355,7 +351,7 @@ /// <returns></returns> public SmartInstance<T> Is(Instance instance) { - _instance.setChild(_propertyName, instance); + _instance.SetChild(_propertyName, instance); return _instance; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |