Reputation: 111
I have a service layer which is used by two applications a web application and window service. I am using simple injector for both application.
Window service does not requires all constructor parameters which are required in web application.So I need to pass null value for those constructor parameters.But Simple Injector does not allows me to pass null values.
I am registering the type for null values like this
container.Register<IDataProtectionProvider>(() => null);
but it give me error
The registered delegate for type IDataProtectionProvider returned null.
Any help
Upvotes: 1
Views: 1562
Reputation: 172646
Simple Injector does not allow passing null values into constructors, because constructors are for required dependencies.
Instead of passing null in, you should pass in a Null Object:
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof). It was first published in the Pattern Languages of Program Design book series.
Example:
container.RegisterSingleton<IDataProtectionProvider>(new NullDataProtectionProvider());
Upvotes: 2