Coravel
Coravel 是 .NET Core 中開源的工具庫,讓您可以輕鬆使用排程任務、快取、佇列、事件、廣播等高級應用程式功能!
Coravel 幫助開發人員在不影響程式碼品質的情況下,快速啟動和執行他們的 .NET Core 應用程式。
它透過提供簡單、富有表達力且直觀的語法,讓高級應用程式功能易於存取和使用。
GitHub 位址: https://github.com/jamesmh/coravel
安裝
dotnet add package coravel
範例
Task Scheduling
設定
在 .NET Core 應用程式的 Startup.cs 檔案中,在 ConfigureServices() 方法內,加入以下內容:
services.AddScheduler()
使用
然後在 Configure() 方法中,可以使用排程器:
var provider = app.ApplicationServices;
provider.UseScheduler(scheduler =>
{
scheduler.Schedule(
() => Console.WriteLine("Every minute during the week.")
)
.EveryMinute()
.Weekday();
});
Queuing
設定
在您的 Startup 檔案中,在 ConfigureServices():
services.AddQueue();
使用
將介面 Coravel.Queuing.Interfaces.IQueue 的一個實例注入到控制器:
IQueue _queue;
public HomeController(IQueue queue) {
this._queue = queue;
}
同步
public IActionResult QueueTask() {
this._queue.QueueTask(() => Console.WriteLine("This was queued!"));
return Ok();
}
非同步
this._queue.QueueAsyncTask(async() => {
await Task.Delay(1000);
Console.WriteLine("This was queued!");
});
Caching
設定
在 Startup.ConfigureServices():
services.AddCache();
這將啟用記憶體(RAM)快取。
使用
要使用快取,將 Coravel.Cache.Interfaces.ICache 透過依賴注入進行注入。
private ICache _cache;
public CacheController(ICache cache)
{
this._cache = cache;
}
Event Broadcasting(事件廣播)
Coravel 的事件廣播允許監聽器訂閱應用程式中發生的事件。
設定
在 ConfigureServices 方法中:
services.AddEvents();
接下來,在 Configure 方法中:
var provider = app.ApplicationServices;
IEventRegistration registration = provider.ConfigureEvents();
註冊事件及其監聽器:
registration
.Register<BlogPostCreated>()
.Subscribe<TweetNewPost>()
.Subscribe<NotifyEmailSubscribersOfNewPost>();
使用
建立一個實作介面 Coravel.Events.Interfaces.IEvent 的類別。就是這樣!
事件只是將提供給每個監聽器的資料物件。它應該公開與此特定事件相關聯的資料。
例如,一個 BlogPostCreated 事件應該接受 BlogPost 建立的資料,然後透過公開屬性暴露它。
public class BlogPostCreated : IEvent
{
public BlogPost Post { get; set; }
public BlogPostCreated(BlogPost post)
{
this.Post = post;
}
}
建立一個新類別,實作您將要監聽的事件 Coravel.Events.Interfaces.IListener 的介面。提示:每個監聽器只能與一個事件相關聯。
IListener 介面要求您實作 HandleAsync(TEvent broadcasted)。
建立一個名為 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
設定
安裝 NuGet 套件 Coravel.Mailer,並建立一些基本檔案:
- ~/Views/Mail/_ViewStart.cshtml- 設定郵件檢視以使用 Coravel 的電子郵件範本
- ~/Views/Mail/_ViewImports.cshtml- 允許您使用 Coravel 的檢視元件
- ~/Views/Mail/Example.cshtml- 範例郵件檢視
- ~/Mailables/Example.cs- 可郵寄範本
在 Startup.ConfigureServices():
services.AddMailer(this.Configuration);
使用
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);
}
}
}
Mailable 的所有設定都在該 Build() 方法中完成。然後,您可以呼叫各種方法,例如 To 和 From 來設定收件人、寄件人等。
如果大家對 .NET 開源專案感興趣,可以持續關注我。