Custom Lifestyle question

5 views
Skip to first unread message

kilfour

unread,
Jul 6, 2010, 6:25:47 AM7/6/10
to Castle Project Users
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 ?

Mauricio Scheffer

unread,
Jul 6, 2010, 10:56:07 PM7/6/10
to castle-pro...@googlegroups.com
I think you would have to call Release on the resolved components at the end of the thread. See http://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


--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


kilfour

unread,
Jul 7, 2010, 2:42:49 AM7/7/10
to Castle Project Users
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 <mauricioschef...@gmail.com>
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
> > castle-project-u...@googlegroups.com<castle-project-users%2Bun subs...@googlegroups.com>
> > .

kilfour

unread,
Jul 7, 2010, 9:36:24 AM7/7/10
to Castle Project Users
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 <kilf...@gmail.com> 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.
Reply all
Reply to author
Forward
0 new messages