Resolving dependency instead of creating instances

Apr 28, 2008 at 3:53 PM
I have use case.

I create instance of Class A.
I register the instance to Unity with RegisterInstance api with Singleton policy.

Now I have Class B that takes Class A as parameter in it's constructor.

I would expect Unity would try to resolve Class A and retrieve previouly created/registered instance and pass it to Class B constructor.
But it tries to instantiate Class A.
I tried registering Type Class A as singleton also. and then RegisterInstance( which I know should not be doing ) but still same result.

Any ideas.

If Class B as dependency Class A and it is already registered as singleton policy it (Unity) should resolve and give it to Class B
Apr 28, 2008 at 6:08 PM
Yes, it should. Can you post some simple sample code that demonstrates the problem? It's probably an issue with the configuration, but it's impossible to tell from your description.
Apr 28, 2008 at 6:20 PM
By the way, this code compiles and runs just fine, and does what you're describing.

    [TestClass]
    public class SingletonFixture
    {
        [TestMethod]
        public void TestMethod1()
        {
            IUnityContainer container = new UnityContainer();
 
            ClassA singleton = new ClassA("Hi there!");
            container.RegisterInstance(singleton);
 
            ClassB result = container.Resolve<ClassB>();
            Assert.AreSame(singleton, result.A);
        }
    }
 
    class ClassA
    {
        public string S { get; set; }
 
        public ClassA(string s)
        {
            this.S = s;
        }
    }
 
    class ClassB
    {
        public ClassA A { get; set; }
 
        public ClassB(ClassA a)
        {
            A = a;
        }
    }
Apr 28, 2008 at 6:20 PM
Including code.

1) Class A : NameServiceConfiguration
namespace UnityExample
{
public class NameServiceConfiguration
{
private string _environment;
public NameServiceConfiguration(string env)
{
_environment = env;
}


public string Environment
{
get { return _environment; }
set { _environment = value; }
}


}
}


1) Class B : Thata uses Class A : MyService

namespace UnityExample
{
public class MyService
{
private NameServiceConfiguration _config;

public MyService(NameServiceConfiguration config)
{
_config = config;
}

}
}


3) Main

namespace UnityExample
{
class Program
{
static void Main(string[] args)
{

UnityContainer unity = new UnityContainer();

NameServiceConfiguration config = new NameServiceConfiguration("QA");
unity.RegisterType<NameServiceConfiguration>(new ContainerControlledLifetimeManager());
unity.RegisterInstance("nameservice", config);



MyService service = unity.Resolve<MyService>();
// This throws Exception : Resolution of the dependency failed, type = "MyService", name = "". See the InnerException for more details.

if(service != null)
{
Trace.WriteLine("success");
}

}
}
}

Apr 28, 2008 at 6:45 PM
Unity makes a distinction between named and unnamed (or default) registrations. It doesn't behave like Windsor, where the first registration becomes the default value.

In the above code, you're registered the NameServiceConfiguration instance with the name "nameservice". Then you try to resolve MyService, which will look for a default registration for NameServiceConfiguration. There is no such default registration, only the named one, so the container tries to new up a new instance.

To fix this, remove the name from your RegisterInstance call (and you can just delete the call to RegisterType completely, the RegisterInstance sets the singleton lifetime manager by default) and it'll work.

Or, if you want to keep the name, you'll need to either put an attribute on MyService to say which named dependency you want or configure that via the API.
Apr 28, 2008 at 6:52 PM
After removing named registration It works now. Thanks . Thanks for the quick help.
Oct 27, 2008 at 8:44 AM
Hi ctavares,

I'm facing a similar situation but i want to keep the name registration.
Can you please tell me how is it done via the API?

Thanks.