Fabio
Fabio

Reputation: 3120

Unity resolving unmapped types with null

If I don't have the concrete class mapped to the interface, when Unity tries to resolve the type it gives me this error: "The current type, IFoo, is an interface and cannot be constructed. Are you missing a type mapping?".

However, for testing purposes, I'd like Unity to pass null to the interfaces that aren't mapped yet to concrete types.

Any suggestion to make this as the default behavior to "resolve" unmapped interfaces?

Thanks

Upvotes: 3

Views: 1722

Answers (1)

Fabio
Fabio

Reputation: 3120

I could let Unity pass null to my dependencies using the OptionalParameter while configuring the constructor injection.

container.RegisterType<IObject, MyObject>(
    new InjectionConstructor(
        new OptionalParameter<IFoo>()
    )
);

Upvotes: 1

Related Questions