Table of Contents

TypeForwardServiceOptions Class

Definition

Namespace
Cuemon.Extensions.DependencyInjection
Assemblies
Cuemon.Extensions.DependencyInjection.dll
Source
src/Cuemon.Extensions.DependencyInjection/TypeForwardServiceOptions.cs

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

Name Description
TypeForwardServiceOptions()

Initializes a new instance of the ServiceOptions class.

Properties

Name Description
NestedTypePredicate

Gets or sets 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.

UseNestedTypeForwarding

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

Methods

Name Description
ValidateOptions()

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

See Also