Multi-module projects

In a project split into several Gradle modules, each feature can own its dependency-injection module, and the application assembles them.

Running the processor in every Gradle module

KSP only processes the sources of the Gradle module it is applied to. Every Gradle module that declares a @DIModule or a @DIFactory therefore needs the processor, as shown in Setup.

Gradle modules that only use modules declared elsewhere only need inara-runtime.

One module per feature

Each feature exposes a public module interface, describing what it offers to the rest of the application:

feature-users/src/commonMain/kotlin/UsersModule.kt
@DIModule
interface UsersModule {
    fun usersRepository(): UsersRepository
}

fun usersModule(httpClient: HttpClient): UsersModule = UsersModule( (1)
    usersRepository = Binding.single { HttpUsersRepository(httpClient) },
)
1 An optional convenience function, which keeps the feature’s wiring inside the feature.
feature-billing/src/commonMain/kotlin/BillingModule.kt
@DIModule
interface BillingModule {
    fun invoices(): InvoiceService
}

Assembling the application

The application module extends the feature modules, and composes their built instances:

app/src/commonMain/kotlin/AppModule.kt
@DIModule(composed = true)
interface AppModule : UsersModule, BillingModule {
    val appName: String
}

val httpClient = HttpClient()

val app = AppModule(
    usersModule = usersModule(httpClient),
    billingModule = BillingModule(invoices = Binding.single { InvoiceService(httpClient) }),
    appName = Binding.value("My App"),
)

Parent modules generated by Inara keep their bindings as they are, even when they were generated in another Gradle module: a single that was already built in a feature module is not built again in the application module.

An AppModule is a UsersModule and a BillingModule, so it can be handed to any code of either feature.

Sharing a dependency between features

Above, the application passes the same HttpClient to every feature by hand. Instead, each feature can require a module that provides it, and ship its wiring as a part:

network/src/commonMain/kotlin/NetworkModule.kt
@DIModule
interface NetworkModule {
    val httpClient: HttpClient
}
feature-users/src/commonMain/kotlin/UsersModule.kt
@DIModule
interface UsersModule : @DIModule.Required NetworkModule {
    fun usersRepository(): UsersRepository
}

fun usersModule(): UsersModulePart = UsersModulePart(
    usersRepository = Binding.single { HttpUsersRepository(httpClient) }, (1)
)
1 httpClient is read from whichever module composes the part.

BillingModule requires NetworkModule the same way, and ships a billingModule() function.

app/src/commonMain/kotlin/AppModule.kt
@DIModule(composed = true)
interface AppModule : NetworkModule, UsersModule, BillingModule {
    val appName: String
}

val app = AppModule(
    networkModule = NetworkModule(httpClient = Binding.single { HttpClient() }),
    usersModule = usersModule(),
    billingModule = billingModule(),
    appName = Binding.value("My App"),
)

The application provides NetworkModule once, and every feature that requires it uses that one HttpClient.

Publishing a module that extends others

The code generated for a module is based on the modules it extends, as they are when it is compiled. A library that publishes a module extending modules of other libraries must therefore be rebuilt when they change, even when the change looks compatible:

  • a new member is missing from the module’s builders and copies, and calling it fails;

  • the binding of a new member with a default body is ignored, so that its default body always answers;

  • a new set member is missing from the module’s sets;

  • a removed member, or one whose type or parameters changed, breaks the code reading it, and so does a renamed parameter of a member with several parameters;

  • a module that becomes partial, or stops being so, is no longer read the same way;

  • a module compiled by another version of Inara may have generated code of another shape.

Recompiling the application does not fix any of these: only rebuilding the library does. Publish a new version of the library whenever a module it extends changes, and make sure that applications use the versions of these modules it was built against.

Inara records the modules of other libraries that a module was compiled against, and reports one that changed instead of running wrong code.

At startup

The generated code of a module checks, the first time the module is built, that every module of another library it extends is still the one it was compiled against. It throws otherwise:

java.lang.IllegalStateException: 'com.example.business.BusinessModule' was compiled against another version of 'com.example.domain.DomainModule' than the one it runs with: recompile the library declaring 'com.example.business.BusinessModule' against it.

The check fails on any change to the members a module declares, to its @DIModule supertypes, to its composed argument or to the sets it lists, even one that would not break the generated code. A change to the body of a binding, or to a default implementation, never fails it. A module of a library built by another version of Inara is not checked: a composed module reading it fails to compile instead.

At compile time

A composed module reads the generated code of its parents. When a parent comes from a library compiled against other versions of the modules it extends, compiling the composed module fails, with what changed:

'BusinessModule' was compiled against another version of 'DomainModule' (member 'auditLog' added): recompile the library declaring 'BusinessModule' against the current 'DomainModule'.

It also fails when the parent was compiled by another version of Inara, or without Inara.

Keeping wiring private

A module interface that is internal cannot be seen from other Gradle modules, and neither can its builder. Use it for wiring that is an implementation detail of a feature:

@DIModule
internal interface UsersInternalModule {
    fun cache(): UsersCache
}

A @DIFactory object can only be public when every type of its generated signature is public, and a type that is internal to another Gradle module can never be passed through. See the visibility rules of factories.