Difference between MEF and IoC containers (like Unity, Autofac, SMap, Ninject, Windsor.Spring.net, etc.)

Eventually what I have concluded about the MEF vs IoC container is as follows: MEF is preferred to be used when one has to deal with unknown types or a plugin based architecture. IoC containers are preferred to be used with known types. Moreover, MEF is an architectural solution for dependency injection Whereas, IoC containers … Read more

Why is MVC4 using the Service Locator Anti-Pattern?

That’s an implementation detail that you shouldn’t care about. The important thing is that now that the Web API uses the DependencyResolver to resolve dependencies for many different facilities, you will be able to use a real dependency injection whenever you want to plug into those facilities. So in your code you will be using … Read more

NInject: Where do you keep your reference to the Kernel?

It’s true that you don’t want to pass around the kernel. Typically, in a web app, I store the kernel in a static property in the HttpApplication. If you need a reference to the kernel, you can just expose a dependency (via constructor argument or property) that is of the type IKernel, and Ninject will … Read more

Is it better to create a singleton to access unity container or pass it through the application? [closed]

The correct approach to DI is to use Constructor Injection or another DI pattern (but Constructor Injection is the most common) to inject the dependencies into the consumer, irrespective of DI Container. In your example, it looks like you require the dependencies TestSuite and TestCase, so your TestSuiteParser class should statically announce that it requires … Read more

MVC, EF – DataContext singleton instance Per-Web-Request in Unity

Yes do not share context and use one context per request. You can also check linked questions in that post to see all problems which a shared context caused. Now about Unity. Idea of PerCallContextLifetimeManager works but I think provided implementation will not work for more than one object. You should use PerHttpRequestLifetimeManager directly: public … Read more

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

Inject Generic Implementation using Guice

In order to use generics with Guice you need to use the TypeLiteral class to bind the generic variants. This is an example of how you’re Guice injector configuration could look like: package your-application.com; import com.google.inject.AbstractModule; import com.google.inject.TypeLiteral; public class MyModule extends AbstractModule { @Override protected void configure() { bind(new TypeLiteral<Repository<Class1>>(){}) .to(new TypeLiteral<MyRepository<Class1>>(){}); } } … Read more

Creating an instance using Ninject with additional parameters in the constructor

The With.ConstructorArgument existed in 1.0 for this purpose. In 2.0, the syntax has changed slightly:- With.Parameters.ConstructorArgument with ninject 2.0 See Inject value into injected dependency for more details and examples of how to use the context, providers and arguments to pass stuff like this around more correctly. EDIT: As Steven has elected to pretend my … Read more

Is it possible to use Dependency Injection with xUnit?

Yes it’s possible with Xunit.DependencyInjection Install-Package Xunit.DependencyInjection and you can inject your services [assembly: TestFramework(“Your.Test.Project.Startup”, “AssemblyName”)] namespace Your.Test.Project { public class Startup : DependencyInjectionTestFramework { public Startup(IMessageSink messageSink) : base(messageSink) { } protected override void ConfigureServices(IServiceCollection services) { services.AddTransient<IDependency, DependencyClass>(); } } } https://github.com/pengweiqhca/Xunit.DependencyInjection