|
| 1 | +#### Generic roots |
| 2 | + |
| 3 | +[](../tests/Pure.DI.UsageTests/Generics/GenericsRootsScenario.cs) |
| 4 | + |
| 5 | + |
| 6 | +```c# |
| 7 | +using Pure.DI; |
| 8 | + |
| 9 | +DI.Setup(nameof(Composition)) |
| 10 | + // This hint indicates to not generate methods such as Resolve |
| 11 | + .Hint(Hint.Resolve, "Off") |
| 12 | + .Bind().To<Dependency<TT>>() |
| 13 | + .Bind().To<Service<TT>>() |
| 14 | + // Creates OtherService manually, |
| 15 | + // just for the sake of example |
| 16 | + .Bind<OtherService<TT>>().To(ctx => |
| 17 | + { |
| 18 | + ctx.Inject(out IDependency<TT> dependency); |
| 19 | + return new OtherService<TT>(dependency); |
| 20 | + }) |
| 21 | + |
| 22 | + // Specifies to define composition roots for all types inherited from IService<TT> |
| 23 | + // available at compile time at the point where the method is called |
| 24 | + .Roots<IService<TT>>("GetMy{type}"); |
| 25 | + |
| 26 | +var composition = new Composition(); |
| 27 | + |
| 28 | +// service = new Service<int>(new Dependency<int>()); |
| 29 | +var service = composition.GetMyService_T<int>(); |
| 30 | + |
| 31 | +// someOtherService = new OtherService<int>(new Dependency<int>()); |
| 32 | +var someOtherService = composition.GetMyOtherService_T<string>(); |
| 33 | + |
| 34 | +interface IDependency<T>; |
| 35 | + |
| 36 | +class Dependency<T> : IDependency<T>; |
| 37 | + |
| 38 | +interface IService<T>; |
| 39 | + |
| 40 | +class Service<T>(IDependency<T> dependency) : IService<T>; |
| 41 | + |
| 42 | +class OtherService<T>(IDependency<T> dependency) : IService<T>; |
| 43 | +``` |
| 44 | + |
| 45 | +<details> |
| 46 | +<summary>Running this code sample locally</summary> |
| 47 | + |
| 48 | +- Make sure you have the [.NET SDK 9.0](https://dotnet.microsoft.com/en-us/download/dotnet/9.0) or later is installed |
| 49 | +```bash |
| 50 | +dotnet --list-sdk |
| 51 | +``` |
| 52 | +- Create a net9.0 (or later) console application |
| 53 | +```bash |
| 54 | +dotnet new console -n Sample |
| 55 | +``` |
| 56 | +- Add reference to NuGet package |
| 57 | + - [Pure.DI](https://www.nuget.org/packages/Pure.DI) |
| 58 | +```bash |
| 59 | +dotnet add package Pure.DI |
| 60 | +``` |
| 61 | +- Copy the example code into the _Program.cs_ file |
| 62 | + |
| 63 | +You are ready to run the example 🚀 |
| 64 | +```bash |
| 65 | +dotnet run |
| 66 | +``` |
| 67 | + |
| 68 | +</details> |
| 69 | + |
| 70 | +The following partial class will be generated: |
| 71 | + |
| 72 | +```c# |
| 73 | +partial class Composition |
| 74 | +{ |
| 75 | + private readonly Composition _root; |
| 76 | + |
| 77 | + [OrdinalAttribute(256)] |
| 78 | + public Composition() |
| 79 | + { |
| 80 | + _root = this; |
| 81 | + } |
| 82 | + |
| 83 | + internal Composition(Composition parentScope) |
| 84 | + { |
| 85 | + _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; |
| 86 | + } |
| 87 | + |
| 88 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 89 | + public OtherService<T3> GetMyOtherService_T<T3>() |
| 90 | + { |
| 91 | + OtherService<T3> transientOtherService0; |
| 92 | + IDependency<T3> localDependency97 = new Dependency<T3>(); |
| 93 | + transientOtherService0 = new OtherService<T3>(localDependency97); |
| 94 | + return transientOtherService0; |
| 95 | + } |
| 96 | + |
| 97 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 98 | + public Service<T3> GetMyService_T<T3>() |
| 99 | + { |
| 100 | + return new Service<T3>(new Dependency<T3>()); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Class diagram: |
| 106 | + |
| 107 | +```mermaid |
| 108 | +--- |
| 109 | + config: |
| 110 | + class: |
| 111 | + hideEmptyMembersBox: true |
| 112 | +--- |
| 113 | +classDiagram |
| 114 | + DependencyᐸT3ᐳ --|> IDependencyᐸT3ᐳ |
| 115 | + Composition ..> OtherServiceᐸT3ᐳ : OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ() |
| 116 | + Composition ..> ServiceᐸT3ᐳ : ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ() |
| 117 | + OtherServiceᐸT3ᐳ *-- DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ |
| 118 | + ServiceᐸT3ᐳ *-- DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ |
| 119 | + namespace Pure.DI.UsageTests.Generics.GenericsRootsScenario { |
| 120 | + class Composition { |
| 121 | + <<partial>> |
| 122 | + +OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ() |
| 123 | + +ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ() |
| 124 | + } |
| 125 | + class DependencyᐸT3ᐳ { |
| 126 | + +Dependency() |
| 127 | + } |
| 128 | + class IDependencyᐸT3ᐳ { |
| 129 | + <<interface>> |
| 130 | + } |
| 131 | + class OtherServiceᐸT3ᐳ { |
| 132 | + } |
| 133 | + class ServiceᐸT3ᐳ { |
| 134 | + } |
| 135 | + } |
| 136 | +``` |
| 137 | + |
0 commit comments