GithubHelp home page GithubHelp logo

bubdm / easydata Goto Github PK

View Code? Open in Web Editor NEW

This project forked from korzhcom/easydata

0.0 0.0 0.0 3.71 MB

An open-source framework for quick and easy implementation of CRUD operations (both API and UI) in ASP.NET Core applications.

License: MIT License

C# 52.01% TypeScript 38.42% JavaScript 1.67% Batchfile 0.04% CSS 6.40% HTML 1.27% PowerShell 0.19%

easydata's Introduction

EasyData

Build .NET Build JS Nuget Npm
Build status Build status NuGet Npm

About

EasyData library lets you quickly build a UI for CRUD (Create, Read, Update, Delete) operations in any ASP.NET Core application that uses Entity Framework Core.

Basically, EasyData does the following two things:

  • First, it “reads” your DbContext object in order to obtain the necessary metadata.

  • Then, based on that metadata, it provides an API endpoint for all CRUD operations and renders the UI (pages and dialogs) that communicate with the API endpoint for data-management tasks.

The real advantage here is that whenever you change something in your DbContext (add a new DbSet or a property in the model class), the UI automatically adjusts to those changes.

So, as you can see, EasyData can be very useful for quick prototyping of any database-related ASP.NET Core project. In just minutes you'll have a working web app with all CRUD forms for your database.

Quick demo

EasyData quick demo

Getting started

First of all, to test EasyData you can open and run one of the sample projects available in this repository.

Installing EasyData to your own project takes the following 3 simple steps:

1. Install EasyData NuGet packages

  • EasyData.AspNetCore
  • EasyData.EntityFrameworkCore.Relational

2. Add EasyData middleware in Startup.Configure

using EasyData.Services;
.    .    .    .    .

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapEasyData(options => {
            options.UseDbContext<AppDbContext>();
        });

        endpoints.MapRazorPages();
    });

In the middleware options we also specify the type of DbContext object that will be used as the source of the metadata.

3. Set up a catch-all page for all CRUD operations

If you're using Razor Pages, add a new page (for example EasyData.chstml). If it’s MVC, you'll need a controller and a view. This page will "catch" all URLs that begin with a certain prefix (/easydata by default but it's configurable). So, we use a special catch-all parameter in the route definition ("/easydata/{**entity}").

We also add EasyData styles and the script file (easydata.min.js), which renders the data-management UI and handles all CRUD operations on the client-side.

@page "/easydata/{**entity}"
@{
    ViewData["Title"] = "EasyData";
}

<link rel="stylesheet" href="https://cdn.korzh.com/ed/1.3.0/easydata.min.css" />

<div id="EasyDataContainer"></div>

@section Scripts {
    <script src="https://cdn.korzh.com/ed/1.3.0/easydata.min.js" type="text/javascript"></script>
    <script>
        window.addEventListener('load', function () {
            new easydata.crud.EasyDataViewDispatcher().run()
        });
    </script>
}

That’s it. Now you can run your web app, open the /easydata URL and enjoy CRUD functionality.

Main features

1. Declarative approach

All aspects of your CRUD UI are controlled by the data structure defined in DbContext. If you need to tune it up (for example, to hide a table or some fields, or, perhaps, to change their names), there are special attributes for your model classes and properties with which to do that.

2. Automatic UI rendering

All data forms and dialogs are rendered automatically by EasyData.JS script according to the metadata acquired from the DbContext and your annotations on model classes and their properties.

The script can be used with any framework or library used on the client-side, such as Razor Pages, MVC Views, Angular, React, Vue, etc.

3. Ad hoc data filtering

In the data view mode, EasyData provides a data-filtering functionality, which works out of the box and requires no additional setup or coding.

Advanced tasks

Sometimes you may need to make some adjustments to the default behavior of EasyData. Here we listed the solutions for the most common problems.

Filtering entities (tables) and their attributes (fields)

By default, EasyData works with all entities (tables) defined in your DbContext. As well as with all fields in those tables. However, very often you need to hide some tables or fields from your end-users. You can do it using MetaEntity and MetaEntityAttr annotations that can be specified for model classes and properties correspondingly.

Here is an example of how to hide the table defined by the Customer model class:

[MetaEntity(false)]
public class Customer
{
 .   .   .   .
}

The same approach (but with MetaEntityAttr attribute now) we can use to hide some property (field):

public class User
{
    [MetaEntityAttr(false)]
    public string PasswordHash { get; set; }
}

Both these annotations also have other properties that allow you to adjust the way your tables and fields are represented in the CRUD UI.

For example, the following line:

[MetaEntity(DisplayName = "Client", DisplayNamePlural = "Clients", Description = "List of clients")]
public class Customer
{
 .   .   .   .
}

will set the display names and the description for the Customers table.

Here is another example, now for a property in some model class:

public class BlogPost 
{
    [MetaEntityAttr(DisplayName = "Created", Editable = false, ShowOnView = true, ShowInLookup = false)]
    public DateTime DateCreated { get; set;}
    .    .    .    .    .
}

Here we change the default display name for this field, make it non-editable (since this value is set on record creation and it can’t be changed later), and tell EasyData to show this field in the main view (with data table) but hide it in lookup dialogs.

MetaEntityAttr annotation also has ShowOnCreate and ShowOnEdit properties that allow you to show/hide the field from the "Create Record" or "Edit Record" dialog, respectively.

Changing the default endpoint

The default EasyData API endpoint is /api/easydata/ but it’s very easy to change it to any possible path.

Server-side configuration:

app.UseEndpoints(endpoints => {
    endpoints.MapEasyData(options => {
        options.Endpoint = "/api/super-easy-crud";
        options.UseDbContext<ApplicationDbContext>();
    });
     .     .     .     .     .
});

On the client-side we can pass some options (including the endpoint) to the dispatcher’s constructor:

    <script>
        window.addEventListener('load', function () {
            new easydata.crud.EasyDataViewDispatcher({
                endpoint: '/api/super-easy-crud'
            }).run()
        });
    </script>

One entity CRUD page

Sometimes you don't need CRUD for all entities in your database but only for a few of them (or even only one). So, you don't need to show that root page with entity selection. You just can start with a data table view for one particular entity.

Especially for this case, there is rootEntity option in EasyDataViewDispatcher class. If it's set, EasyData will no show the default root page with the list of entities but will render the view page for the specified entity (table) instead.

<script>
    window.addEventListener('load', function () {
        new easydata.crud.EasyDataViewDispatcher({
            rootEntity: 'Order'
        }).run()
    });
</script>

Display Formats

It’s also possible to set the display format for each entity attribute (table field). The format looks like {0:XX} where XX here is a format string that has the same meaning as in string.Format function.

Beloew you will find a few examples of using display formats.

This one tells EasyData to use the default "Long Date" format for OrderDate values:

[MetaEntityAttr(DisplayFormat = "{0:D}")]
public DateTime? OrderDate { get; set; }

Here we use a custom format for date values:

[MetaEntityAttr(DisplayFormat = "{0:yyyy-MMM-dd}")]
public DateTime? OrderDate { get; set; }

Here we make it to use a currency format with 2 decimal digits

 [MetaEntityAttr(DisplayFormat = "{0:C2}")]
 public decimal Freight { get; set; }

With this format EasyData will show only digits (no grouping by thousands) and will add leading 0-s up to 8 digits totally (if necessary)

 [MetaEntityAttr(DisplayFormat = "{0:D8}")]
 public int Amount { get; set; }

FAQ

Q: What versions of .NET and ASP.NET (Core) does EasyQuery support?

A: Currently, EasyData supports .NET Core 3.1 and .NET 5 and, obviously, all versions of ASP.NET Core and Entity Framework Core that can work with these versions of .NET (Core). It’s not a great deal to add support for previous versions of .NET Core or even .NET Framework 4.x. If you really need it, please create a GitHub issue about that.

Q: I don’t like annotations. They ruin my pure data structures with some implementation-specific code. Do you support a Fluent API approach for setting all those filters, constraints, validators, and value editors?

A: Not yet. You can add a new GitHub issue about that to make it happen faster.

Contact us

If you want to be the first to know about project updates, just follow Sergiy on Twitter.

If you have a feature request or found a bug in EasyData, please submit an issue. We will try to fix the problem or implement new functionality (if the request is relevant) as soon as possible. However, please don't be too demanding with your requests :). Remember that EasyData is an open-source project, and we maintain it in our spare time.

easydata's People

Contributors

antifree avatar korzh avatar

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.