/IceCoffee.SimpleCRUD

Provides simple CRUD repository that reduces CRUD operations to a single line of code. It also greatly simplifies executing CRUD operations with filters, executing full queries, and executing stored procedures. It supports Async and non-Async in .NET Framework 4.7+ or .NET Standard2.0+ or .NET6+.

Primary LanguageC#MIT LicenseMIT

IceCoffee.SimpleCRUD

Package NuGet Stable Downloads
IceCoffee.SimpleCRUD IceCoffee.SimpleCRUD IceCoffee.SimpleCRUD
IceCoffee.SimpleCRUD.DependencyInjection IceCoffee.SimpleCRUD.DependencyInjection IceCoffee.SimpleCRUD.DependencyInjection

Description

Provides simple CRUD repository that reduces CRUD operations to a single line of code. It also greatly simplifies executing CRUD operations with filters, executing full queries, and executing stored procedures. It supports Async and non-Async in .NET Framework 4.7+ or .NET Standard2.0+ or .NET6+.

DB Provider Supported: SQL Server, SQLite, PostgreSQL, MySQL

Installation

$ dotnet add package IceCoffee.SimpleCRUD
$ dotnet add package IceCoffee.SimpleCRUD.DependencyInjection # (optional) If you want use DI

Installing Database Providers

SQL Server

$ dotnet add package Microsoft.Data.SqlClient

SQLite

$ dotnet add package Microsoft.Data.SQLite

PostgreSQL

$ dotnet add package Npgsql

MySQL

$ dotnet add package MySql.Data

Docs

Metadata attributes

  • [Table] By default the database table name will match the model name but it can be overridden with this.
  • [PrimaryKey] Use for primary key.
  • [Column] By default the column name will match the property name but it can be overridden with this.
  • [IgnoreInsert] This column is not ignore insert.
  • [IgnoreSelect] This column is not ignore select.
  • [IgnoreUpdate] This column is not ignore updated.
  • [NotMapped] For "logical" properties that do not have a corresponding column and have to be ignored by the SQL Generator.

Quick Examples

1. Introducing namespaces

using IceCoffee.SimpleCRUD;
using IceCoffee.SimpleCRUD.OptionalAttributes;

2. Configure database connection options

string connectionString = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";
DbConnectionFactory.Default.ConfigureOptions(connectionString, DbType.SQLite);

4. Define entity

[Table("Foo")]
public class Foo
{
    [PrimaryKey]
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Sex { get; set; }
    [Column("[Address]")]
    public string Address { get; set; }
}

5. CRUD

var repository = new GenericRepository<Foo>();

// Get by ID
var entity = repository.GetById(1);

// Get all records
var entities = repository.GetAll();

// Get paged list by limit and offset
var page1 = repository.GetPagedList(1, 5);
var page2 = repository.GetPagedList(2, 5);

// Delete
int count = repository.Delete(new Foo(){ Id = 1 });
count = repository.DeleteById(2);
count = repository.DeleteByIds(new int[] { 1, 2 });

// Update
count = repository.Update(entity);

// Insert
count = repository.Insert(new Foo() { Id = 3, Name = "Name3", … … });

// Bulk insert with transaction
entities = new Foo[] { new Foo() { Id = 4, Name = "Name4" }, new Foo() { Id = 5, Name = "Name5" } };
count = repository.Insert(entities, true);

// Insert or ignore
count = repository.InsertOrIgnore(entities);

// Insert or replace
count = repository.InsertOrReplace(entities);

// Your code 

5.1 Access multiple databases

string dbAliase1 = "dbStoreA";
string dbAliase2 = "dbStoreB";
DbConnectionFactory.Default.ConfigureOptions(dbAliase1, connectionString1, DbType.SQLite);
DbConnectionFactory.Default.ConfigureOptions(dbAliase2, connectionString2, DbType.SQLite);

var repository1 = new GenericRepository<Foo>(dbAliase1); 
var repository2 = new GenericRepository<Foo>(dbAliase2);
// Your code

6. Unit of work

using (IUnitOfWork uow = UnitOfWorkFactory.Default.Create())
{
    var repository = uow.GetGenericRepository<Foo>();
    repository.Insert(new Foo() { Id = 1, Name = "Name1" });
    repository.Update(new Foo() { Id = 1, Name = "Name2" });
    uow.Commit();
}

6.1 Access multiple databases

string dbAliase = "dbStoreA";
using (IUnitOfWork uow = UnitOfWorkFactory.Default.Create(dbAliase))
{
    // Your code
}

Example with Asp.Net Core and D.I

Implements FooRepository

public interface IFooRepository : IRepository<Foo>
{
    // Your code
}

public class FooRepository : RepositoryBase<Foo>, IFooRepository
{
    public FooRepository(IDbConnectionFactory dbConnectionFactory) : base(dbConnectionFactory)
    {
    }

    // Your code
}

Configure Services

// Register repositories
services.AddDbConnection((options) =>
{
    options.ConnectionString = "";
    options.DbType = DbType.SQLite;
}).AddRepositories(assembly);

Use in API Controller

[Route("[controller]")]
public class FooController : ControllerBase
{
    private readonly IFooRepository _fooRepository;
    public FooController(IFooRepository fooRepository)
    {
        _fooRepository = fooRepository;
    }

    [HttpGet("{id}")]
    public ActionResult<Foo> Get([FromRoute] int id)
    {
        return _fooRepository.GetById(id);
    }

    [HttpPost]
    public ActionResult<Foo> Get([FromBody] Foo model, [FromServices] IUnitOfWorkFactory unitOfWorkFactory)
    {
        using (IUnitOfWork uow = unitOfWorkFactory.Create(dbAliase))
        {
            var repository = uow.GetRepository<IFooRepository>();
            // Your code
            uow.Commit();
        }
    }
}

To Be Continued

👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍

License

All contents of this package are licensed under the MIT license.