|
| 1 | +#### Builder |
| 2 | + |
| 3 | +[](../tests/Pure.DI.UsageTests/Basics/BuilderScenario.cs) |
| 4 | + |
| 5 | +Sometimes you need to build up an existing composition root and inject all of its dependencies, in which case the `Builder` method will be useful, as in the example below: |
| 6 | + |
| 7 | + |
| 8 | +```c# |
| 9 | +using Shouldly; |
| 10 | +using Pure.DI; |
| 11 | + |
| 12 | +DI.Setup(nameof(Composition)) |
| 13 | + .Bind().To(_ => Guid.NewGuid()) |
| 14 | + .Bind().To<Dependency>() |
| 15 | + .Builder<Service>("BuildUpService"); |
| 16 | + |
| 17 | +var composition = new Composition(); |
| 18 | + |
| 19 | +var service = composition.BuildUpService(new Service()); |
| 20 | +service.Id.ShouldNotBe(Guid.Empty); |
| 21 | +service.Dependency.ShouldBeOfType<Dependency>(); |
| 22 | + |
| 23 | +interface IDependency; |
| 24 | + |
| 25 | +class Dependency : IDependency; |
| 26 | + |
| 27 | +interface IService |
| 28 | +{ |
| 29 | + Guid Id { get; } |
| 30 | + |
| 31 | + IDependency? Dependency { get; } |
| 32 | +} |
| 33 | + |
| 34 | +record Service: IService |
| 35 | +{ |
| 36 | + public Guid Id { get; private set; } = Guid.Empty; |
| 37 | + |
| 38 | + // The Dependency attribute specifies to perform an injection |
| 39 | + [Dependency] |
| 40 | + public IDependency? Dependency { get; set; } |
| 41 | + |
| 42 | + [Dependency] |
| 43 | + public void SetId(Guid id) => Id = id; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +<details> |
| 48 | +<summary>Running this code sample locally</summary> |
| 49 | + |
| 50 | +- Make sure you have the [.NET SDK 9.0](https://dotnet.microsoft.com/en-us/download/dotnet/9.0) or later is installed |
| 51 | +```bash |
| 52 | +dotnet --list-sdk |
| 53 | +``` |
| 54 | +- Create a net9.0 (or later) console application |
| 55 | +```bash |
| 56 | +dotnet new console -n Sample |
| 57 | +``` |
| 58 | +- Add references to NuGet packages |
| 59 | + - [Pure.DI](https://www.nuget.org/packages/Pure.DI) |
| 60 | + - [Shouldly](https://www.nuget.org/packages/Shouldly) |
| 61 | +```bash |
| 62 | +dotnet add package Pure.DI |
| 63 | +dotnet add package Shouldly |
| 64 | +``` |
| 65 | +- Copy the example code into the _Program.cs_ file |
| 66 | + |
| 67 | +You are ready to run the example 🚀 |
| 68 | +```bash |
| 69 | +dotnet run |
| 70 | +``` |
| 71 | + |
| 72 | +</details> |
| 73 | + |
| 74 | +The default builder method name is `BuildUp`. The first argument to this method will always be the instance to be built. |
| 75 | + |
| 76 | +The following partial class will be generated: |
| 77 | + |
| 78 | +```c# |
| 79 | +partial class Composition |
| 80 | +{ |
| 81 | + private readonly Composition _root; |
| 82 | + |
| 83 | + [OrdinalAttribute(128)] |
| 84 | + public Composition() |
| 85 | + { |
| 86 | + _root = this; |
| 87 | + } |
| 88 | + |
| 89 | + internal Composition(Composition parentScope) |
| 90 | + { |
| 91 | + _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; |
| 92 | + } |
| 93 | + |
| 94 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 95 | + public Service BuildUpService(Service buildingInstance) |
| 96 | + { |
| 97 | + if (buildingInstance is null) throw new ArgumentNullException(nameof(buildingInstance)); |
| 98 | + Guid transientGuid2 = Guid.NewGuid(); |
| 99 | + Service transientService0; |
| 100 | + Service localBuildingInstance47 = buildingInstance; |
| 101 | + localBuildingInstance47.Dependency = new Dependency(); |
| 102 | + localBuildingInstance47.SetId(transientGuid2); |
| 103 | + transientService0 = localBuildingInstance47; |
| 104 | + return transientService0; |
| 105 | + } |
| 106 | + |
| 107 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 108 | + public T Resolve<T>() |
| 109 | + { |
| 110 | + return Resolver<T>.Value.Resolve(this); |
| 111 | + } |
| 112 | + |
| 113 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 114 | + public T Resolve<T>(object? tag) |
| 115 | + { |
| 116 | + return Resolver<T>.Value.ResolveByTag(this, tag); |
| 117 | + } |
| 118 | + |
| 119 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 120 | + public object Resolve(Type type) |
| 121 | + { |
| 122 | + throw new InvalidOperationException($"{CannotResolveMessage} {OfTypeMessage} {type}."); |
| 123 | + } |
| 124 | + |
| 125 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 126 | + public object Resolve(Type type, object? tag) |
| 127 | + { |
| 128 | + throw new InvalidOperationException($"{CannotResolveMessage} \"{tag}\" {OfTypeMessage} {type}."); |
| 129 | + } |
| 130 | + |
| 131 | + private const string CannotResolveMessage = "Cannot resolve composition root "; |
| 132 | + private const string OfTypeMessage = "of type "; |
| 133 | + |
| 134 | + private class Resolver<T>: IResolver<Composition, T> |
| 135 | + { |
| 136 | + public static IResolver<Composition, T> Value = new Resolver<T>(); |
| 137 | + |
| 138 | + public virtual T Resolve(Composition composite) |
| 139 | + { |
| 140 | + throw new InvalidOperationException($"{CannotResolveMessage}{OfTypeMessage}{typeof(T)}."); |
| 141 | + } |
| 142 | + |
| 143 | + public virtual T ResolveByTag(Composition composite, object tag) |
| 144 | + { |
| 145 | + throw new InvalidOperationException($"{CannotResolveMessage}\"{tag}\" {OfTypeMessage}{typeof(T)}."); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +Class diagram: |
| 152 | + |
| 153 | +```mermaid |
| 154 | +--- |
| 155 | + config: |
| 156 | + class: |
| 157 | + hideEmptyMembersBox: true |
| 158 | +--- |
| 159 | +classDiagram |
| 160 | + Dependency --|> IDependency |
| 161 | + Composition ..> Service : Service BuildUpService(Pure.DI.UsageTests.Basics.BuilderScenario.Service buildingInstance) |
| 162 | + Service *-- Dependency : IDependency |
| 163 | + Service *-- Guid : Guid |
| 164 | + namespace Pure.DI.UsageTests.Basics.BuilderScenario { |
| 165 | + class Composition { |
| 166 | + <<partial>> |
| 167 | + +Service BuildUpService(Pure.DI.UsageTests.Basics.BuilderScenario.Service buildingInstance) |
| 168 | + + T ResolveᐸTᐳ() |
| 169 | + + T ResolveᐸTᐳ(object? tag) |
| 170 | + + object Resolve(Type type) |
| 171 | + + object Resolve(Type type, object? tag) |
| 172 | + } |
| 173 | + class Dependency { |
| 174 | + +Dependency() |
| 175 | + } |
| 176 | + class IDependency { |
| 177 | + <<interface>> |
| 178 | + } |
| 179 | + class Service { |
| 180 | + <<record>> |
| 181 | + } |
| 182 | + } |
| 183 | + namespace System { |
| 184 | + class Guid { |
| 185 | + <<struct>> |
| 186 | + } |
| 187 | + } |
| 188 | +``` |
| 189 | + |
0 commit comments