Git Product home page Git Product logo

entityviews's Introduction

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):

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:

public partial class TodoItemViewModel : INotifyPropertyChanged
{
    private Dictionary<string, string> _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;

    /// <summary>
    /// Validates the view model and returns true if there are no validation errors.
    /// </summary>
    public bool IsValid()
    {
        // ...
    }

    // ...
}

Finally we can quickily validate user interfaces in a client app:

val

In this case, it is a Maui client defined with the next XAML:

<Label Text="Name" />

<Entry Text="{Binding Name}">
    <Entry.Triggers>
        <DataTrigger TargetType="Entry" Binding="{Binding NameHasError}" Value="True">
            <Setter Property="BackgroundColor" Value="#50ff0000"/>
        </DataTrigger>
    </Entry.Triggers>
</Entry>

<Label Text="{Binding NameError}" TextColor="Red"/>

<Button Text="Save" Command="{Binding SaveCommand}"/>

entityviews's People

Contributors

beto-rodriguez avatar

Stargazers

 avatar  avatar fred avatar Jorge Ramos avatar Mohamed Hazem avatar Narasimha Yaddanapudi avatar Russell Camo avatar  avatar angelofb avatar

Watchers

 avatar  avatar

Forkers

fredatgithub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.