Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hamedstack/hamedstack.dapper

A library enhancing Dapper with additional utilities, type handlers, and features for improved data access performance and convenience, making Dapper more powerful for developers.
https://github.com/hamedstack/hamedstack.dapper

csharp csharp-library dapper database dotnet dotnet-core dotnetcore infrastructure repository typehandler utilities utility utility-library

Last synced: 27 days ago
JSON representation

A library enhancing Dapper with additional utilities, type handlers, and features for improved data access performance and convenience, making Dapper more powerful for developers.

Awesome Lists containing this project

README

        

## Usage Guide

### 1. Registering Type Handlers
Before you start using `DateOnly` and `TimeOnly` in your Dapper queries or commands, you need to register the type handlers. This is a one-time setup that should be called before any database operation.

#### Add Type Handlers
```csharp
using HamedStack.Dapper;

// Register type handlers at the start of the application
SqlMapperTypeHandler.AddDateOnlyTimeOnlyTypeHandlers();
```

### 2. Using `DateOnly` and `TimeOnly` in Dapper Queries
Once the type handlers are registered, you can use `DateOnly` and `TimeOnly` types directly in your Dapper queries as parameters or for mapping results.

#### Example: Inserting `DateOnly` and `TimeOnly` Values
```csharp
using Dapper;
using System.Data.SqlClient;

string connectionString = "your_connection_string";

using var connection = new SqlConnection(connectionString);
connection.Open();

var date = new DateOnly(2023, 10, 05); // DateOnly value
var time = new TimeOnly(14, 30, 0); // TimeOnly value

string sql = "INSERT INTO YourTable (DateColumn, TimeColumn) VALUES (@Date, @Time)";

connection.Execute(sql, new { Date = date, Time = time });
```

#### Example: Querying and Mapping to `DateOnly` and `TimeOnly`
```csharp
string query = "SELECT DateColumn, TimeColumn FROM YourTable WHERE Id = @Id";

var result = connection.QuerySingleOrDefault(query, new { Id = 1 });

DateOnly dateResult = result.DateColumn;
TimeOnly timeResult = result.TimeColumn;
```