From: <jer...@us...> - 2009-12-26 05:34:56
|
Revision: 292 http://structuremap.svn.sourceforge.net/structuremap/?rev=292&view=rev Author: jeremydmiller Date: 2009-12-26 05:34:50 +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/Graph/Constructor.cs trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/PluginCache.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -8,8 +8,8 @@ { public class Constructor { - private ConstructorInfo _ctor; private readonly Type _pluggedType; + private ConstructorInfo _ctor; public Constructor(Type pluggedType) { @@ -17,10 +17,7 @@ _ctor = GetConstructor(pluggedType); } - public ConstructorInfo Ctor - { - get { return _ctor; } - } + public ConstructorInfo Ctor { get { return _ctor; } } /// <summary> /// Returns the System.Reflection.ConstructorInfo for the PluggedType. Uses either @@ -102,11 +99,8 @@ catch (Exception e) { string message = - "Trying to visit parameter {0} of type {1} in the constructor for {2}".ToFormat(info.Name, - info. - ParameterType, - _pluggedType. - AssemblyQualifiedName); + "Trying to visit parameter {0} of type {1} in the constructor for {2}" + .ToFormat(info.Name, info.ParameterType, _pluggedType.AssemblyQualifiedName); throw new ApplicationException(message, e); } } @@ -144,7 +138,7 @@ var finder = new ConstructorFinderVisitor(); finder.Visit(expression); - var ctor = finder.Constructor; + ConstructorInfo ctor = finder.Constructor; if (ctor == null) { throw new ApplicationException("Not a valid constructor function"); Modified: trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/GenericsPluginGraph.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -100,7 +100,8 @@ Type[] templatedParameterTypes = templatedType.GetGenericArguments(); - PluginFamily family = CreateTemplatedClone(basicFamily, templatedParameterTypes); + PluginFamily templatedFamily = basicFamily.CreateTemplatedClone(templatedParameterTypes); + PluginFamily family = templatedFamily; profileManager.CopyDefaults(basicType, templatedType, family); return family; @@ -111,12 +112,6 @@ } } - // TODO: This code sucks. What's going on here? - public static PluginFamily CreateTemplatedClone(PluginFamily baseFamily, params Type[] templateTypes) - { - PluginFamily templatedFamily = baseFamily.CreateTemplatedClone(templateTypes); - return templatedFamily; - } public static bool CanBePluggedIntoGenericType(Type pluginType, Type pluggedType, params Type[] templateTypes) @@ -149,10 +144,7 @@ public void ImportFrom(GenericsPluginGraph source) { - foreach (PluginFamily sourceFamily in source._families) - { - ImportFrom(sourceFamily); - } + source._families.Each(ImportFrom); } public void ImportFrom(PluginFamily sourceFamily) Modified: trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/IHeavyweightTypeScanner.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -42,18 +42,22 @@ private IEnumerable<Type> FindPluginTypes(Type type) { + return new Type[0]; // lets not worry about abstract base classes for now return type.GetInterfaces().Where(t => t.IsPublic); } public IEnumerable<TypeMap> GetTypeMaps() { - return _implementations.Contents().Select(pair => new TypeMap(pair.Key, pair.Value)); + return new TypeMap[0]; + throw new NotImplementedException(); + //return _implementations.Contents().Select(pair => new TypeMap(pair.Key, pair.Value)); } public void Dispose() { - _implementations.Clear(); + //throw new NotImplementedException(); + //_implementations.Clear(); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -176,5 +176,7 @@ { return _constructor.FindArgumentType(argumentName) ?? _setters.FindArgumentType(argumentName); } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginCache.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginCache.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/PluginCache.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -2,15 +2,14 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using StructureMap.Emitting; +using StructureMap.Construction; using StructureMap.Util; -using StructureMap.TypeRules; namespace StructureMap.Graph { public static class PluginCache { - private static readonly Cache<Type, InstanceBuilder> _builders; + private static readonly Cache<Type, IInstanceBuilder> _builders; private static readonly Cache<Type, Plugin> _plugins; private static readonly List<Predicate<PropertyInfo>> _setterRules; @@ -29,16 +28,16 @@ }); - _builders = new Cache<Type, InstanceBuilder>(t => + _builders = new Cache<Type, IInstanceBuilder>(t => { try { Plugin plugin = _plugins[t]; - return new InstanceBuilderAssembly(new[] {plugin}).Compile()[0]; + return BuilderCompiler.CreateBuilder(plugin); } catch (Exception e) { - throw new StructureMapException(245, t.AssemblyQualifiedName, e); + throw new StructureMapException(245, e, t.AssemblyQualifiedName); } }); } @@ -48,41 +47,11 @@ return _plugins[pluggedType]; } - public static InstanceBuilder FindBuilder(Type pluggedType) + public static IInstanceBuilder FindBuilder(Type pluggedType) { return _builders[pluggedType]; } - public static void Compile() - { - lock (typeof (PluginCache)) - { - IEnumerable<Plugin> plugins = - _plugins.Where(plugin => pluginHasNoBuilder(plugin) && plugin.CanBeCreated()); - createAndStoreBuilders(plugins); - } - } - - private static void createAndStoreBuilders(IEnumerable<Plugin> plugins) - { - // If this is a nested container and there are no new plugins, - // we don't need to create the InstanceBuilderAssembly - // This was causing OutOfMemoryExceptions when using nested containers - // repeatedly (i.e. every web request in a web app) - if (plugins == null || plugins.Count() == 0) return; - - //NOTE: Calling 'Compile()' on a DynamicAssembly will actually - // compile it as a file on the disk and load it into the AppDomain. - // If you call it repeatedly, you will eventually run out of memory - var assembly = new InstanceBuilderAssembly(plugins); - assembly.Compile().ForEach(b => _builders[b.PluggedType] = b); - } - - private static bool pluginHasNoBuilder(Plugin plugin) - { - return !_builders.Has(plugin.PluggedType); - } - public static void Store(Type pluggedType, InstanceBuilder builder) { _builders[pluggedType] = builder; @@ -114,9 +83,7 @@ //does any of the registered plugins have a setter matching the predicate? if (plugin.PluggedType.GetProperties().Any(s => predicate(s))) { - //rebuild the builder if nessesary - if (_builders[plugin.PluggedType] != null) - createAndStoreBuilders(new[] { plugin }); + _builders.Remove(plugin.PluggedType); } }); Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using StructureMap.Attributes; using StructureMap.Pipeline; using StructureMap.TypeRules; @@ -14,7 +15,10 @@ /// </summary> public class PluginFamily : IPluginFamily { - private readonly Cache<string, Instance> _instances = new Cache<string, Instance>(delegate { return null; }); + private readonly Cache<string, Instance> _instances = new Cache<string, Instance>(delegate + { + return null; + }); private readonly List<InstanceMemento> _mementoList = new List<InstanceMemento>(); private readonly Cache<string, Plugin> _pluggedTypes = new Cache<string, Plugin>(); private readonly Type _pluginType; @@ -148,10 +152,15 @@ if (hasInstanceWithPluggedType(plugin)) return; ConfiguredInstance instance = new ConfiguredInstance(plugin.PluggedType).WithName(key); - AddInstance(instance); + FillInstance(instance); }); } + private void FillInstance(Instance instance) + { + _instances.Fill(instance.Name, instance); + } + private bool hasInstanceWithPluggedType(Plugin plugin) { return _instances.Exists(instance => instance.Matches(plugin)); @@ -280,9 +289,14 @@ templatedFamily.DefaultInstanceKey = DefaultInstanceKey; templatedFamily._lifecycle = _lifecycle; + _instances.GetAll().Select(x => + { + var clone = x.CloseType(templateTypes); + if (clone == null) return null; - // TODO -- Got a big problem here. Intances need to be copied over - EachInstance(i => { ((IDiagnosticInstance) i).AddTemplatedInstanceTo(templatedFamily, templateTypes); }); + clone.Name = x.Name; + return clone; + }).Where(x => x != null).Each(templatedFamily.AddInstance); // Need to attach the new PluginFamily to the old PluginGraph Parent.PluginFamilies.Add(templatedFamily); @@ -365,5 +379,10 @@ public Instance MissingInstance { get; set; } #endregion + + public void ForInstance(string name, Action<Instance> action) + { + _instances.WithValue(name, action); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-26 05:34:29 UTC (rev 291) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2009-12-26 05:34:50 UTC (rev 292) @@ -19,14 +19,14 @@ _properties = new List<SetterProperty>(); _plugin = plugin; - foreach (PropertyInfo property in plugin.PluggedType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) - { - if (property.CanWrite && property.GetSetMethod(false) != null) + + plugin.PluggedType.GetProperties(BindingFlags.Public | BindingFlags.Instance) + .Where(x => x.CanWrite && x.GetSetMethod(false) != null && x.GetSetMethod().GetParameters().Length == 1) + .Each(x => { - var setter = new SetterProperty(property); + var setter = new SetterProperty(x); _properties.Add(setter); - } - } + }); } public int MandatoryCount This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |