Singleton for a single call to Resolve method

Oct 26, 2009 at 8:22 PM
Edited Oct 26, 2009 at 8:24 PM

Hi guys.

The main page of your project states that "The Unity Application Block (Unity) is a lightweight extensible dependency injection"

Could you give me a hint how to implement feature described in the subject of this topic?

More detailed: I have following classes

MainView( IView1 v1, IView2 v2, IMainPresenter mp )
MainPresenter( IPresenter1 p1, IPresenter2 p2 )
View1( IPresenter1 p1 )
View2( IPresenter2 p2)


I wish that MainPresenter, View1 and View2 receive the same instance of IPresenter1 and IPresenter2.

But they must not be registered as singletons, I need that new instances of IPresenter1 and 2 be instantiated for each call of container.Resolve<MainView>();

How to implement this feature without changing the source code of Unity?

Thanks in advance


Oct 28, 2009 at 9:40 AM

Is it impossible?


Oct 28, 2009 at 7:35 PM

"Per build singletons" arenlt supported in the current version of Unity. I'm working on an approach to add it to Unity 2.0.

The bes way to do this right now is to use a child container and mark the presenters as singletons in the child container.

Oct 28, 2009 at 8:11 PM


So I have a related question.

Why don't you allow to pass transient policies into Resolve method?

It will allow to write any strategies and solve problems like this and that (
without asking you to change the core any time we need extend your extensible framework.
Also, it will allow you to write extensions like a parameters ovverides using the same manner, without introducing new entities. Won't it?

best regards


Oct 28, 2009 at 10:38 PM

Hi Ed,

We created a lifetime manager uses the System.Runtime.Remoting.Messaging.CallContext storage bucket.  This allows each request in our web application to get a discrete set of shared instances

    public class CallContextLifetimeManager : LifetimeManager, IDisposable
        private static readonly string ContextKey = "xxxxx";

        public override object GetValue()
            return CallContext.GetData( ContextKey );

        public override void RemoveValue()
            CallContext.FreeNamedDataSlot( ContextKey );

        public override void SetValue(object newValue)
            CallContext.SetData( ContextKey, newValue );

        #region IDisposable Members

        public void Dispose()

        protected void Dispose(bool disposing)
            object value = this.GetValue();

            if (value != null)
                if (value is IDisposable)


Oct 28, 2009 at 10:59 PM

We don't take transient policies directly because we didn't want to expose the underlying Objectbuilder goop in the public API. Although I think there may be an approach built on top of the parameter override stuff for Unity 2.0 that could be used for the same purpose. I'll have to examine it a little more to play with the idea.

Although the CallContextLifetimeManager is a pretty neat idea, I must say.


Oct 29, 2009 at 9:24 PM

Many thanks, rslaney