User: xtoff
Date: 2009/12/10 11:49 PM
Added:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/
DynamicParametersTestCase.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
DynamicParametersDescriptor.cs
Removed:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/
WithParametersTestCase.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
WithParametersDescriptor.cs
Modified:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/
Castle.MicroKernel.Tests-vs2008.csproj
/InversionOfControl/trunk/src/Castle.MicroKernel/
Castle.MicroKernel-vs2008.csproj
/InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
ComponentRegistration.cs
Log:
- changed the WithParameters method to DynamicParameters to avoid confusion
with Parameters method
File Changes:
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/
============================================================
File [modified]: Castle.MicroKernel-vs2008.csproj
Delta lines: +2 -2
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/ComponentRegistration.cs
2009-12-10 22:22:48 UTC (rev 6401)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/ComponentRegistration.cs
2009-12-11 06:48:58 UTC (rev 6402)
@@ -570,10 +570,10 @@
return this;
}
- public ComponentRegistration<S>
WithParameters(WithParametersDelegate action)
+ public ComponentRegistration<S>
DynamicParameters(DynamicParametersDelegate action)
{
- this.AddDescriptor(new
WithParametersDescriptor<S>(action));
+ this.AddDescriptor(new
DynamicParametersDescriptor<S>(action));
return this;
}
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel.Tests/
==================================================================
File [modified]: Castle.MicroKernel.Tests-vs2008.csproj
Delta lines: +149 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/DynamicParametersTestCase.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/DynamicParametersTestCase.cs
2009-12-11 06:48:58 UTC (rev 6402)
@@ -0,0 +1,149 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.MicroKernel.Tests.Registration
+{
+ using Castle.MicroKernel.Registration;
+ using Castle.MicroKernel.Tests.ClassComponents;
+
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class DynamicParametersTestCase
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ kernel = new DefaultKernel();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ kernel.Dispose();
+ }
+
+ private DefaultKernel kernel;
+
+ [Test]
+ public void Can_mix_registration_and_call_site_parameters()
+ {
+ kernel.Register(
+
Component.For<ClassWithArguments>().LifeStyle.Transient.DynamicParameters((k,
d) => d["arg1"] = "foo"));
+
+ var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2 });
+ Assert.AreEqual(2, component.Arg2);
+ Assert.AreEqual("foo", component.Arg1);
+ }
+
+ [Test]
+ public void Can_dynamically_override_services()
+ {
+ kernel.Register(
+ Component.For<ICustomer>()
+ .ImplementedBy<CustomerImpl>()
+ .Named("defaultCustomer"),
+
Component.For<ICustomer>().ImplementedBy<CustomerImpl2>()
+ .Named("otherCustomer")
+ .Parameters(
+ Parameter.ForKey("name").Eq("foo"),
// static parameters, resolved at registration time
+ Parameter.ForKey("address").Eq("bar st
13"),
+ Parameter.ForKey("age").Eq("5")),
+ Component.For<CommonImplWithDependancy>()
+ .LifeStyle.Transient
+ .DynamicParameters((k, d) =>
// dynamic parameters
+ {
+ var randomNumber = 2;
+ if (randomNumber == 2)
+ {
+ d["customer"] =
k.Resolve<ICustomer>("otherCustomer");
+ }
+ }));
+
+ var component =
kernel.Resolve<CommonImplWithDependancy>();
+ Assert.IsInstanceOf<CustomerImpl2>(component.Customer);
+ }
+
+ [Test]
+ public void
Should_have_access_to_parameters_passed_from_call_site()
+ {
+ string arg1 = null;
+ int arg2 = 0;
+
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.DynamicParameters((k,
d) =>
+ {
+ arg1 = (string)d["arg1"];
+ arg2 = (int)d["arg2"];
+ }));
+ var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
+ Assert.AreEqual("foo", arg1);
+ Assert.AreEqual(2, arg2);
+ }
+
+ [Test]
+ public void Should_not_require_explicit_registration()
+ {
+
kernel.Register(Component.For<CommonSub2Impl>().LifeStyle.Transient.DynamicParameters((k,
d) => { }));
+ Assert.DoesNotThrow(() =>
kernel.Resolve<CommonSub2Impl>());
+ }
+
+ [Test]
+ public void Should_override_parameters_passed_from_call_site()
+ {
+ string arg1 = "bar";
+ int arg2 = 5;
+
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.DynamicParameters((k,
d) =>
+ {
+ d["arg1"] = arg1;
+ d["arg2"] = arg2;
+ }));
+ var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
+ Assert.AreEqual(arg1, component.Arg1);
+ Assert.AreEqual(arg2, component.Arg2);
+ }
+
+ [Test]
+ public void Should_handle_multiple_calls()
+ {
+ string arg1 = "bar";
+ int arg2 = 5;
+ kernel.Register(Component.For<ClassWithArguments>()
+ .LifeStyle.Transient
+ .DynamicParameters((k, d) =>
+ {
+ d["arg1"] = arg1;
+ })
+ .DynamicParameters((k, d) =>
+ {
+ d["arg2"] = arg2;
+ }));
+ var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
+ Assert.AreEqual(arg1, component.Arg1);
+ Assert.AreEqual(arg2, component.Arg2);
+ }
+
+ [Test]
+ public void
Should_resolve_component_when_no_parameters_passed_from_call_site()
+ {
+ string arg1 = "bar";
+ int arg2 = 5;
+
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.DynamicParameters((k,
d) =>
+ {
+ d["arg1"] = arg1;
+ d["arg2"] = arg2;
+ }));
+ //Assert.DoesNotThrow(() =>
+ kernel.Resolve<ClassWithArguments>();//);
+ }
+ }
+}
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
=========================================================================
File [modified]: ComponentRegistration.cs
Delta lines: +50 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/DynamicParametersDescriptor.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/DynamicParametersDescriptor.cs
2009-12-11 06:48:58 UTC (rev 6402)
@@ -0,0 +1,50 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.MicroKernel.Registration
+{
+ using System.Collections;
+
+ using Castle.Core;
+ using Castle.MicroKernel.Handlers;
+
+ public delegate void DynamicParametersDelegate(IKernel kernel,
IDictionary parameters);
+
+
+ public class DynamicParametersDescriptor<S> : ComponentDescriptor<S>
+ {
+ private readonly DynamicParametersDelegate action;
+ private static readonly string key =
"component_resolving_handler";
+
+ public DynamicParametersDescriptor(DynamicParametersDelegate
action)
+ {
+ this.action = action;
+ }
+
+ protected internal override void ApplyToModel(IKernel kernel,
ComponentModel model)
+ {
+ ComponentResolvingDelegate handler = (k, c) =>
action(k, c.AdditionalParameters);
+ if (model.ExtendedProperties.Contains(key) == false)
+ {
+ model.ExtendedProperties[key] = handler;
+ return;
+ }
+
+ var @delegate =
(ComponentResolvingDelegate)model.ExtendedProperties[key];
+ @delegate += handler;
+
+ model.ExtendedProperties[key] = @delegate;
+ }
+ }
+}
File [added]: DynamicParametersDescriptor.cs
Delta lines: +0 -50
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/WithParametersDescriptor.cs
2009-12-10 22:22:48 UTC (rev 6401)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/WithParametersDescriptor.cs
2009-12-11 06:48:58 UTC (rev 6402)
@@ -1,50 +0,0 @@
-// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-namespace Castle.MicroKernel.Registration
-{
- using System.Collections;
-
- using Castle.Core;
- using Castle.MicroKernel.Handlers;
-
- public delegate void WithParametersDelegate(IKernel kernel, IDictionary
parameters);
-
-
- public class WithParametersDescriptor<S> : ComponentDescriptor<S>
- {
- private readonly WithParametersDelegate action;
- private static readonly string key =
"component_resolving_handler";
-
- public WithParametersDescriptor(WithParametersDelegate action)
- {
- this.action = action;
- }
-
- protected internal override void ApplyToModel(IKernel kernel,
ComponentModel model)
- {
- ComponentResolvingDelegate handler = (k, c) =>
action(k, c.AdditionalParameters);
- if (model.ExtendedProperties.Contains(key) == false)
- {
- model.ExtendedProperties[key] = handler;
- return;
- }
-
- var @delegate =
(ComponentResolvingDelegate)model.ExtendedProperties[key];
- @delegate += handler;
-
- model.ExtendedProperties[key] = @delegate;
- }
- }
-}
File [removed]: WithParametersDescriptor.cs
Delta lines: +1 -1
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj
2009-12-10 22:22:48 UTC (rev 6401)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj
2009-12-11 06:48:58 UTC (rev 6402)
@@ -204,7 +204,7 @@
<Compile Include="Lifecycle\OnCreateTestCase.cs" />
<Compile
Include="Facilities\Startable\Components\StartableChainWithGenerics.cs" />
<Compile
Include="Facilities\Startable\Components\StartableComponentWithCustomDependencies.cs"
/>
- <Compile Include="Registration\WithParametersTestCase.cs" />
+ <Compile Include="Registration\DynamicParametersTestCase.cs" />
<Compile Include="HandlerForwardingTestCase.cs" />
<Compile Include="KeySearchNamingSubSystemTestCase.cs" />
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/
===============================================================================
File [added]: DynamicParametersTestCase.cs
Delta lines: +0 -153
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/WithParametersTestCase.cs
2009-12-10 22:22:48 UTC (rev 6401)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Registration/WithParametersTestCase.cs
2009-12-11 06:48:58 UTC (rev 6402)
@@ -1,153 +0,0 @@
-// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-namespace Castle.MicroKernel.Tests.Registration
-{
- using System;
-
- using Castle.MicroKernel.Registration;
- using Castle.MicroKernel.Tests.ClassComponents;
-
- using NUnit.Framework;
-
- [TestFixture]
- public class WithParametersTestCase
- {
- [SetUp]
- public void SetUp()
- {
- kernel = new DefaultKernel();
- }
-
- [TearDown]
- public void TearDown()
- {
- kernel.Dispose();
- }
-
- private DefaultKernel kernel;
-
- [Test]
- public void Can_mix_registration_and_call_site_parameters()
- {
- kernel.Register(
-
Component.For<ClassWithArguments>().LifeStyle.Transient.WithParameters((k, d)
=> d["arg1"] = "foo"));
-
- var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2 });
- Assert.AreEqual(2, component.Arg2);
- Assert.AreEqual("foo", component.Arg1);
- }
-
- [Test]
- public void Can_dynamically_override_services()
- {
- kernel.Register(
- Component.For<ICustomer>()
- .ImplementedBy<CustomerImpl>()
- .Named("defaultCustomer"),
-
Component.For<ICustomer>().ImplementedBy<CustomerImpl2>()
- .Named("sundayCustomer")
- .WithParameters((k, d) =>
- {
- d["name"] = "foo";
- d["address"] = "bar st 13";
- d["age"] = 5;
- }),
- Component.For<CommonImplWithDependancy>()
- .LifeStyle.Transient
- .WithParameters((k, d) =>
- {
- var randomNumber = 2;
- if (randomNumber == 2)
- {
- d["customer"] =
k.Resolve<ICustomer>("sundayCustomer");
- }
- }));
-
- var component =
kernel.Resolve<CommonImplWithDependancy>();
- Assert.IsInstanceOf<CustomerImpl2>(component.Customer);
- }
-
- [Test]
- public void
Should_have_access_to_parameters_passed_from_call_site()
- {
- string arg1 = null;
- int arg2 = 0;
-
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.WithParameters((k,
d) =>
- {
- arg1 = (string)d["arg1"];
- arg2 = (int)d["arg2"];
- }));
- var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
- Assert.AreEqual("foo", arg1);
- Assert.AreEqual(2, arg2);
- }
-
- [Test]
- public void Should_not_require_explicit_registration()
- {
-
kernel.Register(Component.For<CommonSub2Impl>().LifeStyle.Transient.WithParameters((k,
d) => { }));
- Assert.DoesNotThrow(() =>
kernel.Resolve<CommonSub2Impl>());
- }
-
- [Test]
- public void Should_override_parameters_passed_from_call_site()
- {
- string arg1 = "bar";
- int arg2 = 5;
-
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.WithParameters((k,
d) =>
- {
- d["arg1"] = arg1;
- d["arg2"] = arg2;
- }));
- var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
- Assert.AreEqual(arg1, component.Arg1);
- Assert.AreEqual(arg2, component.Arg2);
- }
-
- [Test]
- public void Should_handle_multiple_calls()
- {
- string arg1 = "bar";
- int arg2 = 5;
- kernel.Register(Component.For<ClassWithArguments>()
- .LifeStyle.Transient
- .WithParameters((k, d) =>
- {
- d["arg1"] = arg1;
- })
- .WithParameters((k, d) =>
- {
- d["arg2"] = arg2;
- }));
- var component = kernel.Resolve<ClassWithArguments>(new
{ arg2 = 2, arg1 = "foo" });
- Assert.AreEqual(arg1, component.Arg1);
- Assert.AreEqual(arg2, component.Arg2);
- }
-
- [Test]
- public void
Should_resolve_component_when_no_parameters_passed_from_call_site()
- {
- string arg1 = "bar";
- int arg2 = 5;
-
kernel.Register(Component.For<ClassWithArguments>().LifeStyle.Transient.WithParameters((k,
d) =>
- {
- d["arg1"] = arg1;
- d["arg2"] = arg2;
- }));
- //Assert.DoesNotThrow(() =>
- kernel.Resolve<ClassWithArguments>();//);
- }
- }
-}
File [removed]: WithParametersTestCase.cs
Delta lines: +0 -0
===================================================================
--
You received this message because you are subscribed to the Google Groups
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-commits?hl=en.