Table of Contents

Class TypeForwardServiceOptions

Namespace
Cuemon.Extensions.DependencyInjection
Assembly
Cuemon.Extensions.DependencyInjection.dll

Configuration options for Microsoft Dependency Injection that support nested type forwarding.

public class TypeForwardServiceOptions : ServiceOptions, IValidatableParameterObject, IParameterObject
Inheritance
TypeForwardServiceOptions
Implements
Inherited Members
Extension Methods

Examples

The following example customizes TypeForwardServiceOptions so only one nested contract is forwarded when a concrete implementation is added to the dependency-injection container.

using System;
using Cuemon.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Cuemon.Docs.Samples.DependencyInjection
{
    public static class TypeForwardServiceOptionsExample
    {
        public static void Demonstrate()
        {
            var forwarding = new TypeForwardServiceOptions
            {
                Lifetime = ServiceLifetime.Singleton,
                NestedTypeSelector = type => type.GetInterfaces(),
                NestedTypePredicate = type => type == typeof(IMessageHandler)
            };

            Console.WriteLine(forwarding.UseNestedTypeForwarding);
            forwarding.ValidateOptions();

            var services = new ServiceCollection();
            services.Add<MessageDispatcher>(options =>
            {
                options.Lifetime = forwarding.Lifetime;
                options.UseNestedTypeForwarding = forwarding.UseNestedTypeForwarding;
                options.NestedTypeSelector = forwarding.NestedTypeSelector;
                options.NestedTypePredicate = forwarding.NestedTypePredicate;
            });

            using var provider = services.BuildServiceProvider();

            var dispatcher = provider.GetRequiredService<MessageDispatcher>();
            var handler = provider.GetRequiredService<IMessageHandler>();
            var diagnostics = provider.GetService<IDiagnosticSink>();

            Console.WriteLine(object.ReferenceEquals(dispatcher, handler));
            Console.WriteLine(diagnostics is null);
        }

        public interface IMessageHandler
        {
            void Handle(string message);
        }

        public interface IDiagnosticSink
        {
            void Write(string message);
        }

        public sealed class MessageDispatcher : IMessageHandler, IDiagnosticSink
        {
            public void Handle(string message)
            {
                Console.WriteLine(message);
            }

            public void Write(string message)
            {
                Console.WriteLine(message);
            }
        }
    }
}

Constructors

TypeForwardServiceOptions()

Initializes a new instance of the ServiceOptions class.

public TypeForwardServiceOptions()

Remarks

The following table shows the initial property values for an instance of ServiceOptions.

PropertyInitial Value
NestedTypePredicateserviceType => serviceType.GetInterfaces();
NestedTypeSelector_ => true;

Properties

NestedTypePredicate

Gets or sets the function delegate that will test each element for a condition based on a Type.

public Func<Type, bool> NestedTypePredicate { get; set; }

Property Value

Func<Type, bool>

The function delegate that will test each element for a condition based on a Type.

NestedTypeSelector

Gets or sets the function delegate that will fetch nested types of a service.

public Func<Type, IEnumerable<Type>> NestedTypeSelector { get; set; }

Property Value

Func<Type, IEnumerable<Type>>

The function delegate that will fetch nested types of a service.

UseNestedTypeForwarding

Gets or sets a value indicating whether nested type forwarding should be part of the operation.

public bool UseNestedTypeForwarding { get; set; }

Property Value

bool

true if nested type forwarding should be part of the operation; otherwise, false.

Methods

ValidateOptions()

Determines whether the public read-write properties of this instance are in a valid state.

public void ValidateOptions()

Remarks

This method is expected to throw exceptions when one or more conditions fails to be in a valid state.

Exceptions

InvalidOperationException

NestedTypePredicate cannot be null - or - NestedTypeSelector cannot be null.

See Also