|
| 1 | +/* |
| 2 | +$v=true |
| 3 | +$p=5 |
| 4 | +$d=Root arguments |
| 5 | +$h=Use root arguments when you need to pass state into a specific root. Define them with `RootArg<T>(string argName)` (optionally with tags) and use them like any other dependency. A root that uses at least one root argument becomes a method, and only arguments used in that root's object graph appear in the method signature. Use unique argument names to avoid collisions. |
| 6 | +$h=Root arguments are useful when runtime values belong to one entry point, not to the whole composition. |
| 7 | +$h=>[!NOTE] |
| 8 | +$h=>Actually, root arguments work like normal bindings. The difference is that they bind to the values of the arguments. These values will be injected wherever they are required. |
| 9 | +$h= |
| 10 | +$f=When using root arguments, compilation warnings are emitted if `Resolve` methods are generated because these methods cannot create such roots. Disable `Resolve` via `Hint(Hint.Resolve, "Off")`, or ignore the warnings and accept the risks. |
| 11 | +$r=Shouldly |
| 12 | +$f=Limitations: roots with root arguments become methods and are incompatible with generated `Resolve` methods. |
| 13 | +$f=Common pitfalls: |
| 14 | +$f=- Reusing ambiguous argument names for different concepts. |
| 15 | +$f=- Forgetting to disable or avoid `Resolve` usage in these setups. |
| 16 | +$f=See also: [Composition arguments](composition-arguments.md), [Resolve hint](resolve-hint.md). |
| 17 | +*/ |
| 18 | + |
| 19 | +// ReSharper disable ClassNeverInstantiated.Local |
| 20 | +// ReSharper disable CheckNamespace |
| 21 | +// ReSharper disable UnusedParameter.Local |
| 22 | +// ReSharper disable ArrangeTypeModifiers |
| 23 | + |
| 24 | +namespace Pure.DI.UsageTests.Basics.RootArgumentsScenarioNullable; |
| 25 | + |
| 26 | +using Shouldly; |
| 27 | +using Xunit; |
| 28 | +using static Tag; |
| 29 | + |
| 30 | +// { |
| 31 | +//# using Pure.DI; |
| 32 | +//# using static Pure.DI.Tag; |
| 33 | +// } |
| 34 | + |
| 35 | +public class ScenarioNullable |
| 36 | +{ |
| 37 | + [Fact] |
| 38 | + public void Run() |
| 39 | + { |
| 40 | + // Root arguments make Resolve unusable, so disable Resolve generation |
| 41 | + // Resolve = Off |
| 42 | +// { |
| 43 | + DI.Setup(nameof(Composition)) |
| 44 | + // Disable Resolve methods because root arguments are not compatible |
| 45 | + .Hint(Hint.Resolve, "Off") |
| 46 | + .Bind<IDatabaseServiceNullable>().To<DatabaseServiceNullable>() |
| 47 | + .Bind<IApplicationNullable>().To<ApplicationNullable>() |
| 48 | + |
| 49 | + // Root arguments serve as values passed |
| 50 | + // to the composition root method |
| 51 | + .RootArg<int?>("port") |
| 52 | + .RootArg<string?>("connectionString") |
| 53 | + |
| 54 | + // An argument can be tagged |
| 55 | + // to be injectable by type and this tag |
| 56 | + .RootArg<string>("appName", AppDetail) |
| 57 | + |
| 58 | + // Composition root |
| 59 | + .Root<IApplicationNullable>("CreateApplication"); |
| 60 | + |
| 61 | + var composition = new Composition(); |
| 62 | + |
| 63 | + string? connectionString = null; |
| 64 | + int? port = 8080; |
| 65 | + |
| 66 | + // Creates an application with specific arguments |
| 67 | + var app = composition.CreateApplication( |
| 68 | + appName: "MySuperApp", |
| 69 | + port: port, |
| 70 | + connectionString: connectionString); |
| 71 | + |
| 72 | + app.Name.ShouldBe("MySuperApp"); |
| 73 | + app.Database.Port.ShouldBe(8080); |
| 74 | + app.Database.ConnectionString.ShouldBeNull(); |
| 75 | +// } |
| 76 | + composition.SaveClassDiagram(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// { |
| 81 | +interface IDatabaseServiceNullable |
| 82 | +{ |
| 83 | + int? Port { get; } |
| 84 | + |
| 85 | + string? ConnectionString { get; } |
| 86 | +} |
| 87 | + |
| 88 | +class DatabaseServiceNullable(int? port, string? connectionString) : IDatabaseServiceNullable |
| 89 | +{ |
| 90 | + public int? Port { get; } = port; |
| 91 | + |
| 92 | + public string? ConnectionString { get; } = connectionString; |
| 93 | +} |
| 94 | + |
| 95 | +interface IApplicationNullable |
| 96 | +{ |
| 97 | + string Name { get; } |
| 98 | + |
| 99 | + IDatabaseServiceNullable Database { get; } |
| 100 | +} |
| 101 | + |
| 102 | +class ApplicationNullable( |
| 103 | + [Tag(AppDetail)] string name, |
| 104 | + IDatabaseServiceNullable database) |
| 105 | + : IApplicationNullable |
| 106 | +{ |
| 107 | + public string Name { get; } = name; |
| 108 | + |
| 109 | + public IDatabaseServiceNullable Database { get; } = database; |
| 110 | +} |
| 111 | +// } |
0 commit comments