Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Interface to formalize how tooling obtains the application's service provider #524

Closed
divega opened this issue May 3, 2017 · 18 comments
Closed
Assignees

Comments

@divega
Copy link

divega commented May 3, 2017

This a more general purpose alternative to the feature described in dotnet/efcore#8331.

Currently tools that require access to the application's runtime services and configuration (e.g. EF Core tools, ASP.NET Core Scaffolding, possibly Razor tooling and others) need to hardcode heuristics which

  • often require for tools to take additional unwanted dependencies, e.g. EF tools brings ASP.NET Core Hosting in 1.0 even if it can be used in non-ASP.NET Core applications
  • only work if the application follows specific (often version-specific) conventions to build the service provider (e.g. Startup.ConfigureServices() in ASP.NET Core 1.0, Program.BuildWebHost() in ASP.NET Core 2.0, etc.)
  • and require restoring to bespoke fallback mechanisms (like IDbContextFactory in EF) if the convention is not matched

This issue is about defining a common accessor interface that tooling can scan for at design time which provides access to the application's service provider. We believe this approach:

  • Could be used in ASP.NET Core applications as well as any other application that uses our DI
  • Could be leveraged by any tool that requires access to the application's services, rather than being component-specific
  • Removes the need of hardcoding heuristics in tools by moving the necessary code to the application
  • Enables project templates can incorporate an implementation of the interface that is tailored to the patterns they used to construct the application's service provider, e.g. for the ASP.NET Core 2.0 templates the implementation can return Program.BuildWebHost(args).Services. If the mechanism changes in the future, the template can be updated
  • Addresses all the concerns described in Design: Allow open generic implementations of IDbContextFactory dotnet/efcore#8331, e.g. it allows EF Core tooling to discover any DbContext registered with AddDbContext regardless of their location, making IDbContextFactory unnecessary for most common cases

Example:

public class DesignTimeServiceProviderAccessor : IDesignTimeServiceProviderAccessor
{
    public IServiceProvider GetServiceProvider(string[] args)
        => Program.BuildWebHost(args).Services;
}

Open issues:

  • Still need to run the idea by the architect 😄
  • General shape and naming of the interface (or abstract type?) is up in the air
  • Need to decide where we want to put it in templates

cc @bricelam @ajcvickers @DamianEdwards @davidfowl

@davidfowl
Copy link
Member

davidfowl commented May 3, 2017

I don't see what this has to do with DI. Are we just looking for a place to put an interface?

@DamianEdwards
Copy link
Member

Sure, where should we put it?

@davidfowl
Copy link
Member

Honestly, I don't understand what this solves... Are we going to have shared code that calls into this interface or something?

@divega
Copy link
Author

divega commented May 3, 2017

Not necessarily, but yes we could.

As the title says and hopefully the description explains this interface formalizes the convention that tools need to implement in order to obtain the application's service provider at design time. E.g. instead of having to fake a host and instantiate Startup to call ConfigureServices() based on yesterday's templates, calling Program.BuildWebHost() based on tomorrow‘s templates, whatever we come up in the future or however customers decide to factor their code, we establish that tools will look for a type implementing this interface in the startup project and we start including one by default in the templates.

The main affinity with DI is that it is about obtaining the IServiceProvider. I guess it could go instead in a tooling abstractions package if we had such thing.

@ericwj
Copy link

ericwj commented May 15, 2017

It is an interesting idea for Universal apps as well. There is always the problem of being unable to instantiate ViewModels at design time which have constructor arguments that are meant to come from DI at runtime. Everybody goes about crafting a solution, and they aren't always pretty. Sometimes they're awful. This isn't about 'tooling' per se, not in the .NET Core sense, but how cool would it be if XDesProc could use this? Well, phase one would be me using this with if (DesignModeEnabled)...

@Eilon
Copy link
Member

Eilon commented May 15, 2017

@divega @davidfowl @ajcvickers @DamianEdwards @muratg @bricelam - what is the status of this? We met last week, and I thought maybe we arrived at a conclusion, but I don't recall the concrete next steps. Whatever we decide, we should try to get it in this week.

@ajcvickers
Copy link
Member

@Eilon Diego, Brice, and I talked about it a little while ago. Brice is going to send some pull requests and we will take it from there.

@ericwj
Copy link

ericwj commented May 16, 2017

Can you comment a bit on bringing some UI folks onboard? Xamarin, UWP? Is this something you see benefit in? Or should I raise this issue in a few other places?

@bricelam
Copy link
Contributor

@ericwj It's worth raising in the respective places.

I think it's a little less interesting with MVVM because most people probably aren't using Microsoft.Extensions.DependencyInjection.Abstraction there like we do in ASP.NET Core.

@ericwj
Copy link

ericwj commented May 16, 2017

No, not today, but behold, .NET Standard is coming. I don't think there is mention of a date for UWP to arrive at 2.0, but once it's there, it'll make quite an impact I'm sure. Sharing basics like this just removes friction and making switching project types less of a culture shock. And it'd be opt in anyway I'm sure...

I've tried to find a suitable repo for XAML, but I couldn't really find one. Can you direct me somewhere, or should I use Provide a suggestion... from VS and post it at https://visualstudio.uservoice.com?

@bricelam
Copy link
Contributor

VS Feedback would be a good place to start. Maybe dotnet/project-system would also get the right people looking at it.

@ericwj
Copy link

ericwj commented May 16, 2017

And Xamarin? They don't have issues. Only pull requests 🥇

@bricelam
Copy link
Contributor

Sorry, no idea on that one

@ericwj
Copy link

ericwj commented May 16, 2017

TY anyway, I'll get there

bricelam added a commit that referenced this issue May 16, 2017
Provides access to the application's IServiceProvider to design-time services.

Resolves #524
@bricelam bricelam assigned divega and unassigned bricelam May 18, 2017
@bricelam bricelam modified the milestones: 2.0.0, 2.0.0-preview2 May 18, 2017
@divega divega removed this from the 2.0.0 milestone May 19, 2017
@divega
Copy link
Author

divega commented May 19, 2017

Clearing up milestone so that this can be re-triaged. We are not planning to do it for 2.0.0. Also some relevant comments in the conversation on the PR: #527 (comment).

@muratg
Copy link

muratg commented Oct 20, 2017

@divega What milestone should this go in? 2.2 or backlog?

@divega
Copy link
Author

divega commented Oct 24, 2017

@muratg I think it is fine for this to be in the backlog. It still seems nice and from time to time we hear from people that want something that works with DI without ASP.NET Core and without going all the way down to IDesignTimeDbContextFactory, but it does not seem to be a high priority.

Cc @bricelam and @ajcvickers in case they think we should plan to have it sooner.

@aspnet-hello
Copy link

This issue was moved to dotnet/aspnetcore#2342

@aspnet aspnet locked and limited conversation to collaborators Jan 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants