New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: make autogenerated BlazorComponent views partial class #278

Open
uazo opened this Issue Mar 16, 2018 · 11 comments

Comments

Projects
8 participants
@uazo
Contributor

uazo commented Mar 16, 2018

I would like to split the Razor c # + html template from the purely functional code of the view and I tried to make every generated class contained in "BlazorRazorComponents.g.cs" with the partial modifier (simply adding it in "Microsoft.AspNetCore.Blazor.Razor. Extensions \ BlazorRazorEngine.cs ").

So my views are composed in

nomeview.cshtml <- html layout
nomeview.cshtml.cs <- functional code

The compilation works without problems.
The problem is Visual Studio is unable to "read" the contents of the code in cshtml.cs, showing the error CS1061 'Class' does not contain a definition for 'Method'.

Now, adding the directive

@inherits <name of the same class>

instead Visual Studio works correctly, while, obviously, the compilation is not successful because the autogenerated code becomes

public partial class <classname>: <classname> <- the same

But if the code remain

public partial class <classname>: BlazorComponent

both the compilation and Visual Studio would work.

Is it possible to make that if implements the same class, BlazorComponent is kept as a base class?

@SteveSandersonMS

This comment has been minimized.

Member

SteveSandersonMS commented Mar 16, 2018

You can use the @inherits mechanism today in the way you've described if you make your "code behind" class have a different name. For example, have MyPage.cshtml inherit from MyPageBase.cs.

However I agree with you that partial would be more convenient. I'm totally open to doing that. @rynowak we discussed this before - do you think we should? I can easily add the partial modifier to our compiler if we want that.

@uazo

This comment has been minimized.

Contributor

uazo commented Mar 16, 2018

Yes, I know, in fact for now I'm proceeding this way.

But to simplify the navigation of the project (for example through intellisense), I would prefer not to have basic classes that for now that are not useful for the project.
Among other things, it would also be useful for the future to have an "abstract" directive, so that the component can be defined as abstract, although in that case it is necessary to review the way in which the sequence number is constructed in the BuildRenderTree builder.

@rynowak

This comment has been minimized.

Member

rynowak commented Mar 16, 2018

Yeah, I agree with @uazo - I think partial makes a lot more sense for the Blazor use case. There's no reason not to just do this.

@vibeeshan025

This comment has been minimized.

vibeeshan025 commented Mar 19, 2018

Is there any possiblity to expect support for the View class to have the ability to be used as Generic class, WPF, Silverlight, UWP none of them support this, however this helps us to swap the view or the model depending on the scenario.

public partial class MyView<T>: BlazorComponent { public T ViewModel; }

An extenral constructor can decide, what the view and viewmodel will be depending on the scenario.

Since Blazor only supportes typed binding (other XAML technologies support reflection based also), I think supporting Generics when generating partial class is a must. Please try to consider this scenario when implementing. Otherwise we have to duplicate Views and viewmodels for small changes.

SteveSandersonMS added a commit that referenced this issue Mar 19, 2018

@SteveSandersonMS

This comment has been minimized.

Member

SteveSandersonMS commented Mar 19, 2018

@vibeeshan025 Could you write up your idea as a separate issue? Importantly, can you phase it in terms of a specific scenario example? Rather than saying the types should be generic, could you give an example based on some business scenario like "editing a list of products", and sketch out what sort of code you would want to work? It's difficult to evaluate it otherwise. Thanks!

@vibeeshan025

This comment has been minimized.

vibeeshan025 commented Mar 19, 2018

@SteveSandersonMS , thanks, I will write-up a separate issue.

@danroth27 danroth27 added this to the Backlog milestone Mar 24, 2018

@danroth27 danroth27 added this to To Do in Blazor via automation Mar 24, 2018

@KelsonBall

This comment has been minimized.

KelsonBall commented Mar 27, 2018

I'm interested in becoming a contributor to Blazor and came to see if this exact feature had been proposed. Is this feature available to be assigned to a new contributor?

@rynowak

This comment has been minimized.

Member

rynowak commented Mar 28, 2018

This is a really simple change to make in effect and there's already a PR for it actually. #294

Unfortunately this will totally break the editing experience, so totally blocked until we can figure out a solution to that.

@Euphoric

This comment has been minimized.

Euphoric commented Apr 5, 2018

Using partial to separate design and code-behind

What is this? WinForms?

@DarrellBailey

This comment has been minimized.

DarrellBailey commented Jul 11, 2018

@rynowak Can you explain what you mean by "this will totally break the editing experience"? Is it something with intellisense?

@rynowak

This comment has been minimized.

Member

rynowak commented Jul 11, 2018

Sure, when running in the editor, the component generated code has a mangled class name. This is needed to work around the fact that that the generated code also exists in the project with its original unmangled class name.

When you reference a component by type in your C# code this works because the generated code exists in the project with it's original unmangled class name.

If you allow the generated code to exist in the project twice with the same class name, then you will get lots and lots of errors at edit time due to duplicate definitions.

If you allow users to create a 'code behind' partial class, then it that will match up fine with the generated code with its original class name, but not with the editor's version, which mangles the name. So you'll get errors in the editor for everything defined in the partial class.

The solution is to fix the underlying problem, we don't have a mechanism to control the how the editor interacts with code already in the project. We're trying to address that for VS 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment