Default lifetime manager

Dec 26, 2008 at 5:46 PM
I don't know if this question was asked before, excuse me if it was.

I have 50 classes that needs to be registered to the container. I do not want to use configuration file (xml), and do not want to type all the registration mappings in code. I know that Unity can automagically create objects without any configuration but all those automatically created objects are created with transient lifetime manager. Is there a way to change this behavior? I mean is it possible to change lifetime manager for the container itself? I want Unity to create all instances with per thread lifetime manager without entering any configuration, is it possible?

Thanks.
Dec 28, 2008 at 9:55 AM
Edited Dec 28, 2008 at 9:57 AM
IUnityContainer container = new UnityContainer();
container.RegisterType<IFoo, Foo>(new PerThreadLifetimeManager());
container.Resolve<IFoo>();

For a more detailed discussion, check this out...

Using Lifetime Managers
http://msdn.microsoft.com/en-us/library/cc440953.aspx


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

Dec 29, 2008 at 10:00 AM
Edited Dec 29, 2008 at 10:09 AM
Hi,

Sarah, the method you suggest works only if you resolve your type using the 'IFoo' interface. We have the same problem as you Theyang.

We solved it by enumerating the types in the assembly and registering the good types.
I.E, we want to register all the types that implements the interface "IRepository" with the "MyLifetimeManager" :

foreach(Type type in typeof(CommandRepository).Assembly.GetTypes())      
{
 if (!type.IsAbstract && typeof(IRepository).IsAssignableFrom(type))        
 {
          UnityContext.CurrentContainer.RegisterType(type, new MyLifetimeManager());       
 }
}

The problem is that method is quite slow (using reflection). Happily, in our case, it occurs only once at the startup of our application.
Maybe this can help... If anybody has a better solution, i'll take it !!! Maybe a custom ObjectBuilder policy ?

Regards.

Nicolas Séveno

Dec 29, 2008 at 10:58 AM
Hi,

nseveno thank you for the help. I thought of a similar solution but my problem is, the types I want to register don't inherit from a common interface. So IsAssignableFrom(type) is useless in my case. I think being able to change default lifetime manager is a must, or at least it should be allowed to change it with a unity extension or some other way.

Thanks.
Jan 5, 2009 at 7:06 AM
I don't think this is possible.  The LifeTimeStrategy class checks a non default ILifeTimePolicy and if it doesn't get any, it will create a TransientLifeTimePolicy (LifeTimeStrategy.GetLifeTimePolicy(IBuilderContext context)).  The approach one might think is to create a custom policy but policies are associated to a specific build key so you'll go back again to the argument that you don't want to register all those 50 types.  I come up with this solution, however, I'm not sure if this will be acceptable for you.

public

class MyContainer : UnityContainer
{
    
static List<Type> listOfTypes = new List<Type>();
    
    public override object Resolve(Type t, string name)
    {
        
if (!listOfTypes.Contains(t))
        {
            
this.RegisterType(t, new PerThreadLifetimeManager());
            listOfTypes.Add(t);
        }
        
return base.Resolve(t, name);
    }
}

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jan 6, 2009 at 1:53 AM
That solution might work, but it's pretty hacky.

There's no direct support for setting the container-wide default lifetime manager. However, what you could do is write an extension that sits in the strategy chain before the Lifetime Strategy. It would check
if there's a lifetime manager registered for the current type, and add the per thread manager if there isn't.

Jan 6, 2009 at 6:26 PM
I prefer the solution of the extension instead of overriding the "Resolve" method.
I will post the code when i'ts done if it's simple enough...

Thanks Chris.

Regards.

Nicolas Séveno