0

I'm using a Unity container to load up my types and wire up my interfaces.

for example:

   // type registrations
   container
        .RegisterType(typeof(Startup))
        .RegisterType<IUserProfileHelper, UserProfileHelper>()
        .RegisterType<IUserSecurityManager, UserSecurityManager>()
        .RegisterType(typeof(IDog), typeof(Dog))
        .RegisterType(typeof(ICat), typeof(Cat))

However, Cat requires Meow:

public Class Cat
{
    public Cat(Meow meow)
    {
    }
}

Where Meow is a DTO:

public class Meow
{
    public int Becibals {get;set;}
    public bool Scratchy {get;set;}
    //etc
}

If a client populates the class Meow during runtime, how do I inject Meow into my container?

If you believe this is a code smell please let me know an alternative pattern to use.

Please note that during runtime, we are outside the scope of when the types are being registered. So perhaps I would need to change the Cat class to not have a constructor at all?

Julian
  • 33,915
  • 22
  • 119
  • 174
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 1
    http://stackoverflow.com/questions/787001/can-i-pass-constructor-parameters-to-unitys-resolve-method ? Note that without usage sample it is hard to see what you actually trying to achieve. – Alexei Levenkov Sep 28 '16 at 21:12
  • Yes, what you are doing is an anti-pattern. Your [application components should not require runtime data during construction](https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99). – Steven Sep 29 '16 at 06:09
  • @Steven well im not talking about construction, i mean i need it during runtime – Alex Gordon Sep 29 '16 at 13:38
  • Your question states "If a client populates the class Meow during runtime, how do I inject Meow into my container?". In other words, `Meow` is a *runtime* value and you wish to inject it into your `Cat` component. I think my response still applies: don't do this. – Steven Sep 29 '16 at 14:04

0 Answers0