Caliburn.Micro.Autofac icon indicating copy to clipboard operation
Caliburn.Micro.Autofac copied to clipboard

Support Component Lifetimes

Open dbuksbaum opened this issue 13 years ago • 2 comments

From blog comment: http://buksbaum.us/2010/08/20/bootstrapping-caliburn-micro-with-autofac/comment-page-1/#comment-1694

tracstarr August 24th, 2011 at 21:41 Reply | Quote | Edit | #3 What about when I need to use specific lifetimescope within a using from a viewmodel? How can I do this without exposing the container? Or will I just have to?

using (var scope = Container.BeginLifetimeScope(“Commit”)) { …. }

david August 24th, 2011 at 22:53 Reply | Quote | Edit | #4 Good question. I don’t have an answer yet, but I will give it some thought. Can you provide a use case for this?

tracstarr August 25th, 2011 at 07:25 Reply | Quote | Edit | #5 Well, in my case it’s for accessing my data layer services. I use the lifetimescope in this way to make sure I don’t have a constant open connection and instances of service classes open.

What I’ve done since this post was to expose the container property as public, and then in the GetInstance i added:

if (serviceType == this.GetType()) return this

That allows me to use IoC.Get().Container.BeginLifetimeScope

It’s the only thing i’ve come up with so far.

dbuksbaum avatar Aug 25 '11 12:08 dbuksbaum

So i've since changed this. the first thing in the TYpedBootStrapper.GetInstance is now:

if (serviceType == typeof(ILifetimeScope)) { if (string.IsNullOrEmpty(key)) return Container.BeginLifetimeScope(); return Container.BeginLifetimeScope(key);

        }

This then allows me to simply use:

IoC.Get<ILifetimeScope>("key")

to get my scopes.

tracstarr avatar Oct 06 '11 17:10 tracstarr

see http://stackoverflow.com/questions/10400849/how-to-dispose-of-autofac-container-used-with-caliburn-micro for related information

dbuksbaum avatar Jul 10 '12 21:07 dbuksbaum