Hosting icon indicating copy to clipboard operation
Hosting copied to clipboard

Why Startup class doesn't implement generic interface?

Open Yorie opened this issue 11 years ago • 10 comments

The question is why Startup class, that is automatically created from Visual Studio project template for vNext projects, doesn't implement any interface?

Of course, in simple application it is not required, because it is enough to have one startup class for project. But if I want to use some (let's say it is abstract) modular system, I would like to create startup classes for each module I implement to be able to initialize each module in the way vNext does. So, I don't want to remember all startup methods and their arguments, and wouldn't it be nice to just implement startup interface (or even base class to support constructor signature?). Of course I can create similar interface manually, but shouldn't it be part of framework?

And it is just feeling that Startup class should implement interface or inherited from base class.

Yorie avatar Oct 05 '15 15:10 Yorie

Not sure if I understand your scenario with the modular system. Can you give some concrete examples?

Apps need an entry point, which Startup provides. Not sure what multiple entry points would be useful for.

muratg avatar Oct 05 '15 15:10 muratg

(I swear we've answered this several times, but I can't find the others right now.) There is no interface or base class because the startup process supports multiple class and method signatures.

  1. The class should be named Startup or Startup[Environment] where Environment is specified by the Hosting:Environment configuration parameter (default: Production).
  2. The constructor may take DI parameters.
  3. The class may have an optional ConfigureServices or ConfigureServices[Environment] method.
  4. The class must have a Configure or Configure[Environment] method, which may also take DI parameters.

Tratcher avatar Oct 05 '15 16:10 Tratcher

See https://gist.github.com/davidfowl/700097cca459c1e78c86

davidfowl avatar Oct 05 '15 16:10 davidfowl

@Tratcher we have https://github.com/aspnet/Home/issues/722

davidfowl avatar Oct 05 '15 16:10 davidfowl

Okay, now it's clear that we depend on DI and cannot make startup strict on interface. Thanks for the answer.

P.S. Sorry for doubling question. Just searched questions about startup class interface, but didn't find it due to some reasons..

Yorie avatar Oct 05 '15 16:10 Yorie

I don't agree at all.

  1. If Startup does come with several signatures, then there could be several base classes or interfaces to choose from. No constraint exists to have the Startup class derive from one single signature only. startup startup
  2. Why should Dependency Injection be an argument against interfaces or base classes?

I believe the concept of runtime errors versus type safe programming should be strongly reconsidered.

SetTrend avatar Dec 13 '15 20:12 SetTrend

The class should be named Startup or Startup[Environment] where Environment is specified by the Hosting:Environment configuration parameter (default: Production).

I think it feels so magical to have Startup[Environment].

The constructor may take DI parameters.

Yes but this shouldn't effect implementing an interface or inheriting an abstract class.

The class may have an optional ConfigureServices or ConfigureServices[Environment] method.

This is the actual place where I struggle a lot. Why do we have separate ConfigureServices method? Wouldn't it be possible to handle this at the constructor? I remember that ConfigureServices can also return IServiceProvider. I am not sure if this is still the case but if so, this can be handled inside the ctor assigning it to a known property (could be an abstract, virtual or part of the interface). ConfigureServices[Environment] just makes me so sad to have. To much magic.

The class must have a Configure or Configure[Environment] method, which may also take DI parameters.

Same for Configure[Environment] and I think Configure may not take any unknown dependencies. It can again be the responsibility of the ctor to prepare what Configure needs (getting stuff injected in through ctor, assigning values to private fields, etc.).

Is there any technical reasons not to do something like this (e.g. construction of Startup and calling ConfigureServices/Configure happens at very different stages, etc.)?

tugberkugurlu avatar Dec 14 '15 17:12 tugberkugurlu

To be honest, having an interface would be fine. It's just not required because of all the things listed in the previous bug. So if you don't care about injection into the Configure method or Configure{environment} then it's usable.

It can again be the responsibility of the ctor to prepare what Configure needs (getting stuff injected in through ctor, assigning values to private fields, etc.).

Except it can't. Services are configured in he startup class so the ctor can not be used.

davidfowl avatar Dec 14 '15 17:12 davidfowl

having an interface would be fine. It's just not required because of all the things listed in the previous bug.

Which previous bug are you referring to in particular? Having an interface or a base class may not be necessary at run-time, but it's definitively a necessity at compile time to (1) provide Intellisense (2) create compile time errors when a required signature is missing or misspelled.

If the run-time environment requires something then there shall be a strict definition of what it actually requires.

What's a specific user story that'd render it impossible to have Startup.cs derive from base classes or interfaces?

SetTrend avatar Dec 14 '15 18:12 SetTrend

Hmm, maybe then in the template could be at least generated a URL comment pointing to some (current) MSDN article describing all possible options for setting up Startup class? So one has not to waste time on searching for it ;-)

Sebastian-Gruchacz avatar Nov 16 '18 11:11 Sebastian-Gruchacz