Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

FlexDiConfiguration

Craig Fowler edited this page Jan 17, 2018 · 1 revision

FlexDi is highly configurable, many of its features may be enabled or disabled when the IContainer instance is created. The recommended way to create a container is via a container builder.

Creating a container

Using a container builder

You would use a container builder like so.

using CSF.FlexDi;

var container = Container.CreateBuilder()
    // Choose your options here
    .Build();

Using new Container()

Although it is the recommended way to create a container, you do not need to use a builder. You may use the Container class' public constructor. This accepts many parameters, including an instance of ContainerOptions. This parameter controls most of the options which would be set up by the builder.

Options available via the builder

Each option is listed by the name of the builder method which sets them. The majority of these methods also has a 'do not' counterpart method which sets the option to false. Except where noted, each of these builder methods corresponds directly to a near-identically-named option upon the ContainerOptions class.

UseNonPublicConstructors

When this option is set to false, when the container is resolving Type registrations it will only consider public constructors upon the class to be instantiated. When it is set to true then the container will consider any non-static constructor, regardless of its protection level.

Default: false

ResolveUnregisteredTypes

When this option is set to false, when the container attempts to resolve a type which has has no registration, it will raise an exception (indicating that all types must be registered first). When set to true, the container will make an attempt to resolve such an object by creating a type registration on-demand.

This is possible only if:

  • The type to be resolved is a non-abstract class
  • It has a constructor 'visible' to the resolver

If both criteria above are satisfied then a Type registration will be immediately created for the class (during resolution) and resolution will continue as normal.

Default: false

UseInstanceCache

When this option is set to false, no instances created by the container are ever cached. Thus two subsequent resolutions which use the same registration would produce two different objects (all objects are created afresh every time they are resolved). If this option is set to true then when an object is resolved, its instance is cached within the current container and subsequent resolutions which would use the same registration will return the same object instance.

This may be further overridden by the Cacheable setting within individual registrations.

Default: true

SelfRegisterAResolver

When this option is set to true, the container will automatically create an instance registration for the service CSF.FlexDi.IResolvesServices, using an object which resolves from that same container. When the option is set to false then no such registration will be created.

Default: true

SelfRegisterTheRegistry

When this option is set to true, the container will automatically create an instance registration for the service CSF.FlexDi.IReceivesRegistrations, using an object which resolves from that same container. When the option is set to false then no such registration will be created.

Default: false

ThrowOnCircularDependencies

When this option is set to true, if a circular dependency is found then an exception will immediately be raised during resolution. When it is false the resolution operation will be allowed to continue indefinitely.

Be very careful if you choose to switch this option off - circular dependencies are likely to cause StackOverflowException in your resolutions and these will not indicate where the circular dependency exists. The advice is to only disable this if you plan to deal with circular dependencies a different way.

Default: true

SupportResolvingNamedInstanceDictionaries

When this option is set to true, it becomes possible to resolve an object of one of the following types:

  • IDictionary<string,TServiceType>
  • IDictionary<TEnum,TServiceType>

In each case, this will resolve a dictionary whose values are all of the available instances of the given service type. The keys are the names under which each instance was registered. The second form uses an enum type instead of a string to name the instances. Each enumeration constant must match (case insensitive) a string name for the registration.

When this option is set to false, this functionality is not available.

Default: false

SupportResolvingLazyInstances

This enables resolution of Lazy<T> instances. This works as illustrated in the following example.

// Presume that IMyService is registered in some manner
var lazyService = container.Resolve<Lazy<IMyService>>();

// The resolution has not yet created the object which
// satisfies IMyService

var serviceImplementation = lazyService.Value;

// Now that the value has been accessed, the resolution
// completes and resolves IMyService as if it had been
// directly resolved at that point.

Lazy resolution is useful where you might need access to a service (which might have a costly/resource-intensive resolution), but you do not wish to place a dependency upon the FlexDi container into your code (in order to resolve it dynamically on-demand).

Default: true

MakeAllResolutionOptional

When this option is enabled, resolution will never fail because the requested service type has not been registered (even if it is an interface). Instead, all failed resolutions will be resolved with null, but considered to have succeeded.

Default: false

UseCustomResolverFactory

This builder method does not correspond to a member of ContainerOptions. This corresponds to the parameter ICreatesResolvers resolverFactory on the constructor of the Container type. When an instance is provided then the default resolver factory: CSF.FlexDi.Resolution.ResolverFactory will not be used to create the resolver used by the container. Instead the custom factory instance (provided via this method) will be used instead.

Default: null (indicating that the default resolver factory should be used)

Clone this wiki locally