Giter VIP home page Giter VIP logo

dapper.fluentcolumnmapping's Introduction

Dapper.FluentColumnMapping

Fluent Object-Column Mappings for use with Dapper

Build status

How to use

Install the Dapper.FluentColumnMapping package from NuGet.

// Create yourself a new mapping collection
var mappings = new ColumnMappingCollection();

// Start defining the mappings between each property/column for a type
mappings.RegisterType<State>()
        .MapProperty(x => x.Id).ToColumn("STATE_ID")
        .MapProperty(x => x.Name).ToColumn("DESCRIPTION")
        .MapProperty(x => x.Country).ToColumn("COUNTRY_NAME");

// You can add multiple type mappings to a collection too
mappings.RegisterType<Activity>()
        .MapProperty(x => x.EventId).ToColumn("ACTIVITY_ID")
        .MapProperty(x => x.State.Id).ToColumn("STATE_ID") //<== Map complex properties
        .MapProperty(x => x.ActivityDate).ToColumn("ACTIVITY_DATE")
        .MapProperty(x => x.ActivityType).ToColumn("ACTIVITY_TYPE")
        .MapProperty(x => x.Details).ToColumn("DETAILS"); //<== This is redundant, as the property name will be used by default

// Tell Dapper to use our custom mappings
mappings.RegisterWithDapper();

// Grab our data from the database
IEnumerable<State> states;
using (var dbConnection = CreateNewConnection())
    states = dbConnection.Query<State>("SELECT * FROM STATES");

Note: Typically, you only have to configure and register your mappings once, as Dapper keeps the mappings using a static object.

License

This project is licensed under The MIT License; please refer to the LICENSE file for additional information.

dapper.fluentcolumnmapping's People

Contributors

alexander-87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dapper.fluentcolumnmapping's Issues

Output data not automatically mapping non-specified fields

I have a model class:

public class QueryResults
{
  public double Distance { get; set; }
  public string StreetLine1 { get; set; }
  public string StreetLine2 { get; set; }
  public string City { get; set; }
  public string State { get; set; }
}

In my data access layer, one of the methods maps some of the model's fields.

var mappings = new ColumnMappingCollection();

// Start defining the mappings between each property/column for a type
mappings.RegisterType<QueryResults>()
  .MapProperty(x => x.Distance).ToColumn("distance")
  .MapProperty(x => x.StreetLine1).ToColumn("street1")
  .MapProperty(x => x.StreetLine2).ToColumn("street2");

mappings.RegisterWithDapper();

string query = "SELECT distance, " +
  "sites.street1, " +
  "sites.street2,  " +
  "sites.city, " +
  "sites.state, " +
  "FROM mytable ";

// Execute my query using Dapper 
IEnumerable<QueryResults> results = connection.Query<QueryResults>(query);

The results returned by the query do not include the unmapped properties/fields. Specifically, the City and State fields do not get populated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.