ToreAurstadIt.DapperHelpers.NetStandard
1.4.0
See the version list below for details.
dotnet add package ToreAurstadIt.DapperHelpers.NetStandard --version 1.4.0
NuGet\Install-Package ToreAurstadIt.DapperHelpers.NetStandard -Version 1.4.0
<PackageReference Include="ToreAurstadIt.DapperHelpers.NetStandard" Version="1.4.0" />
paket add ToreAurstadIt.DapperHelpers.NetStandard --version 1.4.0
#r "nuget: ToreAurstadIt.DapperHelpers.NetStandard, 1.4.0"
// Install ToreAurstadIt.DapperHelpers.NetStandard as a Cake Addin #addin nuget:?package=ToreAurstadIt.DapperHelpers.NetStandard&version=1.4.0 // Install ToreAurstadIt.DapperHelpers.NetStandard as a Cake Tool #tool nuget:?package=ToreAurstadIt.DapperHelpers.NetStandard&version=1.4.0
ToreAurstadIT.DapperHelpers.NetStandard
This Nuget package contains assorted helper methods for Dapper. To use the helper methods, add a using of the following namespace to your code first to access the public extension methods of the library.
using ToreAurstadIT.DapperUtils;
Now you can access the methods of this lib via your IDbConnection ADO.NET DB Connection instance. Extended readme here (or check out Fuget explorer): <a href='https://github.com/toreaurstadboss/DapperUtils/blob/main/ExtendedReadme.md'>ExtendedReadme.md</a>
Library methods
Inner joins with lambda expressions
Inner joins in Dapper has never been easier for those of us who wants to use Lambda expression and not write tedious manual sql. This gives us Intellisense / autocomplete and a way to express join relations. Supported are inner joins among 2-7 tables with overloads. The example below shows also how filters can be applied!
[Test]
public void InnerJoinSixTablesWithFilterWithoutManualSqlReturnsExpected()
{
var joinedproductsandcategory = Connection.InnerJoin(
(Order o, OrderDetail od) => o.OrderID == od.OrderID,
(Order o, Employee e) => o.EmployeeID == e.EmployeeID,
(OrderDetail od, Product p) => od.ProductID == p.ProductID,
(Product p, Category c) => p.CategoryID == c.CategoryID,
(Product p, Supplier s) => p.SupplierID == s.SupplierID,
new Tuple<string, Type>[] { new Tuple<string, Type>("CategoryID = 1", typeof(Product)) });
dynamic firstRow = joinedproductsandcategory.ElementAt(0);
Assert.AreEqual(firstRow.EmployeeID + firstRow.Title + firstRow.OrderID + firstRow.ShipName + firstRow.ProductID.ToString() + firstRow.ProductName + firstRow.CategoryID + firstRow.CategoryName + firstRow.SupplierID + firstRow.CompanyName, "3Sales Representative10253Hanari Carnes39Chartreuse verte1Beverages18Aux joyeux eccl�siastiques");
}
Create Update and Delete helpers
The lib includes generic helpers for modifying data:
- Insert
- InsertMany
- UpdateMany
- Delete
UpdateMany needs a property bag of type IDictionary<string, object> to specify which properties to set.
[Test]
public async Task InsertManyAndUpdateManyRemoveAgainPerformsExpected()
{
var product = new Product
{
ProductName = "Misvaerost",
SupplierID = 15,
CategoryID = 4,
QuantityPerUnit = "300 g",
UnitPrice = 2.70M,
UnitsInStock = 130,
UnitsOnOrder = 0,
ReorderLevel = 20,
Discontinued = false
};
var anotherProduct = new Product
{
ProductName = "Jarslbergost",
SupplierID = 15,
CategoryID = 4,
QuantityPerUnit = "170 g",
UnitPrice = 2.80M,
UnitsInStock = 70,
UnitsOnOrder = 0,
ReorderLevel = 10,
Discontinued = false
};
var products = new List<Product> { product, anotherProduct };
var productIds = await Connection.InsertMany(products);
productIds.Cast<int>().Count().Should().Be(2, "Expected to insert two rows into the DB.");
productIds.Cast<int>().All(p => p > 0).Should().Be(true, "Expected to insert two rows into the DB with non-zero ids");
var updatePropertyBag = new Dictionary<string, object>
{
{ "UnitPrice", 133 },
{ "UnitsInStock", 192 }
};
products[0].ProductID = productIds.Cast<int>().ElementAt(0);
products[1].ProductID = productIds.Cast<int>().ElementAt(1);
var updatedProductsIds = await Connection.UpdateMany(products, updatePropertyBag);
foreach (var productId in productIds.Cast<int>())
{
var productAfterUpdateToDelete = Connection.Query<Product>($"select * from Products where ProductID = {productId}").First();
productAfterUpdateToDelete.UnitPrice.Should().Be(133);
productAfterUpdateToDelete.UnitsInStock.Should().Be(192);
await Connection.Delete(productAfterUpdateToDelete);
}
}
Additional methods in the lib
The lib also includes methods for:
- Paginated access
- Parameterized Query
- Like operator support
- ToExpandoObject method for converting for example DapperRow to a dynamic object via ExpandoObject
- Aggregate methods on a column that supports all major aggregate functions in T-SQL such as Avg, Min, Max, Stdev, Count, Sum and so on, with lambda syntax support.
<hr />
Read the extended readme for details about usage and also check out the Fuget explorer link on Nuget.org for API details.
Last update 2021-07-19, Tore Aurstad
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 was computed. 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 was computed. 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. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Supported frameworks are any .NET Framework and .NET Core support .NET Standard 2.