dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
290 stars 64 forks source link

Let's have VB.NET ASP.NET MVC core! #396

Open VBAndCs opened 5 years ago

VBAndCs commented 5 years ago

I want to take action in adding VB.NET to ASP.NET MVC Core, as I posted in https://github.com/aspnet/AspNetCore/issues/8583 If anyone can help or participate, I will be glad.

VBAndCs commented 5 years ago

This is the formal response I got: https://github.com/aspnet/AspNetCore/issues/8583#issuecomment-474133744 Seems that MS has killed VB.NET already!

Nukepayload2 commented 5 years ago

@VBAndCs They have locked VB.NET on desktop app development.

VBAndCs commented 5 years ago

@Nukepayload2 This will end at some point, when all new developers start with C# and the remaining VB developers migrate to C#! Web and mobile apps are the most demand in business today. It is wise to stop writing any new app with VB!

ericmutta commented 5 years ago

@VBAndCs It is wise to stop writing any new app with VB!

:-) I just have to smile at this because every few months we hear the same thing here or some other place.

The world is not binary. It is not a "use C#" or "use VB" world. Some of us use both: like in my case I use both for Xamarin Android development...and it all works just fine...no need to be alarmed!

The other interesting thing is that after using VB for almost 23 years now I can honestly say if they never added another thing to it, it would still be a really kick ass development tool. Targeting .NET Standard 2.0 you can pretty much write any app you care about using 95% VB and be a very happy human being!

pricerc commented 5 years ago

if they never added another thing to it, it would still be a really kick ass development tool.

This.

I think that it's easy for people to forget that without VB of old, C# probably wouldn't exist in its current form. I dabbled in X-Windows programming in the 90's, and MS-Windows programming at the time was just as arcane. VB brought human-friendly tooling to Windows programming, which via the .NET effort found its way to curly-bracket land in the form of C#.

If you look at the changes that VB has been through since .NET 1.0, most of them have been quality-of-life things that we could have lived without, but are grateful for. We'd actually already solved most of the problems with well-known patterns and practices. Things like auto-implemented properties are awesome, but, by the same token were previously implemented by auto-complete.

Another mistake I think people make around VB is to assume that a lack of VB-related questions on, e.g. StackOverflow, mean that hardly anyone is using VB. What makes more sense to me, is that people using VB have fewer questions to ask, because the language is more programmer friendly than C#, even if C# developers don't want to admit it.

VBAndCs commented 5 years ago

@pricerc

What makes more sense to me, is that people using VB have fewer questions to ask, because the language is more programmer friendly than C#, even if C# developers don't want to admit it.

I disagree. I ask my VB questions in C#! This is becuase those who have low level knowlage and experts in systems, compilers and advanced sofware engines are mostly former C++ and java before becoming C# programmers. VB.NET programmers numebres are much bigger than those of C#, but most of them work in uasal business, nad less of them participate in open source, so this creted the illusion that C# is dominating! VB.NET has most developers. C# has most experts. MS has the illusion that C# is the most wanted language!

VBAndCs commented 5 years ago

@Nukepayload2 @ericmutta @pricerc @srivatsn @AnthonyDGreen @AdamSpeight2008 @KathleenDollard

I want to simplfy the problem of vbhtml, by using a code behind file.. suppose we have this cshml file:

@model IEnumerable<MvcMusicStore.Models.Genre>
@{
    ViewBag.Title = "Store";
}
<h3>Browse Genres</h3>
<p>
    Select from @Model.Count()
genres:</p>
<ul>
    @foreach (var genre in Model)
    {
        <li>@genre.Name</li>
    }
</ul>

I want to write the vbhtml file as:

<CodeBehind ClassName = "View1Code" FileName= "View1.vb"/>
<h3>Browse Genres</h3>
<p>
    Select from @ModelCount genres:
</p>
<ul>
    @Genres
</ul>

and view1.vb:

   Friend Shared Class View1Code
          Dim Model As IEnumerable(Of MvcMusicStore.Models.Genre)

    Public Sub New(model as IEnumerable(Of MvcMusicStore.Models.Genre)
          Me.Model = model
          ViewBag.Title = "Store"
    End sub

     Public Property Model.Count As integer = Model.Count( )

     Public ReadOnly Property Model.Count As String 
           Get
                Dim s = "";
                For Each genre in Model
                         s += &"<li>{genre.Name}</li>{vbCrLf}"
               Next
               Return s
            End Get
     End Property

   End Class

1- This will eliminate the need for parsing, compiling and debugging the vbhtml file, because it only contains a static placeholders that will be replaced with the evaluated properties at runtime. This will leave this file as a prototype html file.

2- The code-behind file is a normal vb file which doesn't need any special treatment! This allows to use this model to write a razor for F# or any other language without any further coding!

3- No new effort needed in every new ASP.NET Core version. Any change in other parts of the framework will not affect the way we write these razor pages.

4- This is a simple divide and conquer approach, which makes the problem too simple. We may need to write some longer code in the code behind file, but it is direct and most of it can be generated with snippets or code generators. On the other hand, the vbhtml file will be cleaner. The @variable parts can view the actual code in tool tips or in a peak definition window.

If some one is willing to help me do this, we can have VB razor up and running in a few days. My problem is that I am lost in the ASPNETCore source code, and even failed to build the project on my PC! I am not experienced in project templates, and can't integrate the vbthml file and its code-behind in the MVC project. I can write parts of this works, put I can't put the whole parts together! So, I need help.

VBAndCs commented 5 years ago

@Nukepayload2 @ericmutta @pricerc @srivatsn @AnthonyDGreen @AdamSpeight2008 @KathleenDollard Surprise: VB.NET has a built-in razor pages! Look at this:

Friend Class View1
    Dim Model As IEnumerable(Of MvcMusicStore.Models.Genre)

    Public Sub New(model As IEnumerable(Of MvcMusicStore.Models.Genre))
        Me.Model = model
        ViewBag.Title = "Store"
    End Sub

    'ViewBag.Title = "Store"
    Public ReadOnly Property Razor
        Get
            Return _
    <html>
        <h3> Browse Genres</h3>
        <p>
    Select from <%= Model.Count() %> genres:
    </p>
        <ul>
            <%= (From genre In Model Select <li><%= genre.Name %></li>) %>
        </ul>
    </html>
        End Get
    End Property

End Class

The only thing we need is an ASP.NET core template for VB, and a little adjustments to generate the render HTML page from the View1.Razor property! voila!

VBAndCs commented 5 years ago

I added a new proposal about the xml literals here #397

VBAndCs commented 5 years ago

@Nukepayload2 @ericmutta @pricerc @srivatsn @AnthonyDGreen @AdamSpeight2008 @KathleenDollard Finally: A working VB.NET ASP.NET MVC Core Razor sample! https://github.com/VBAndCs/VB.NET-Razor I implemented a simple VBRazorViewEngine in the VbRazor project. To use VBRazorViewEngine in the project, I added these two statements to the Startup.ConfigureServices method:

services.AddTransient(Of IConfigureOptions(Of MvcViewOptions), VBRazor.VBRazorMvcViewOptionsSetup)()
services.AddSingleton(Of IViewEngine, VBRazor.VBRazorViewEngine)()

The VBRazor is just a VB class that implements the IVBRazor Interface:

Public Interface IVBRazor
    ReadOnly Property Razor As String

End Interface

The Razor property uses the xml literals to compose the HTML code and returns it as a string.. Example:

Imports VbRazor

Public Class IndexView
    Implements IVBRazor

    Dim students As List(Of Student)

    Public Sub New(students As List(Of Student))
        Me.students = students
    End Sub

    Public ReadOnly Property Razor As String Implements IVBRazor.Razor
        Get
            Dim x = <html>
                        <h3> Browse Students</h3>
                        <p>Select from <%= students.Count() %> students:</p>
                        <ul>
                            <%= (Iterator Function()
                                     For Each std In students
                                         Yield <li><%= std.Name %></li>
                                     Next
                                 End Function)() %>
                        </ul>
                    </html>
            Return x.ToString()

        End Get
    End Property
End Class

To use the IndexView from the Controller, I passed it to the View method as the model data in the action method, and passed the actual model data to its constructor:

Public Function Index() As IActionResult
    Return View(New IndexView(Students))
End Function

That’s all!! If you run the project, you will see this web page: VBRazor

This was really easy, but needs more work, so I hope you start contribute to this project to make it a real productive tool! The first thing to do, it to create a VB.NET template for ASP.NET MVC Core. I had to create a C# project then convert it to VB!

The second thing to do, is to add intellisense support for html attributes in xml literals in VB!

We need to try more advanced pages with JavaScript and other components. I hope VB team give us the support wee need to make the most of xml literals.

KathleenDollard commented 5 years ago

I like that we have a new proposal #397 to focus the technical discussion on XML Literals instead of .cshtml in MVC development. Since there is some ancillary discussion about VB.NET, I'm not going to close this one at present, but let's keep the technical side in #397

VBAndCs commented 5 years ago

Mission complete, and here is Vazor 1.0: https://github.com/VBAndCs/Vazor

VBAndCs commented 5 years ago

Current Progress:

The Vazor version for .NET Core 3 preview 3 version is here: https://github.com/VBAndCs/Vazor

Class StudentView
       Inherits VazorView

       Dim std as Student

       Public Sub New (StudentID as Integer)
              Student = FindStudentInDB(ID)              
       End Sub

       Function FindStudentInDB(ID as integer)
           ' …………
       End Function

End Class

We have the flexibility to do whatever we want in Vazor views.

VBAndCs commented 5 years ago

There is a workaround for the HTML5 support: When you want to edit the HTML code, Right-click the .vbxml.vb file in the solution explorer, click the "open with" command from the context menu, then choose "Html Editor". This will give you intellisense supports for HTML, js, and css. After you finished, close the page, and double click on its name in the solution explorer to open it with VB editor, so you can edit vb code! I find this solution effective regarding the zero cost paid! I will try to construct a demo site with Vazor to test it in action. But I think it is born mature, because it is just a combination of existing stable .net parts!

zspitz commented 5 years ago

Does VB.NET support Intellisense from an XML schema? If yes, then perhaps including a schema would be a good solution. Further down the line, I could imagine the default editor for .vbxml.vb files automatically using the said schema, even if it's not explicitly declared in the file.

VBAndCs commented 5 years ago

Does VB.NET support Intellisense from an XML schema?

It is supposed to, but I was trying all day with no luck. Maybe I did something wrong, or this feature lost while writing Roslyn. I files an issue about that few hours ago, and waiting for response: https://github.com/dotnet/roslyn/issues/34816

Further down the line, I could imagine the default editor for .vbxml.vb files automatically using the said schema, even if it's not explicitly declared in the file.

It took my a while to fine an Html5.xsd file, but it doesn't include java script. I doubt Intellisense will show object tips and method signatures! So, I mainly put this solution as the last resort. Hat I am trying to so and not there yet is this: https://docs.microsoft.com/en-us/visualstudio/extensibility/supporting-multiple-document-views?view=vs-2019 I want to open the vbxml file in a frame that has tabs (vb and html) at the bottom. It will need less effort from the users, and will use MS intrinsic html editor with full intellisense support and no worry about futue changes in HTML standards. But I can't find an easy sample build upon yet.

VBAndCs commented 5 years ago

Vazor now have project and item templates: Download this file: https://github.com/VBAndCs/Vazor/blob/master/VazorTemplateSetup.zip?raw=true then unzip it. Double-click the file VazorTemplateSetup.vsix to setuo the Vazor templates: 1- A Vazor project template for ASP.NET MVC Core 2.2 . 2- A Vazor project template for ASP.NET MVC Core 3.0 . 3- A Vazor project template for ASP.NET Web Pages Core 2.2 . 4- A Vazor project template for ASP.NET Web Pages Core 3.0 . 5- A VazorView item template to add a new vazor view (.vazor and .vbxml files) to the MVC project. 6- A VazorPage item template to add a new vazor page (.cshtml, .cshtml.vb, .vazor and .vbxml files) to the Razor Pages project. After installation, open .net and create a new project. In the search box, write Vazor, and choose one of the 4 vazor project templates. In the project created, right-click a folder in solution explorer and select Add/New Item. From the dialoge box select VazorView (if this is an MVC project) or VazorPage (if this is a Razor Pages project).

VBAndCs commented 5 years ago

I have a new idea to implement Vazor view, called ZML. vbxml code is not compatable with Tag Helpers. I used workarounds to make it work, but the resulted code is longer than it should! So, I decided to expand my data template idea but in another direction: Writing structural code as xml tags, so we have a new ZML Razor layer built on tip of C# Razor! I already implemented it in Vazor 1.5 and hope it can be implemented in .NET, so please support this proposal (all details are there): https://github.com/aspnet/AspNetCore/issues/9270 Thanks.

VBAndCs commented 5 years ago

ZML 1.0 now has its own repo and NuGet.