<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Cache-and-Collect Lifecycle Management in Ninject 2.0</title>
	<atom:link href="http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/feed/" rel="self" type="application/rss+xml" />
	<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/</link>
	<description>Rambling and occasional wisdom from Nate Kohari</description>
	<lastBuildDate>Wed, 01 Sep 2010 18:45:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Good read on the life and times of a Ninject object &#124; fzysqr.com</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-2819</link>
		<dc:creator>Good read on the life and times of a Ninject object &#124; fzysqr.com</dc:creator>
		<pubDate>Tue, 31 Aug 2010 13:47:13 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-2819</guid>
		<description>[...] 2.0 IoC implementation yesterday when I came across this great post from the author of himself: Ninject 2.0 Object Lifcycle. The bug I was trying to squash turned out to be completely related to the scope of one of my [...]</description>
		<content:encoded><![CDATA[<p>[...] 2.0 IoC implementation yesterday when I came across this great post from the author of himself: Ninject 2.0 Object Lifcycle. The bug I was trying to squash turned out to be completely related to the scope of one of my [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JP</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-306</link>
		<dc:creator>JP</dc:creator>
		<pubDate>Sat, 20 Jun 2009 01:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-306</guid>
		<description>hey, Nate. You forgot to put some labels on this post.</description>
		<content:encoded><![CDATA[<p>hey, Nate. You forgot to put some labels on this post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Ellis</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-305</link>
		<dc:creator>Ben Ellis</dc:creator>
		<pubDate>Tue, 05 May 2009 08:48:44 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-305</guid>
		<description>http://msdn.microsoft.com/en-us/library/cc713687.aspx

^ Do objects get finalized other than in a Full GC? Could you not use these hooks?</description>
		<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/cc713687.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/cc713687.aspx</a></p>
<p>^ Do objects get finalized other than in a Full GC? Could you not use these hooks?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Owain Cleaver</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-304</link>
		<dc:creator>Owain Cleaver</dc:creator>
		<pubDate>Wed, 11 Mar 2009 17:21:28 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-304</guid>
		<description>I&#039;m not too concerned, it&#039;s just an interesting problem to think about :-).  Looking at the current implementation it&#039;s the locking I have more concern about.  Also the weak reference collection rules, is it specified they are treated as complete non-refs - otherwise the whole idea falls over.

It&#039;s definitely a cool idea, just a difficult one to pull off well given the tools available.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not too concerned, it&#8217;s just an interesting problem to think about :-).  Looking at the current implementation it&#8217;s the locking I have more concern about.  Also the weak reference collection rules, is it specified they are treated as complete non-refs &#8211; otherwise the whole idea falls over.</p>
<p>It&#8217;s definitely a cool idea, just a difficult one to pull off well given the tools available.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Kohari</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-303</link>
		<dc:creator>Nate Kohari</dc:creator>
		<pubDate>Mon, 09 Mar 2009 20:33:51 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-303</guid>
		<description>The problem with the EndRequest event on HttpApplication is that you can&#039;t subscribe to it from outside the HttpApplication itself. That leaves us with the same solution that exists in Ninject 1.x (the OnePerRequestModule).

Honestly, I don&#039;t understand the concern with having a thread that frees the cached items. You don&#039;t mind the garbage collector running, so why would you worry about a thread that watches the GC and prunes the cache? Remember, we&#039;re talking about one thread for the entire container, not one thread per service or anything like that.</description>
		<content:encoded><![CDATA[<p>The problem with the EndRequest event on HttpApplication is that you can&#8217;t subscribe to it from outside the HttpApplication itself. That leaves us with the same solution that exists in Ninject 1.x (the OnePerRequestModule).</p>
<p>Honestly, I don&#8217;t understand the concern with having a thread that frees the cached items. You don&#8217;t mind the garbage collector running, so why would you worry about a thread that watches the GC and prunes the cache? Remember, we&#8217;re talking about one thread for the entire container, not one thread per service or anything like that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Levi</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-302</link>
		<dc:creator>Levi</dc:creator>
		<pubDate>Mon, 09 Mar 2009 20:23:25 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-302</guid>
		<description>@Owain - In most cases, you could use the HttpApplication.EndRequest event to know when the request is complete.  Of course, if the application was using the EndRequest event and needed to access the resources associated with the context lifetime, this wouldn&#039;t work since you can&#039;t ensure the order the handlers are raised.  Also the HttpApplication is a pooled object, so you&#039;d have to be careful not to wire up to the event more than once.

I don&#039;t know of a way to do it with Treads.  I agree with Nate about making deactivation deterministic.  However, the idea of having a dedicated thread devoted to polling the lifetime container seems like a lot of overhead.</description>
		<content:encoded><![CDATA[<p>@Owain &#8211; In most cases, you could use the HttpApplication.EndRequest event to know when the request is complete.  Of course, if the application was using the EndRequest event and needed to access the resources associated with the context lifetime, this wouldn&#8217;t work since you can&#8217;t ensure the order the handlers are raised.  Also the HttpApplication is a pooled object, so you&#8217;d have to be careful not to wire up to the event more than once.</p>
<p>I don&#8217;t know of a way to do it with Treads.  I agree with Nate about making deactivation deterministic.  However, the idea of having a dedicated thread devoted to polling the lifetime container seems like a lot of overhead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Kohari</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-310</link>
		<dc:creator>Nate Kohari</dc:creator>
		<pubDate>Mon, 09 Mar 2009 13:56:04 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-310</guid>
		<description>@Owain: In order to assure that every object is eventually deactivated, the objects have to remain in the cache until the scope object is collected. This is really no different than how the CLR garbage collector works -- objects remain in the heap until the GC recognizes that they can be collected.

Also, if there was any way I could make deactivation deterministic without requiring interaction from client code, I would do it. There&#039;s no event that you can subscribe to on HttpContext or Thread that will let you know when it&#039;s disposed. As far as I can tell, there&#039;s no way to do this effectively without polling on a background thread.</description>
		<content:encoded><![CDATA[<p>@Owain: In order to assure that every object is eventually deactivated, the objects have to remain in the cache until the scope object is collected. This is really no different than how the CLR garbage collector works &#8212; objects remain in the heap until the GC recognizes that they can be collected.</p>
<p>Also, if there was any way I could make deactivation deterministic without requiring interaction from client code, I would do it. There&#8217;s no event that you can subscribe to on HttpContext or Thread that will let you know when it&#8217;s disposed. As far as I can tell, there&#8217;s no way to do this effectively without polling on a background thread.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Owain Cleaver</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-309</link>
		<dc:creator>Owain Cleaver</dc:creator>
		<pubDate>Mon, 09 Mar 2009 13:41:47 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-309</guid>
		<description>Nice idea.

Like Levi i&#039;m not sure i&#039;m so keen on the background thread idea; can&#039;t you just check the scoping reference when an object is requested or am i missing something fundamental here?  It seems more likely folks will go down the deterministic route in most cases anyway, as with your HttpContext example, having objects returned scoped to some previous request until it happens to be collected doesn&#039;t seem too useful to me.

The finalization idea could be interesting too though i wonder how frequent the disposal of these short-lived callback objects would be on a concurrent GC.  Also while i think about it, if you&#039;re using a WeakReference to monitor the scope object, i was always under the impression that weak referenced object collection was contingent on memory constraints, not necessarily &quot;normal&quot; pruning rules - i might be wrong though and i guess this could depend on the implementation.

Finally, maybe for deterministic expiration of (POCO) scope objects you could do something like :-

    public class EventDispatchDisposer : INotifyWhenDisposed
    {
        public EventDispatchDisposer(Action registerEventCallback)
        {
            registerEventCallback(this);
        }

        #region INotifyWhenDisposed ...
    }

then:

    scope = new EventDispatchDisposer(ed =&gt; pcoObj.SomeEvent += (() =&gt; ed.Dispose()));

Not sure which event you could attach to for the HttpContext example using this method.</description>
		<content:encoded><![CDATA[<p>Nice idea.</p>
<p>Like Levi i&#8217;m not sure i&#8217;m so keen on the background thread idea; can&#8217;t you just check the scoping reference when an object is requested or am i missing something fundamental here?  It seems more likely folks will go down the deterministic route in most cases anyway, as with your HttpContext example, having objects returned scoped to some previous request until it happens to be collected doesn&#8217;t seem too useful to me.</p>
<p>The finalization idea could be interesting too though i wonder how frequent the disposal of these short-lived callback objects would be on a concurrent GC.  Also while i think about it, if you&#8217;re using a WeakReference to monitor the scope object, i was always under the impression that weak referenced object collection was contingent on memory constraints, not necessarily &#8220;normal&#8221; pruning rules &#8211; i might be wrong though and i guess this could depend on the implementation.</p>
<p>Finally, maybe for deterministic expiration of (POCO) scope objects you could do something like :-</p>
<p>    public class EventDispatchDisposer : INotifyWhenDisposed<br />
    {<br />
        public EventDispatchDisposer(Action registerEventCallback)<br />
        {<br />
            registerEventCallback(this);<br />
        }</p>
<p>        #region INotifyWhenDisposed &#8230;<br />
    }</p>
<p>then:</p>
<p>    scope = new EventDispatchDisposer(ed =&gt; pcoObj.SomeEvent += (() =&gt; ed.Dispose()));</p>
<p>Not sure which event you could attach to for the HttpContext example using this method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nate Kohari</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-308</link>
		<dc:creator>Nate Kohari</dc:creator>
		<pubDate>Sat, 07 Mar 2009 03:21:39 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-308</guid>
		<description>@Levi: Good points.

Pools could probably be implemented by adding a maximum number of instances per scoping object. I&#039;ve never had that much use for the pooled lifecycle though.

As far as threads go, the cache only uses one thread to watch the garbage collector for *all* activated instances -- not one thread per instance. I do agree that I might be able to implement it using a finalizer though. I&#039;ll have to think about that.</description>
		<content:encoded><![CDATA[<p>@Levi: Good points.</p>
<p>Pools could probably be implemented by adding a maximum number of instances per scoping object. I&#8217;ve never had that much use for the pooled lifecycle though.</p>
<p>As far as threads go, the cache only uses one thread to watch the garbage collector for *all* activated instances &#8212; not one thread per instance. I do agree that I might be able to implement it using a finalizer though. I&#8217;ll have to think about that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Levi</title>
		<link>http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/comment-page-1/#comment-307</link>
		<dc:creator>Levi</dc:creator>
		<pubDate>Sat, 07 Mar 2009 02:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/#comment-307</guid>
		<description>Interesting approach.  I have two questions about it.

1) What about object pools?  For instance, say you want to recycle object instances, instead of creating new instances every time.  There&#039;s not an obvious object to use as scope. You can&#039;t use the pool instance, since that would result in a singleton model.  I suppose you could create a pool of &quot;scope&quot; instances instead of objects.  But managing that seems pretty difficult.  Perhaps pooling would be better managed else where in the framework.

2) Why not use a finalizer?  Using a thread to monitor an instance seems expensive.  Instead, you could create an object which implements a finalizer which notifies the engine when called.  The finalizer is called on a different thread, you&#039;d probably want to use a different thread to prune the scope cache to allow the finalizer thread to continue to execute.  You might also want to create a new &quot;listener&quot; object each time instead of resurrecting the same object.  I&#039;m honestly not sure which is more performance expensive, but it might be worth exploring.</description>
		<content:encoded><![CDATA[<p>Interesting approach.  I have two questions about it.</p>
<p>1) What about object pools?  For instance, say you want to recycle object instances, instead of creating new instances every time.  There&#8217;s not an obvious object to use as scope. You can&#8217;t use the pool instance, since that would result in a singleton model.  I suppose you could create a pool of &#8220;scope&#8221; instances instead of objects.  But managing that seems pretty difficult.  Perhaps pooling would be better managed else where in the framework.</p>
<p>2) Why not use a finalizer?  Using a thread to monitor an instance seems expensive.  Instead, you could create an object which implements a finalizer which notifies the engine when called.  The finalizer is called on a different thread, you&#8217;d probably want to use a different thread to prune the scope cache to allow the finalizer thread to continue to execute.  You might also want to create a new &#8220;listener&#8221; object each time instead of resurrecting the same object.  I&#8217;m honestly not sure which is more performance expensive, but it might be worth exploring.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
