dependency-injection

Dependency injection injecting null when missing registration in Azure functions

青春壹個敷衍的年華 提交于 2020-08-06 12:14:10
问题 I'm getting null injected into my constructor that has a dependency which I forgot to register. In the below example dependency would be null when you forget to register IDepencency in the startup class public class AzureFunction { public AzureFunction(IDepencency dependency) { } } I expected it to work the same as in .net core DI. Is this expected behavior? And can I maybe change a setting to enable throwing exceptions? Edit: After reading HariHaran's answer, I realized it's only happening

Bean with multiple constructors in Java-based Spring configuration

坚强是说给别人听的谎言 提交于 2020-08-04 13:29:01
问题 I am trying to refactor some application to use Spring DI instead of plain java and stuck with the issue. Basically i have a class with several constructors: public MyClass() { this(new A()); } public MyClass(A a) { this(a, new B())); } public MyClass(String string) { this(new A(string)); } public MyClass(A a, B b) { this.a = a; this.c = a.getC(); this.b = b; this.d = b.getD(); } public MyClass(A a, B b, D d) { this.a = a; this.c = a.getC(); this.b = b; this.d = d; } These constructors are

Bean with multiple constructors in Java-based Spring configuration

拥有回忆 提交于 2020-08-04 13:27:06
问题 I am trying to refactor some application to use Spring DI instead of plain java and stuck with the issue. Basically i have a class with several constructors: public MyClass() { this(new A()); } public MyClass(A a) { this(a, new B())); } public MyClass(String string) { this(new A(string)); } public MyClass(A a, B b) { this.a = a; this.c = a.getC(); this.b = b; this.d = b.getD(); } public MyClass(A a, B b, D d) { this.a = a; this.c = a.getC(); this.b = b; this.d = d; } These constructors are

Dependency injection: HttpClient or HttpClientFactory?

Deadly 提交于 2020-08-02 07:36:46
问题 Everywhere I can see three main approaches to create clients (basic, named, typed) in DI, but I have found nowhere if to inject IHttpClientFactory or HttpClient (both possible). Q1 : What is the difference between injecting IHttpClientFactory or HttpClient please? Q2 : And if IHttpClientFactory is injected, should I use factory.CreateClient() for each call? 回答1: Summary HttpClient can only be injected inside Typed clients for other usages, you need IHttpClientFactory In both scenarios, the

Dependency injection: HttpClient or HttpClientFactory?

拈花ヽ惹草 提交于 2020-08-02 07:36:31
问题 Everywhere I can see three main approaches to create clients (basic, named, typed) in DI, but I have found nowhere if to inject IHttpClientFactory or HttpClient (both possible). Q1 : What is the difference between injecting IHttpClientFactory or HttpClient please? Q2 : And if IHttpClientFactory is injected, should I use factory.CreateClient() for each call? 回答1: Summary HttpClient can only be injected inside Typed clients for other usages, you need IHttpClientFactory In both scenarios, the

Injected HttpClient ignores IHttpClientFactory configuration

。_饼干妹妹 提交于 2020-08-02 05:32:43
问题 I've created a custom library which automatically sets up Polly policies for specific services which depend on HttpClient . This is done using the IServiceCollection extension methods and the typed client approach. A simplified example: public static IHttpClientBuilder SetUpFooServiceHttpClient(this IServiceCollection services) { return services .AddHttpClient<FooService>() .AddPolicyHandler(GetRetryPolicy()); } The example service: public class FooService { private readonly HttpClient

Dependency Injection on AuthorizationOptions Requirement in DotNet Core

删除回忆录丶 提交于 2020-08-01 03:57:09
问题 I have a .NET core project and am trying to create a custom policy using AuthorizationOptions as shown in the documentation located here: ASP.NET.Core Authorization - Dependency Injection in requirement handlers The examples show setting up an authorization requirement with 1 parameter - a simple int value. My custom requirement requires a string parameter as well as a DbContext object. I want to inject the DbContext into the requirement's constructor at runtime. I am using the Autofac

Dependency Injections with Mutable Objects

拈花ヽ惹草 提交于 2020-07-23 03:34:29
问题 Would it be a bad idea to pass anything that could possibly not work in the future to a class? For instance, passing a database connection (or anything that can possibly have it's methods rendered useless) to multiple classes? With JavaScript, these are passed by reference, therefore if the database connection is canceled outside of the class, the object within the classes wouldn't work? Now would this be bad, seeing now that all the models and things which use the database in methods will

Dependency Injections with Mutable Objects

跟風遠走 提交于 2020-07-23 03:34:15
问题 Would it be a bad idea to pass anything that could possibly not work in the future to a class? For instance, passing a database connection (or anything that can possibly have it's methods rendered useless) to multiple classes? With JavaScript, these are passed by reference, therefore if the database connection is canceled outside of the class, the object within the classes wouldn't work? Now would this be bad, seeing now that all the models and things which use the database in methods will

Dependency Injections with Mutable Objects

时光毁灭记忆、已成空白 提交于 2020-07-23 03:32:45
问题 Would it be a bad idea to pass anything that could possibly not work in the future to a class? For instance, passing a database connection (or anything that can possibly have it's methods rendered useless) to multiple classes? With JavaScript, these are passed by reference, therefore if the database connection is canceled outside of the class, the object within the classes wouldn't work? Now would this be bad, seeing now that all the models and things which use the database in methods will