Coravel是.NetCore中开源的工具库,可以让你使用定时任务,缓存,队列,事件,广播等高级应用程序变得轻而易举!...

Coravel是一个开源工具库,为.NET Core应用提供了定时任务、缓存、队列、事件广播等功能。通过简单的API,开发者可以快速集成并提升应用性能。例如,配置定时任务只需在Startup.cs中添加代码,然后使用调度器设定任务执行周期。对于队列,添加依赖注入即可实现任务的同步或异步处理。此外,Coravel还支持内存缓存、事件监听和邮件发送,极大地丰富了.NET Core应用的功能。
摘要由CSDN通过智能技术生成

Coravel

Coravel是.NetCore中开源的工具库,可以让你使用定时任务,缓存,队列,事件,广播等高级应用程序变得轻而易举!

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 mail 这将安装 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开源项目感兴趣可以持续关注我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值