All Projects → anet-team → Anet

anet-team / Anet

Licence: mit
A .NET Core Common Library , Framework and Boilerplate.

Projects that are alternatives of or similar to Anet

Bing.netcore
Bing是基于 .net core 2.0 的框架,旨在提升团队的开发输出能力,由常用公共操作类(工具类、帮助类)、分层架构基类,第三方组件封装,第三方业务接口封装等组成。
Stars: ✭ 758 (+197.25%)
Mutual labels:  framework, aspnetcore, netcore
Revo
Event Sourcing, CQRS and DDD framework for C#/.NET Core.
Stars: ✭ 162 (-36.47%)
Mutual labels:  framework, aspnetcore, netcore
XAF Security E4908
This repository contains examples for Role-based Access Control, Permission Management, and OData / Web / REST API Services for Entity Framework and XPO ORM
Stars: ✭ 47 (-81.57%)
Mutual labels:  aspnetcore, netcore
X.Extensions.Logging.Telegram
Telegram logging provider
Stars: ✭ 32 (-87.45%)
Mutual labels:  aspnetcore, netcore
Awesome-Nuget-Packages
📦 A collection of awesome and top .NET packages sorted by most popular needs.
Stars: ✭ 87 (-65.88%)
Mutual labels:  aspnetcore, netcore
Lad
👦 Lad is the best Node.js framework. Made by a former Express TC and Koa team member.
Stars: ✭ 2,112 (+728.24%)
Mutual labels:  framework, boilerplate
Devarchitecture
DevArchitecture Backend Project
Stars: ✭ 243 (-4.71%)
Mutual labels:  framework, aspnetcore
comcms
COMCMS_Core 版本
Stars: ✭ 32 (-87.45%)
Mutual labels:  aspnetcore, netcore
Aspnetboilerplate
ASP.NET Boilerplate - Web Application Framework
Stars: ✭ 10,061 (+3845.49%)
Mutual labels:  framework, aspnetcore
SignalR-Core-SqlTableDependency
Shows how the new SignalR Core works with hubs and sockets, also how it can integrate with SqlTableDependency API.
Stars: ✭ 36 (-85.88%)
Mutual labels:  aspnetcore, netcore
AspSqliteCache
An ASP.NET Core IDistributedCache provider backed by SQLite
Stars: ✭ 39 (-84.71%)
Mutual labels:  aspnetcore, netcore
Ark.Tools
Ark set of helper libraries
Stars: ✭ 20 (-92.16%)
Mutual labels:  aspnetcore, netcore
Knockout Spa
A mini but full-fledged SPA framework and boilerplate to build SPAs fast and scalable
Stars: ✭ 145 (-43.14%)
Mutual labels:  framework, boilerplate
SharpPlugs
.Net Core 鋒利扩展
Stars: ✭ 26 (-89.8%)
Mutual labels:  aspnetcore, netcore
Alpha
Craft your own web-based chatbot
Stars: ✭ 113 (-55.69%)
Mutual labels:  framework, boilerplate
grandnode2
Free, Open source, Fast, Headless, Multi-tenant eCommerce platform built with .NET Core, MongoDB, AWS DocumentDB, Azure CosmosDB, LiteDB, Vue.js.
Stars: ✭ 626 (+145.49%)
Mutual labels:  aspnetcore, netcore
awesome-dotnet-async
A curated list of awesome articles and resources to learning and practicing about async, threading, and channels in .Net platform. 😉
Stars: ✭ 84 (-67.06%)
Mutual labels:  aspnetcore, netcore
Abotx
Cross Platform C# Web crawler framework, headless browser, parallel crawler. Please star this project! +1.
Stars: ✭ 63 (-75.29%)
Mutual labels:  framework, netcore
Slim3
Slim Framework 3 Skeleton Application
Stars: ✭ 70 (-72.55%)
Mutual labels:  framework, boilerplate
mssql-restapi
A simple REST API for SQL Server, Azure SQL DB and Azure SQL DW using SMO on .NET Core 2.0
Stars: ✭ 33 (-87.06%)
Mutual labels:  aspnetcore, netcore

什么是 Anet

Anet 是一个 .NET Core 通用框架,特点是简单易用。它的定义是:

A .NET Core Common Lib, Framework and Boilerplate.

它的取名正是来自于这个定义的前面四个字母:ANET。Anet 的宗旨是使 .NET 项目开发变得简单和快速。它适用于面向现代化微服务开发 WebAPI、服务程序和网站。

为什么选择 Anet

很多传统的 .NET 开源框架模板(比如 ABP)都比较重,学习成本高,使用起来条条框框,比较麻烦。而 Anet 就简单易用得多,尤其适合面向微服务快速开发。

和其它模板框架一样,Anet 封装了一些实用工具类,集成了轻量 ORM 框架 Dapper。但 Anet 对 Dapper 做了一些改进,使得事务可以放在业务层独立处理,数据访问层则不需要关心事务。

Anet 的使用

下面贴一些 Anet 的使用示例,这些示例代码都可以在本仓库中找到。

使用前先安装 Nuget 包:

Install-Package Anet
# 或者
dotnet add package Anet

1. 查询操作

public class UserRepository : RepositoryBase<AnetUser>
{
    public UserRepository(Database db) : base(db)
    {
    }

    public Task<IEnumerable<UserResponseDto>> GetAllAsync()
    {
        var sql = "SELECT * FROM AnetUser;";
        return Db.QueryAsync<UserResponseDto>(sql);
    }

    public Task<UserResponseDto> GetByIdAsync(long id)
    {
        var sql = Sql.Select("AnetUser", new { Id = id });
        return Db.QueryFirstOrDefaultAsync<UserResponseDto>(sql);
    }
}

2. 新增操作

public class UserService
{
    private readonly UserRepository userRepository;
    public UserService(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

    public async Task CreateUserAsync(UserRequestDto dto)
    {
        var newUser = new AnetUser { UserName = dto.UserName };

        using (var tran = userRepository.BeginTransaction())
        {
            await userRepository.InsertAsync(newUser);

            // Other business logic code.

            tran.Commit();
        }
    }

    // ...(其它代码)
}

3. 更新操作

public class UserService
{
    private readonly UserRepository userRepository;
    public UserService(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

    public async Task UpdateUserAsync(long userId, UserRequestDto dto)
    {
        var user = await userRepository.FindAsync(userId);
        if (user == null)
            throw new NotFoundException();

        using(var tran = userRepository.BeginTransaction())
        {
            await userRepository.UpdateAsync(
                update: new { dto.UserName },
                clause: new { Id = userId });

            tran.Commit();
        }
    }

    // ...(其它代码)
}

4. 删除操作

public class UserService
{
    private readonly UserRepository userRepository;
    public UserService(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

    public async Task DeleteUserAsync(long id)
    {
        var rows = await userRepository.DeleteAsync(id);
        if (rows == 0)
            throw new NotFoundException();
    }

    // ...(其它代码)
}

5. 定时任务

Anet 封装了一个 Scheduler,它可以满足大部分任务调度的需求。下面演示如何通过 Anet 来实现一个简单任务轮循程序,模拟一个发送消息的任务调度服务。这个示例也可以在 GitHub 仓库中找到源代码。

首先创建一个 Console(.NET Core)应用,需要先安装 Anet 的两个包:

Install-Package Anet
Install-Package Anet.Job

要添加一个定时任务就添加一个 IJob 接口的实现。这里添加一个 MessageJob 类,使它实现 IJob 接口,代码如下:

public class MessageJob : IJob
{
    private readonly ILogger<MessageJob> _logger;
    public MessageJob(ILogger<MessageJob> logger)
    {
        _logger = logger;
    }

    public Task ExecuteAsync()
    {
        // 模拟异步发送消息
        return Task.Run(() =>
        {
            _logger.LogInformation("正在发送消息...");
            Thread.Sleep(3000);
            _logger.LogInformation("消息发送成功。");
        });
    }

    public Task OnExceptionAsync(Exception ex)
    {
        _logger.LogError(ex,"发送消息出错。");
        return Task.FromResult(0);
    }
}

你要关心的就是 ExecuteAsync 方法,把你的执行代码放在此方法中。

然后只需在 Program.cs 的入口 Main 方法中进行初始化和配置即可,例如:

// 初始化应用
App.Init((config, services) =>
{
    // 绑定配置
    Settings = new SettingsModel();
    config.Bind(Settings);

    // 注册服务
    services.AddTransient<MessageJob>();
});

var logger = App.ServiceProvider.GetRequiredService<ILogger<Program>>();

logger.LogInformation("程序已启动。");

// 启动定时轮循任务
Scheduler.StartNew<MessageJob>(Settings.JobIntervalSeconds);

logger.LogInformation("已启动消息发送任务处理程序。");

// 等待程序关闭
Scheduler.WaitForShutdown();

一个简单的消息发送服务就做好了,每隔指定秒数就会执行发送任务。此外,你还可以用 Scheduler.StartNewAt 方法进一步实现定时需求:

// 等同于 Scheduler.StartNew<MessageJob>(Settings.JobIntervalSeconds);
Scheduler.StartNewAt<MessageJob>(DateTime.Now, TimeSpan.FromSeconds(Settings.JobIntervalSeconds), true);

// 10秒钟后开启任务, 且只执行一次
Scheduler.StartNewAt<MessageJob>(DateTime.Now.AddSeconds(10), TimeSpan.Zero);

// 每天1点执行任务
// 第三个参数默认为 true, 指定当到达开始时间时, 是否立即执行一次任务
Scheduler.StartNewAt<MessageJob>(DateTime.Today.AddHours(1), TimeSpan.FromDays(1), false);

// 此任务无法执行, 因为10秒钟后不立即执行且不继续进行, 会抛出 ArgumentException 异常
// Scheduler.StartNewAt<MessageJob>(DateTime.Now.AddSeconds(10), TimeSpan.Zero, false);

运行后在控制台看到的效果是:

这个示例包含了记录日志,控制台上的信息都是临时的,你也可以查看运行目录下的 logs 文件夹中的日志文件。完整代码请在本仓库查看。

Anet 的目前状态

Anet 才刚起步,处在最小可用状态。它目前只是一个通用库,封装了一些常用的类(比如基于 Snowflake 算法的 Id 生成器、用户密码加密等),还算不上框架,还有很多事情要做。后面我也会写一些文章介绍这个项目。

但一个人的力量终究是有限的,特别希望大家能加入到这个项目中和我一起开发。

贡献者

Thanks goes to these wonderful people:


Liam Wang

Zae
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].