Coravel
Coravel is an open-source tool library for .NET Core that makes it easy to use advanced application features like scheduled tasks, caching, queues, events, broadcasting, and more!
Coravel helps developers quickly get their .NET Core applications up and running without sacrificing code quality.
It makes advanced application features accessible and easy to use by providing you with a simple, expressive, and straightforward syntax.
GitHub URL: https://github.com/jamesmh/coravel
Installation
dotnet add package coravel
Examples
Task Scheduling
Configuration
In the Startup.cs file of your .NET Core application, inside the ConfigureServices() method, add the following:
services.AddScheduler()
Usage
Then, in the Configure() method, you can use the scheduler:
var provider = app.ApplicationServices;
provider.UseScheduler(scheduler =>
{
scheduler.Schedule(
() => Console.WriteLine("Every minute during the week.")
)
.EveryMinute()
.Weekday();
});
Queuing
Configuration
In your Startup file, inside ConfigureServices():
services.AddQueue();
Usage
Inject an instance of the Coravel.Queuing.Interfaces.IQueue interface into a controller:
IQueue _queue;
public HomeController(IQueue queue) {
this._queue = queue;
}
Synchronous
public IActionResult QueueTask() {
this._queue.QueueTask(() => Console.WriteLine("This was queued!"));
return Ok();
}
Asynchronous
this._queue.QueueAsyncTask(async() => {
await Task.Delay(1000);
Console.WriteLine("This was queued!");
});
Caching
Configuration
In Startup.ConfigureServices():
services.AddCache();
This enables in-memory (RAM) caching.
Usage
To use caching, inject Coravel.Cache.Interfaces.ICache via dependency injection.
private ICache _cache;
public CacheController(ICache cache)
{
this._cache = cache;
}
Event Broadcasting
Coravel's event broadcasting allows listeners to subscribe to events occurring in your application.
Configuration
In the ConfigureServices method:
services.AddEvents();
Next, in the Configure method:
var provider = app.ApplicationServices;
IEventRegistration registration = provider.ConfigureEvents();
Register events and their listeners:
registration
.Register<BlogPostCreated>()
.Subscribe<TweetNewPost>()
.Subscribe<NotifyEmailSubscribersOfNewPost>();
Usage
Create a class implementing the Coravel.Events.Interfaces.IEvent interface. That's it!
An event is simply a data object that will be provided to each listener. It should expose the data associated with this particular event.
For example, a BlogPostCreated event should accept a BlogPost created, and then expose it via a public property.
public class BlogPostCreated : IEvent
{
public BlogPost Post { get; set; }
public BlogPostCreated(BlogPost post)
{
this.Post = post;
}
}
Create a new class that implements the Coravel.Events.Interfaces.IListener interface for the event you want to listen to. Note: Each listener can only be associated with one event.
The IListener interface requires you to implement HandleAsync(TEvent broadcasted).
Create a listener named TweetNewPost:
public class TweetNewPost : IListener<BlogPostCreated>
{
private TweetingService _tweeter;
public TweetNewPost(TweetingService tweeter){
this._tweeter = tweeter;
}
public async Task HandleAsync(BlogPostCreated broadcasted)
{
var post = broadcasted.Post;
await this._tweeter.TweetNewPost(post);
}
}
Mailing
Configuration
Install the NuGet package Coravel.Mailer, then scaffold some basic files:
- ~/Views/Mail/_ViewStart.cshtml - Configures mail views to use Coravel's email templates
- ~/Views/Mail/_ViewImports.cshtml - Allows you to use Coravel's view components
- ~/Views/Mail/Example.cshtml - Example mail view
- ~/Mailables/Example.cs - Sample mailable
In Startup.ConfigureServices():
services.AddMailer(this.Configuration);
Usage
Coravel uses Mailables to send emails. Mailables inherit Coravel.Mailer.Mail.Mailable and accept a generic type representing the model you wish to associate with the sent email.
using Coravel.Mailer.Mail;
using App.Models;
namespace App.Mailables
{
public class NewUserViewMailable : Mailable<UserModel>
{
private UserModel _user;
public NewUserViewMailable(UserModel user) => this._user = user;
public override void Build()
{
this.To(this._user)
.From("from@test.com")
.View("~/Views/Mail/NewUser.cshtml", this._user);
}
}
}
All configuration for a Mailable is done inside the Build() method. You can then call various methods like To and From to configure recipients, senders, etc.
If you're interested in .NET open-source projects, feel free to follow me for more.