|
From: <jer...@us...> - 2008-08-19 17:56:07
|
Revision: 142
http://structuremap.svn.sourceforge.net/structuremap/?rev=142&view=rev
Author: jeremydmiller
Date: 2008-08-19 17:56:03 +0000 (Tue, 19 Aug 2008)
Log Message:
-----------
More SmartInstance work
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs
trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs
trunk/Source/StructureMap/Pipeline/SmartInstance.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptAllInstancesOfPluginTypeTester.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs
Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -31,11 +31,16 @@
});
}
- public CreatePluginFamilyExpression<PLUGINTYPE> AddInstances(params Instance[] instances)
+ public CreatePluginFamilyExpression<PLUGINTYPE> AddInstances(Action<InstanceExpression<PLUGINTYPE>> action)
{
+ List<Instance> list = new List<Instance>();
+
+ InstanceExpression<PLUGINTYPE> child = new InstanceExpression<PLUGINTYPE>(i => list.Add(i));
+ action(child);
+
return alterAndContinue(family =>
{
- foreach (Instance instance in instances)
+ foreach (Instance instance in list)
{
family.AddInstance(instance);
}
@@ -181,6 +186,14 @@
return this;
}
+ public IsExpression<PLUGINTYPE> TheDefault
+ {
+ get
+ {
+ return new InstanceExpression<PLUGINTYPE>(i => TheDefaultIs(i));
+ }
+ }
+
[Obsolete("Kill!")]
public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIs(PLUGINTYPE @object)
{
Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -42,5 +42,25 @@
{
get { return this; }
}
+
+ public ReferencedInstance References(string key)
+ {
+ return returnInstance(new ReferencedInstance(key));
+ }
+
+ public DefaultInstance TheDefault()
+ {
+ return returnInstance(new DefaultInstance());
+ }
+
+ public ConstructorInstance<T> ConstructedBy(Func<T> func)
+ {
+ return returnInstance(new ConstructorInstance<T>(func));
+ }
+
+ public ConstructorInstance<T> ConstructedBy(Func<BuildSession, T> func)
+ {
+ return returnInstance(new ConstructorInstance<T>(func));
+ }
}
}
Modified: trunk/Source/StructureMap/Pipeline/SmartInstance.cs
===================================================================
--- trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -80,6 +80,11 @@
return new DependencyExpression<T, SETTERTYPE>(this, propertyName);
}
+ public DependencyExpression<T, SETTERTYPE> SetterDependency<SETTERTYPE>()
+ {
+ return CtorDependency<SETTERTYPE>();
+ }
+
public ArrayDefinitionExpression<T, CHILD> TheArrayOf<CHILD>()
{
if (typeof(CHILD).IsArray)
@@ -128,11 +133,10 @@
_propertyName = propertyName;
}
- public SmartInstance<T> Is(Func<InstanceExpression<CHILD>, Instance> func)
+ public SmartInstance<T> Is(Action<InstanceExpression<CHILD>> action)
{
var expression = new InstanceExpression<CHILD>(i => _instance.setChild(_propertyName, i));
- Instance instance = func(expression);
-
+ action(expression);
return _instance;
}
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -31,14 +31,15 @@
Concretion concretion1 = new Concretion();
Concretion concretion2 = new Concretion();
- IContainer manager = new Container(registry => registry.ForRequestedType<Abstraction>()
- .AddInstances(
- ConstructedBy<Abstraction>(() => concretion1).WithName("One"),
- ConstructedBy<Abstraction>(() => concretion2).WithName("Two")
- ));
+ IContainer container = new Container(r =>
+ r.ForRequestedType<Abstraction>().AddInstances(x =>
+ {
+ x.ConstructedBy(() => concretion1).WithName("One");
+ x.ConstructedBy(() => concretion2).WithName("Two");
+ }));
- Assert.AreSame(concretion1, manager.GetInstance<Abstraction>("One"));
- Assert.AreSame(concretion2, manager.GetInstance<Abstraction>("Two"));
+ Assert.AreSame(concretion1, container.GetInstance<Abstraction>("One"));
+ Assert.AreSame(concretion2, container.GetInstance<Abstraction>("Two"));
}
[Test]
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -126,6 +126,26 @@
)
));
}
+
+
+ [Test]
+ public void DeepInstanceTest_with_SmartInstance()
+ {
+ assertThingMatches(registry =>
+ {
+ registry.ForRequestedType<Thing>().TheDefault.Is.OfConcreteType<Thing>()
+ .WithCtorArg("name").EqualTo("Jeremy")
+ .WithCtorArg("count").EqualTo(4)
+ .WithCtorArg("average").EqualTo(.333)
+ .SetterDependency<Rule>().Is(x =>
+ {
+ x.OfConcreteType<WidgetRule>().SetterDependency<IWidget>().Is(
+ c => c.OfConcreteType<ColorWidget>().WithCtorArg("color").EqualTo("yellow"));
+ });
+ });
+ }
+
+
}
public class Thing
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/InjectArrayTester.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -170,7 +170,32 @@
Assert.IsInstanceOfType(typeof (Handler1), processor.Handlers[1]);
}
+
[Test]
+ public void PlaceMemberInArrayByReference_with_SmartInstance()
+ {
+ IContainer manager = new Container(registry =>
+ {
+ registry.AddInstanceOf<IHandler>().UsingConcreteType<Handler1>().WithName("One");
+ registry.AddInstanceOf<IHandler>().UsingConcreteType<Handler2>().WithName("Two");
+
+ registry.ForRequestedType<Processor>().TheDefault.Is.OfConcreteType<Processor>()
+ .WithCtorArg("name").EqualTo("Jeremy")
+ .TheArrayOf<IHandler>().Contains(x =>
+ {
+ x.References("Two");
+ x.References("One");
+ });
+
+ });
+
+ var processor = manager.GetInstance<Processor>();
+
+ Assert.IsInstanceOfType(typeof(Handler2), processor.Handlers[0]);
+ Assert.IsInstanceOfType(typeof(Handler1), processor.Handlers[1]);
+ }
+
+ [Test]
public void ProgrammaticallyInjectArrayAllInline()
{
IContainer manager = new Container(registry => registry.ForRequestedType<Processor>()
@@ -191,6 +216,30 @@
Assert.IsInstanceOfType(typeof (Handler3), processor.Handlers[2]);
}
+ [Test]
+ public void ProgrammaticallyInjectArrayAllInline_with_smart_instance()
+ {
+ IContainer container = new Container(r =>
+ {
+ r.ForRequestedType<Processor>().TheDefault.Is.OfConcreteType<Processor>()
+ .WithCtorArg("name").EqualTo("Jeremy")
+ .TheArrayOf<IHandler>().Contains(x =>
+ {
+ x.OfConcreteType<Handler1>();
+ x.OfConcreteType<Handler2>();
+ x.OfConcreteType<Handler3>();
+ });
+
+ int number = 0;
+ });
+
+ var processor = container.GetInstance<Processor>();
+
+ Assert.IsInstanceOfType(typeof(Handler1), processor.Handlers[0]);
+ Assert.IsInstanceOfType(typeof(Handler2), processor.Handlers[1]);
+ Assert.IsInstanceOfType(typeof(Handler3), processor.Handlers[2]);
+ }
+
[Test,
ExpectedException(typeof (StructureMapException),
ExpectedMessage =
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptAllInstancesOfPluginTypeTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptAllInstancesOfPluginTypeTester.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptAllInstancesOfPluginTypeTester.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -18,19 +18,31 @@
_defaultRegistry = (registry =>
{
- registry.ForRequestedType<IService>()
- .AddInstances(
- Instance<ColorService>().WithName("Red").WithProperty("color").
- EqualTo(
- "Red"),
- Object<IService>(new ColorService("Yellow")).WithName("Yellow"),
- ConstructedBy<IService>(
- delegate { return new ColorService("Purple"); })
- .WithName("Purple"),
- Instance<ColorService>().WithName("Decorated").WithProperty("color")
- .
- EqualTo("Orange")
- );
+ //registry.ForRequestedType<IService>()
+ // .AddInstances(
+ // Instance<ColorService>().WithName("Red").WithProperty("color").
+ // EqualTo(
+ // "Red"),
+ // Object<IService>(new ColorService("Yellow")).WithName("Yellow"),
+ // ConstructedBy<IService>(
+ // delegate { return new ColorService("Purple"); })
+ // .WithName("Purple"),
+ // Instance<ColorService>().WithName("Decorated").WithProperty("color")
+ // .
+ // EqualTo("Orange")
+ // );
+
+ registry.ForRequestedType<IService>().AddInstances(x =>
+ {
+ x.OfConcreteType<ColorService>().WithName("Red").WithProperty("color").EqualTo("Red");
+
+ x.Object(new ColorService("Yellow")).WithName("Yellow");
+
+ x.ConstructedBy(() => new ColorService("Purple")).WithName("Purple");
+
+ x.OfConcreteType<ColorService>().WithName("Decorated").WithProperty("color").EqualTo("Orange");
+
+ });
});
}
@@ -95,4 +107,92 @@
Assert.AreEqual(yellow, _lastService);
}
}
+
+ [TestFixture]
+ public class InterceptAllInstancesOfPluginTypeTester_with_SmartInstance : Registry
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ _lastService = null;
+ _manager = null;
+
+ _defaultRegistry = (registry =>
+ registry.ForRequestedType<IService>().AddInstances(x =>
+ {
+ x.OfConcreteType<ColorService>().WithName("Red")
+ .WithCtorArg("color").EqualTo("Red");
+
+ x.Object(new ColorService("Yellow")).WithName("Yellow");
+
+ x.ConstructedBy(() => new ColorService("Purple")).WithName("Purple");
+
+ x.OfConcreteType<ColorService>().WithName("Decorated").WithCtorArg("color").EqualTo(
+ "Orange");
+ }));
+ }
+
+ #endregion
+
+ private IService _lastService;
+ private IContainer _manager;
+ private Action<Registry> _defaultRegistry;
+
+ private IService getService(Action<Registry> action, string name)
+ {
+ if (_manager == null)
+ {
+ _manager = new Container(registry =>
+ {
+ _defaultRegistry(registry);
+ action(registry);
+ });
+ }
+
+ return _manager.GetInstance<IService>(name);
+ }
+
+ [Test]
+ public void EnrichForAll()
+ {
+ Action<Registry> action = registry => registry.ForRequestedType<IService>()
+ .EnrichWith(s => new DecoratorService(s))
+ .AddInstance(
+ ConstructedBy<IService>(() => new ColorService("Green"))
+ .WithName("Green"));
+
+
+ IService green = getService(action, "Green");
+
+
+ var decoratorService = (DecoratorService) green;
+ var innerService = (ColorService) decoratorService.Inner;
+ Assert.AreEqual("Green", innerService.Color);
+ }
+
+ [Test]
+ public void OnStartupForAll()
+ {
+ Action<Registry> action = registry => registry.ForRequestedType<IService>()
+ .OnCreation(s => _lastService = s)
+ .AddInstance(
+ ConstructedBy<IService>(() => new ColorService("Green"))
+ .WithName("Green"));
+
+
+ IService red = getService(action, "Red");
+ Assert.AreSame(red, _lastService);
+
+ IService purple = getService(action, "Purple");
+ Assert.AreSame(purple, _lastService);
+
+ IService green = getService(action, "Green");
+ Assert.AreSame(green, _lastService);
+
+ IService yellow = getService(action, "Yellow");
+ Assert.AreEqual(yellow, _lastService);
+ }
+ }
}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs 2008-08-19 15:27:23 UTC (rev 141)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/InterceptorTesting.cs 2008-08-19 17:56:03 UTC (rev 142)
@@ -15,41 +15,65 @@
{
_lastService = null;
- _manager = new Container(registry => registry.ForRequestedType<IService>().AddInstances
- (
- Instance<ColorService>()
- .OnCreation<ColorService>(s => _lastService = s)
- .WithName("Intercepted")
- .WithProperty("color").EqualTo("Red"),
- Instance<ColorService>()
- .WithName("NotIntercepted")
- .WithProperty("color").EqualTo("Blue"),
- Object<IService>(new ColorService("Yellow"))
- .WithName("Yellow")
- .OnCreation<ColorService>(s => _lastService = s),
- ConstructedBy<IService>(() => new ColorService("Purple"))
- .WithName("Purple")
- .EnrichWith<IService>(s => new DecoratorService(s)),
- Instance<ColorService>()
- .WithName("Decorated")
- .EnrichWith<IService>(s => new DecoratorService(s))
- .WithProperty("color").EqualTo("Orange"),
- Object<IService>(new ColorService("Yellow"))
- .WithName("Bad")
- .OnCreation<ColorService>(obj => { throw new ApplicationException("Bad!"); })
- ));
+ //_container = new Container(registry => registry.ForRequestedType<IService>().AddInstances
+ // (
+ // Instance<ColorService>()
+ // .OnCreation<ColorService>(s => _lastService = s)
+ // .WithName("Intercepted")
+ // .WithProperty("color").EqualTo("Red"),
+ // Instance<ColorService>()
+ // .WithName("NotIntercepted")
+ // .WithProperty("color").EqualTo("Blue"),
+ // Object<IService>(new ColorService("Yellow"))
+ // .WithName("Yellow")
+ // .OnCreation<ColorService>(s => _lastService = s),
+ // ConstructedBy<IService>(() => new ColorService("Purple"))
+ // .WithName("Purple")
+ // .EnrichWith<IService>(s => new DecoratorService(s)),
+ // Instance<ColorService>()
+ // .WithName("Decorated")
+ // .EnrichWith<IService>(s => new DecoratorService(s))
+ // .WithProperty("color").EqualTo("Orange"),
+ // Object<IService>(new ColorService("Yellow"))
+ // .WithName("Bad")
+ // .OnCreation<ColorService>(obj => { throw new ApplicationException("Bad!"); })
+ // ));
+
+ _container = new Container(r => r.ForRequestedType<IService>().AddInstances(x =>
+ {
+ x.OfConcreteType<ColorService>()
+ .OnCreation<ColorService>(s => _lastService = s)
+ .WithName("Intercepted")
+ .WithProperty("color").EqualTo("Red");
+
+ x.OfConcreteType<ColorService>()
+ .WithName("NotIntercepted")
+ .WithProperty("color").EqualTo("Blue");
+
+ x.Object(new ColorService("Yellow"))
+ .WithName("Yellow")
+ .OnCreation<ColorService>(s => _lastService = s);
+
+ x.ConstructedBy(() => new ColorService("Purple")).WithName("Purple").EnrichWith<IService>(s => new DecoratorService(s));
+
+ x.OfConcreteType<ColorService>().WithName("Decorated").EnrichWith<IService>(s => new DecoratorService(s))
+ .WithCtorArg("color").EqualTo("Orange");
+
+ x.Object(new ColorService("Yellow")).WithName("Bad")
+ .OnCreation<ColorService>(obj => { throw new ApplicationException("Bad!"); });
+ }));
}
#endregion
private ColorService _lastService;
- private IContainer _manager;
+ private IContainer _container;
[Test]
public void DecorateAConstructedService()
{
- IService service = _manager.GetInstance<IService>("Purple");
+ IService service = _container.GetInstance<IService>("Purple");
DecoratorService decoratorService = (DecoratorService) service;
ColorService innerService = (ColorService) decoratorService.Inner;
@@ -59,7 +83,7 @@
[Test]
public void DecorateInline()
{
- IService service = _manager.GetInstance<IService>("Decorated");
+ IService service = _container.GetInstance<IService>("Decorated");
DecoratorService decoratorService = (DecoratorService) service;
ColorService innerService = (ColorService) decoratorService.Inner;
@@ -71,7 +95,7 @@
public void OnCreationWithAConstructedService()
{
Assert.IsNull(_lastService);
- IService interceptedService = _manager.GetInstance<IService>("Yellow");
+ IService interceptedService = _container.GetInstance<IService>("Yellow");
Assert.AreSame(_lastService, interceptedService);
}
@@ -82,10 +106,10 @@
// "NotIntercepted" should not.
Assert.IsNull(_lastService);
- _manager.GetInstance<IService>("NotIntercepted");
+ _container.GetInstance<IService>("NotIntercepted");
Assert.IsNull(_lastService);
- IService interceptedService = _manager.GetInstance<IService>("Intercepted");
+ IService interceptedService = _container.GetInstance<IService>("Intercepted");
Assert.AreSame(_lastService, interceptedService);
}
@@ -94,7 +118,7 @@
{
try
{
- _manager.GetInstance<IService>("Bad");
+ _container.GetInstance<IService>("Bad");
Assert.Fail("Should have thrown an error");
}
catch (StructureMapException e)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|