2

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? How to handle that. lets say

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

that works with registerType but as we have to creat the instance first

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

How can this work? our consultant said, use private getters to set the dependencies, let the class handle its dependent object? couldn't understand how to do that?


Update: I need to know where i am wrong, How it will create the object of Dataservicemanger when i create clientUser
BootStrapper

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

Unit Test:

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

ClientUser Class:

public class ClientUser : UserServiceBase, IClientUser
{
    [Dependency]
    private IDataServiceManager DataServiceMgr { get; set; }
}
1
  • Is there an overload of the ClientUser constructor you're not showing us? The second code sample seems to suggest that - otherwise it wouldn't compile... Mar 21, 2011 at 10:45

4 Answers 4

3

If I understand you correctly this should work

SnapFlowUnityContainer.Instance.RegisterType<IDataServiceManager, DataServiceManager>();

var clientUser = SnapFlowUnityContainer.Instance.Resolve<ClientUser>();

SnapFlowUnityContainer.Instance.RegisterInstance<IClientUser>(clientUser);

ClientUser will get the DataServiceManager upon Resolve

1

If you want to enable Property injection in Unity, you can put a [Dependency] attribute on the property where you want to enable it. Something like this :

[Dependency]
public IClientUser  ClientUser
{
    get { return _clientUser; }
    set
    {
        if (value == null) throw new ArgumentNullException("value",
            String.Format(Thread.CurrentThread.CurrentUICulture,
            Properties.Resource.ERR_ARGUMENT_NULL_USERSERVICE));

        _clientUser = value;
    }
}

Then, if you use RegisterInstance(clientUser) like in your example, this should work. Another thing is when you need to to wire up objects not created by the container. Then you should use the method BuildUp

Hope this helps,

Thomas

6
  • Is the [Dependency] attribute really necessary? In general Unity supports Property Injection without the [Dependency] attribute, but I don't think I tried it with the combination of the BuildUp method, so it may be required in that specific scenario... Mar 21, 2011 at 10:52
  • @Thomas I have updated my code, my internet is acting up a bit, can't google much. can you help me with the buildup thing, i am not sure where to use it. I have a bootstrapper where i am registering my instances and i have unit test where i want to create a clientUser object. Client user has a dependent object called dataservice
    – Waqas
    Mar 21, 2011 at 12:21
  • @Mark according to MSDN msdn.microsoft.com/en-us/library/ff650198.aspx Notes on Using Property (Setter) Injection it's mandatory Mar 21, 2011 at 13:50
  • @Waqas I looked at your modified example. Why don't you change your bootstrapper code like this : Mar 21, 2011 at 13:58
  • IDataServiceManager dsm = new DataServiceManager(); IClientUser clientUser = new ClientUser(dsm); // unless you need it in other services that _clientUser SnapFlowUnityContainer.Instance.RegisterInstance<IDataServiceManager>(dsm); SnapFlowUnityContainer.Instance.RegisterInstance<IClientUser>(clientUser); Mar 21, 2011 at 14:04
0
public class ClientUser : UserServiceBase, IClientUser   
{
     IDataServiceManager _dataServiceManager;      
     public ClientUser()    
     {             
     }         

     private IDataServiceManager DataServiceMgr     
     {         
          get { 
                _dataServiceManager = SnapFlowUnityContainer.Instance.Resolve<IClientUser>();   
                return _dataServiceManager; 
              }         
       }
}
0

You do not need the [Dependency] attribute on properties, it can be done either via a configuration file or the fluent registration API. There's 2 ways of achieving this with the fluent API, for your scenario you will probably use the first method:

Method 1:

SnapFlowUnityContainer.Instance
  .Configure<InjectedMembers>()
                    .ConfigureInjectionFor<IClientUser>(
                        new InjectionProperty("DataServiceMgr"));

Method 2:

SnapFlowUnityContainer.Instance
  .RegisterType<IClientUser, ClientUser>(
                    new ExternallyControlledLifetimeManager(),
                    new InjectionProperty("DataServiceMgr"))
  .BuildUp<IClientUser>(clientUser);

Be careful of using "SnapFlowUnityContainer.Instance" to register and resolve (service location pattern) types, preferably you need to make sure that registration only occurs once during the application lifetime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.