z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
140 stars 10 forks source link

Auto-injecting of required members - breaking change #156

Closed eValker closed 6 months ago

eValker commented 10 months ago

Hello,

I have upgraded Stashbox to version v5.13.0, and I immediately noticed a breaking change related to the auto-injecting of required members feature. I understand the intention, but I am not sure this is the best approach, especially since this is default behavior.

Let's have a look at the example code below:

using Stashbox;
using Stashbox.Attributes;

var container = new StashboxContainer();
// var container = new StashboxContainer(static op => { op.WithAutoMemberInjection(filter: static _ => false); }); // working fine
// var container = new StashboxContainer(static op => { op.WithAutoMemberInjection(enabled: false); }); // does nothing for this case :)

container.Register<ClassA>();
container.Register<ClassB>();

container.Validate();

var resolver = container.Resolve<ClassA>();

return 0;

public sealed class ClassA
{
    public required int SimpleTypeProperty { get; init; }

    [Dependency]
    public required ClassB ClazzB { get; init; }
}

public sealed class ClassB
{
}

Before 5.13, it was working ok - you have to add an attribute to the property in order to make the container automatically inject it. However, starting with version 5.13, the container is now invalid because of SimpleTypeProperty. While trying to validate the container, I am getting an error: image

I understand that my case is very specific, and most of the users will not be affected. However, I think you should add a way to disable this change (on a global level and also on a type level with an attribute or something).

For now, the only way to disable it is to use the WithAutoMemberInjection setting with a filter specified. Also, disabling it with the enabled option (WithAutoMemberInjection(enabled: false)) is not working; it will still try to auto-inject value into the property.

Valker

z4kn4fein commented 10 months ago

Hi @eValker, thank you for reporting this issue! I'm going to work on a way to let users disable this feature easier.

z4kn4fein commented 10 months ago

Hi @eValker, I've released a new version v5.14.0 where you can disable the required member injection with the .WithRequiredMemberInjection(enabled: false) container or registration configuration option.

Let me know if you have further issues or questions!

github-actions[bot] commented 7 months ago

This issue is marked stale because it has been open for 90 days with no activity.

github-actions[bot] commented 6 months ago

This issue was closed due to no activity.