4

I'm writing some testing code in an ASP.NET core MVC application. In the default Startup.cs it uses MVC like following:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc(routes => { /* ... */ });
}

Check the source code we can see .UseMvc is an extension method of IApplicationBuilder.

Now I want to add swagger support to this project. There is now a pre-release version, I added the reference, and now I can use:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseMvc(routes => { /* ... */ });
    app.UseSwagger(...);
}

But wait! I didn't add any using statements before use .UseSwagger! And then I found the tricky thing: In the referenced swagger library they have a namespace called Microsoft.AspNetCore.Builder, which is IApplicationBuilder belongs to (in another library), so this namespace is absolutely "using" in Startup.cs.

I'd admit it's a clever way, users will not waste their time on looking for the extension method's namespace (even nowadays VS2015 can detect extension methods' namespace but imagine other IDE e.g. VS CODE). But is there any risk to add a totally unrelated namespace CompanyB.ProjectB.XXX in CompanyA.ProjectA.XXX.dll?

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • A close vote w/o a comment explaining why? Really? – Good Night Nerd Pride Aug 24 '16 at 08:12
  • @GoodNightNerdPride Probably because of primarily opinion-based, I think. – Cheng Chen Aug 24 '16 at 08:13
  • Why should it be a problem? If you build a somehow global extension for a specific namespace then put it in the same namespace and you are fine – Sir Rufo Aug 24 '16 at 08:18
  • 1
    See http://stackoverflow.com/a/3672987/660536 for more info about potential conflicts – GazTheDestroyer Aug 24 '16 at 08:19
  • @SirRufo - right up until the point where two independent projects give the same name to their extension methods, and it all falls apart and forces consumers of those projects (if both are used) to have to use aliases and stop using extension methods. – Damien_The_Unbeliever Aug 24 '16 at 08:19
  • @DannyChen, still common decency demands more than just lazily clicking the close vote link. And as a matter of fact I don't think the question is primarily opinion-based. There are best practices out there which are not just opinion-based. There might be advantages and disadvantages that can be stated and compared. However you could reformulate your question to aim for the "bigger picture" and give the case of Swagger just as an example. – Good Night Nerd Pride Aug 24 '16 at 08:22
  • It's there by design. ASP.NET Core does it with all the middlewares and DI stuff, so it becomes instantly available within the startup class. There shouldn't be so many conflicts though, as the extension methods are named by the middleware and you wouldn't use two or more of the same middleware, i.e. AddCors or AddMvc. Another Mvc framework would use `AddNancyMvc` for example etc. – Tseng Aug 24 '16 at 08:37
  • @GoodNightNerdPride One does not need to *explain* a close vote. The close vote reason does that already perfectly well. And if you *personally* think that the question is not opinion-based, then that’s fine. I am of a different opinion, which is why I voted to close the question. – poke Aug 24 '16 at 08:38
  • This question can be equally answered as "do" or "don't". Comment @GazTheDestroyer is a good point for "don't", but is it an answer? Don't think so, you can use unlikely_to_be_used names (and arguments for methods) to never run into troubles. Does it sounds like "do"? – Sinatr Aug 24 '16 at 09:01

0 Answers0