Coravel
Coravel is an open source tool library in. NET Core that makes it easy for you to use advanced applications such as timed tasks, caching, queuing, events, and broadcasts!
Coravel helps developers quickly get up and running their. NET Core applications without compromising code quality.
It makes advanced application features easy to access and use by providing you with simple, expressive and straightforward syntax.
GitHub 地址: https://github.com/jamesmh/coravel
installation
dotnet add package coravel
example
Task Scheduling
-
- Configuration **
In the Startup.cs file of the. NET Core application, within the ConfigureServices () method, add the following:
services.AddScheduler()
-
- Use **
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, in ConfigureServices ():
services.AddQueue();
-
- Use **
将接口的一个实例Coravel.Queuing.Interfaces.IQueue注入到控制器
IQueue _queue;
public HomeController(IQueue queue) {
this._queue = queue;
}
-
- Sync **
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 will enable memory (RAM) caching.
-
- Use **
要使用缓存,将Coravel.Cache.Interfaces.ICache通过依赖注入进行注入。
private ICache _cache;
public CacheController(ICache cache)
{
this._cache = cache;
}
Event Broadcasting
Coravel's event broadcasts allow listeners to subscribe to events that occur in the 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>();
-
- Use **
Create a class Coravel.Events. Interfaces.IEvent. That's it!
Events are just data objects that will be provided to each listener. It should expose the data associated with this specific event.
For example, a BlogPostCreated event should accept what BlogPost created and then expose it through public attributes.
public class BlogPostCreated : IEvent
{
public BlogPost Post { get; set; }
public BlogPostCreated(BlogPost post)
{
this.Post = post;
}
}
创建一个新类,该类实现您将要监听的事件Coravel.Events.Interfaces.IListener的接口。提示:每个侦听器只能与一个事件相关联。
该 IListener 接口需要您实现HandleAsync(TEvent broadcasted)。
Create a listener called 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 **
安装 NuGet 包Coravel.Mailer,搭建一些基本文件:
- ~/Views/Mail/_ViewStart. cshtml-Configure the mail view to use Coravel's email template
- ~/Views/Mail/_ViewImports. cshtml-allows you to use Coravel's view component
- ~/Views/Mail/Example. cshtml-Example Mail View
- ~/Mailables/Example. cs-mailable samples
In Startup. ConfigureServices ():
services.AddMailer(this.Configuration);
-
- Use **
Coravel 使用Mailables发送邮件。Mailables 继承Coravel.Mailer.Mail.Mailable并接受一个泛型类型,该类型表示您希望与发送邮件相关联的模型。
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 of Mailable is completed in this Build () method. You can then call various methods, such as To and From, to configure recipients, senders, and so on.
If you are interested in.net open source projects, you can continue to pay attention to me.