Including an Injector
An injector defines a graph of components with service inputs and service outputs. So it would be possible to create a fragment with appropriate provider methods so that the injector can be included in a different injector.
Imagine an injector defined by:
@Injector( inputs = { @Injector.Input( type = FinancialService.class ),
@Injector.Input( type = UserService.class ) } )
public interface LibraryApplication
{
BookCatalog getBookCatalog();
ResearchIndex getResearchIndex();
}
This injector could be exposed using a fragment such as:
@Fragment
public interface LibraryApplicationFragment
{
default LibraryApplication provideLibraryApplication( FinancialService financialService,
UserService userService )
{
return new Sting_LibraryApplication( financialService, userService );
}
default BookCatalog provideBookCatalog( LibraryApplication injector )
{
return injector.getBookCatalog();
}
default ResearchIndex provideResearchIndex( LibraryApplication injector )
{
return injector.getResearchIndex();
}
}
Rather than mechanically translating the injectors inputs and outputs into a fragment, Sting can generate
the fragment for you if you set the @Injector.injectable
parameter to true
. The generated code is of the form:
@Fragment
public interface Sting_LibraryApplication_Provider {
@Nonnull
default LibraryApplication provide(@Nonnull final FinancialService input1,
@Nonnull final UserService input2) {
return new Sting_LibraryApplication(input1, input2);
}
default BookCatalog getBookCatalog(@Nonnull final LibraryApplication injector) {
return injector.getBookCatalog();
}
default ResearchIndex getResearchIndex(@Nonnull final LibraryApplication injector) {
return injector.getResearchIndex();
}
}
The generated fragment could be then included in the @Injector.includes
and/or the @Fragment.includes
parameters. However, the implementation also
uses the "external" framework integration techniques so it is possible to just
include the injector class directly in the includes. This avoids the developer having to remember the name of
the generated fragment.
For example, the following demonstrates it is perfectly acceptable to include an injector
(i.e. LibraryApplication
) in another injectors includes:
@Injector(
includes = { LibraryApplication.class,
FinancialService.class,
UserService.class }
)
public interface AcademyApplication
{
BookCatalog getBookCatalog();
UserService getUserService();
}