Dependencyinjection: Feature request: property injection

Created on 22 Oct 2017  路  5Comments  路  Source: aspnet/DependencyInjection

property injection is useful in base class, is there any plan to add this feature?

question

Most helpful comment

Believe me when I say that you should not be using property injection. You are hiding your dependencies and that is a really bad thing to do :) You can use constructor injection in base classes too :)

All 5 comments

We are not planning on adding property injection to Microsoft.Extensions.DependencyInjection or making it a requirement for containers to implement it. You can still use other container implementation that supports it (http://docs.autofac.org/en/latest/register/prop-method-injection.html, http://structuremap.github.io/setter-injection/)

@TimRowe I think, you can to use Scrutor

var serviceCollection = new ServiceCollection();
serviceCollection
    .AddTransient<IFirstService, FirstService>()
    .AddTransient<ISecondService, SecondService>()
    .AddTransient<IMyService, MyService>()
    .Decorate<IMyService>((myService, provider) => {
        myService.SecondService = provider.GetService<ISecondService>();
        return myService;
    });

var serviceProvider = serviceCollection.BuildServiceProvider();
var service = serviceProvider.GetService<IMyService>();

Believe me when I say that you should not be using property injection. You are hiding your dependencies and that is a really bad thing to do :) You can use constructor injection in base classes too :)

@seesharper Tasks are different
Why I can't to use property injection, if I can this class?

    [FindBy(Id = "registrationForm")]
    public class RegistrationForm : Form
    {
        [FindBy(Id = "name")]
        public IInput Name { get; set; }

        [FindBy(Id = "lastName")]
        public IInput LastName { get; set; }

        [FindBy(Id = "login")]
        public IInput Login { get; set; }

        [FindBy(Id = "register")]
        public IButton RegisterButton { get; set; }

        [FindBy(Id = "cancel")]
        public IButton CancelButton { get; set; }
    }

This issue was moved to aspnet/Home#2331

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastienros picture sebastienros  路  4Comments

GrabYourPitchforks picture GrabYourPitchforks  路  14Comments

seesharper picture seesharper  路  9Comments

prasannapattam picture prasannapattam  路  8Comments

jdom picture jdom  路  6Comments