IceCoffee.SimpleCRUD
1.3.13
dotnet add package IceCoffee.SimpleCRUD --version 1.3.13
NuGet\Install-Package IceCoffee.SimpleCRUD -Version 1.3.13
<PackageReference Include="IceCoffee.SimpleCRUD" Version="1.3.13" />
paket add IceCoffee.SimpleCRUD --version 1.3.13
#r "nuget: IceCoffee.SimpleCRUD, 1.3.13"
// Install IceCoffee.SimpleCRUD as a Cake Addin #addin nuget:?package=IceCoffee.SimpleCRUD&version=1.3.13 // Install IceCoffee.SimpleCRUD as a Cake Tool #tool nuget:?package=IceCoffee.SimpleCRUD&version=1.3.13
IceCoffee.SimpleCRUD
Package | NuGet Stable | Downloads |
---|---|---|
IceCoffee.SimpleCRUD | ||
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages (1)
Showing the top 1 NuGet packages that depend on IceCoffee.SimpleCRUD:
Package | Downloads |
---|---|
IceCoffee.SimpleCRUD.DependencyInjection
Dependency injection extension for IceCoffee.SimpleCRUD. |
GitHub repositories
This package is not used by any popular GitHub repositories.