12

I'm trying to grok Dapper and seem to be missing something very fundamental, can someone explain the following code taken from the Dapper home page on Google code and explain why there is no From clause, and the second param to the Query method (dynamic) is passed an anonymous type, I gather this is somehow setting up a command object, but would like an explanation in mere mortal terminology.

Thank you, Stephen

public class Dog {    
    public int? Age { get; set; }    
    public Guid Id { get; set; }    
    public string Name { get; set; }    
    public float? Weight { get; set; }    
    public int IgnoredProperty {
        get { return 1; }
    }
}

var guid = Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });            

dog.Count().IsEqualTo(1);
dog.First().Age.IsNull();
dog.First().Id.IsEqualTo(guid);
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Stephen Patten
  • 6,333
  • 10
  • 50
  • 84
  • I'm about to have a question on Dapper, too. I'm trying to figure out how to send a fully populated TEntity[] array to the Execute function. Looking to see if Marc pokes his head in the door ... – IAbstract Jun 16 '11 at 23:03
  • @IAbstract: Why don't you ask your own question then? – Robert Koritnik Jun 17 '11 at 12:35
  • @Robert: I will ... when/if I cannot figure it out. It was just a comment ... see my comment under the answer :) – IAbstract Jun 17 '11 at 13:13

1 Answers1

11

The first two examples just don't do any "real" data access, probably in order to keep them simple.
Yes, there is a connection used (connection.Query(...)), but only because that's the only way to call Dapper's methods (because they extend the IDbConnection interface).

Something like this is perfectly valid SQL code:

select 'foo', 1

...it just does "generate" its result on the fly, without actually selecting anything from a table.

The example with the parameters and the anonymous type:

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });)

...just shows Dapper's ability to submit SQL parameters in the form of an anonymous type.
Again, the query does not actually select anything from a table, probably in order to keep it simple.

Crypth
  • 1,576
  • 18
  • 32
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • Thank you Christian. I am also to assume that because they are using the @ sign that this is the dialect of SQL Server? Should I be replacing the @ with : for Oracle? – Stephen Patten Jun 16 '11 at 23:16
  • Sorry, I over-read the fact that you use Oracle (I use SQL Server, and don't know Oracle). Yes, "@" indicates a SQL parameter. Since Dapper itself is completely database independent (http://code.google.com/p/dapper-dot-net/#Will_dapper_work_with_my_db_provider), you will have to use the Oracle equivalent of "@". – Christian Specht Jun 16 '11 at 23:23
  • +1: good information...And for MySql, I would replace the '@' with '?' don't I? – IAbstract Jun 16 '11 at 23:34
  • 1
    If "?" indicates a parameter in MySql, then yes. I'm no MySql guru either ;-) – Christian Specht Jun 16 '11 at 23:40
  • Where did MySQL come from?, yes I know Oracle owns the product now, but the Oracle 11g parameter for ODP.Net is the colon sign, not he question mark, which as it turns out is for MySQL. ;) Guess we're all right. – Stephen Patten Jun 16 '11 at 23:50
  • This may be the reason I could not get my execution to work properly (referencing my first comment to the OPs question). – IAbstract Jun 17 '11 at 13:16
  • I know this is old, but since there is discussion of a variety of DB's going on, I'd like to throw in that this query is not valid syntax in ProgressDB and will fail. You cannot perform an assignment statement in the SELECT clause in ProgressDB, however, the query parameters will still work if used after the FROM clause (which btw is also required in ProgressDB) – akousmata Apr 14 '15 at 16:05