Claypool.Dapper.ColumnMapper
1.0.1
dotnet add package Claypool.Dapper.ColumnMapper --version 1.0.1
NuGet\Install-Package Claypool.Dapper.ColumnMapper -Version 1.0.1
<PackageReference Include="Claypool.Dapper.ColumnMapper" Version="1.0.1" />
<PackageVersion Include="Claypool.Dapper.ColumnMapper" Version="1.0.1" />
<PackageReference Include="Claypool.Dapper.ColumnMapper" />
paket add Claypool.Dapper.ColumnMapper --version 1.0.1
#r "nuget: Claypool.Dapper.ColumnMapper, 1.0.1"
#:package Claypool.Dapper.ColumnMapper@1.0.1
#addin nuget:?package=Claypool.Dapper.ColumnMapper&version=1.0.1
#tool nuget:?package=Claypool.Dapper.ColumnMapper&version=1.0.1
Dapper.ColumnMapper
An extension library for Dapper that enables attribute-based mapping of database column names to class properties using custom attributes. This simplifies data access code by eliminating the need for manual property mappings.
Table of Contents
Features
- Attribute-based mapping using
[ColumnName("db_column_name")]
- Automatic type map registration for Dapper
- Eliminates the need for manual property mappings
- Compatible with standard Dapper methods (
Query
,Execute
, etc.) - Supports .NET Framework, .NET Core, and .NET 5/6+
Getting Started
Prerequisites
- .NET SDK (.NET Core 3.1 or later recommended)
- Dapper installed
- SQL Server or any other database supported by Dapper
Installation
Install the Dapper.ColumnMapper
package via NuGet Package Manager:
Install-Package Dapper.ColumnMapper
Usage
Defining Models Decorate your model properties with the [ColumnName] attribute to specify the corresponding database column names.
using Dapper.ColumnMapper;
public class User
{
[ColumnName("user_id")]
public Guid Id { get; set; }
[ColumnName("user_name")]
public string Name { get; set; }
// Other properties...
}
Registering Type Maps At application startup, register the type maps to enable Dapper to use your custom mappings. This can be done in your Main method or startup configuration.
Copy code
using Dapper.ColumnMapper;
public class Program
{
public static void Main(string[] args)
{
// Register custom type maps
DapperTypeMappingConfig.RegisterTypeMaps();
// Continue with application startup...
}
}
Executing Queries Use Dapper's standard methods to execute queries. The custom mappings will be applied automatically.
Copy code
using Dapper;
using Microsoft.Data.SqlClient;
public class UserRepository
{
private readonly string _connectionString = "YourConnectionString";
public IEnumerable<User> GetAllUsers()
{
using (var connection = new SqlConnection(_connectionString))
{
return connection.Query<User>("SELECT user_id, user_name FROM Users");
}
}
}
Examples
Example Model
using Dapper.ColumnMapper;
public class Product
{
[ColumnName("product_id")]
public int Id { get; set; }
[ColumnName("product_name")]
public string Name { get; set; }
[ColumnName("price")]
public decimal Price { get; set; }
}
Example Repository
using Dapper;
using Microsoft.Data.SqlClient;
public class ProductRepository
{
private readonly string _connectionString = "YourConnectionString";
public ProductRepository()
{
// Ensure type maps are registered
DapperTypeMappingConfig.RegisterTypeMaps();
}
public IEnumerable<Product> GetAllProducts()
{
using (var connection = new SqlConnection(_connectionString))
{
return connection.Query<Product>("SELECT product_id, product_name, price FROM Products");
}
}
}
Full Example Usage
using System;
using System.Collections.Generic;
using Dapper;
using Dapper.ColumnMapper;
using Microsoft.Data.SqlClient;
namespace SampleApp
{
class Program
{
static void Main(string[] args)
{
// Register custom type maps
DapperTypeMappingConfig.RegisterTypeMaps();
// Connection string to your database
var connectionString = "YourConnectionString";
using var connection = new SqlConnection(connectionString);
// Example query
var products = connection.Query<Product>("SELECT product_id, product_name, price FROM Products");
foreach (var product in products)
{
Console.WriteLine($"{product.Id}: {product.Name} - ${product.Price}");
}
}
}
}
Contributing
Contributions are welcome! Please follow these steps to contribute:
Fork the repository on GitHub.
Clone your fork:
git clone https://github.com/yourusername/Dapper.ColumnMapper.git
Create a feature branch:
git checkout -b feature/your-feature-name
Commit your changes:
git commit -am "Add your feature"
Push to the branch:
git push origin feature/your-feature-name
Open a Pull Request on GitHub.
Building the Project Open the solution in JetBrains Rider or Visual Studio and build the solution to restore dependencies and compile the projects.
Running Tests The solution includes a Dapper.ColumnMapper.Tests project with unit tests.
Using the IDE: Run the tests using the built-in test runner.
Using the .NET CLI:
dotnet test
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
Dapper - Simple object mapper for .NET
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Dapper (>= 2.1.35)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.