/// <summary> /// Initializes a new instance of the <see cref="T:CSF.FlexDi.Container"/> class. /// </summary> /// <remarks> /// <para> /// All of the parameters to this method are optional. Any which are not provided will be fulfilled using /// default implementations. /// </para> /// <para> /// Of the parameters which might be used, the most useful is likely to be <paramref name="options"/> and /// <paramref name="parentContainer"/>. /// </para> /// </remarks> /// <param name="registry">An optional service registry instance.</param> /// <param name="cache">An optional service cache instance.</param> /// <param name="resolver">An optional resolver instance.</param> /// <param name="disposer">An optional service disposer instance.</param> /// <param name="options">A set of container options.</param> /// <param name="parentContainer">An optional parent container - indicating that this container is the child of another.</param> /// <param name="resolverFactory">An optional resolver factory instance.</param> public Container(IRegistersServices registry = null, ICachesResolvedServiceInstances cache = null, IFulfilsResolutionRequests resolver = null, IDisposesOfResolvedInstances disposer = null, ContainerOptions options = null, IContainer parentContainer = null, ICreatesResolvers resolverFactory = null) { disposedValue = false; this.parentContainer = parentContainer; this.options = GetContainerOptions(options, parentContainer); constructorSelector = new ConstructorWithMostParametersSelector(this.options.UseNonPublicConstructors); this.registry = registry ?? new Registry(); this.cache = cache ?? new ResolvedServiceCache(); this.disposer = disposer ?? new ServiceInstanceDisposer(); this.registryStack = new RegistryStackFactory().CreateRegistryStack(this); this.resolver = resolver ?? GetResolver(resolverFactory); this.resolver.ServiceResolved += InvokeServiceResolved; PerformSelfRegistrations(); }
/// <summary> /// Indicates that the container should be created using a custom implementation of <see cref="ICreatesResolvers"/>. /// </summary> /// <remarks> /// <para> /// This is an advanced customisation, as it allows direct control over the resolution process used by /// FlexDi. This includes the ability to override almost all of the options set by this builder and the /// <see cref="ContainerOptions"/> type. /// </para> /// <para> /// You should look at the existing <see cref="ResolverFactory"/> if you wish to create your own resolver, and /// definitely consider subclassing the existing resolver or at least <see cref="ResolverFactoryBase"/>. /// </para> /// <para> /// Those existing resolver factory types make use of the proxy & chain of responsibility patterns in order /// to perform resolution. Most of the individual <see cref="IResolver"/> implementations proxy other implementations /// and modify the results of the overall operation. /// </para> /// </remarks> /// <returns>The builder instance.</returns> /// <param name="resolverFactory">A custom resolver factory implementation.</param> public ContainerBuilder UseCustomResolverFactory(ICreatesResolvers resolverFactory) { if (resolverFactory == null) { throw new ArgumentNullException(nameof(resolverFactory)); } this.resolverFactory = resolverFactory; return(this); }
IResolver GetResolver(ICreatesResolvers resolverFactory) { var factory = resolverFactory ?? new ResolverFactory(); var output = factory.CreateResolver(this); if (output == null) { var message = String.Format(Resources.ExceptionFormats.ResolverFactoryMustNotReturnNull, nameof(ICreatesResolvers), nameof(IResolver)); throw new ArgumentException(message, nameof(resolverFactory)); } return(output); }