Dependency Injection best practices and anti-patterns

I really enjoyed this article regarding DI, as it’s targeted towards people who don’t have a ton of DI experience, or don’t even know what it is. https://mtaulty.com/2009/08/10/m_11554/ What’s Unity? It’s a “dependency injection container”. Now, at that point a bunch of folks reading this will say “Yes, we know and we’re already using it … Read more

Will Spring support CDI? [closed]

Even though Spring is open source and used and supported by a large community, its future development is controlled by a single company (spring source / vmware). As such, its decisions are inherently non-public and certainly influenced by a large number of factors – like the currents demands of the community, but certainly also financial … Read more

How to use other Angular2 service inside an ngrx/Store reducer?

There is no mechanism for injecting services into reducers. Reducers are supposed to be pure functions. Instead, you should use ngrx/effects – which is the mechanism for implementing action side-effects. Effects listens for particular actions, perform some side-effect and then (optionally) emit further actions. Typically, you would split your action into three: the request; the … Read more

What does AsSelf do in autofac? [duplicate]

Typically you would want to inject interfaces, rather than implementations into your classes. But let’s assume you have: interface IFooService { } class FooService { } Registering builder.RegisterType<FooService>() allows you to inject FooService, but you can’t inject IFooService, even if FooService implements it. This is equivalent to builder.RegisterType<FooService>().AsSelf(). Registering builder.RegisterType<FooService>().As<IFooService>() allows you to inject IFooService, … Read more

Angular InjectionToken throws ‘No provider for InjectionToken’

After asking on the official angular repository, it turns out to be a simple solution. Instead of passing the service name as a string, you’ll have pass the tokens through the component into the view into another component. Globally define the injection token I did this alongside my service itself to make it easier to … Read more

How to conditionally inject service into component?

You can use the Injector import { Injector } from ‘@angular/core’ … constructor(private injector: Injector){ if(true) { this.oneService = <OneService>this.injector.get(OneService); } else { this.twoService = <TwoService>this.injector.get(TwoService); } } As @MeirionHughes mentioned this is called the service locator pattern: The technique is an example of the service locator pattern. Avoid this technique unless you genuinely need … Read more

Base Class type for ILogger using Dependency Injection

use a none generic ILogger in your base class, but ILogger<DerivedClass> in your derived class. This way you can simply pass the ILogger to your base class if needed: public abstract class BaseClassExample { private readonly ILogger logger; public class BaseClassExample(ILogger logger) { this.logger = logger; } } and public class DerivedClass : BaseClassExample { … Read more