简体   繁体   中英

how to pass null value in to constructor in simple injector?

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

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM