IceCoffee.SimpleCRUD.DependencyInjection
1.4.1
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
.NET Framework 4.7
This package targets .NET Framework 4.7. The package is compatible with this framework or higher.
dotnet add package IceCoffee.SimpleCRUD.DependencyInjection --version 1.4.1
NuGet\Install-Package IceCoffee.SimpleCRUD.DependencyInjection -Version 1.4.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IceCoffee.SimpleCRUD.DependencyInjection" Version="1.4.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IceCoffee.SimpleCRUD.DependencyInjection --version 1.4.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: IceCoffee.SimpleCRUD.DependencyInjection, 1.4.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IceCoffee.SimpleCRUD.DependencyInjection as a Cake Addin #addin nuget:?package=IceCoffee.SimpleCRUD.DependencyInjection&version=1.4.1 // Install IceCoffee.SimpleCRUD.DependencyInjection as a Cake Tool #tool nuget:?package=IceCoffee.SimpleCRUD.DependencyInjection&version=1.4.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 was computed. 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. net9.0 is compatible. |
.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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.7
- IceCoffee.SimpleCRUD (>= 1.4.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
-
.NETStandard 2.0
- IceCoffee.SimpleCRUD (>= 1.4.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
-
net8.0
- IceCoffee.SimpleCRUD (>= 1.4.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
-
net9.0
- IceCoffee.SimpleCRUD (>= 1.4.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.