Skip to content

Fluent registration api

Peter Csajtai edited this page May 9, 2019 · 15 revisions

The fluent API allows the following operations:

  • WithName - 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")

  • WithLifetime - 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());
  • WhenHas - Sets an attribute condition for the registration.

    container.Register<IDrow, Drizzt>(config => config.WhenHas<NeutralGoodAttribute>());
  • WhenDependantIs - Sets a parent target condition for the registration.

    container.Register<IWeapon, Twinkle>(config => config.WhenDependantIs<Drizzt>());
  • When - sets a custom user-defined condition for the registration.

    container.Register<IWeapon, Twinkle>(config => config.When(typeInfo => typeInfo.ParentType == typeof(Drizzt)));
  • WithInjectionParameters - Sets injection parameters for the registration.

    container.Register<IDrow, Drizzt>(context => 
      context.WithInjectionParameters(new InjectionParameter { Value = new Icingdeath(), Name = "rightHand" });
  • WithInjectionParameters - Sets an injection parameter for the registration.

    container.Register<IDrow, Drizzt>(context => context.WithInjectionParameter(rightHand, new Icingdeath());
  • WithFactory - Sets a delegate factory for the registration.

    container.Register<IDrow>(context => context.WithFactory(resolver => new Drizzt(resolver.Resolve<IWeapon>()));
  • WithInstance - Sets an existing instance for the registration.

    container.Register<IDrow>(context => context.WithInstance(new Drizzt()));

    By passing true for the wireUp the container will perform member and method injection on the registered instance. context.WithInstance(new Drizzt(), wireUp: true)

  • WithAutoMemberInjection - enables the auto member injection and sets the rule for it.

    container.Register<IDrow>(context => context.WithAutoMemberInjection());
  • WithConstructorSelectionRule - sets the constructor selection rule for the registration.

  • WithConstructorByArgumentTypes - selects a constructor by its argument types, which will be used during the resolution.

  • WithConstructorByArguments - selects a constructor by its arguments for resolution, those arguments will be used to invoke the selected constructor.

  • WithoutDisposalTracking - force disables the disposal tracking on the registration.

  • WithFinalizer - sets a custom cleanup delegate which will be invoked when the scope holding the instance is being disposed.

  • WithInitializer - sets a custom initializer delegate which will be invoked when the given service is being resolved.

  • InjectMember - marks a member (property / field) as a dependency, which should be filled by the container.

  • ReplaceExisting - tells the container that it should replace an existing registration if there is any with the given one.

  • AsImplementedTypes - the service will be mapped to all of its implemented interfaces and base types.

  • AsServiceAlso - binds the currently configured registration to an additional service type.

  • InNamedScope - sets a scope name condition for the registration, it will be used only when a scope with the same name requests it.

  • DefinesScope - this registration would be used as a logical scope for its dependencies, the dependencies registered with the InNamedScope setting with the same name will be preferred during the resolution.

  • WithPerResolutionRequestLifetime - sets the lifetime to ResolutionRequestLifetime. The container will inject this registration in a singleton per resolution request manner.

Example:

container.Register<Drizzt>(context => context
.WithConstructorByArgumentTypes(typeof(Twinkle), typeof(Icingdeath))
.WithName("Drizzt")
.WithoutDisposalTracking()
.WhenHas<InfravisionNeeded>()
.WithLifetime(new DrowLifetime())
.AsImplementedTypes());