|
From: <jer...@us...> - 2009-07-04 17:45:17
|
Revision: 253
http://structuremap.svn.sourceforge.net/structuremap/?rev=253&view=rev
Author: jeremydmiller
Date: 2009-07-04 17:45:11 +0000 (Sat, 04 Jul 2009)
Log Message:
-----------
added the Context.All functionality
Modified Paths:
--------------
trunk/Source/StructureMap/BuildSession.cs
trunk/Source/StructureMap/IContext.cs
trunk/Source/StructureMap/InstanceCache.cs
trunk/Source/StructureMap.Testing/BuildSessionTester.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
Added Paths:
-----------
trunk/Source/StructureMap.Testing/InstanceCacheTester.cs
Modified: trunk/Source/StructureMap/BuildSession.cs
===================================================================
--- trunk/Source/StructureMap/BuildSession.cs 2009-07-04 17:12:20 UTC (rev 252)
+++ trunk/Source/StructureMap/BuildSession.cs 2009-07-04 17:45:11 UTC (rev 253)
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using StructureMap.Graph;
using StructureMap.Interceptors;
using StructureMap.Pipeline;
@@ -111,6 +112,14 @@
return _pipelineGraph.HasInstance(typeof (T), name) ? ((IContext) this).GetInstance<T>(name) : null;
}
+ public IEnumerable<T> All<T>() where T : class
+ {
+ var list = new List<T>();
+ _cache.Each<T>(list.Add);
+
+ return list;
+ }
+
#endregion
public virtual object CreateInstance(Type pluginType, string name)
Modified: trunk/Source/StructureMap/IContext.cs
===================================================================
--- trunk/Source/StructureMap/IContext.cs 2009-07-04 17:12:20 UTC (rev 252)
+++ trunk/Source/StructureMap/IContext.cs 2009-07-04 17:45:11 UTC (rev 253)
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using StructureMap.Pipeline;
namespace StructureMap
@@ -63,5 +64,13 @@
/// <param name="name"></param>
/// <returns></returns>
T TryGetInstance<T>(string name) where T : class;
+
+ /// <summary>
+ /// Gets all objects in the current object graph that can be cast
+ /// to T
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <returns></returns>
+ IEnumerable<T> All<T>() where T : class;
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceCache.cs
===================================================================
--- trunk/Source/StructureMap/InstanceCache.cs 2009-07-04 17:12:20 UTC (rev 252)
+++ trunk/Source/StructureMap/InstanceCache.cs 2009-07-04 17:45:11 UTC (rev 253)
@@ -61,5 +61,20 @@
}
cache.Add(Instance, result);
}
+
+ public void Each<T>(Action<T> action) where T : class
+ {
+ foreach (var dictionary in _objects.Values)
+ {
+ foreach (var o in dictionary.Values)
+ {
+ T t = o as T;
+ if (t != null)
+ {
+ action(t);
+ }
+ }
+ }
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/BuildSessionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2009-07-04 17:12:20 UTC (rev 252)
+++ trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2009-07-04 17:45:11 UTC (rev 253)
@@ -5,6 +5,7 @@
using StructureMap.Pipeline;
using StructureMap.Testing.Widget;
using StructureMap.Testing.Widget3;
+using System.Linq;
namespace StructureMap.Testing
{
@@ -242,8 +243,46 @@
var session = new BuildSession(graph);
session.GetInstance<IService>("red").ShouldBeTheSameAs(red);
}
+
+ [Test]
+ public void can_get_all_of_a_type_during_object_creation()
+ {
+ var container = new Container(x =>
+ {
+ x.For<IWidget>().AddInstances(o =>
+ {
+ o.OfConcreteType<AWidget>();
+ o.ConstructedBy(() => new ColorWidget("red"));
+ o.ConstructedBy(() => new ColorWidget("blue"));
+ o.ConstructedBy(() => new ColorWidget("green"));
+ });
+
+ x.ForConcreteType<TopClass>().Configure.OnCreation((c, top) =>
+ {
+ top.Widgets = c.All<IWidget>().ToArray();
+ });
+ });
+
+ container.GetInstance<TopClass>().Widgets.Count().ShouldEqual(4);
+ }
}
+ public class TopClass
+ {
+ public TopClass(ClassWithWidget classWithWidget)
+ {
+ }
+
+ public IWidget[] Widgets { get; set; }
+ }
+
+ public class ClassWithWidget
+ {
+ public ClassWithWidget(IWidget[] widgets)
+ {
+ }
+ }
+
public interface IClassWithRule
{
}
Added: trunk/Source/StructureMap.Testing/InstanceCacheTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/InstanceCacheTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/InstanceCacheTester.cs 2009-07-04 17:45:11 UTC (rev 253)
@@ -0,0 +1,42 @@
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Testing
+{
+ [TestFixture]
+ public class InstanceCacheTester
+ {
+ [Test]
+ public void call_on_each_test()
+ {
+ var target1 = MockRepository.GenerateMock<ITypeTarget>();
+ var target2 = MockRepository.GenerateMock<ITypeTarget>();
+ var target3 = MockRepository.GenerateMock<ITypeTarget>();
+ var target4 = MockRepository.GenerateMock<ITypeTarget>();
+
+ var cache = new InstanceCache();
+ cache.Set(typeof(int), new SmartInstance<int>(), target1);
+ cache.Set(typeof(int), new SmartInstance<int>(), new object());
+ cache.Set(typeof(int), new SmartInstance<int>(), new object());
+ cache.Set(typeof(bool), new SmartInstance<int>(), target2);
+ cache.Set(typeof(bool), new SmartInstance<int>(), new object());
+ cache.Set(typeof(string), new SmartInstance<int>(), target3);
+ cache.Set(typeof(string), new SmartInstance<int>(), new object());
+ cache.Set(typeof(string), new SmartInstance<int>(), new object());
+ cache.Set(typeof(string), new SmartInstance<int>(), target4);
+
+ cache.Each<ITypeTarget>(x => x.Go());
+
+ target1.AssertWasCalled(x => x.Go());
+ target2.AssertWasCalled(x => x.Go());
+ target3.AssertWasCalled(x => x.Go());
+ target4.AssertWasCalled(x => x.Go());
+ }
+ }
+
+ public interface ITypeTarget
+ {
+ void Go();
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
===================================================================
--- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-07-04 17:12:20 UTC (rev 252)
+++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-07-04 17:45:11 UTC (rev 253)
@@ -332,6 +332,7 @@
<Compile Include="Graph\TypePathTester.cs" />
<Compile Include="ImplicitPluginFromPluggedTypeAttributeTester.cs" />
<Compile Include="InstanceBuilderListTester.cs" />
+ <Compile Include="InstanceCacheTester.cs" />
<Compile Include="InstanceMementoInstanceCreationTester.cs" />
<Compile Include="MementoTester.cs" />
<Compile Include="MergingTester.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|