Skip to content

[discussion] bugfix - interface is needed for DI#236 #238

Description

@J0EK3R

I’m currently trying to understand why the issue #236 with Autofac failing to resolve the dependency occurred. I’ve already found the crucial part of the code where the problem originates.

Registering the MouldKingDeviceManager by calling the Extension RegisterDeviceManager

internal class MouldKing : Vendor<MouldKing>
{
...
    protected override void Register(VendorBuilder<MouldKing> builder)
    {
...
        // device manager
        builder.RegisterDeviceManager<MouldKingDeviceManager>()
            .SingleInstance();
    }

The extension is registering the type as IBluetoothLEDeviceManager only not as TManager itself

public static class ContainerBuilderExtensions
{
    /// <summary>
    /// Register <typeparamref name="TManager"/> as implementation of <see cref="IBluetoothLEDeviceManager"/> in order to use it for device discovery
    /// </summary>
    /// <param name="builder">DI container builder</param>
    /// <returns>Registration builder in order to allow additional registration, such as .As<>()</returns>
    public static IRegistrationBuilder<TManager, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterDeviceManager<TManager>(this ContainerBuilder builder)
        where TManager : class, IBluetoothLEDeviceManager
        => builder.RegisterType<TManager>().As<IBluetoothLEDeviceManager>().SingleInstance();

We could improve that by adding the .AsSelf() to the extension

    public static IRegistrationBuilder<TManager, ConcreteReflectionActivatorData, SingleRegistrationStyle> RegisterDeviceManager<TManager>(this ContainerBuilder builder)
        where TManager : class, IBluetoothLEDeviceManager
        => builder.RegisterType<TManager>().AsSelf().As<IBluetoothLEDeviceManager>().SingleInstance();

or the registration of the MouldKingDeviceManager:

        // device manager
        builder.RegisterDeviceManager<MouldKingDeviceManager>()
            .AsSelf()
            .SingleInstance();

then we could revert 5fd13f3


The other thing is why the UnitTests did not uncover the problem...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions