ASP.Net Core Call a controller from another controller

How can I use the dependency injection system builtin to ASP.Net 5 to create an instance of the required API controller for me? In your Startup.cs can tell the MVC to register all your controllers as services. services.AddMvc().AddControllersAsServices(); Then you can simply inject the desired controller in your other controller via the DI mechanism and … Read more

MEF (Managed Extensibility Framework) vs IoC/DI

The principle purpose of MEF is extensibility; to serve as a ‘plug-in’ framework for when the author of the application and the author of the plug-in (extension) are different and have no particular knowledge of each other beyond a published interface (contract) library. Another problem space MEF addresses that’s different from the usual IoC suspects, … Read more

Autofac – InstancePerHttpRequest vs InstancePerLifetimeScope

InstancePerHttpRequest and InstancePerApiRequest essentially do the same thing – you get a single instance of your service for each discrete web request. I’ll use InstancePerHttpRequest for the rest of the answer, but keep in mind that these two are interchangeable. InstancePerLifetimeScope means a new instance of the service will be created for every lifetime scope … Read more

Spring JUnit: How to Mock autowired component in autowired component

You could use Mockito. I am not sure with PostConstruct specifically, but this generally works: // Create a mock of Resource to change its behaviour for testing @Mock private Resource resource; // Testing instance, mocked `resource` should be injected here @InjectMocks @Resource private TestedClass testedClass; @Before public void setUp() throws Exception { // Initialize mocks … Read more

Dependency Injection with classes other than a Controller class

Below is a working example of using DI without anything that involves MVC Controllers. This is what I needed to do to understand the process, so maybe it will help somebody else. The ShoppingCart object gets, via DI, an instance of INotifier (which notifies the customer of their order.) using Microsoft.Extensions.DependencyInjection; using System; namespace DiSample … Read more

Null User on HttpContext obtained from StructureMap

I haven’t worked with OWIN, but when hosting in IIS integrated mode the HttpContext is not populated until after the HttpApplication.Start event is complete. In terms of DI, this means that you cannot rely on using properties of HttpContext in any constructor. This makes sense if you think about it because the application should be … Read more

How to implement the “robot legs” use case with Google Guice?

As documented in the Guice Wiki, you need to install two PrivateModules, each of which exposes a Service with the right annotation for you. public class MyModule extends AbstractModule { @Override protected void configure() { install(new PrivateModule() { @Override public void configure() { // Bind Source to SourceDatabase. bind(Source.class).to(SourceDatabase.class); // Bind @Named(“database”) Service to Service. … Read more