-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInjectionDeclaration.cs
144 lines (112 loc) · 8.05 KB
/
InjectionDeclaration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace OverallSuccess;
using OverallSuccess.ChildFactory;
using OverallSuccess.ConstructorSelection;
using OverallSuccess.Generics;
using OverallSuccess.InterfaceImplementationBindings;
using OverallSuccess.InterfaceSegregationBindings;
using OverallSuccess.MultipleImplementations;
using OverallSuccess.NewInstance;
using OverallSuccess.Operations;
using OverallSuccess.OptionalInterface;
using OverallSuccess.OverridableNew;
using OverallSuccess.RequiredInterface;
using OverallSuccess.SingleInstancePerFactory;
using OverallSuccess.SingleInstancePerRequest;
using OverallSuccess.TypeResolver;
using OverallSuccessDependency;
using Sundew.Injection;
using Sundew.Injection.Interception;
internal class InjectionDeclaration : IInjectionDeclaration
{
public void Configure(IInjectionBuilder injectionBuilder)
{
// The default method for matching declared parameters
injectionBuilder.RequiredParameterInjection = Inject.ByParameterName;
// Each consumer will have its own parameter generated
injectionBuilder.AddParameter<IInjectedSeparately>(Inject.Separately);
// All consumers of this type will receive the same parameter
injectionBuilder.AddParameter<IInjectedByType>();
// Declares that all properties are eligible as parameters
injectionBuilder.AddParameterProperties<IRequiredParameters>();
injectionBuilder.AddParameter<RequiredParameter>();
// Same as above, but these parameters are optional
injectionBuilder.AddParameterProperties<OptionalParameters>();
// Generic binding for any type of IEnumerable<> to an ImmutableList<>
injectionBuilder.BindGeneric<IGeneric<object>, Generic<object>>(Scope.Auto, () => CreateGeneric<object>(default!));
// Declares parameters for controlling initialization and disposal of the generated factory
injectionBuilder.Bind<IInitializationParameters, IDisposalParameters, ILifecycleParameters, LifecycleParameters>(isInjectable: true);
// Interface to implementation binding, where the instance may be provided by a parameter (see OptionalParameters)
injectionBuilder.Bind<IInjectableByInterface, InjectableByInterface>(isInjectable: true);
// Multiple implementation of the same interface binding, inject IEnumerable<IMultipleImplementationForArray> to consume
injectionBuilder.Bind<IMultipleImplementationForArray, MultipleImplementationForArrayA>();
injectionBuilder.Bind<IMultipleImplementationForArray, MultipleImplementationForArrayB>();
// Multiple implementation of the same interface binding, inject IEnumerable<IMultipleImplementationForEnumerable> to consume
injectionBuilder.Bind<IMultipleImplementationForEnumerable, MultipleImplementationForEnumerableA>();
injectionBuilder.Bind<IMultipleImplementationForEnumerable, MultipleImplementationForEnumerableB>();
// Segregated interface binding as a singleton, that allows new to be overriden in a derived factory
injectionBuilder.Bind<IInterfaceSegregationOverridableNewA, IInterfaceSegregationOverridableNewB, IInterfaceSegregationOverridableNew, InterfaceSegregationOverridableNewImplementation>(Scope.SingleInstancePerFactory(), isNewOverridable: true);
// Singleton bindings
injectionBuilder.Bind<IInterfaceSingleInstancePerFactory, InterfaceSingleInstancePerFactory>(Scope.SingleInstancePerFactory());
injectionBuilder.Bind<ImplementationSingleInstancePerFactory>(Scope.SingleInstancePerFactory());
// Single instance per request binding
injectionBuilder.Bind<IInjectableSingleInstancePerRequest, InjectableSingleInstancePerRequest>(Scope.SingleInstancePerRequest, isInjectable: true);
// Factory selector binding
injectionBuilder.Bind<ISelectFactoryMethod, SelectFactoryMethod>(Scope.Auto, () => CreateFeatureService1(default!, default!, default!, default!));
// Constructor selector binding
injectionBuilder.Bind<ISelectConstructor, SelectConstructor>(Scope.Auto, () => new SelectConstructor(default!, default!, default!));
// Segregated interface binding, that where the instance may be provided as a parameter
// In this case the IInterfaceSegregation interface must implement all previous interfaces, but consumers can inject which ever they see fit
injectionBuilder.Bind<IInterfaceSegregationA, IInterfaceSegregationB, IInterfaceSegregation, InterfaceSegregationImplementation>(Scope.SingleInstancePerRequest, isInjectable: true);
injectionBuilder.Bind<NewInstanceAndDisposable>(isInjectable: true);
injectionBuilder.Bind<OverrideableNewImplementation>(isNewOverridable: true);
// Add an interceptor (Not implemented yet)
injectionBuilder.AddInterceptor<IInterceptor>();
// Intercept all methods except the list ones
injectionBuilder.Intercept<Intercepted>(Methods.Exclude, x => x.Title, x => x.Description, x => x.Link);
// Intercept all methods
injectionBuilder.Intercept<InjectableByInterface>();
injectionBuilder.Bind<IIntercepted, Intercepted>();
// Creates a factory for ConstructedChild
injectionBuilder.ImplementFactory<ConstructedChildFactory, IConstructedChildFactory>(
x => x.Add<ConstructedChild>());
injectionBuilder.Bind<IMultipleImplementationForTypeResolver, MultipleImplementationForTypeResolverA>();
injectionBuilder.Bind<IMultipleImplementationForTypeResolver, MultipleImplementationForTypeResolverB>();
injectionBuilder.Bind<DependencyShared>(Scope.SingleInstancePerFactory());
injectionBuilder.ImplementFactory<MultipleImplementationForTypeResolverFactory, IMultipleImplementationForTypeResolverFactory>(
x => x.Add<IMultipleImplementationForTypeResolver>());
// Binding to a generated factory in another assembly
injectionBuilder.BindFactory<DependencyFactory>();
// Binding to the CreateNewInstance method and ManualMultipleSingletonDependency property of a manual factory
injectionBuilder.BindFactory<ManualMultipleDependencyFactory>(x => x
.Add(x => x.CreateNewInstance())
.Add(x => x.ManualMultipleSingletonDependency));
// Binding to the CreateNewInstance method and ManualMultipleSingletonDependency property of a manual factory
injectionBuilder.BindFactory<ManualDependencyFactory>(x => x.CreateNewInstance());
injectionBuilder.BindFactory<ManualSingletonDependencyFactory>(x => x.ManualSingletonDependency);
// Creates a factory with a create method for each of the added operations
injectionBuilder.ImplementFactory<GeneratedOperationFactory, IGeneratedOperationFactory>(
factories => factories
.Add<IOperation, OperationA>()
.Add<IOperation, OperationB>());
// Creates a factory for ResolveRoot and generates an interface for it as well
injectionBuilder.ImplementFactory<ResolveRootFactory, IResolveRootFactory>(
factories => factories
.Add<IResolveRoot, ResolveRoot>()
.Add<IInterfaceSingleInstancePerFactory>()
.Add<IMultipleImplementationForTypeResolver, MultipleImplementationForTypeResolverC>());
injectionBuilder.ImplementServiceProvider<ServiceProvider>(x => x
.Add<MultipleImplementationForTypeResolverFactory>()
.Add<DependencyFactory>()
.Add<ManualMultipleDependencyFactory>()
.Add<GeneratedOperationFactory>()
.Add<ResolveRootFactory>());
}
internal static Generic<T> CreateGeneric<T>(T defaultItem)
{
return new Generic<T>(defaultItem);
}
internal static SelectFactoryMethod CreateFeatureService1(IInterfaceSingleInstancePerFactory interfaceSingleInstancePerFactory, IInjectableSingleInstancePerRequest injectableSingleInstancePerRequest, IRequiredService requiredService, IInterfaceSegregationB interfaceSegregationB)
{
return new(interfaceSingleInstancePerFactory, injectableSingleInstancePerRequest, requiredService, interfaceSegregationB);
}
}