RegisterInstance with dependentObject?

Mar 21, 2011 at 2:08 PM

I am not sure if it is the right place....I am using Unity block for Ioc implementation. I am using registerInstance and thats the way it will be. can't change it. The problem is if we do it what about the dependent objects

public ClientUser(IDataServiceManager dsm)
{
    oDataServiceManager = dsm;
}

that works with registerType but as we have to creat the instance first, i will be using default paramter now

My bootstrapper class contains

        IDataServiceManager dsm = new DataServiceManager();
        IClientUser clientUser = new ClientUser();
        SnapFlowUnityContainer.Instance.RegisterInstance<IDataServiceManager>(dsm);
        SnapFlowUnityContainer.Instance.RegisterInstance<IClientUser>(clientUser);


[ClientUser Class]

 

public class ClientUser : UserServiceBase, IClientUser
  {
    IDataServiceManager _dataServiceManager;

    public ClientUser()
    {        
    }  

    [Dependency]
    private IDataServiceManager DataServiceMgr
    {
        get { return _dataServiceManager; }

        set { _dataServiceManager= value; }

    }


DataServiceMgr is always null when clientUser object is created.. let me show how it is created.

BootStrapper.Register();
IClientUser oIclientUser = SnapFlowUnityContainer.Instance.Resolve<IClientUser>();

It doesn't get resolved... please help me.

Mar 22, 2011 at 1:19 AM

That's the expected behavior.  When you registered an instance without the dependencies, you'll get the same instance without the dependencies when you resolved for it.  To inject dependencies to an existing instance of an object, you can make use of the BuildUp method.  In your case, you can do this:

oIclientUser = SnapFlowUnityContainer.Instance.BuildUp(oIclientUser);

This will inject the IDataServiceManager to your IClientUser object.  The next time you resolve for IClientUser, you'll already obtain an instance with the IDataServiceManager injected since the container maintains a strong reference to objects registered using RegisterInstance.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com