Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Resolve a Dependency Injection class

$
0
0

I have a base class that I would like use the IOption pattern (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.0). However if I inject my option class in the constructor then I will need to add a constructor in each derived class (dozens) just to call the base class with the option. That seems like a waste and the opposite of DRY principals, since I'm literally repeating adding a constructor to each class just to call the base class.

private string somevalue;

protected BaseClass(IOptions options){
  this.somevalue = options.value;
}

public MyClass(IOptions options): BaseClass(option){
}

I would prefer to do something like this:

private string somevalue;

protected BaseClass(){
  var container = GetDIContainerScope();
  var options = container.Resolve<IOption>();
  this.somevalue = options.value;
}

Viewing all articles
Browse latest Browse all 9386

Trending Articles