RepoDb 1.0.3
Prefix ReservedSee the version list below for details.
dotnet add package RepoDb --version 1.0.3
NuGet\Install-Package RepoDb -Version 1.0.3
<PackageReference Include="RepoDb" Version="1.0.3" />
paket add RepoDb --version 1.0.3
#r "nuget: RepoDb, 1.0.3"
// Install RepoDb as a Cake Addin
#addin nuget:?package=RepoDb&version=1.0.3
// Install RepoDb as a Cake Tool
#tool nuget:?package=RepoDb&version=1.0.3
How to use RepoDb?
Creating an Entity
- Map attribute defines which object to use in the database. The value of this attribute could be the actual name of the table or a stored procedure
- Map attribute second parameter is a CommandType. It tells whether the object is for TableDirect, StoredProcedure or Text base execution.
- Ignore attribute is a property level attribute used to signify whether the property will be ignore on a certain commands (SELECT, DELETE, UPDATE, INSERT, MERGE)
- Primary attribute is used to flag a property as the primary field of the table (or a result set).
public interface IStock : IDataEntity
{
int Id { get; set; }
string ProductId { get; set; }
string Code { get; set; }
string Name { get; set; }
int Quantity { get; set; }
DateTime CreatedDate { get; set; }
DateTime ModifiedDate { get; set; }
}
[Map("[dbo].[Stock]")]
public class Stock : DataEntity
{
[Ignore(Command.Insert | Command.Update)]
[Primary]
public int Id { get; set; }
public string ProductId { get; set; }
[Ignore(Command.Update)]
public string Code { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
[Ignore(Command.Insert | Command.Update)]
public DateTime CreatedDate { get; set; }
[Ignore(Command.Insert)]
public DateTime ModifiedDate { get; set; }
}
Creating a repository
- The class must inherit the BaseRepository<TEntity, TDbConnection> or DbRepository<TDbConnection>.
- The TEntity dynamic parameter type is used to define which entity objects to used on this repository.
- The TDbConnection dynamic parameter type is used to define the type of Connection object to be used when connecting to the database (SqlServer, Oracle, SqlLite, etc etc).
- The constructor of the BaseRepository accepts two parameters, the ConnectionString and the CommandTimeout.
public class StockRepository : BaseRepository<Stock, SqlConnection>
{
public StockRepository(ISettings settings)
: base(settings.ConnectionString)
{
}
}
Querying a data
- The first query will return the Stock data from the database where the Id is equals to 256;
- The second query will return the Stock data from the database where the Id is greater than or equals to 50;
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
Dynamic approach
var stock = stockRepository.Query({ Id = 256 });
Explicit approach
var stock = stockRepository.Query(new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 50) });
Updating a data
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });
stock.Name = $"{stock.Name} - some updates";
Default approach, uses the PrimaryKey
var affectedRows = stockRepository.Update(stock);
Dynamic approach
var affectedRows = stockRepository.Update(stock, new { Id = 256 });
Explicit approach
var affectedRows = stockRepository.Update(stock, new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 50) });
Deleting a data
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });
Dynamic approach
var affectedRows = stockRepository.Delete({ Id = 256 });
Explicit approach
var affectedRows = stockRepository.Delete(stock, new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 256) });
Merging a data
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });
stock.Name = $"{stock.Name} - some merge updates";
Default approach, uses the PrimaryKey as the qualifiers
var affectedRows = stockRepository.Merge(stock);
Explicit approach, uses the ProductId and Code fields as the qualifiers
var affectedRows = stockRepository.Merge(stock, Field.From("ProductId", "Code"));
Bulk inserting a data
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stocks = new List<Stock>();
stocks.Add(new Stock() { ... });
...
stocks.Add(new Stock() { ... });
var affectedRows = stockRepository.BulkInsert(stocks);
You can as well use the DbRepository<TDbConnection> object to avoid the binding on the Entity level repository. With this object, you can dynamically manipulate (CRUD) all the objects or entities that you wish to manipulate from the database.
Explicit Executions
var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);
Explicit ExecuteNonQuery Below is the way on how to delete a Stock with Id equals to 1
var stocks = repository.CreateConnection().ExecuteNonQuery("DELETE FROM [dbo].[Stock] WHERE (Id = @Id);", new { Id = 1 });
Explicit ExecuteReader
var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);
Suppose you have a Product table, it will return a Product with Name equals "Dairy Milk"
var product = repository.Query<Product>({ Name = "Dairy Milk" });
Get the stock of the Product milk in dynamic approach
var stock = repository.Query<Stock>({ ProductId = product.Id });
Use a SQL Statement directly to return the result at the desired object
var productsByDate = repository.CreateConnection().ExecuteReader<Product>("SELECT * FROM [dbo].[Product] WHERE (TransactionDate = @TransactionDate);", new { TransactionDate = DateTime.Parse("2018-01-01 00:00:00.000") });
Use the extended version of the ExecuteReader named ExecuteReaderEx to return the dynamic objects
var stocks = repository.CreateConnection().ExecuteReader("SELECT * FROM [dbo].[Stock];");
stocks.ForEach(stock => {
var current = (dynamic)product;
var name = current.Name;
});
Transactions with RepoDb
var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);
using (var connection = repository.CreateConnection())
{
using (var transaction = connection.BeginTransaction())
{
repository.ExecuteNonQuery("DELETE FROM [dbo].[Stock] WHERE CreatedDate > @CreatedDate);", new { CreatedDate = DateTime.Parse("2017-01-01") });
var stocks = repository.Query<Stock>({ ProductId = 12 });
stocks.ForEach(stock => {
stock.Quantity = 100;
});
repository.Merge<Stock>(stocks);
transaction.RollBack(); // RollBack Everything
}
}
Threading with RepoDb
Lastly, the RepoDb also supports the Threading by calling each "Async" method on the repository and on the DbConnection extensions. Below are the following methods that are Multi-Threaded.
- QueryAsync
- DeleteAsync
- InsertAsync
- UpdateAsync
- MergeAsync
- BulkInsertAsync
- ExecuteNonQueryAsync
- ExecuteScalarAsync
- ExecuteReaderAsync
- ExecuteReaderExAsync
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages (17)
Showing the top 5 NuGet packages that depend on RepoDb:
Package | Downloads |
---|---|
RepoDb.SqlServer
A hybrid .NET ORM library for SQL Server. |
|
RepoDb.SqlServer.BulkOperations
An extension library that contains the official Bulk Operations of RepoDb for SQL Server. |
|
RepoDb.PostgreSql
A hybrid .NET ORM library for PostgreSQL. |
|
RepoDb.MySql
A hybrid .NET ORM library for MySQL (using MySql.Data). |
|
RepoDb.PostgreSql.BulkOperations
An extension library that contains the official Bulk Operations of RepoDb for PostgreSQL. |
GitHub repositories (6)
Showing the top 5 popular GitHub repositories that depend on RepoDb:
Repository | Stars |
---|---|
mikependon/RepoDB
A hybrid ORM library for .NET.
|
|
itlibrium/DDD-starter-dotnet
Sample implementation and comparison of various approaches to building DDD applications. Useful as a baseline to quickly start a DDD dot net project.
|
|
TortugaResearch/DotNet-ORM-Cookbook
This repository is meant to show how to perform common tasks using C# with variety of ORMs.
|
|
bcssov/IronyModManager
Mod Manager for Paradox Games. Official Discord: https://discord.gg/t9JmY8KFrV
|
|
FransBouma/RawDataAccessBencher
Bench code which tests entity materialization speed of various .NET data access / ORM implementations
|
Version | Downloads | Last updated |
---|---|---|
1.13.2-alpha1 | 713 | 2/26/2024 |
1.13.1 | 143,505 | 3/16/2023 |
1.13.0 | 71,485 | 11/2/2022 |
1.12.10 | 308,955 | 2/18/2022 |
1.12.9 | 30,745 | 9/27/2021 |
1.12.8 | 82,972 | 9/23/2021 |
1.12.7 | 169,841 | 2/6/2021 |
1.12.6 | 4,629 | 1/13/2021 |
1.12.5 | 9,705 | 12/30/2020 |
1.12.4 | 68,999 | 10/3/2020 |
1.12.3 | 3,110 | 9/29/2020 |
1.12.2 | 1,326 | 9/28/2020 |
1.12.1 | 1,387 | 9/25/2020 |
1.12.0 | 4,430 | 9/24/2020 |
1.3.2-alpha1 | 296 | 2/26/2024 |
- Bug Fix: Error when converting the SqlDataReader data back to the class object when the value is DBNull.Value
- Minor code optimizations and refactoring