Composition roots from other assemblies or projects can be used as a source of bindings passed through root arguments. When you add a binding to a composition from another assembly or project, the roots of the composition with the RootKind.Exposed
type will be used in the bindings automatically. For example, in some assembly a composition is defined as:
public partial class CompositionInOtherProject
{
private static void Setup() =>
DI.Setup()
.Bind().As(Lifetime.Singleton).To<MyDependency>()
.Bind().To<MyService>()
.Root<IMyService>("MyService", kind: RootKinds.Exposed);
}
class Program(IMyService myService)
{
public void DoSomething() => myService.DoSomething();
}
DI.Setup(nameof(Composition))
.Hint(Hint.Resolve, "Off")
// Binds to exposed composition roots from other project
.RootArg<CompositionInOtherProject>("baseComposition")
.Root<Program>("GetProgram");
var baseComposition = new CompositionInOtherProject();
var composition = new Composition();
var program = composition.GetProgram(baseComposition);
program.DoSomething();
The following partial class will be generated:
partial class Composition
{
private readonly Composition _root;
[OrdinalAttribute(10)]
public Composition()
{
_root = this;
}
internal Composition(Composition parentScope)
{
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Program GetProgram(Integration.CompositionInOtherProject baseComposition)
{
Integration.IMyService transientIMyService1;
Integration.CompositionInOtherProject localInstance_1182D1275 = baseComposition;
transientIMyService1 = localInstance_1182D1275.MyService;
return new Program(transientIMyService1);
}
}