Zen is platform-independent .NET Core middleware that simplifies the implementation of data-driven, API-focused apps.
Not quite. Zen provides several features like ORM, caching, encryption and RESTful endpoints out of the box with zero setup in most cases. Think of it like a drop-in middleware that'll get your PoC app working with persistent storage to REST endpoints in no time and with minimal scaffolding - and you can add more features as the need arises.
Its core aspect is the ability to handle data for multiple connectors at the same time, regardless of the target database: Both relational and document (no-SQL) models are supported. Pull data from Oracle and write to Mongo, this kind of stuff.
To have it working straight out of the box we need Zen.Base
(the ORM handler) and a database adapter; let's pick Zen.Module.Data.LiteDB
for this example.
Extremely complicated example ahead. Pay attention!
- Create a class that inherits from the
Zen.Base.Module.Data<>
generic class; - Mark the property you want to use as a unique identifier with the [Key] Data Annotation attribute.
C# example:
using System.ComponentModel.DataAnnotations;
using System;
using Zen.Base.Module;
namespace Test
{
public class Person : Data<Person>
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
}
}
Congratulations! You created a LiteDB-backed ORM class. A default database was created, together with a collection to store entries.
Oh, REST! Right. So, once you decide you want to expose your ORM class data through a REST endpoint, do this:
- Add a reference to 📦 Zen.Web
- Add Zen to the Service collection and configure it with the Application builder:
using Zen.Base.Service.Extensions;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{ services.AddZen(); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ app.UseZen(); }
}
- Use the simplified Builder:
using Zen.Web.Host;
namespace Sample
{
public class Program
{
public static void Main(string[] args)
{ Builder.Start<Startup>(args); }
}
}
- Implement a class deriving from
Zen.Web.Data.Controller.DataController<>
, and assign a route to it:
[Route("api/people")]
public class PersonController : DataController<Person> {}
- ...and that's it.
DataController<>
implements standard REST operations (GET
, GET/{ID}
, POST
, DELETE
, PATCH
) as well as some useful extentions (e.g. GET/new
). These can be expanded by modules that offer functionalities like moderation and set versioning.
Now run your project, and reach the endpoint you specified. If you're running the sample provided (Sample02-REST
), you can try the following URLs:
https://localhost:5001/api/people
[{"Id":"1","Name":"Halie","LastName":"Ebert","Email":"[email protected]"},{"Id":"2","Name":"Meta","LastName":"Mayert","Email":"[email protected]"},{"Id":"3","Name":"Deonte","LastName":"Orn","Email":"[email protected]"},,(...)
https://localhost:5001/api/people/1
{"Id":"1","Name":"Halie","LastName":"Ebert","Email":"[email protected]"}
https://localhost:5001/api/people/new
{"Id":"fc8dbb27-42fe-45db-be09-18a84361d509","Name":null,"LastName":null,"Email":null}
The relational Data module wraps around Stack Exchange Dapper, an abusively fast IDbConnection interface extender.
MIT - a permissive free software license originating at the Massachusetts Institute of Technology (MIT), it puts only very limited restriction on reuse and has, therefore, an excellent license compatibility. It permits reuse within proprietary software provided that all copies of the licensed software include a copy of the MIT License terms and the copyright notice.
Check the LICENSE file for more details.