Aspnetcore: Make autogenerated BlazorComponent views partial class

Created on 16 Mar 2018  路  16Comments  路  Source: dotnet/aspnetcore

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?

Components Big Rock Done Tooling L area-blazor enhancement

Most helpful comment

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.

All 16 comments

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.

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.

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.

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.

@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!

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

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?

This is a really simple change to make in effect and there's already a PR for it actually. https://github.com/aspnet/Blazor/pull/294

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

Using partial to separate design and code-behind

What is this? WinForms?

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

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.

Depends on #4065

Such partial classes would also allow to specify generic constraints easily when using @typeparam https://github.com/aspnet/AspNetCore/issues/5603

@ajaybhargavb should this be resolved as Done ?

Yeah. I'll file a new issue to track addressing feedback and fixing bugs.

Was this page helpful?
0 / 5 - 0 ratings