Skip to content

Fluent registration api

Peter Csajtai edited this page May 21, 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" });
  • WithInjectionParameter - 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, Drizzt>(context => context.WithAutoMemberInjection());

    Custom auto member injection rules:

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

    Member selection filter:

    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.

  • WithConstructorSelectionRule - Sets the constructor selection rule for the registration.

    container.Register<IDrow, Drizzt>(context => context.WithConstructorSelectionRule());

    Custom constructor selection rules:

    • 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 */ })
  • WithConstructorByArgumentTypes - Selects a constructor by its argument types, which will be used during the resolution.

    container.Register<IDrow, Drizzt>(context => context.WithConstructorByArgumentTypes(typeof(IWeapon)));
  • WithConstructorByArguments - 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()));
  • WithoutDisposalTracking - Force disables the disposal tracking on the registration.

    container.Register<IDrow, Drizzt>(context => context.WithoutDisposalTracking());
  • WithFinalizer - 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 - 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()));
  • InjectMember - 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.

  • 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());
  • 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>());
  • InNamedScope - 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 - 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.

    container.Register<IDrow, Drizzt>(context => context.DefinesScope("DualWield"));
  • WithPerResolutionRequestLifetime - Sets the lifetime to ResolutionRequestLifetime. That means this registration will behave like a singleton within every resolution request.

    container.Register<IDrow, Drizzt>(context => context.WithPerResolutionRequestLifetime());