1

I think inject parameters in constructor is redundant code

private readonly ILogger<PrivacyModel> _log;
        
public PrivacyModel(ILogger<PrivacyModel> log) {
    _log = log;
}

public void OnGet() {
    _log.LogInformation("test");
}

how can I replace with

[inject] private readonly ILogger<PrivacyModel> _log;

public PrivacyModel() {
}

public void OnGet() {
    _log.LogInformation("test");
}
noviceman
  • 11
  • 2
  • How does the `injectAttribute` class work? Does something reflect over it? How does that happen? – Flydog57 Jul 07 '21 at 05:28
  • 2
    Property injection is considered bad for a variety of reasons. Construction and method injection are the new hotness, also you should have bigger things to worry about than the small amount of code saving this would acheive – TheGeneral Jul 07 '21 at 05:30
  • 2
    The former is crystal clear on dependencies - the public contract spells out what it depends on. The latter is not (the dependencies are completely hidden from the caller, making things like writing unit tests harder etc). Which is why people _generally_ prefer the former. Verbose is better than unclear. – mjwills Jul 07 '21 at 05:35
  • Pls, see https://blogs.cuttingedge.it/steven/posts/2014/dependency-injection-in-attributes-dont-do-it/ – Mansur Kurtov Jul 07 '21 at 05:46
  • You cannot set a private member on the outside world of the class. It is really private and C# does not have the concept of friends like C++. You could consider using a third party IOC Container for property injection but please think twice and consider if you really need this. https://stackoverflow.com/a/38459987/1987258 – Daan Jul 07 '21 at 06:25

2 Answers2

1

The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method.

Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

Property Injection: In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.

Method Injection: In this type of injection, the client class implements an interface that declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.

When to use Property Dependency Injection over Constructor Injection and vice versa?

The Constructor Dependency Injection in C# is the standard for dependency injection. It ensures that all the dependency objects are initialized before we are going to invoke any methods or properties of the dependency object, as a result, it avoids the null reference exceptions.

The Setter/Property Dependency Injection in C# is rarely used in real-time applications. For example, if I have a class that has several methods but those methods do not depend on any other objects.

Now I need to create a new method within the same class but that new method now depends on another object. If we use the constructor dependency injection here, then we need to change all the existing constructor calls where we created this class object. This can be a very difficult task if the project is a big one. Hence, in such scenarios, the Setter or Property Dependency Injection can be a good choice.

Matt Qafouri
  • 1,449
  • 2
  • 12
  • 26
0

addition I can do somethink like this if use only cshtml without PageModel class

@page
@inject ILogger<Pages_Privacy> _log
@functions{
    
    public void OnGet()
    {
        _log.LogInformation("test");
    }
}
noviceman
  • 11
  • 2