dependency-injection

Unable to resolve ILogger from Microsoft.Extensions.Logging

随声附和 提交于 2020-01-12 12:51:36
问题 I've configured my console application's Main like so var services = new ServiceCollection() .AddLogging(logging => logging.AddConsole()) .BuildServiceProvider(); And then I try to use it in another class like so private readonly ILogger _logger; public MyClass(ILogger logger) { _logger = logger; } public void MyFunc() { _logger.Log(LogLevel.Error, "My Message"); } System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' I've tried the

Unable to resolve ILogger from Microsoft.Extensions.Logging

≡放荡痞女 提交于 2020-01-12 12:50:46
问题 I've configured my console application's Main like so var services = new ServiceCollection() .AddLogging(logging => logging.AddConsole()) .BuildServiceProvider(); And then I try to use it in another class like so private readonly ILogger _logger; public MyClass(ILogger logger) { _logger = logger; } public void MyFunc() { _logger.Log(LogLevel.Error, "My Message"); } System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' I've tried the

View dependency injection with dagger 2

会有一股神秘感。 提交于 2020-01-12 07:49:21
问题 I have a custom view extending a TextView . Where should I call my component to inject the view? component.inject(customTextView); 回答1: So, I've find out that I need to add the injection in the constructor of my custom view (in all of them, or make one call the other) Example: public class CustomTextView extends TextView { @Inject AnyProvider anyProvider; public CustomTextView(Context context) { this(context, null); } public CustomTextView(Context AttributeSet attrs) { super(context, attrs);

View dependency injection with dagger 2

主宰稳场 提交于 2020-01-12 07:49:12
问题 I have a custom view extending a TextView . Where should I call my component to inject the view? component.inject(customTextView); 回答1: So, I've find out that I need to add the injection in the constructor of my custom view (in all of them, or make one call the other) Example: public class CustomTextView extends TextView { @Inject AnyProvider anyProvider; public CustomTextView(Context context) { this(context, null); } public CustomTextView(Context AttributeSet attrs) { super(context, attrs);

Constructor Injection - Do we inject factories as well?

醉酒当歌 提交于 2020-01-12 07:16:27
问题 After listening to the Clean Code Talks, I came to understand that we should use factories to compose objects. So, for example, if a House has a Door and a Door has a DoorKnob , in HouseFactory we create a new DoorKnob and pass it to the constructor of Door , and then pass that new Door object to the constructor of House . But what about the class that uses the House (say the class name is ABC ) ? It will depend on the HouseFactory , right? So do we pass the HouseFactory in the constructor of

Funq usage in ServiceStack

隐身守侯 提交于 2020-01-12 05:35:20
问题 How can I access Container instance out of controller? I have to use Container.Resolve in my class but how can I access Container instance? Is it singleton? Can I use new Container() or is there any chain like Funq.StaticContainer ? Thanks to Mythz for gist hint, a) or b) or c). I will use Mythz's solution, it is accepted by me but there are concerns for it's pattern ( ServiceLocator Pattern), you can check here for extra info. 回答1: There are a couple of ways to statically reference your

Minimum JARs for Spring 3.0 dependency injection

删除回忆录丶 提交于 2020-01-12 04:40:11
问题 Similarly to this question regarding an earlier Spring version, what are the minimum dependencies required for an application to use Spring 3.0 dependency injection only? The application context will be configured by XML only. Spring depends on a logging framework, so assume I already include these JARs for logging: jcl-over-slf4j.jar logback-classic.jar logback-core.jar slf4j-api.jar 回答1: As stated in another answer, maven is the true path. If; however, you choose to stray, then based on

How to do dependency injection to Action Filter on ASP.NET Web API

扶醉桌前 提交于 2020-01-12 04:06:30
问题 I really get stuck on the approach to do dependency injection into action filter of web api. I have an action filter like this: public class AuthorizationAttribute : ActionFilterAttribute { public IApiKeyRepository Repository { get; set; } private Guid GetApiKey(string customerKey) { return Repository.GetApiKey(customerKey); } public override void OnActionExecuting(HttpActionContext actionContext) { } } I would like to do property injection on the property Repository by using Windsor (but it

How to do dependency injection to Action Filter on ASP.NET Web API

為{幸葍}努か 提交于 2020-01-12 04:06:29
问题 I really get stuck on the approach to do dependency injection into action filter of web api. I have an action filter like this: public class AuthorizationAttribute : ActionFilterAttribute { public IApiKeyRepository Repository { get; set; } private Guid GetApiKey(string customerKey) { return Repository.GetApiKey(customerKey); } public override void OnActionExecuting(HttpActionContext actionContext) { } } I would like to do property injection on the property Repository by using Windsor (but it

Dependency injection for ASP.NET Web API action method parameters

泪湿孤枕 提交于 2020-01-12 03:31:37
问题 I am working on an ASP.NET Web API project in C# for a JSON interface to a mobile application. My idea was to create interfaces for all requests and then only use these interfaces in the Web API code. I ended up with something like this: public interface IApiObject {} public interface IApiResponse<T> : IApiObject where T : IApiObject {} public interface IApiRegistrationRequest : IApiObject {} My controller looks like this: public class MyApiController : ApiController { public IApiResponse