Skip to content

Latest commit

 

History

History
111 lines (92 loc) · 2.63 KB

README.md

File metadata and controls

111 lines (92 loc) · 2.63 KB

Hydrogen Repo

This package uses SqlKata as query builder, if you have doubts check its documentation.

Installation

dotnet add package Hydrogen.Repo
dotnet add package Hydrogen.Repo.Abstractions

Settings

var connectionBuilder = new NpgsqlConnectionStringBuilder()
{
    Host = "localhost",
    Port = 5432,
    Username = "postgres",
    Password = "1234567890",
    Database = "master"
};
        
var npgsqlConnection = new NpgsqlConnection(connectionBuilder.ConnectionString);

collection.AddScoped(_ => new HydrogenContext(connection, DbConnectionType.Npgsql));
collection.AddScoped<IHydrogenUnitOfWork, HydrogenUnitOfWork>(); // Only if you need transactions

Repositories

Create a model

The model interface, implements the Id field and a dynamic type.

public class User : IHydrogenModel<Guid>
{
      [AutoGenerateGuid]
      public Guid Id {  get; set; }
      public Guid RoleId { get; set; }
      public string Name { get; set; }
      public string LastName { get; set; }
      public string? Email { get; set; }
      public string? PhoneNumber { get; set; }
      public string Password { get; set; }
}

Create repository

The repository already implements several basic functions.

public interface IUserRepository : IHydrogenRepository<User, Guid>
{
    // Add your functions
}

The abstract class implements the functions of the base interface.

public class UserRepository : HydrogenRepository<User, Guid>, IUserRepository
{
    public UserRepository(HydrogenContext hydrogenContext) : base(hydrogenContext, "Users" //<- Table name)
    {
    }
}

Add to the service collection

var connectionBuilder = new NpgsqlConnectionStringBuilder()
{
    Host = "localhost",
    Port = 5432,
    Username = "postgres",
    Password = "1234567890",
    Database = "master"
};
        
var npgsqlConnection = new NpgsqlConnection(connectionBuilder.ConnectionString);

collection.AddScoped(_ => new HydrogenContext(connection, DbConnectionType.Npgsql));
collection.AddScoped<IHydrogenUnitOfWork, HydrogenUnitOfWork>();
collection.AddScoped<IUserRepository, UserRepository>();

Use in a service

private readonly IUserRepository _userRepository;

public OptionService(IUserRepository userRepository)
{
    _userRepository = userRepository;
}

public async Task<IEnumerable<UserDto>> GetAllAsync(CancellationToken ct)
{
    return _userRepository.GetAsync<UserDto>(ct);
}

Available functions

  • Any
  • Count
  • Insert
  • Bulk insert
  • Update
  • Delete
  • Get
  • Pagination
  • First
  • First or default