Net Core Dependency Injection for Non-Controller

You can easily define a static class with one property like: public static class StaticServiceProvider { public static IServiceProvider Provider { get; set; } } after defined class you have to scope the service in the Startup.ConfigureServices method: public void ConfigureServices(IServiceCollection services) { //TODO: … services.AddScoped<IUnitOfWork, HttpUnitOfWork>(); services.AddSingleton<ISomeInterface, ISomeImplementation>(); } then inside the Startup.Configure method … Read more

Ruby dependency injection libraries

Jamis Buck, who wrote Copland and Needle, posted here about Needle, dependency injection and their usefulness in a Ruby world. It’s long but worth reading, but in case you want the single paragraph most relevant to your question, I’d suggest this one, from just before the end: DI frameworks are unnecessary. In more rigid environments, … Read more

Use both AddDbContextFactory() and AddDbContext() extension methods in the same project

It is, it’s all about understanding the lifetimes of the various elements in play and getting those set correctly. By default the DbContextFactory created by the AddDbContextFactory() extension method has a Singleton lifespan. If you use the AddDbContext() extension method with it’s default settings it will create a DbContextOptions with a Scoped lifespan (see the … Read more

Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)

Ninject is Dependency Injection for .NET. RhinoMocks and Moq are both mocking frameworks. Now both have nothing to do with each other. I really had trouble understanding both so here I go trying to explain. Dependency Injection: is an implementation (lets call it) of Inversion of Control. You don’t confuse the two. You are taking … Read more

Passing constructor arguments when using StructureMap

I suggest declaring that with the StructureMap configuration. Using the slightly newer StructureMap code: For<IProductProvider>().Use<ProductProvider> .Ctor<string>(“connectionString”).Is(someValueAtRunTime); This way you don’t burden your client code from having to know the value and can keep your IoC configuration separate from your main code.