Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed IOC-300 - OnCreate registration does not work for generic component #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Castle.Windsor.Tests/Lifecycle/OnCreateTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ public void CanModify_when_transient_multiple_ordered()

service = container.Resolve<IService2>();
Assert.That(service.Name, Is.EqualTo("abab"));
}

[Test]
public void CanModify_when_component_is_generic()
{
container.Register(Component.For(typeof(GenericService1<>))
.OnCreate((kernel, instance) => ((IWithName)instance).Name = "a"));
var service = container.Resolve<GenericService1<int>>();
Assert.That(service.Name, Is.EqualTo("a"));
}
}

Expand Down Expand Up @@ -127,6 +135,16 @@ public string Name

#endregion
}

public interface IWithName
{
string Name { get; set; }
}

public class GenericService1<T> : IWithName
{
public string Name { get; set; }
}
}


19 changes: 19 additions & 0 deletions src/Castle.Windsor/MicroKernel/Handlers/DefaultGenericHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ namespace Castle.MicroKernel.Handlers
{
using System;
using System.Collections.Generic;
using System.Linq;

using Castle.Core;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.LifecycleConcerns;
using Castle.MicroKernel.Proxy;

/// <summary>
Expand Down Expand Up @@ -138,6 +141,22 @@ private void CloneParentProperties(ComponentModel newModel)
// by a facility already
newModel.Interceptors.AddIfNotInCollection(interceptor);
}

// Inherit the parent handler lifecycle steps
var commissionStepsToAdd = ComponentModel.LifecycleSteps.GetCommissionSteps()
.Except(newModel.LifecycleSteps.GetCommissionSteps())
.ToArray();
foreach (ILifecycleConcern step in commissionStepsToAdd)
{
newModel.LifecycleSteps.Add(LifecycleStepType.Commission, step);
}
var decommissionStepsToAdd = ComponentModel.LifecycleSteps.GetDecommissionSteps()
.Except(newModel.LifecycleSteps.GetDecommissionSteps())
.ToArray();
foreach (ILifecycleConcern step in decommissionStepsToAdd)
{
newModel.LifecycleSteps.Add(LifecycleStepType.Decommission, step);
}
}
}
}