C# is often linked to being an unpopular language for rapid application development because it is strongly typed and is partially used in multinational companies, where for some reason, which I don’t quite understand, that means you can’t make software quickly.
One of the complaints is that we have to manually build the entire admin section, which means a lot of time, something that doesn’t happen in other languages like Ruby, where you just add a gem (equivalent to a library) and suddenly you have the whole database administration panel in just a few lines of code with ActiveAdmin
.
In this post, we are going to explore the EasyData library for .NET.
1 - What is EasyData?
EasyData is a library in .NET that lets us create an interface to access the database from the front end.
You might think that doing this is crazy, but it really isn’t. The vast majority of startups, if not all, focus on bringing value to the user, allowing them to create and edit items, etc. Only the user operations are programmed. But sometimes users make mistakes, and those mistakes can’t be undone by the user themselves.
For example, in an app like Airbnb, if you delete one of your rooms by mistake and wanted to delete another, you can’t do anything as a user, so you have to recreate it, losing all your history, etc.
Alternatively, you can contact support and see if they can fix it.
Assuming nobody does hard deletes anymore, the support person could say yes, it can be fixed, but it’ll take some time. The reason is simple: someone needs to go into the database server and manually execute a command.
But there’s another option: having an internal-only front end where we can control this information. This is where EasyData comes in, or ActiveAdmin if you use Ruby.
2 - Implementing EasyData in C#
For this example, we’ll add the library to an existing codebase. Obviously, you can add it to a new project, but it’s always better to see it with an existing one.
For this demo, we’ll use the code from my book The Complete Full Stack Development Guide with .NET. Here we have a website where we can create feature flags.
As shown in the image, we can create, edit, and delete flags, basically, a CRUD. But for example, if you delete a Feature Flag, there’s no way to reactivate it.
In this case, it’s not a big issue since a feature flag is easy to replace. But if you have hotel descriptions you spent hours on, or longtime statistics, it can be a real problem.
So, what you need to do is install the EasyData library, specifically two packages that you can download from NuGet.
EasyData.AspNetCore
EasyData.EntityFrameworkCore.Relational
After installing these two packages, just a small configuration part remains: add the EasyData middleware to your app configuration:
app.MapEasyData(options => {
options.UseDbContext<ApplicationDbContext>();
});
And of course, a page to render the view with the content to show, so you need to create the controller and the view:
[Route("easydata")]
public class EasyDataController : Controller
{
private readonly ILogger<EasyDataController> _logger;
public EasyDataController(ILogger<EasyDataController> logger)
{
_logger = logger;
}
[Route("{**entity}")]
public IActionResult Index(string entity)
{
if (string.IsNullOrEmpty(entity))
{
_logger.LogInformation("Index page");
}
else
{
_logger.LogInformation($"{entity} page");
}
return View();
}
}
@{
ViewData["Title"] = "Entities";
}
<div id="EasyDataContainer"></div>
@section Scripts {
<link rel="stylesheet" href="https://cdn.korzh.com/ed/1.4.18/easydata.min.css" />
<script src="https://cdn.korzh.com/ed/1.4.18/easydata.min.js" type="text/javascript"></script>
<script>
window.addEventListener('load', function () {
new easydata.crud.EasyDataViewDispatcher().run()
});
</script>
}
Note: this example uses MVC, but if you use Blazor, Razor, or even React on the front end, there are different options, all documented in the official GitHub.
Now we can access /easydata where, by default, we’ll have access to all entities in our application.
And that’s it. Now we can access each of the entities individually.
Note: in this example, the page has no authentication and you should add it. Also, when you modify an entity, you’re doing it directly in the database, which means you’re bypassing any business rules that exist in the code, only the logic in the DbContext will be executed.
If we access one of the entities we can see that it loads the full list:
Note: for demonstration purposes, I’ve removed the following code from the DbContext:
modelBuilder.Entity<FlagEntity>()
.HasQueryFilter(a => !a.IsDeleted
&& a.UserId == flagUserDetails.UserId);
From here, we can do CRUD operations directly, only following the rules of the database and entities, so obviously we won’t use this for clients, but only internally for admins or staff.
Its features don’t end here, it actually has many, all available on its GitHub. Just to mention the most important: different formats for dates and decimals, ignoring entities so they can’t be edited, hiding properties from some entities, or even renaming what you see in the interface.
3 - Is EasyData what you need?
Figuring out if EasyData or similar is what your application needs really depends. The big advantage of using this library is that it lets you focus just on user logic, bringing value to users, while leaving admin management simple but totally functional.
Obviously it won’t work for all scenarios. Say you work at a store and inventory is sometimes off. You could build an interface to fix the stock, which takes time to implement and maintain with all its logic, or you could add EasyData and make those manual changes in seconds. It all depends on which part of your system you want to prioritize.
You might think using tools like this is too risky, but the reality is that AirBnB relied on ActiveAdmin (the Ruby alternative to EasyAdmin) until quite recently, when it was already valued at several billion dollars. Trust me, if they could use it, so can you.
If there is any problem you can add a comment bellow or contact me in the website's contact form