I don't know specifically about Unity, but the general way to solve this is to use a factory type wherever you would require your type directly. That is, instead of this:
class NeedsAFoo
{
public NeedsAFoo(Foo foo) { ... }
}
class Foo
{
public Foo(string something) { ... }
}
you adapt NeedsAFoo
to accept a IFooFactory
instead of a Foo
directly:
class NeedsAFoo
{
public NeedsAFoo(IFooFactory fooFactory) { ... }
}
interface IFooFactory
{
Foo Create(string something) { ... }
}
This of course only shifts the problem away from NeedsAFoo
to IFooFactory
-- the good news, however, is that DI containers usually have special support for factories. (And even if they didn't, you now have one central place where the new-ing up Foo
objects is encapsulated, namely in the implementation of an IFooFactory
.)
Hopefully, someone more knowledgeable about Unity will provide the details. As a start, maybe some other posts here on SO will help you along, e.g. this one.