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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Claypool.Dapper.ColumnMapper" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Claypool.Dapper.ColumnMapper" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Claypool.Dapper.ColumnMapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Claypool.Dapper.ColumnMapper --version 1.0.1
                    
#r "nuget: Claypool.Dapper.ColumnMapper, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Claypool.Dapper.ColumnMapper@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Claypool.Dapper.ColumnMapper&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Claypool.Dapper.ColumnMapper&version=1.0.1
                    
Install as a Cake Tool

Dapper.ColumnMapper

NuGet Version Build Status License

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 123 11/1/2024
1.0.0 110 11/1/2024