Skip to main content

Shipping modules from a library

A library often keeps its implementations internal and exposes only interfaces. Its module registers them like any other type, and a container in another assembly imports the module without the library granting InternalsVisibleTo. This page is for library authors: it shows how that works and which rules matter most. The library module rules hold the details.

A minimal example​

The library keeps Kettle internal and declares its module public static partial:

Kettles/KettleModule.cs
public interface IKettle;
internal sealed class Kettle(ITimeSystem time) : IKettle; // stays internal

[Module]
[Singleton<Kettle, IKettle>]
public static partial class KettleModule; // partial, so the generator can add the factory

The application imports the module and resolves IKettle. It never names Kettle:

CoffeeShop/CoffeeShop.cs
[Container]
[Import(typeof(KettleModule))]
[Singleton<RealTimeSystem, ITimeSystem>]
public static partial class CoffeeShop;

The module compiles the registration in its own build: it emits a public factory that constructs Kettle and returns IKettle. The container in the application calls that factory instead of the constructor it cannot reach.

How it works​

  • For everything a container in another assembly can't reach, the module generates code in its own build: a factory for an internal implementation or one without a public constructor, a wrapper for a non-public member or hook, a handle for a service registered under an internal type, and a factory per match of its [Scan].
  • The module keeps these in a nested class named Internals, hidden from IntelliSense, so they stay apart from the module's own members. The name is reserved: a module or container that declares a member named Internals itself is an error (AWT235).
  • A container in another assembly calls the generated code instead of the constructor or member it can't reach, and keeps everything else the registration declares: lifetime, keys, Fallback, hooks, async initialization, disposal, collections and decorators.
  • A container in the module's own assembly, or in one the library grants InternalsVisibleTo, builds what it can reach itself, through the same constructor the factory uses, so moving a module between projects never changes what it registers. The who builds what table lists every case.
Rebuild against each library version

The members a module generates into Internals are public members of the library, but not a stable binary contract:

  • Their signatures follow the internal types, and which of them exist follows the library's internal graph, so renaming an internal service, changing a constructor or taking an internal service in one more place can add, change or remove them.
  • A container compiled against an older version of the library can then fail at run time, for example with a TypeLoadException or MissingMethodException.
  • Rebuild a container against each new version of the library, even when the library's own public API didn't change.
  • A package that ships a container importing a module of another package should depend on exactly the version of that package it was built against.
  • A library built with another Awaiten version than the container is covered in libraries built by another Awaiten version.

Rules​

The library's build checks what is wrong for every importer, so the library author sees these problems, not the consumer:

  • Declare the module public static partial and non-generic (AWT152, AWT194, AWT201). Every class that contains it must be partial too.
  • The constructor of an implementation another assembly can't construct takes public types, or internal services that a module of the library registers (AWT195), each directly or as one of the relationships a handle supports (AWT237).
  • A hook that runs through a wrapper takes public types or internal services of the library (AWT203), and none of them marked [FromKey] or [Inject] (AWT204).
  • An internal async initializer convention runs through a wrapper, so its method takes public types or internal services of the library (AWT234).
  • One implementation is one instance in every container: one registered under several services that no type covers is shared through a handle, and a scan of the module that also matches it is an error (AWT197).
  • An [Inject] property the generated factory can't set the way a container would is an error (AWT200).
  • A scan of the module sweeps the module's own assembly only (AWT202), and a match of a public module registers only through interfaces another assembly can name (AWT196).

The consuming container reports what depends on its own graph or on how the library was built, at its [Import] or at the dependency:

  • a registration, decorator, composite or member the module generated no factory or wrapper for, with the reason the module recorded (AWT230, AWT231, AWT108, AWT109, AWT247),
  • an internal service only a module it didn't import provides (AWT238),
  • a scan match or registration that can't share the instance the module builds (AWT240, AWT241),
  • a closing over an internal type that nothing can provide (AWT253),
  • a collection the module fixed empty although the container registers its elements (AWT252),
  • a scan match it can't construct under SkipUnconstructable (AWT141),
  • a module with no generated code it can read (AWT154).

When the library grants InternalsVisibleTo, some of the library's errors become records that only containers without the grant report; see when the library grants InternalsVisibleTo.

Where to go next​

  • Library module rules for internal implementations, non-public members, services only the module uses and self-compiled scans in detail.
  • Modules for declaring and importing modules, overridable defaults and collections.
  • Scanning for the scan options a module scan shares with a container scan.