Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Current implementation is a service-locator, which is an anti-pattern #40

Open
kind-serge opened this issue Jan 4, 2018 · 0 comments
Open

Comments

@kind-serge
Copy link

kind-serge commented Jan 4, 2018

The entire idea of DI is to resolve all dependencies once at the app root, however current implementation of this factory extension violates that principle by resolving the object being produced by a factory and its dependencies on demand. This can lead to unexpected behavior at runtime instead of fail-fast at the startup. Such problem does not occur when you implement a factory by hand, because the compiler will do the compile-time checks.

Here is an example:

public interface IFooFactory
{
    IFoo Create();
}

class Foo : IFoo
{
    public Foo(IBar bar) { ... }
}

static void Main()
{
    // Create bindings, but forget about IFoo and IBar
    var kernel = new StandardKernel();
    kernel.Bind<IFooFactory>().ToFactory();
    // Resolve dependency graph at startup
    var factory = kernel.Get<IFooFactory>();
    // Somewhere down the stream: Oops! IFoo or one of its dependencies is not bound :(
    var foo = factory.Create();
}

Those dependency checks should be done when a factory is instantiated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant