Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beto-rodriguez/EntityViews
A source generator to auto generate View Models.
https://github.com/beto-rodriguez/EntityViews
Last synced: 3 months ago
JSON representation
A source generator to auto generate View Models.
- Host: GitHub
- URL: https://github.com/beto-rodriguez/EntityViews
- Owner: beto-rodriguez
- Created: 2024-01-26T15:44:50.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-02-24T04:04:39.000Z (9 months ago)
- Last Synced: 2024-06-12T12:17:54.356Z (5 months ago)
- Language: C#
- Homepage:
- Size: 319 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-dotnet-maui - EntityViews - rodriguez/EntityViews?style=flat-square)](https://github.com/beto-rodriguez/EntityViews/stargazers)|[![GitHub last-commit](https://img.shields.io/github/last-commit/beto-rodriguez/EntityViews?style=flat-square)](https://github.com/beto-rodriguez/EntityViews/commits) (UI)
README
# EntityViews
This project generates ViewModels based on the models of the application using source generators.
The generated ViewModels implement `INotifyPropertyChanged` and re-use any `DataAnnotations` attribute to simplify the client-side validation by reducing boilerplate code.
## Example
Imagine we have a WebApi to build a TodoList (the next class is also a valid class for EntityFramework):
```c#
public class TodoItem
{
public int Id { get; set; }[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;public bool IsComplete { get; set; }
}
```Now we need to build a client so the user can populate the ToDo list, this projects uses source generators to build a ViewModel based on the previous object, the generated code
will use the `Required` and `MaxLength` attributes to validate the object and quickly build the user interface.This project generates a lot of boring, repeating, and neccessary code for us. the auto-generated view model will look like:
```c#
public partial class TodoItemViewModel : INotifyPropertyChanged
{
private Dictionary _validationErrors = [];// ...
private string _name = string.Empty;
[Required]
[MaxLength(50)]
public string Name { get => _name; set => SetProperty(ref _name, value, nameof(Name)); }
public string NameError => GetError("Name");
public bool NameHasError => NameError.Length > 0;// ...
public event PropertyChangedEventHandler? PropertyChanged;
///
/// Validates the view model and returns true if there are no validation errors.
///
public bool IsValid()
{
// ...
}// ...
}
```Finally we can quickily validate user interfaces in a client app:
![val](https://github.com/beto-rodriguez/EntityViews/assets/10853349/a7f0005c-318f-40c7-91ff-00973b9e1922)
In this case, it is a Maui client defined with the next XAML:
```xml
```