I have a class library with a class like this:
public class GoogleSearch
{
private IGoogleApiHelper _googleApiHelper;
public GoogleSearch(IGoogleApiHelper googleApiHelper)
{
_googleApiHelper = googleApiHelper;
}
//some methods
}
googleApiHelper is a service got from IoC container of the library. Can I create an instance of GoogleSearch class from outside of library and make googleApiHelper service to be taken from IoC container?
googleApiHelper is a service got from IoC container of the library.
Prevent doing this. Libraries should not use a DI Container, only the start-up project should have a reference to the DI Container and should make all the required registration, including the types that are part of your library. For more information, read about the Composition Root .
If you follow the Composition Root pattern, you won't have any problems, since everything is composed in that single place.
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.