I have an interface, implementation, and purpose:
public interface IPerson { public string Name { get; } } public class Person: IPerson { public string Name { get { return "John"; } } } public class Target { public Target(IPerson person) {} }
I use Autofac to bundle things:
builder.RegisterType<Person>().As<IPerson>().SingleInstance();
The problem is that IPerson lives in the general assembly, Person lives in the plugin (which may or may not be there), and Target lives in the main application that loads the plugins. If there are no plugins loaded that implement IPerson , Autofac ballistic about the inability to resolve Target dependencies. And I can't blame him for that.
However, I know that Target is able to handle the absence of IPerson and will be more than happy to get null . In fact, I'm sure all the components that rely on IPerson are ready to take null its place. So, how can I say Autofac: "It's alright, don’t worry, just give me null , okay?"
One way to find the following parameter: Target :
public class Target { public Target(IPerson person = null) {} }
This works, but then I need to do this for all components that require IPerson . Can I also do it the other way around? Somehow say Autofac "If all else fails to resolve IPerson , return null"?
Vilx- source share