aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

[Blazor] Add @classname directive that identifies the class name

Open mwpowellhtx opened this issue 4 years ago • 7 comments

Summary

Add an optional @classname directive, used in lieu of the default class naming convention.

Motivation and goals

By default Razor uses the file name to identify the class name, which is fine in perhaps 75-85% of the cases. In our case, and in a couple of others that I know of, we have a naming convention something like MyComponent{T1, T2, ...}.razor in order to better identify files and their generic types, which ends up naming a component something like MyComponent_T1_T2..._. This is incorrect, obviously. So we propose an optional @classname directive to allow folks to work around that limitation.

Risks / unknowns

I cannot think of any; I cannot imagine it would be any better or worse than similarly named directives, i.e. @typename, for instance. In fact we think it would be fairly consistent with those other, similar directives.

Examples

<!-- ... -->

@classname MyComponent

@typename T1
@typename T2
<!-- ... -->

Which we think should yield the following generated code:

public partial class MyComponent<T1, T2, ...> { ... }

Or, if there is truly a conflict, perhaps something like @componentname. We are not attached to the naming conventions therein; only that we have a directive based alternative to the file name being the convention.

Detailed design

Might follow an evaluation pattern, something like;

if (TryGenerateClassnameDirectiveClass(...) || TryGenerateFilenameClass(...)) { ... }

I think if would be fairly easy to identify, assuming sufficient metadata is available at the moment of CG. This is one opinion; happy to modify that opinion given a more concrete source context from which to work.

In whatever form that takes, bottom line, we think that it would be simple to prioritize the directive first, then fall back on a default convention when the directive was not discovered.

If this looks okay, would be happy to submit a PR, given direction to a file or files when component naming occurs, with availability of directive meta data, etc.

mwpowellhtx avatar Jan 21 '21 13:01 mwpowellhtx

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Jan 25 '21 13:01 ghost

Wow, thank you. I've got a sense of the code base at the moment, a working draft in progress, so feel to contact me privately to discuss further.

mwpowellhtx avatar Jan 25 '21 14:01 mwpowellhtx

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jul 20 '21 17:07 ghost

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Oct 21 '21 22:10 ghost

Does removing this issue from .NET 7 Planning milestone mean it won't be considered for the upcoming release and we may expect it at the earliest of .NET 8?

KifoPL avatar Apr 28 '22 19:04 KifoPL

What is the current state of this issue? Would be really helpful for component libraries.

FelixCCWork avatar Feb 13 '24 09:02 FelixCCWork

I could really use this too.

TonyValenti avatar Mar 14 '24 16:03 TonyValenti

I am not seeing a class name that matches the filename. I am seeing something randomly generated like AspNetCore_$(GUID). I ended up here looking for a way to define it. Should it be the filename? And what could have I done to make it not the filename?

A9G-Data-Droid avatar Mar 29 '24 23:03 A9G-Data-Droid

I am not seeing a class name that matches the filename. I am seeing something randomly generated like AspNetCore_$(GUID). I ended up here looking for a way to define it. Should it be the filename? And what could have I done to make it not the filename?

Not sure what you are talking about, "file name"? Correlated to the class name? Does not have to be, that would be silly if that was the implementation.

mwpowellhtx avatar Mar 30 '24 01:03 mwpowellhtx

Sorry for the confusion. My filename and class name are the same. However, references to the class as seen by intellisense show this generated class ID. As a result, I get odd warnings in my IDE about class members not being found. I looked for a naming conflict and I don't see any other object or property with the same name as the class. The class name in my case is unique.

A9G-Data-Droid avatar Apr 01 '24 15:04 A9G-Data-Droid