dotnet / dotnet/vblang

[Proposal] Built-in MVVM pattern

Open
#460 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I've been thinking for a year, that why VB isn't paid attention by MS (but F# do!). Now I think that it is because VB is very much like C#, and it is more easy for a C/C++/Java programmer to learn C# than VB. After they have learned C#, they found they needn't learn VB at all - there's nearly nothing that VB can do but C# cannot! Therefore, I suggest we should think about more amazing and crazy ideas to save VB from being marginalized and discarded.

Usually it is a bit complicated to write a observable class in VB. To simplify it, I suggest a new API: `ObservablePropertyAttribute`, and we can simply write
``` vb.net
Class MyObservableClass

Public Property Title As String
End Class
```
And it will be expanded by the compiler to
``` vb.net
Class MyObservableClass
Inherits ObservableObject
Private _Title As String
Public Property Title As String
Get
Return _Title
End Get
Set(value As String)
SetProperty(_Title, value)
End Set
End Property
End Class
```
where `ObservableObject` is also a new pre-defined class just like [this](https://github.com/jamesmontemagno/mvvm-helpers/blob/master/MvvmHelpers/ObservableObject.cs).

A class with `ObservableProperty` property can only inherits `ObservableObject` or its derived class. `ObservableObject` should be a public class in a independent package.

If you want a callback or validation, you can declare the functions and pass them to the attribute like
``` vb.net
Class MyObservableClass

Public Property Title As String
' Sub TitleChanged and Function TitleValidation
End Class
```
And it'll be expanded to
``` vb.net
Class MyObservableClass
Inherits ObservableObject
Private _Title As String
Public Property Title As String
Get
Return _Title
End Get
Set(value As String)
SetProperty(_Title, value, onChanged:=AddressOf TitleChanged, validateValue:=AddressOf TitleValidation)
End Set
End Property
End Class
```

Sometimes we don't write an observable object but a dependency object. I suggest a new API: `DependencyPropertyAttribute`
``` vb.net
Class MyDependencyClass

Public Property Title As String = "Hello world!"
End Class
```
And it'll be expanded to
``` vb.net
Class MyDependencyClass
Inherits DependencyObject
Public Shared Readonly TitleProperty As DependencyProperty = DependencyProperty.Register(NameOf(Title), GetType(String), GetType(MyDependencyClass), New PropertyMetadata("Hello world!"))
Public Property Title As String
Get
Return GetValue(TitleProperty)
End Get
Set(value As String)
SetValue(TitleProperty, value)
End Set
End Property
End Class
```
The types `DependencyObject`, `DependencyProperty`, `PropertyMetadata` may be different when targeting UWP or WPF, and it could be specified by command line args to vbc.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.