-
Notifications
You must be signed in to change notification settings - Fork 10
Fluent registration api
Peter Csajtai edited this page Nov 2, 2020
·
15 revisions
The fluent API allows the following operations:
-
WithLifetime(ILifetime lifetime) - Sets a custom lifetime for the registration.
container.Register<IDrow, Drizzt>(config => config.WithLifetime(new DrowLifeTime()));
-
WithSingletonLifetime() - Sets a singleton lifetime for the registration.
container.Register<IDrow, Drizzt>(config => config.WithSingletonLifetime());
-
WithScopedLifetime() - Sets a scoped lifetime for the registration.
container.Register<IDrow, Drizzt>(config => config.WithScopedLifetime());
-
WithPerScopedRequestLifetime() - Sets the lifetime to
PerScopedRequestLifetime
. That means this registration will behave like a singleton within every scoped resolution request.container.Register<IDrow, Drizzt>(context => context.WithPerScopedRequestLifetime());
-
WhenHas(Type attributeType) - Sets an attribute condition for the registration.
container.Register<IDrow, Drizzt>(config => config.WhenHas<NeutralGoodAttribute>());
-
WhenDependantIs(Type targetType) - Sets a parent target condition for the registration.
container.Register<IWeapon, Twinkle>(config => config.WhenDependantIs<Drizzt>());
-
When(Func<TypeInformation, bool> resolutionCondition) - sets a custom user-defined condition for the registration.
container.Register<IWeapon, Twinkle>(config => config .When(typeInfo => typeInfo.ParentType == typeof(Drizzt)));
-
WithConstructorSelectionRule(Func<IEnumerable, IEnumerable> rule) - Sets the constructor selection rule for the registration.
Custom constructor selection rules:
container.Register<IDrow, Drizzt>(context => context.WithConstructorSelectionRule());
-
PreferMostParameters - Prefers the constructor which has the longest parameter list.
context.WithConstructorSelectionRule(Rules.ConstructorSelection.PreferMostParameters)
-
PreferLeastParameters - Prefers the constructor which has the shortest parameter list.
context.WithConstructorSelectionRule(Rules.ConstructorSelection.PreferLeastParameters)
-
Custom
context.WithConstructorSelectionRule(constructors => { /* custom constructor sorting logic */ })
-
PreferMostParameters - Prefers the constructor which has the longest parameter list.
-
WithConstructorByArgumentTypes(params Type[] argumentTypes) - Selects a constructor by its argument types, which will be used during the resolution.
container.Register<IDrow, Drizzt>(context => context .WithConstructorByArgumentTypes(typeof(IWeapon)));
-
WithConstructorByArguments(params object[] arguments) - Selects a constructor by its arguments for resolution, those arguments will be used to invoke the selected constructor.
container.Register<IDrow, Drizzt>(context => context .WithConstructorByArguments(new Twinkle()));
-
WithAutoMemberInjection(AutoMemberInjectionRules rule) - Enables the auto member injection and sets the rule for it.
Custom auto member injection rules:
container.Register<IDrow, Drizzt>(context => context.WithAutoMemberInjection());
-
PropertiesWithPublicSetter - With this flag the container will perform auto injection on properties which has a public setter.
context.WithAutoMemberInjection(Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter)
-
PropertiesWithLimitedAccess - With this flag the container will perform auto injection on properties which has a non public setter as well.
context.WithAutoMemberInjection(Rules.AutoMemberInjectionRules.PropertiesWithLimitedAccess)
-
PrivateFields - With this flag the container will perform auto injection on private fields too.
context.WithAutoMemberInjection(Rules.AutoMemberInjectionRules.PrivateFields)
container.Register<IDrow, Drizzt>(context => context .WithAutoMemberInjection(filter: member => member.Type != typeof(IDrow)));
With the filter above, the container will exclude all the class members with the type
IDrow
from auto injection. -
PropertiesWithPublicSetter - With this flag the container will perform auto injection on properties which has a public setter.
-
InjectMember(string memberName, object dependencyName = null) - marks a member (property / field) as a dependency, which should be filled by the container.
container.Register<IDrow, Drizzt>(context => context .InjectMember(drizzt => drizzt.RightHand, "Twinkle"));
The second parameter used to set the name of the dependency.
-
WithInjectionParameters(params KeyValuePair<string, object>[] injectionParameters) - Sets injection parameters for the registration.
container.Register<IDrow, Drizzt>(context => context.WithInjectionParameters( new KeyValuePair<string, object>("rightHand", new Icingdeath()));
-
WithInjectionParameter(string name, object value) - Sets an injection parameter for the registration.
container.Register<IDrow, Drizzt>(context => context .WithInjectionParameter("rightHand", new Icingdeath());
-
WithFactory(Func<IDependencyResolver, object> factory, bool isCompiledLambda = false) - Sets a delegate factory for the registration.
container.Register<IDrow>(context => context .WithFactory(resolver => new Drizzt(resolver.Resolve<IWeapon>()));
You can use a parameterless delegate as well:
.WithFactory(() => new Drizzt());
-
InNamedScope(object scopeName) - Sets a scope name condition for the registration, it will be used only when a scope with the same name requests it.
container.Register<IDrow, Drizzt>(context => context.InNamedScope("CompanionsOfTheHall"));
-
DefinesScope(object scopeName) - This registration would be used as a logical scope for its dependencies, the dependencies registered with the
InNamedScope()
setting with the same name are preferred during the resolution.container.Register<IDrow, Drizzt>(context => context.DefinesScope("DualWield"));
-
InScopeDefinedBy() - Sets a condition for the registration that it will be used only within the scope defined by the given type.
container.Register<IWeapon, Twinkle>(context => context.InScopeDefinedBy<Drizzt>()); container.Register<IDrow, Drizzt>(context => context.DefinesScope());
-
DefinesScope() - This registration would be used as a logical scope for its dependencies, the dependencies bound to the current registration by the
InScopeDefinedBy()
are preferred during the resolution.container.Register<IDrow, Drizzt>(context => context.DefinesScope());
-
WithName(object name) - Sets the name of the registration.
container.Register<IDrow, Drizzt>(config => config.WithName("Drizzt"));
Allows to resolve a service by it's name like:
container.Resolve<Drow>("Drizzt")
-
WithInstance(object instance, bool wireUp = false) - Sets an existing instance for the registration.
container.Register<IDrow>(context => context.WithInstance(new Drizzt()));
By passing true for the
wireUp
parameter, the container will perform member and method injection on the registered instance.context.WithInstance(new Drizzt(), wireUp: true)
-
WithoutDisposalTracking() - Force disables the disposal tracking on the registration.
container.Register<IDrow, Drizzt>(context => context.WithoutDisposalTracking());
-
WithFinalizer(Action finalizer) - Sets a custom cleanup delegate which will be invoked when the scope holding the instance is being disposed.
container.Register<IDrow, Drizzt>(context => context .WithFinalizer(drizzt => drizzt.CallGuenhwyvarBack()));
-
WithInitializer(Action<TService, IDependencyResolver> initializer) - Sets a custom initializer delegate which will be invoked when the given service is being resolved.
container.Register<IDrow, Drizzt>(context => context .WithInitializer(drizzt => drizzt.SummonGuenhwyvar()));
-
ReplaceExisting() - Tells the container that it should replace an existing registration if there is any with the given one.
container.Register<IDrow, Drizzt>(context => context.ReplaceExisting());
-
ReplaceOnlyIfExists() - Tells the container that it should replace an existing registration with the current one, but only if there is an existing registration.
container.Register<IDrow, Drizzt>(context => context.ReplaceOnlyIfExists());
-
AsImplementedTypes() - The service will be mapped to all of its implemented interfaces and base types.
container.Register<IDrow, Drizzt>(context => context.AsImplementedTypes());
-
AsServiceAlso() - Binds the currently configured registration to an additional service type.
container.Register<IDrow, Drizzt>(context => context.AsServiceAlso<ICreature>());
-
WithDependencyBinding(Type dependencyType, object dependencyName) - Binds a constructor or method parameter to a named registration, so the container will perform a named resolution on the bound dependency.
container.Register<IDrow, Drizzt>(context => context.WithDependencyBinding(typeof(IWeapon), "Icingdeath"));
There is also a method which gets the parameter name instead of its type:
WithDependencyBinding(string parameterName, object dependencyName)
- Service registration
- Factory registration
- Assembly registration
- Composition
- Fluent registration api
- Service resolution
- Resolution by attributes
- Conventional resolution
- Delegate resolution
- Conditional resolution
- Multi resolution
- Lifetimes
- Generics
- Generic wrappers
- Decorators
- Resolvers
- Scopes
- Container configuration
- Container diagnostics
- Exceptions