dependency-injection

ASP.Net Core Call a controller from another controller

佐手、 提交于 2020-01-22 10:30:52
问题 In my ASP.Net Core MVC 6 solution I have two sets of controllers. One set contains the webpages with their regular views. Another set contains the API controllers. To avoid duplicating db logic the web controllers are using the API controllers. Currently I am creating an instance of the required controller manually by handing it a DbContext as constructor argument. This is the DbContext given to web controller by dependency injection. But whenever I add another constructor parameter to the

Unit testing a class with autowired notation using Junit and EasyMock?

扶醉桌前 提交于 2020-01-22 05:14:31
问题 I am trying to write a Unit test for a class that has several of its fields marked @Autowired. Given the fact that Spring is automatically resolving the concrete implementations for these fields, I am having a hard time figuring out how to plug my Mock objects(created via EasyMock) as the dependencies during the test-run. Using @Autowired in the class means lack of setters in that class. Is there a way for me to plug my mock objects in without creating additional setters in class? Here's an

How DI frameworks resolve dependency for same Interface with multiple configuration?

左心房为你撑大大i 提交于 2020-01-22 02:02:38
问题 Consider below code sample: public interface IMyInterface { void SetName(string name); string GetName(); } public class MyInterfaceImplementor1 : IMyInterface { protected string Name { set; get; } public void SetName(string name) { this.Name = name; } public virtual string GetName() { return this.Name; } } public class MyInterfaceImplementor2 : MyInterfaceImplementor1 { public override string GetName() { return String.Format("Hello! {0}", base.Name); } } And the DI configuration for this: (

Dependency Injection resolution with constructors having multiple parameters whose values are determined at runtime .NET Core 3

北城余情 提交于 2020-01-21 14:03:12
问题 As a reply to an earlier question (Accessing ILogger from non-Controller classes in Class Libary .NET Core 3), we can inject the dependent class into the controller. The DI Container resolves dependencies on registration of Transient services at the time of Startup. Hence if a ClassA is instantiated by a Controller, as a result of this mechanism, we can access ILogger in Class A. Original question code sample: public class Startup { ... public void ConfigureServices(IServiceCollection

Dependency Injection resolution with constructors having multiple parameters whose values are determined at runtime .NET Core 3

て烟熏妆下的殇ゞ 提交于 2020-01-21 13:59:22
问题 As a reply to an earlier question (Accessing ILogger from non-Controller classes in Class Libary .NET Core 3), we can inject the dependent class into the controller. The DI Container resolves dependencies on registration of Transient services at the time of Startup. Hence if a ClassA is instantiated by a Controller, as a result of this mechanism, we can access ILogger in Class A. Original question code sample: public class Startup { ... public void ConfigureServices(IServiceCollection

Common Service Registry

做~自己de王妃 提交于 2020-01-21 12:36:52
问题 For a long time now we've been fortunate enough to have the Common Service Locator (CSL) to resolve services from an unknown source. However, there has never been any container-agnostic solution for registering these services in the first place. We've always been faced with the problem of having to couple our composition code to a particular IoC container, and furthermore, be committed to using that container throughout our entire application. I feel I may be trying to achieve the impossible

Common Service Registry

谁说我不能喝 提交于 2020-01-21 12:36:28
问题 For a long time now we've been fortunate enough to have the Common Service Locator (CSL) to resolve services from an unknown source. However, there has never been any container-agnostic solution for registering these services in the first place. We've always been faced with the problem of having to couple our composition code to a particular IoC container, and furthermore, be committed to using that container throughout our entire application. I feel I may be trying to achieve the impossible

How to use CDI Events in Java Jersey?

送分小仙女□ 提交于 2020-01-21 11:57:22
问题 I am trying to follow this post's upvoted answer: Best practice for REST token-based authentication with JAX-RS and Jersey I'm using the CDI approach and in my filter i have the definition: @Inject @AuthenticatedUser Event<String> userAuthenticatedEvent; But, when I run tomcat 7, I get the following error: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Event<String>,parent=AuthenticationFilter,qualifiers={

Deferred binding failed

谁都会走 提交于 2020-01-21 09:10:22
问题 My application is throwing: java.lang.RuntimeException: Deferred binding failed for 'com.mygwtapp.client.gin.ClientAppGinjector' (did you forget to inherit a required module?) on this code inside the EntryPoint: private final ClientAppGinjector injector = GWT.create(ClientAppGinjector.class); What could be the problem? Here's the full error Log: java.lang.RuntimeException: Deferred binding failed for 'com.mygwtapp.client.gin.ClientAppGinjector' (did you forget to inherit a required module?)

Can Autofac inject dependencies into layout view files?

家住魔仙堡 提交于 2020-01-21 02:50:12
问题 I'm trying to inject a dependency into the shared layout view page to avoid having to do it in every view that uses the layout. I've followed the guidance in the wiki for injecting dependencies into views, but the property is always null. Can Autofac inject properties into a custom view page that is a layout file? Here's my setup. CustomViewPage namespace MyApp { using System.Web.Mvc; public abstract class CustomViewPage : WebViewPage { public IHelper Helper { get; set; } } } ~/Views/Shared/