.NET open source project Anet  GitHub address:


https://github.com/anet-team/anet

Anet is a .NET Core generic framework that is easy to use.
Its goal is to:

A .NET Core Common Lib, Framework and Boilerplate.

Its name comes from the first four letters of this definition: ANET.
Anet’s mission is to make .NET project development simple and fast.
It is suitable for developing WebAPIs, services and websites for microservices.

Anet says it’s a framework, it’s actually very simple, and there is almost no learning cost.

Why choose Anet

Many traditional .NET open source framework templates (such as ABP) are relatively heavy, costly to learn, and easy to use.
Anet is much easier to use, especially for rapid development of microservices.

Like other template frameworks, Anet encapsulates some utility classes that integrate the lightweight ORM framework Dapper.
But Anet has made some improvements to Dapper so that transactions can be handled independently at the business layer, and the data access layer does not need to care about transactions (examples below).

Use of Anet

Below are some examples of the use of Anet, which can be found in Anet’s GitHub repository.

Install the Nuget package before use:

Install-Package Anet
# or
dotnet add package Anet

Query operation

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. Add an operation

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. Update operation

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. Delete operation

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. Timed tasks

Anet encapsulates a JobScheduler that meets the needs of most task scheduling.
The following demonstrates how to implement a simple task round robin through Anet to simulate a task scheduling service that sends messages.
This example can also be found in the GitHub repository.

First create a Console (.NET Core) application, you need to install two packages of Anet first:

Install-Package Anet
Install-Package Anet.Job

To add a timing task, add an implementation of the IJob interface.
Add a MessageJob class here to make it implement the IJob interface, the code is as follows:

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

    public Task ExecuteAsync()
    {
        // send aysn message
        return Task.Run(() =>
        {
            _logger.LogInformation("sending...");
            Thread.Sleep(3000);
            _logger.LogInformation("success");
        });
    }

    public Task OnExceptionAsync(Exception ex)
    {
        _logger.LogError(ex,"error");
        return Task.FromResult(0);
    }
}

You should be concerned about is the

ExecuteAsync

way to execute your code in this method.

Then just initialize and configure it in the Main method of the Program.cs entry, for example:

// Init
App.Init((config, services) =>
{
    // Config binding
    Settings = new SettingsModel();
    config.Bind(Settings);

    // 
    services.AddTransient<MessageJob>();
});

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

logger.LogInformation("started");

// time task
Scheduler.StartNew<MessageJob>(Settings.JobIntervalSeconds);

logger.LogInformation("sending task started");

Scheduler.WaitForShutdown();

A simple messaging service is ready, and the sending task is executed every specified number of seconds.
The effect seen on the console after running is:

NET-open-source-project-Anet-introduction1

This example contains logging, the information on the console is temporary, you can also view the log files in the logs folder under the running directory.
Please go to GitHub for the full code.

Current status of Anet

Anet is just getting started and is in a minimum usable state.
It is currently only a general-purpose library, which encapsulates some commonly used classes (such as the Snowflake algorithm-based Id generator, user password encryption, etc.). It is not a framework, there are still many things to do..