Comparing Castle Windsor, Unity and StructureMap

See here and here for a pretty thorough technical comparison of several IoC containers, although somewhat outdated by now (they’re from before Windsor 2.0) However, I don’t think there are really any vital features that Windsor offers and other containers don’t. Windsor, StructureMap, Spring.NET have all been around for several years and have been used … Read more

Ninject: Registering an already created instance with Ninject?

You can use the ToConstant method which takes an already existing instance and registers it as singleton. var kernel = new StandardKernel(); kernel.Bind<MyClass>().ToConstant(myClassInstance); If you want to something more complex you can use the ToMethod (where you can use a Func to get your instance) combined with the InSingletonScope var kernel = new StandardKernel(); kernel.Bind<MyClass>().ToMethod(context … Read more

How is AutoFac better than Microsoft.Extensions.DependencyInjection?

It’s a fair question; fundamentally the difference is in the additional features Autofac can offer you if you need them. For simple applications, the Microsoft DI may offer enough functionality, but I’ve found as my application grows there’s some extra features I find myself wanting/needing. The features that in my mind encouraged me to originally … 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

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

How to extend a component with dependency injection?

If you want to avoid this “boiler plate” code injecting services in child classes just for injection in parent classes’ constructor and then effectively using that services in child classes through inheritance, you could do this: edit: from Angular 5.0.0 ReflectiveInjector has been deprecated in favour of StaticInjector, below is updated code to reflect this … Read more

Dagger 2 – what is the purpose of a @Singleton annotation class

@Singleton (and any other scope annotation) makes your class a single instance in your dependencies graph (it means that this instance will be “singleton” as long as Component object exists). In short – everytime you’re injecting @Singleton annotated class (with @Inject annotation) it will be the same instance as long as you inject it from … Read more

Dagger @Reusable scope vs @Singleton

Use @Singleton if you rely on singleton behavior and guarantees. Use @Reusable if an object would only be a @Singleton for performance reasons. @Reusable bindings have much more in common with unscoped bindings than @Singleton bindings: You’re telling Dagger that you’d be fine creating a brand-new object, but if there’s a convenient object already created … Read more

Property Injection in ASP.NET Core

No, the built-in DI/IoC container is intentionally kept simple in both usage and features to offer a base for other DI containers to plug-in. So there is no built-in support for: Auto-Discovery, Auto-Registrations, Decorators or Injectors, or convention based registrations. There are also no plans to add this to the built-in container yet as far … Read more