-
Notifications
You must be signed in to change notification settings - Fork 223
Add support for @namespace to configure the namespace the generated class is in #1159
Comments
Love it. Does it work for views as well? |
@rynowak of course! |
My only concern about the automatic folder-name-based namespace comes from this:
I think most users would be OK with this, but do we want to consider an opt-out mechanism for those who do care? The opt-out here would just be for the folder-name part, where we concat folder names to the closest |
The opt-out would be putting the |
Yep, what @rynowak said. |
Good with me, as long as we're comfortable with it. Is it going to be breaking to do this for Razor views (or rather, is it going to be breaking in a way that we care about?). They'll end up in subfolder namespaces now, when they didn't before (or is the idea that we wouldn't do any namespace generation unless we saw an |
It's totally opt-in, we intend to update the templates, not the in-box defaults. |
Ok, it wasn't clear in my first reading that this statement referred to the final namespace for the page, rather than the base namespace (since the previous section was talking about finding the base namespace, to which we would add the folder names)
|
@DamianEdwards - I like the idea that we'd made this effect the generated class name as well. For instance - here's your folder structure with the current kinds of names we generate now:
And here's what I'm pitching. This is pretty much exactly what the prototype does.
|
Looks great. What would the class name be if we didn't do that? |
Ignore me, I suck. Love it, let's do it. |
We should figure out if there's a risk of type name collisions when we do precompiled views. They're all compiled in to a single assembly, unlike during runtime compilation. |
The Problem
The classes that are generated by Razor files are placed in a fixed namespace of
AspNetCore
.As part of Razor Pages, it is idiomatic to specify the page's model using
@model
, e.g.:The class
IndexModel
is specified in a sibling CS file named for the CSHTML file it applies to, e.g.Index.cshtml.cs
In page-focused layout it's also normal to nest identically named files in sub-folders as the site structure is built up, e.g.
It's also common to have a
_ViewImports.cshtml
in the pages root to set the default namespace imports for all pages, e.g.:You might also include subsequent
_ViewImports.cshtml
files in the sub-folders to import namespaces for types declared in those sub-folders, e.g.:This results in it being impossible to refer to page models via
@model
without using the fully qualified class name (including namespace), as there are multiple namespaces imported where the model class name is present. This isn't ideal as it makes it harder to move the page around independently of its content, such that it breaks if you decide to reorganize the layout.The Proposal
Let's introduce support for changing the namespace of the generated view/page class via the
@namespace
directive. This directive would be set in the_ViewImports.cshtml
of new ASP.NET Core projects (via the project templates) to the application's root namespace by default, much like the default@using
directives are today.The directive will take a very similar form to the
@using
directive, allowing specification of a namespace following white-space after the directive. At design-time it will be projected to the namespace declaration line in the generated file buffer.Only a single instance of the
@namespace
directive is supported, and it can be overridden by more specific_ViewImports.cshtml
files (more specific based on folder depth, similar to@model
).The actual namespace used will be the combination of that specified by the closest
@namespace
directive in a_ViewImports.cshtml
flie, combined with the names of the sub-folders down to the folder containing the page (the path). This way, the namespace will typically only need to be set once for the application in the root_ViewImports.cshtml
file, e.g.:If no
@namespace
is specified in a view/page's directives (inherited or direct), the existing default namespaceAspNetCore
will be used.The text was updated successfully, but these errors were encountered: