Skip to content

Latest commit

 

History

History
65 lines (52 loc) · 1.99 KB

exposed-roots-via-root-arg.md

File metadata and controls

65 lines (52 loc) · 1.99 KB

Exposed roots via root arg

CSharp

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);
  }
}