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

Discussion around API for MVC DI wireup #1388

Closed
ctolkien opened this issue Jan 12, 2015 · 4 comments

Comments

Projects
None yet
3 participants
@ctolkien
Copy link

commented Jan 12, 2015

Firstly, apologies if I've missed some guidance somewhere on making this work.Taken from this wiki page, all the examples have a controller taking a direct dependency on a context, not an interface.

Looking at the default wireup for EF services in MVC:

// Add EF services to the services container.
services.AddEntityFramework(Configuration)
    .AddSqlServer()
    .AddDbContext<SomeDbContext>();

There doesn't seem to be a way to register this context in the DI container as an interface?

Perhaps something like

.AddDbContext<SomeDbContext, IAsThisDbInterface>()

@rowanmiller rowanmiller added this to the 7.0.0 milestone Jan 14, 2015

@rowanmiller

This comment has been minimized.

Copy link
Member

commented Jan 14, 2015

We should still register as the context type in addition to the other type (to support tooling etc.)

@divega

This comment has been minimized.

Copy link
Member

commented Jan 14, 2015

If we do register both we need to do it in a way that you get the same instance regardless of what type you ask for (simply registering both services won't do that).

@rowanmiller

This comment has been minimized.

Copy link
Member

commented Dec 8, 2015

I don't think we need anything EF specific here, as DI already allows you to register an interface (or base type) to resolve to another service that is already registered in DI.

services.AddScoped<IAsThisDbInterface>(servideProvider => servideProvider.GetRequiredService<SomeDbContext>());

@divega agree that this can be closed?

@ctolkien

This comment has been minimized.

Copy link
Author

commented Feb 17, 2016

I'm happy with the scenario @rowanmiller has outlined above as a solution.

@ctolkien ctolkien closed this Feb 17, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.