User: xtoff
Date: 2009/12/10 12:46 PM
Added:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Lifecycle/
OnCreateTestCase.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/
OnCreateActionDelegate.cs, OnCreatedStep.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
OnCreateComponentDescriptor.cs
Removed:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Facilities/OnCreate/
OnCreateTestCase.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Facilities/OnCreate/
/InversionOfControl/trunk/src/Castle.MicroKernel/Registration/Facilities/
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:
- refactored OnCreateFacility. The functionality uses now standard commission
lifecycle concern instead of custom facility. The facility is removed, which is
a breaking change. It is however trivial to fix - just remove the call to
container.AddFacility<OnCreateFacility>();
- minor fixes (spaces -> tabs) in ComponentRegistration.cs
File Changes:
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/
============================================================
File [modified]: Castle.MicroKernel-vs2008.csproj
Delta lines: +18 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/OnCreateActionDelegate.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/OnCreateActionDelegate.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -0,0 +1,18 @@
+// 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.LifecycleConcerns
+{
+ public delegate void OnCreateActionDelegate<T>(IKernel kernel, T item);
+}
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel.Tests/
==================================================================
File [modified]: Castle.MicroKernel.Tests-vs2008.csproj
Delta lines: +0 -130
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Facilities/OnCreate/OnCreateTestCase.cs
2009-12-08 18:30:27 UTC (rev 6397)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Facilities/OnCreate/OnCreateTestCase.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -1,131 +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.Facilities.OnCreate
-{
- using MicroKernel.Facilities.OnCreate;
- using MicroKernel.Registration;
- using NUnit.Framework;
-
- [TestFixture]
- public class OnCreateTestCase
- {
-
- #region Setup/Teardown
-
- [SetUp]
- public void SetUp()
- {
- container = new DefaultKernel();
- container.AddFacility<OnCreateFacility>();
- }
-
- #endregion
-
- private IKernel container;
-
- [Test]
- public void CanModify_when_singleton()
- {
-
container.Register(Component.For<IService>().ImplementedBy<MyService>()
-
.OnCreate((kernel, instance) => instance.Name+="a"));
- var service = container.Resolve<IService>();
- Assert.That(service.Name, Is.EqualTo("a"));
- service = container.Resolve<IService>();
- Assert.That(service.Name, Is.EqualTo("a"));
- }
- [Test]
- public void CanModify_when_singleton_multiple_ordered()
- {
-
container.Register(Component.For<IService>().ImplementedBy<MyService>()
- .OnCreate((kernel, instance) =>
instance.Name+="a",
- (kernel, instance) =>
instance.Name+="b"));
- var service = container.Resolve<IService>();
- Assert.That(service.Name, Is.EqualTo("ab"));
- service = container.Resolve<IService>();
- Assert.That(service.Name, Is.EqualTo("ab"));
- }
-
- [Test]
- public void CanModify_when_transient()
- {
- MyService2.staticname = "";
-
container.Register(Component.For<IService2>().ImplementedBy<MyService2>()
-
.LifeStyle.Transient.OnCreate((kernel, instance) => instance.Name+="a"));
- var service = container.Resolve<IService2>();
- Assert.That(service.Name, Is.EqualTo("a"));
- service = container.Resolve<IService2>();
- Assert.That(service.Name, Is.EqualTo("aa"));
- }
-
- [Test]
- public void CanModify_when_transient_multiple_ordered()
- {
- MyService2.staticname = "";
-
container.Register(Component.For<IService2>().ImplementedBy<MyService2>()
-
.LifeStyle.Transient.OnCreate((kernel, instance) => instance.Name+="a",
-
(kernel, instance) => instance.Name+="b"));
- var service = container.Resolve<IService2>();
- Assert.That(service.Name, Is.EqualTo("ab"));
-
- service = container.Resolve<IService2>();
- Assert.That(service.Name, Is.EqualTo("abab"));
-
- }
- }
-
- public interface IService
- {
- string Name { get; set; }
- }
-
- public class MyService : IService
- {
- public MyService()
- {
- this.Name = "";
- }
- #region IService Members
-
- public string Name { get; set; }
-
- #endregion
- }
-
-
- public interface IService2
- {
- string Name { get; set; }
- }
-
- public class MyService2 : IService2
- {
- static MyService2()
- {
- staticname = "";
- }
-
- public static string staticname;
-
- #region IService2 Members
-
- public string Name
- {
- get { return staticname; }
- set { staticname = value; }
- }
-
- #endregion
- }
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Lifecycle/
============================================================================
File [added]: OnCreateTestCase.cs
Delta lines: +0 -0
===================================================================
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/
==============================================================================
File [added]: OnCreateActionDelegate.cs
Delta lines: +39 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/OnCreatedStep.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/LifecycleConcerns/OnCreatedStep.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -0,0 +1,39 @@
+// 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.LifecycleConcerns
+{
+ using Castle.Core;
+
+ public class OnCreatedStep<S>:ILifecycleConcern
+ {
+ private readonly OnCreateActionDelegate<S>[] actions;
+ private readonly IKernel kernel;
+
+ public OnCreatedStep(OnCreateActionDelegate<S>[] actions,
IKernel kernel)
+ {
+ this.actions = actions;
+ this.kernel = kernel;
+ }
+
+ public void Apply(ComponentModel model, object component)
+ {
+ var item = (S)component;
+ foreach (var action in actions)
+ {
+ action(kernel, item);
+ }
+ }
+ }
+}
File [added]: OnCreatedStep.cs
Delta lines: +120 -121
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/ComponentRegistration.cs
2009-12-08 18:30:27 UTC (rev 6397)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/ComponentRegistration.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -24,8 +24,8 @@
using Castle.Core.Interceptor;
using Castle.MicroKernel;
using Castle.MicroKernel.Facilities.OnCreate;
+ using Castle.MicroKernel.LifecycleConcerns;
using Castle.MicroKernel.Proxy;
- using Facilities;
/// <summary>
/// Delegate to filter component registration.
@@ -35,7 +35,7 @@
/// <returns>true if accepted.</returns>
public delegate bool ComponentFilter(IKernel kernel, ComponentModel
model);
- public delegate T Function<T>();
+ public delegate T Function<T>();
/// <summary>
/// Registration for a single type as a component with the kernel.
@@ -56,7 +56,7 @@
private readonly List<ComponentDescriptor<S>> descriptors;
private ComponentModel componentModel;
private bool registered;
- private readonly List<IRegistration> additionalRegistrations;
+ private readonly List<IRegistration> additionalRegistrations;
/// <summary>
/// Initializes a new instance of the <see
cref="ComponentRegistration{S}"/> class.
@@ -67,24 +67,25 @@
registered = false;
serviceType = typeof(S);
descriptors = new List<ComponentDescriptor<S>>();
- additionalRegistrations = new List<IRegistration>();
+ additionalRegistrations = new List<IRegistration>();
}
/// <summary>
/// Initializes a new instance of the <see
cref="ComponentRegistration{S}"/> class
/// with an existing <see cref="ComponentModel"/>.
/// </summary>
- protected ComponentRegistration(ComponentModel componentModel)
: this()
+ protected ComponentRegistration(ComponentModel componentModel)
+ : this()
{
if (componentModel == null)
{
throw new
ArgumentNullException("componentModel");
}
-
+
this.componentModel = componentModel;
name = componentModel.Name;
serviceType = componentModel.Service;
- implementation = componentModel.Implementation;
+ implementation = componentModel.Implementation;
}
/// <summary>
@@ -110,7 +111,7 @@
public Type ServiceType
{
get { return serviceType; }
- protected set { serviceType = value; }
+ protected set { serviceType = value; }
}
/// <summary>
@@ -134,12 +135,12 @@
{
get { return implementation; }
}
-
+
internal bool IsOverWrite
{
get { return overwrite; }
}
-
+
/// <summary>
/// With the overwrite.
/// </summary>
@@ -166,7 +167,7 @@
String message = String.Format("This component
has " +
"already been assigned name '{0}'",
this.name);
- throw new
ComponentRegistrationException(message);
+ throw new
ComponentRegistrationException(message);
}
this.name = name;
@@ -180,8 +181,8 @@
/// </summary>
/// <typeparam name="C">The type that is the implementation for
the service.</typeparam>
/// <returns></returns>
- public ComponentRegistration<S> ImplementedBy<C>() where C : S
- {
+ public ComponentRegistration<S> ImplementedBy<C>() where C : S
+ {
return ImplementedBy(typeof(C));
}
@@ -198,7 +199,7 @@
{
String message = String.Format("This component
has " +
"already been assigned implementation
{0}", implementation.FullName);
- throw new
ComponentRegistrationException(message);
+ throw new
ComponentRegistrationException(message);
}
implementation = type;
@@ -353,7 +354,7 @@
/// <returns></returns>
public ComponentRegistration<S> DependsOn(params Property[]
dependencies)
{
- return AddDescriptor(new
CustomDependencyDescriptor<S>(dependencies));
+ return AddDescriptor(new
CustomDependencyDescriptor<S>(dependencies));
}
/// <summary>
@@ -366,7 +367,7 @@
/// <returns></returns>
public ComponentRegistration<S> DependsOn(IDictionary
dependencies)
{
- return AddDescriptor(new
CustomDependencyDescriptor<S>(dependencies));
+ return AddDescriptor(new
CustomDependencyDescriptor<S>(dependencies));
}
/// <summary>
@@ -488,7 +489,7 @@
/// <returns></returns>
public ComponentRegistration<S> Configuration(params Node[]
configNodes)
{
- return AddDescriptor( new
ConfigurationDescriptor<S>(configNodes));
+ return AddDescriptor(new
ConfigurationDescriptor<S>(configNodes));
}
/// <summary>
@@ -530,7 +531,7 @@
/// to the kernel, before registering this component.</remarks>
public ComponentRegistration<S> Startable()
{
- return AddAttributeDescriptor("startable", "true");
+ return AddAttributeDescriptor("startable", "true");
}
/// <summary>
@@ -543,7 +544,7 @@
public ComponentRegistration<S> StartUsingMethod(string
startMethod)
{
return Startable()
- .AddAttributeDescriptor("startMethod",
startMethod);
+ .AddAttributeDescriptor("startMethod",
startMethod);
}
/// <summary>
@@ -561,13 +562,11 @@
/// <summary>
/// Stores a set of <see cref="OnCreateActionDelegate{T}"/>
which will be invoked when the component
- /// is created
+ /// is created and before it's returned from the container.
/// </summary>
- /// <param name="actions">A setof actions</param>
- /// <remarks>Be sure that you first added the <see
cref="OnCreateFacility"/>
- /// to the kernel, before registering this component.</remarks>
+ /// <param name="actions">A set of actions to be executed right
after the component is created and before it's returned from the
container.</param>
public ComponentRegistration<S> OnCreate(params
OnCreateActionDelegate<S>[] actions)
- {
+ {
this.AddDescriptor(new
OnCreateComponentDescriptor<S>(actions));
return this;
}
@@ -629,20 +628,20 @@
IConfiguration configuration =
EnsureComponentConfiguration(kernel);
- foreach(ComponentDescriptor<S> descriptor in
descriptors)
+ foreach (ComponentDescriptor<S> descriptor in
descriptors)
{
descriptor.ApplyToConfiguration(kernel,
configuration);
}
if (componentModel == null &&
isInstanceRegistration == false)
{
- componentModel =
kernel.ComponentModelBuilder.BuildModel(name, serviceType, implementation,
null);
- }
+ componentModel =
kernel.ComponentModelBuilder.BuildModel(name, serviceType, implementation,
null);
+ }
else if (componentModel == null &&
isInstanceRegistration)
{
componentModel = new
ComponentModel(name, serviceType, implementation);
}
-
+
foreach (ComponentDescriptor<S> descriptor in
descriptors)
{
descriptor.ApplyToModel(kernel,
componentModel);
@@ -654,7 +653,7 @@
options.OmitTarget = true;
}
- if ((ifFilter == null || ifFilter(kernel,
componentModel)) &&
+ if ((ifFilter == null || ifFilter(kernel,
componentModel)) &&
(unlessFilter == null ||
!unlessFilter(kernel, componentModel)))
{
kernel.AddCustomComponent(componentModel);
@@ -664,11 +663,11 @@
kernel.RegisterHandlerForwarding(type, name);
}
- foreach (IRegistration r in additionalRegistrations)
- {
- r.Register(kernel);
- }
- }
+ foreach (IRegistration r in
additionalRegistrations)
+ {
+ r.Register(kernel);
+ }
+ }
}
}
@@ -717,7 +716,7 @@
{
if (implementation == null)
{
- implementation = serviceType;
+ implementation = serviceType;
}
if (String.IsNullOrEmpty(name))
@@ -749,40 +748,40 @@
return configuration;
}
- /// <summary>
- /// Uses a factory method to instantiate the component.
- /// Requires the <see cref="FactorySupportFacility"/> to be installed.
- /// </summary>
- /// <typeparam name="T">Implementation type</typeparam>
- /// <param name="factoryMethod">Factory method</param>
- /// <returns></returns>
- public ComponentRegistration<S> UsingFactoryMethod<T>(Function<T>
factoryMethod) where T: S
- {
- string factoryName = Guid.NewGuid().ToString();
-
additionalRegistrations.Add(Component.For<GenericFactory<T>>().Named(factoryName)
- .Instance(new GenericFactory<T>(factoryMethod)));
- ConfigureFactoryWithId(factoryName);
- return this;
- }
+ /// <summary>
+ /// Uses a factory method to instantiate the component.
+ /// Requires the <see cref="FactorySupportFacility"/> to be
installed.
+ /// </summary>
+ /// <typeparam name="T">Implementation type</typeparam>
+ /// <param name="factoryMethod">Factory method</param>
+ /// <returns></returns>
+ public ComponentRegistration<S>
UsingFactoryMethod<T>(Function<T> factoryMethod) where T : S
+ {
+ string factoryName = Guid.NewGuid().ToString();
+
additionalRegistrations.Add(Component.For<GenericFactory<T>>().Named(factoryName)
+
.Instance(new GenericFactory<T>(factoryMethod)));
+ ConfigureFactoryWithId(factoryName);
+ return this;
+ }
- /// <summary>
- /// Uses a factory method to instantiate the component.
- /// Requires the <see cref="FactorySupportFacility"/> to be installed.
- /// </summary>
- /// <typeparam name="T">Implementation type</typeparam>
- /// <param name="factoryMethod">Factory method</param>
- /// <returns></returns>
- public ComponentRegistration<S>
UsingFactoryMethod<T>(Converter<IKernel, T> factoryMethod) where T : S
- {
- string factoryName = Guid.NewGuid().ToString();
- string factoryMethodName = Guid.NewGuid().ToString();
-
additionalRegistrations.Add(Component.For<KernelToT<T>>().Named(factoryMethodName)
- .Instance(new KernelToT<T>(factoryMethod)));
-
additionalRegistrations.Add(Component.For<GenericFactoryWithKernel<T>>().Named(factoryName)
-
.ServiceOverrides(ServiceOverride.ForKey("factoryMethod").Eq(factoryMethodName)));
- ConfigureFactoryWithId(factoryName);
- return this;
- }
+ /// <summary>
+ /// Uses a factory method to instantiate the component.
+ /// Requires the <see cref="FactorySupportFacility"/> to be
installed.
+ /// </summary>
+ /// <typeparam name="T">Implementation type</typeparam>
+ /// <param name="factoryMethod">Factory method</param>
+ /// <returns></returns>
+ public ComponentRegistration<S>
UsingFactoryMethod<T>(Converter<IKernel, T> factoryMethod) where T : S
+ {
+ string factoryName = Guid.NewGuid().ToString();
+ string factoryMethodName = Guid.NewGuid().ToString();
+
additionalRegistrations.Add(Component.For<KernelToT<T>>().Named(factoryMethodName)
+ .Instance(new KernelToT<T>(factoryMethod)));
+
additionalRegistrations.Add(Component.For<GenericFactoryWithKernel<T>>().Named(factoryName)
+
.ServiceOverrides(ServiceOverride.ForKey("factoryMethod").Eq(factoryMethodName)));
+ ConfigureFactoryWithId(factoryName);
+ return this;
+ }
/// <summary>
/// Uses a factory method to instantiate the component.
@@ -800,45 +799,45 @@
return this;
}
- private void ConfigureFactoryWithId(string factoryId)
- {
- Configuration(
- Attrib.ForName("factoryId").Eq(factoryId),
- Attrib.ForName("factoryCreate").Eq("Create")
- );
- }
+ private void ConfigureFactoryWithId(string factoryId)
+ {
+ Configuration(
+ Attrib.ForName("factoryId").Eq(factoryId),
+ Attrib.ForName("factoryCreate").Eq("Create")
+ );
+ }
- /// <summary>
- /// Uses a factory to instantiate the component
- /// </summary>
- /// <typeparam name="U">Factory type. This factory has to be
registered in the kernel.</typeparam>
- /// <typeparam name="V">Implementation type.</typeparam>
- /// <param name="factory">Factory invocation</param>
- /// <returns></returns>
- public ComponentRegistration<S> UsingFactory<U, V>(Converter<U, V>
factory) where V : S
- {
- return UsingFactoryMethod(kernel =>
factory.Invoke(kernel.Resolve<U>()));
- }
+ /// <summary>
+ /// Uses a factory to instantiate the component
+ /// </summary>
+ /// <typeparam name="U">Factory type. This factory has to be
registered in the kernel.</typeparam>
+ /// <typeparam name="V">Implementation type.</typeparam>
+ /// <param name="factory">Factory invocation</param>
+ /// <returns></returns>
+ public ComponentRegistration<S> UsingFactory<U, V>(Converter<U,
V> factory) where V : S
+ {
+ return UsingFactoryMethod(kernel =>
factory.Invoke(kernel.Resolve<U>()));
+ }
}
- /// <summary>
- /// Helper wrapper around Converter
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class KernelToT<T>
- {
- private readonly Converter<IKernel, T> fun;
+ /// <summary>
+ /// Helper wrapper around Converter
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public class KernelToT<T>
+ {
+ private readonly Converter<IKernel, T> fun;
- public KernelToT(Converter<IKernel, T> fun)
- {
- this.fun = fun;
- }
+ public KernelToT(Converter<IKernel, T> fun)
+ {
+ this.fun = fun;
+ }
- public T Call(IKernel kernel)
- {
- return fun(kernel);
- }
- }
+ public T Call(IKernel kernel)
+ {
+ return fun(kernel);
+ }
+ }
/// <summary>
/// Helper factory class
@@ -878,26 +877,26 @@
}
}
- /// <summary>
- /// Helper factory class
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class GenericFactoryWithKernel<T>
- {
- private readonly KernelToT<T> factoryMethod;
- private readonly IKernel kernel;
+ /// <summary>
+ /// Helper factory class
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public class GenericFactoryWithKernel<T>
+ {
+ private readonly KernelToT<T> factoryMethod;
+ private readonly IKernel kernel;
- public GenericFactoryWithKernel(KernelToT<T> factoryMethod, IKernel
kernel)
- {
- this.factoryMethod = factoryMethod;
- this.kernel = kernel;
- }
+ public GenericFactoryWithKernel(KernelToT<T> factoryMethod,
IKernel kernel)
+ {
+ this.factoryMethod = factoryMethod;
+ this.kernel = kernel;
+ }
- public T Create()
- {
- return factoryMethod.Call(kernel);
- }
- }
+ public T Create()
+ {
+ return factoryMethod.Call(kernel);
+ }
+ }
#region Nested Type: ComponentRegistration
@@ -937,6 +936,6 @@
return this;
}
}
-
+
#endregion
Directory:
/InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Facilities/OnCreate/
======================================================================================
File [removed]: OnCreateTestCase.cs
Delta lines: +131 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Lifecycle/OnCreateTestCase.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Lifecycle/OnCreateTestCase.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -0,0 +1,132 @@
+// 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.Lifecycle
+{
+ using Castle.MicroKernel.Registration;
+
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class OnCreateTestCase
+ {
+
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ container = new DefaultKernel();
+ }
+
+ #endregion
+
+ private IKernel container;
+
+ [Test]
+ public void CanModify_when_singleton()
+ {
+
container.Register(Component.For<IService>().ImplementedBy<MyService>()
+ .OnCreate((kernel, instance) =>
instance.Name+="a"));
+ var service = container.Resolve<IService>();
+ Assert.That(service.Name, Is.EqualTo("a"));
+ service = container.Resolve<IService>();
+ Assert.That(service.Name, Is.EqualTo("a"));
+ }
+ [Test]
+ public void CanModify_when_singleton_multiple_ordered()
+ {
+
container.Register(Component.For<IService>().ImplementedBy<MyService>()
+ .OnCreate((kernel, instance) =>
instance.Name+="a",
+ (kernel, instance) =>
instance.Name+="b"));
+ var service = container.Resolve<IService>();
+ Assert.That(service.Name, Is.EqualTo("ab"));
+ service = container.Resolve<IService>();
+ Assert.That(service.Name, Is.EqualTo("ab"));
+ }
+
+ [Test]
+ public void CanModify_when_transient()
+ {
+ MyService2.staticname = "";
+
container.Register(Component.For<IService2>().ImplementedBy<MyService2>()
+
.LifeStyle.Transient.OnCreate((kernel, instance) => instance.Name+="a"));
+ var service = container.Resolve<IService2>();
+ Assert.That(service.Name, Is.EqualTo("a"));
+ service = container.Resolve<IService2>();
+ Assert.That(service.Name, Is.EqualTo("aa"));
+ }
+
+ [Test]
+ public void CanModify_when_transient_multiple_ordered()
+ {
+ MyService2.staticname = "";
+
container.Register(Component.For<IService2>().ImplementedBy<MyService2>()
+
.LifeStyle.Transient.OnCreate((kernel, instance) => instance.Name+="a",
+
(kernel, instance) => instance.Name+="b"));
+ var service = container.Resolve<IService2>();
+ Assert.That(service.Name, Is.EqualTo("ab"));
+
+ service = container.Resolve<IService2>();
+ Assert.That(service.Name, Is.EqualTo("abab"));
+
+ }
+ }
+
+ public interface IService
+ {
+ string Name { get; set; }
+ }
+
+ public class MyService : IService
+ {
+ public MyService()
+ {
+ Name = "";
+ }
+ #region IService Members
+
+ public string Name { get; set; }
+
+ #endregion
+ }
+
+
+ public interface IService2
+ {
+ string Name { get; set; }
+ }
+
+ public class MyService2 : IService2
+ {
+ static MyService2()
+ {
+ staticname = "";
+ }
+
+ public static string staticname;
+
+ #region IService2 Members
+
+ public string Name
+ {
+ get { return staticname; }
+ set { staticname = value; }
+ }
+
+ #endregion
+ }
+}
+
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Registration/
=========================================================================
File [modified]: ComponentRegistration.cs
Delta lines: +42 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/OnCreateComponentDescriptor.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Registration/OnCreateComponentDescriptor.cs
2009-12-10 19:46:56 UTC (rev 6398)
@@ -0,0 +1,43 @@
+// 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 Castle.Core;
+ using Castle.MicroKernel.LifecycleConcerns;
+
+ /// <summary>
+ /// Adds the actions to ExtendedProperties.
+ /// </summary>
+ /// <typeparam name="S"></typeparam>
+ public class OnCreateComponentDescriptor<S> : ComponentDescriptor<S>
+ {
+ private readonly OnCreateActionDelegate<S>[] actions;
+
+ public OnCreateComponentDescriptor(OnCreateActionDelegate<S>[]
actions)
+ {
+ this.actions = actions;
+ }
+
+ protected internal override void ApplyToModel(IKernel kernel,
ComponentModel model)
+ {
+ if (actions == null)
+ return;
+
model.LifecycleSteps.AddFirst(LifecycleStepType.Commission, new
OnCreatedStep<S>(actions, kernel));
+ }
+
+ }
+}
+
File [added]: OnCreateComponentDescriptor.cs
Delta lines: +4 -1
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj
2009-12-08 18:30:27 UTC (rev 6397)
+++
InversionOfControl/trunk/src/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj
2009-12-10 19:46:56 UTC (rev 6398)
@@ -200,7 +200,7 @@
<Compile
Include="Facilities\FactorySupport\FactorySupportFluentTestCase.cs" />
<Compile
Include="Facilities\FactorySupport\FactorySupportProgrammaticTestCase.cs" />
<Compile Include="Facilities\FactorySupport\FactorySupportTestCase.cs" />
- <Compile Include="Facilities\OnCreate\OnCreateTestCase.cs" />
+ <Compile Include="Lifecycle\OnCreateTestCase.cs" />
<Compile
Include="Facilities\Startable\Components\StartableChainWithGenerics.cs" />
<Compile
Include="Facilities\Startable\Components\StartableComponentWithCustomDependencies.cs"
/>
<Compile Include="HandlerForwardingTestCase.cs" />
@@ -300,6 +300,9 @@
<ItemGroup>
<Service Include="{B4F97281-0DBD-4835-9ED8-7DFB966E87FF}" />
</ItemGroup>
+ <ItemGroup>
+ <Folder Include="Facilities\OnCreate\" />
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
--
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.