RepoDb 1.0.1
Prefix ReservedSee the version list below for details.
dotnet add package RepoDb --version 1.0.1
NuGet\Install-Package RepoDb -Version 1.0.1
<PackageReference Include="RepoDb" Version="1.0.1" />
paket add RepoDb --version 1.0.1
#r "nuget: RepoDb, 1.0.1"
// Install RepoDb as a Cake Addin
#addin nuget:?package=RepoDb&version=1.0.1
// Install RepoDb as a Cake Tool
#tool nuget:?package=RepoDb&version=1.0.1
How to use RepoDb
Creating an Entity
It is required that you have your entity class inherit from DataEntity class. It is also advisable that you must create an explicit interface that implements the IDataEntity interface.
See this class as a sample entity.
public interface IStock : IDataEntity
{
int Id { get; set; }
string ProductId { get; set; }
string Name { 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; }
...
}
- 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).
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 an Entity
- 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, notice the operation (>=)
var stock = stockRepository.Query(new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 50) });
Updating an Entity
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 an Entity
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 an Entity
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 an Entity
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 | 563 | 2/26/2024 |
1.13.1 | 129,378 | 3/16/2023 |
1.13.0 | 70,633 | 11/2/2022 |
1.12.10 | 303,424 | 2/18/2022 |
1.12.9 | 30,534 | 9/27/2021 |
1.12.8 | 81,631 | 9/23/2021 |
1.12.7 | 168,741 | 2/6/2021 |
1.12.6 | 4,574 | 1/13/2021 |
1.12.5 | 9,653 | 12/30/2020 |
1.12.4 | 68,059 | 10/3/2020 |
1.12.3 | 3,067 | 9/29/2020 |
1.12.2 | 1,302 | 9/28/2020 |
1.12.1 | 1,371 | 9/25/2020 |
1.12.0 | 4,394 | 9/24/2020 |
1.3.2-alpha1 | 282 | 2/26/2024 |
- Initial Release for RepoDb (.Net Extension)
- Supported Dynamic DbConnection
- Supported Mapping Command Type (Entity Level)
- Supported ExecuteScalar, ExecuteNonQuery, ExecuteReader (DataEntity and ExpandoObject)
- Supported Primary Attribute (IsIdentity)
- Supported BaseRepository that uses the functionality of the DbRepository
- Supported the GetSqlStatement (Entity Level)
- Supported Object Driven approach when doing a query, delete, update and merge
- Supported Merge Method (Entity Level only)
- Supported Bulk Insert (SqlBulkCopy only)
- Supported to get the Connection at all Repositories
- Supported Transaction Handlers
- Supported Async methods
- Supported DbRepository property at the BaseRepository
- Supported Dynamic object returns for ExecuteReaderEx
- Supported code-guards at the DbRepository (Queryable, Insertable, Deletable, Mergeable, Updateable)
- Supported Type Mapper