I got it working the way I wanted too... all tests pass ;-)
It does involve a serious hack though : my own IReleasePolicy which
needs to be aware of the AbstractLifestyleManager.
And because only Track() is virtual in the provided ReleasePolicies, I
had to duplicate some code.
Here's a test demonstrating why I have this issue
[Fact]
public void StrangeSingleCallToRelease()
{
IWindsorContainer container =
new WindsorContainer()
.Register(Component.For<MyComponent>()
.LifeStyle.Custom<SingletonLifestyleManagerSpy>());
var componentOne = container.Resolve<MyComponent>();
var componentTwo = container.Resolve<MyComponent>();
Assert.Equal(componentOne, componentTwo);
Assert.Equal(2, SingletonLifestyleManagerSpy.Resolved);
container.Release(componentOne);
Assert.Equal(1, SingletonLifestyleManagerSpy.Released); // This
does nothing
container.Release(componentTwo);
Assert.Equal(1, SingletonLifestyleManagerSpy.Released); //
Release not called
}
I actually need this second call to LifestyleManager.Release ...
On Jul 7, 8:42 am, kilfour <[email protected]> wrote:
> It does not only limit the reference to a thread.
> For that I could have used the PerThread
> Lifestyle.http://stw.castleproject.org/Windsor.LifeStyles.ashx#PerThread_3.
>
> I can't really call release, as my project does not know about the
> container...
>
> The idea is :
> ComponentA depends on DEP
> ComponenrB deppends on DEP
>
> var a = container.Resolve<ComponentA>();
> var b = container.Resolve<ComponentB>();
> container.Release(a);
> container.Release(b);
>
> Should construct one DEP.
>
> var a = container.Resolve<ComponentA>();
> container.Release(a);
> var b = container.Resolve<ComponentB>();
> container.Release(b);
>
> Should construct two DEP's.
>
> On Jul 7, 4:56 am, Mauricio Scheffer <[email protected]>
> wrote:
>
>
>
> > I think you would have to call Release on the resolved components at the end
> > of the thread. Seehttp://stw.castleproject.org/Windsor.Release-Policy.ashx
>
> > Anyway, can you elaborate on the purpose
> > of OneReferencePerThreadLifestyleManager ? I don't any reason to limit
> > references per thread...
>
> > Cheers,
> > Mauricio
>
> > On Tue, Jul 6, 2010 at 7:25 AM, kilfour <[email protected]> wrote:
> > > I 'm trying to do the following :
>
> > > public class Ref
> > > {
> > > public int RefCount;
> > > public object Target;
> > > }
>
> > > public class OneReferencePerThreadLifestyleManager :
> > > AbstractLifestyleManager
> > > {
> > > private readonly Dictionary<long, Ref> references = new
> > > Dictionary<long, Ref>();
> > > private static long Key() { return
> > > Thread.CurrentThread.ManagedThreadId; }
>
> > > public override object Resolve(CreationContext context)
> > > {
> > > if (!references.ContainsKey(Key()))
> > > references.Add(Key(), new Ref());
> > > Ref reference = references[Key()];
> > > if (reference.RefCount == 0)
> > > reference.Target = base.Resolve(context);
> > > reference.RefCount++;
> > > return reference.Target;
> > > }
>
> > > public override bool Release(object instance)
> > > {
> > > Ref reference = references[Key()];
> > > reference.RefCount--;
> > > if (reference.RefCount == 0)
> > > {
> > > reference.Target = null;
> > > references.Remove(Key());
> > > return base.Release(instance);
> > > }
> > > return true;
> > > }
>
> > > public override void Dispose()
> > > {
> > > references.ForEach(kv =>
> > > Kernel.ReleaseComponent(kv.Value.Target));
> > > }
> > > }
>
> > > Release() however is only called once, even if I resolve more than one
> > > instance...
> > > Any ideas how to get something like this working ?
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "Castle Project Users" group.
> > > To post to this group, send email to [email protected]
> > > .
> > > To unsubscribe from this group, send email to
> > > [email protected]<castle-project-users%2Bun
> > > [email protected]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/castle-project-users?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" 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-users?hl=en.