Any good samples for getting started with Dapper?

asp.net-mvc-3 dapper orm

Question

I'm trying to get started with Dapper in an exisiting MVC3 project and although it looks very easy to use, I can't seem to find any tutorials on how to set it up intially. Any links or suggestions would be highly appreciated.

Thanks a lot.

Expert Answer

That is, in part, because there is nothing to set up - all you need is a database (which it doesn't care about) and some classes (which it doesn't care about).

The core methods just take parameterised SQL, and are deliberately close to LINQ-to-SQL's sql-based methods (hint: we use dapper as a direct drop-in replacement whenever we get issues with LINQ-to-SQL).

If you want a few examples, the "tests" project contains examples of the core APIs.

If you mean "how do I add dapper" - two choices; a single file added to your project, or a nuget package. The nuget pacakge tends to lag a little bit, but not much.

But ultimately, usage is just:

// get all open orders for this customer
var orders = connection.Query<Order>(
    "select * from Orders where CustomerId = @custId and Status = 'Open'",
    new { custId = customerId }).ToList();

where your Orders class has properties with names matching the database (it is a very direct map). No attributes are required; no special tooling is required. In our case, we tend to use LINQ-to-SQL generated classes with it, or a specific class created for some subset of columns (or composite between several tables, etc).




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why