GithubHelp home page GithubHelp logo

dapper-dot-net's Introduction

Dapper - a simple object mapper for .Net

Features

Dapper is a single file you can drop in to your project that will extend your IDbConnection interface.

It provides 3 helpers:

Execute a query and map the results to a strongly typed List

Note: all extension methods assume the connection is already open, they will fail if the connection is closed.

public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

Example usage:

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);

Execute a query and map it to a list of dynamic objects

public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

This method will execute SQL and return a dynamic list.

Example usage:

var rows = connection.Query("select 1 A, 2 B union all select 3, 4");

((int)rows[0].A)
   .IsEqualTo(1);

((int)rows[0].B)
   .IsEqualTo(2);

((int)rows[1].A)
   .IsEqualTo(3);

((int)rows[1].B)
    .IsEqualTo(4);

Execute a Command that returns no results

public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)

Example usage:

connection.Execute(@"
  set nocount on 
  create table #t(i int) 
  set nocount off 
  insert #t 
  select @a a union all select @b 
  set nocount on 
  drop table #t", new {a=1, b=2 })
   .IsEqualTo(2);

Execute a Command multiple times

The same signature also allows you to conveniently and efficiently execute a command multiple times (for example to bulk-load data)

Example usage:

connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
    new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
  ).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"

This works for any parameter that implements IEnumerable for some T.

Performance

A key feature of Dapper is performance. The following metrics show how long it takes to execute 500 SELECT statements against a DB and map the data returned to objects.

The performance tests are broken in to 3 lists:

  • POCO serialization for frameworks that support pulling static typed objects from the DB. Using raw SQL.
  • Dynamic serialization for frameworks that support returning dynamic lists of objects.
  • Typical framework usage. Often typical framework usage differs from the optimal usage performance wise. Often it will not involve writing SQL.

Performance of SELECT mapping over 500 iterations - POCO serialization

Method Duration Remarks
Hand coded (using a SqlDataReader) 47ms Can be faster
Dapper ExecuteMapperQuery 49ms
ServiceStack.OrmLite (QueryById) 50ms
PetaPoco 52ms
BLToolkit 80ms
SubSonic CodingHorror 107ms
NHibernate SQL 104ms
Linq 2 SQL ExecuteQuery 181ms
Entity framework ExecuteStoreQuery 631ms

Performance of SELECT mapping over 500 iterations - dynamic serialization

Method Duration Remarks
Dapper ExecuteMapperQuery (dynamic) 48ms  
Massive 52ms
Simple.Data 95ms

Performance of SELECT mapping over 500 iterations - typical usage

Method Duration Remarks
Linq 2 SQL CompiledQuery 81ms Not super typical involves complex code
NHibernate HQL 118ms  
Linq 2 SQL 559ms  
Entity framework 859ms  
SubSonic ActiveRecord.SingleOrDefault 3619ms  

Performance benchmarks are available here

Feel free to submit patches that include other ORMs - when running benchmarks, be sure to compile in Release and not attach a debugger (ctrl F5)

Parameterized queries

Parameters are passed in as anonymous classes. This allow you to name your parameters easily and gives you the ability to simply cut-and-paste SQL snippets and run them in Query analyzer.

new {A = 1, B = "b"} // A will be mapped to the param @A, B to the param @B 

List Support

Dapper allow you to pass in IEnumerable and will automatically parameterize your query.

For example:

connection.Query<int>("select * from (select 1 as Id union all select 2 union all select 3) as X where Id in @Ids", new { Ids = new int[] { 1, 2, 3 });

Will be translated to:

select * from (select 1 as Id union all select 2 union all select 3) as X where Id in (@Ids1, @Ids2, @Ids3)" // @Ids1 = 1 , @Ids2 = 2 , @Ids2 = 3

Buffered vs Unbuffered readers

Dapper's default behavior is to execute your sql and buffer the entire reader on return. This is ideal in most cases as it minimizes shared locks in the db and cuts down on db network time.

However when executing huge queries you may need to minimize memory footprint and only load objects as needed. To do so pass, buffered: false into the Query method.

Multi Mapping

Dapper allows you to map a single row to multiple objects. This is a key feature if you want to avoid extraneous querying and eager load associations.

Example:

var sql = 
@"select * from #Posts p 
left join #Users u on u.Id = p.OwnerId 
Order by p.Id";
 
var data = connection.Query<Post, User, Post>(sql, (post, user) => { post.Owner = user; return post;});
var post = data.First();
 
post.Content.IsEqualTo("Sams Post1");
post.Id.IsEqualTo(1);
post.Owner.Name.IsEqualTo("Sam");
post.Owner.Id.IsEqualTo(99);

important note Dapper assumes your Id columns are named "Id" or "id", if your primary key is different or you would like to split the wide row at point other than "Id", use the optional 'splitOn' parameter.

Multiple Results

Dapper allows you to process multiple result grids in a single query.

Example:

var sql = 
@"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";
 
using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
} 

Stored Procedures

Dapper supports fully stored procs:

var user = cnn.Query<User>("spGetUser", new {Id = 1}, 
        commandType: CommandType.StoredProcedure).First();}}}

If you want something more fancy, you can do:

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 

int b = p.Get<int>("@b");
int c = p.Get<int>("@c"); 

Ansi Strings and varchar

Dapper supports varchar params, if you are executing a where clause on a varchar column using a param be sure to pass it in this way:

Query<Thing>("select * from Thing where Name = @Name", new {Name = new DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi = true });

On Sql Server it is crucial to use the unicode when querying unicode and ansi when querying non unicode.

Limitations and caveats

Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache.

Dapper's simplicity means that many feature that ORMs ship with are stripped out, there is no identity map, there are no helpers for update / select and so on.

Dapper does not manage your connection's lifecycle, it assumes the connection it gets is open AND has no existing datareaders enumerating (unless MARS is enabled)

Will dapper work with my db provider?

Dapper has no DB specific implementation details, it works across all .net ado providers including sqlite, sqlce, firebird, oracle, MySQL and SQL Server

Do you have a comprehensive list of examples?

Dapper has a comprehensive test suite in the test project

Who is using this?

Dapper is in production use at:

Stack Overflow, helpdesk

(if you would like to be listed here let me know)

dapper-dot-net's People

Contributors

mgravell avatar samsaffron avatar vosen avatar greygeek avatar panesofglass avatar brannon avatar joaomatossilva avatar xps avatar azhmur avatar jedidja avatar mrange avatar tms avatar markrendle avatar danzel avatar andrexx avatar swisscheeze avatar seburgi avatar korsg avatar jlogar avatar jkonecki avatar andrxs avatar anaisbetts avatar majorsilence avatar phillip-haydon avatar richczyzewski avatar bundy845 avatar romanych avatar techniq avatar ssboisen avatar tallesl avatar

Watchers

James Cloos avatar

dapper-dot-net's Issues

Stored Procedure DBNull Output Parameter Casting Exception

What steps will reproduce the problem?
1. Create a sproc with an output parameter
2. In the sproc set the parameter to null
3. Use DynamicParameters to add an output parameter
4. Call sproc with dapper
5. Use DynamicParameters.Get<T> to get value

What is the expected output? What do you see instead?
For dbnulls to be treated as nulls and casted to the destination type 
accordingly. Getting an invalid cast exception instead @ 
"Dapper.DynamicParameters.Get[T](String name)" line 1589.

What version of the product are you using? On what operating system?
1.5, Windows 7, Sql Server 2008 R2

Please provide any additional information below.
Locally I've added a DBNull check to "public T Get<T>(string name)" changing it 
from:
(T)parameters[Clean(name)].AttachedParam.Value;

to

var value = parameters[Clean(name)].AttachedParam.Value;
return (T)(value == DBNull.Value ? null : value);

which works well enough.

Original issue reported on code.google.com by [email protected] on 15 Jul 2011 at 5:56

Creating a DynamicParameters object from an anonymous type

When calling a stored procedure with output parameters I have to create a 
DynamicParameters object like this:

DynamicParameters p = new DynamicParameters();
p.Add("@GameID", gameId);
p.Add("@GUIDin", sessionId);
p.Add("@StandIDin", standIDin);
p.Add("@sectionin", section);
p.Add("@zonein", zone);
p.Add("@NumberOfSeats", numberOfSeats);
p.Add("@SeatTypeIDin", seatTypes);
p.Add("@SeatCountOut", dbType: DbType.Int32, direction: 
ParameterDirection.Output);

It would be nice if most of the parameters could be initialized using an 
anonymous object like other calls using Dapper.Net, and then the 
output/ref/return parameters are added as needed:

DynamicParameters p = DynamicParameters.FromObject({ gameid, sessionId, 
standIDin, section, zone, numberOfSeats, seatTypes });
p.Add("@SeatCountOut", dbType: DbType.Int32, direction: 
ParameterDirection.Output);

Original issue reported on code.google.com by [email protected] on 31 May 2011 at 10:34

Optin on more detailed info on cast exception

Not a biggie just for the wish list, which would make debugging easier.

What steps will reproduce the problem?
  Have mapped type become invalid because changes in database or
  select. i.e. int becomes bigint. SQL Server can "upcast" implicity
  if someone wasn't thinking when creating a procedure i.e.

What is the expected output? What do you see instead?
  Now we just get an invalid cast exception, would be great if at
  least could optin to get an more detailed exception (field name,
  field type expected type) or perhaps have an option to have an
  InvalidCast callback which gets called with the reader/command
  before the exceptions is re thrown and the end user handles
  detailed logging.

What version of the product are you using? On what operating system?
  changeset:   60:bda6f2c8327a

Please provide any additional information below.
  Sample of implicit casting
  SELECT    1 AS First,
        2147483648 AS Second,
        ISNULL(@long, 1) AS Third

  will result in:
   First    int
   Second   numeric
   Third    bigint

Original issue reported on code.google.com by mattias%[email protected] on 28 Apr 2011 at 9:33

Property matching on POCO's is case sensitive

In rev. 2f73f171cfdf Dapper expects database column names to match property 
names exactly. When you have a database naming convention that uses lower case 
first letters while the objects adhere to the C# naming conventions (i.e. 
upper-case first letters), the properties are never set.

Solution:

Either document the case sensitivity or enable case insensitive property 
matching.

For the latter option a single change on line 419 is all it takes:              


select new { Name = n, Info = properties.FirstOrDefault(p => p.Name.Equals( n, 
StringComparison.OrdinalIgnoreCase ) ) }
                          ).ToList();

Original issue reported on code.google.com by [email protected] on 19 Apr 2011 at 12:39

Support CommanType.StoredProcedure and output/returnvalues

Would be great if stored procedure output values and return values where 
supported.
Don't know if the need is great, but I replaced old linq2sql code with dapper 
in a project and the need emerged.

I have made a clone which supports this by adding optional commanType parameter 
to the query methods and an ParameterDirection attribute which I decorate the 
properties which should get values from output or return value.

Sample parameter class and usage attached, just fragment but you should get the 
flow.

Clone can be found here
http://code.google.com/r/mattias-dapper-dot-net-commandtype-storedprocedure-outp
ut/source
This can probably be improved with il & refactored, but it solved my needs.

Original issue reported on code.google.com by mattias%[email protected] on 3 May 2011 at 3:09

Attachments:

Added the Soma library to the performance tests

I added the Soma library (http://soma.codeplex.com/) as an additional 
performance test comparison. You can find the additions at my clone in the 
default branch.

http://code.google.com/r/panesofglass-dapper-dot-net/source/detail?r=465edb436e4
07c45dd60fcde630343871d0d5327

Original issue reported on code.google.com by [email protected] on 28 Apr 2011 at 7:30

char(1) not mapped to char (SQL Server 2008R2)

I have a class containing "char Delimiter" field that is represented in the 
database by char(1) column. It is used for CSV parsing.

Using a simple select query, I expect to map some rows from database to a list 
of such objects.


Instead, I'm receiving an exception:

System.Data.DataException: Error parsing column 18 (Delimiter=, - String) ---> 
System.InvalidCastException: Specified cast is not valid.


To reproduce:

1. Create class "Test" with field "public char Delimiter"
2. Create table "Test" with column "Delimiter char(1)"
3. Connection.Query<Test>("SELECT * FROM Test");


If it's not a bug, I would appreciate a suggestion, how to handle this using 
Dapper. Thanks.

Original issue reported on code.google.com by [email protected] on 4 Jul 2011 at 2:59

Multimapping with nested objects and having ID into first column.

What steps will reproduce the problem?
1. Create a table and stored procedure (SpGetAllPersons) which returns below 
schema:

PersonGUID, FirstName, LastName, Age, Sex, Address

2.  Create model as per below:

Class Person
{
int PersonGUID { get; set; } 
Name name { get; set; }
int Age { get; set; }
bool Sex { get; set; }
string Address { get; set; }
}

3.  Write a dapper query to populate fields.

List<Person> persons = DbConn.Query<Name,Person,Person>
  ("SpGetAllPersons",
     (name,person) => {person.Name = name; return person;} 
     commandType: CommandType.StoredProcedure, 
     splitOn: "PersonGUID, Age, Sex, Address").ToList();


What is the expected output? What do you see instead?
Expected Output : All the values should be prefilled correctly into Model.
Actual Output : All the IDs were 0.


What version of the product are you using? On what operating system?
Windows XP Sp3, .NET 4.0, SQL Mapper version: acacfa6a6d59

Please provide any additional information below.
I debugged the code and found below:

Line 496: There is a function like below:

Func<int> nextSplit = () =>
                        {
                            var currentSplit = splits[splitIndex];
                            if (splits.Length > splitIndex + 1)
                            {
                                splitIndex++;
                            }
                            int pos;
                            for (pos = current + 1; pos < reader.FieldCount; pos++)
                            {
                                // some people like ID some id ... assuming case insensitive splits for now
                                if (splitOn == "*" || string.Equals(reader.GetName(pos), currentSplit, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    break;
                                }
                            }
                            current = pos;
                            return pos;
                        };

Here Pos starts from 1 instead of 0 so it will always skip the 1st column of 
Reader.  I removed 1 and Dapper started populating all the ID values correctly.

Original issue reported on code.google.com by [email protected] on 7 Jun 2011 at 11:27

Multipe Resultsets

Hello World

We're in an environment where Multiple Resultsets are important.

Is there a plan to support this?

i.e. 
  var results = c.QueryMultiple<Order,IEnumerable<OrderRow>>( ... );
  var order = results.First;
  var rows = results.Second;

.... etc

Original issue reported on code.google.com by icanhasjonas on 27 Apr 2011 at 7:27

Automatic conversion between database ints and booleans in code

This isn't a bug report, it's a feature request/wish..

A lot of databases have strange behaviors when it comes to storing, indexing 
and querying boolean fields.

Most databases combine multiple bit fields in storage to save storage space. 
The downside of this, is that this costs performance and some database engine 
refused to index bit fields.

One of the more popular workarounds is to store booleans fields as TinyInt. 
When false is equal to zero and true to everything else (> 0). 

And as diskspace is cheap and CPU resources are expensive I also often use this 
workaround as a optimalization technique. 


How to reproduce the situation.

[SQL]
CREATE TABLE Users (UserID int IDENTITY(1,1) not null, Email varchar(255) not 
null, Password Char(40) not null, Active TinyInt not null, )
GO
ALTER TABLE Users ADD CONSTRAINT PK_Users PRIMARY KEY CLUSTERED (UserID)
GO
ALTER TABLE Users ADD CONSTRAINT DF_Users_Active DEFAULT (0) FOR Active
GO



[C#]

public class User
{
  public User()
  {
     Email = Password = String.Empty;
  }
  public int UserID { get; set; }
  public string Email { get; set; }
  public string Password { get; set; }
  public bool Active { get; set;
}

SqlConnection conn = new SqlConnection("server=...;");
conn.Open();

//This query will throw an conversion exception (boolean != TinyInt)
var activeUsers = conn.ExecuteMapper<User>("SELECT UserID, Email, Password, 
Active FROM Users WHERE (Active=1)");


In the past I have written an DataRecordHelper class that accepts a data reader 
as input. My GetBoolean(int ordinal) method uses GetFieldType() to determine 
the underlying data type. If it's a bit field it uses GetBoolean, if it's a 
tinyint it uses GetByte() or it uses GetIntxx() when it's a SmallInt or a 
regular integer.  

Well this isn't the fastest way to convert data types, it is simple and easy to 
understand. For dapper.net it might be feasable to use an array of expression 
to specify a custom conversion. 

For now I'm using a workaround like:
(Query has changed to 'SELECT UserID, Email, Password, Active as _Active FROM 
Users ...')

public class User
{
  ...
  public bool Active
  {
    get { return _Active > 0; }
    set { return _Active = value ? 1 : 0; }
  }

  //Alias property for mapping tinyint <--> boolean
  public int _Active { get; set; }
}
















Original issue reported on code.google.com by [email protected] on 28 Jun 2011 at 7:51

System.Data.Linq.Binary type

I have following class definition (.NET version 3.5):

public class Document
{
    Binary Report { get; set; }

    //other fields
}

In the database table (MS SQL Server 2008) Report field has image data type. 
I'm trying execute connection.Query<Document> method and I'm having exception 
like this: System.InvalidCastException: cannot cast object of type Syste.Byte[] 
to type System.Data.Linq.Binary.
So I can't use SqlMapper with Binary type? Or maybe I'm doing something wrong?

Original issue reported on code.google.com by [email protected] on 17 Jun 2011 at 1:04

Typemap Extensibility

What steps will reproduce the problem?
1. Create a db table with a DATETIMEOFFSET NULL column.
2. Create a POCO with a DateTime?
3. Trying to select will throw InvalidCastException.

I'm using RIA services to serialize over the wire, and they don't support 
DateTimeOffset, so I'm forced to use DatTime. 

If I could plug in and provide a Cast helper function or something, 
Func<TSource,TResult>.

Original issue reported on code.google.com by [email protected] on 17 May 2011 at 8:49

Using the like operator with DynamicParameters

From my question on SO: 
http://stackoverflow.com/questions/6711012/dapper-like-operator-using-dynamicpar
ameters

----------------------------------

This works:

var list = conn.Query<int>(
  "select Id from Person where Id in @ids", 
  new { ids = new int[] { 1, 2, 3 } }
);
This throws "No mapping exists from object type System.Int32[] to a known 
managed provider native type.":

DynamicParameters parameters = new DynamicParameters(
  new { ids = new int[] { 1, 2, 3 } }
);
var list2 = conn.Query<int>(
  "select Id from Person where Id in @ids", 
  parameters
);

Original issue reported on code.google.com by [email protected] on 15 Jul 2011 at 7:45

Can the current limit of 4 joins in a Multi Map Query be increased?

Not a bug, just a feature request.

It looks like there is currently a limit of about 4 joins in a Multi Map query, 
which most of the time is fine, but occasionally we need to join on more tables 
than this.

Can this limit be increased to permit up to about 10 joins please?

Original issue reported on code.google.com by [email protected] on 19 Jul 2011 at 12:18

Enum support in parameters

Are there any plans to support enums?

If not, some kind of mapping override as suggested by Matt 
(http://code.google.com/p/dapper-dot-net/issues/detail?id=24) would be useful. 
This way we could cast to the enums ourselves.

Original issue reported on code.google.com by [email protected] on 3 Jun 2011 at 3:58

Multi Mapping for one-to-many associations

Currently I think the only way to map an entity and a one-to-many association 
is by mapping multiple result sets;

It would be nice to be able to do this:

    var data = cn.Query<PostModel, IEnumerable<Tag>, PostModel>(query,
        (p, tags) => {
            p.Tags = tags;
            return p;
        }, new { id = id });

If this "should" work already then please let me know :)

Original issue reported on code.google.com by [email protected] on 2 Jun 2011 at 7:02

Nuget package installation fails in .Net3.5 projects

What steps will reproduce the problem?
1. Create a .Net3.5 project
2. Try to install dapper.

What is the expected output? What do you see instead?
Expected to get the package installed. 

Error message: Install failed. Rolling back...
Unable to find framework assemblies that are compatible with the target 
framework '.NETFramework,Version=v3.5'.

What version of the product are you using? On what operating system?
Dapper 1.5, Visual Studio 2010 SP1, Windows 7.

Please provide any additional information below.
The installation fails because of references to .Net 4.0 assemblies.

Original issue reported on code.google.com by [email protected] on 15 Jul 2011 at 8:28

Attachments:

Extend tests for Enumerable<T>

Please provide a similar class as IntDynamicParam for a more generic 
Enumerable<T> where T is a complex type. 

It would be useful to allow for another parameter which doesn't need to be 
mapped onto a tvp. This should make it clearer what needs to be done to build a 
custom dynamic parameters for a sproc that takes as input, tvps and non-tvps.

Original issue reported on code.google.com by [email protected] on 10 Jul 2011 at 4:32

CommandTimout not exposed to end consumer

What steps will reproduce the problem?
 1. Have an query that takes longer than standard timeout (30 seconds)

What is the expected output? What do you see instead?
 Optional parameter for commandTimeout

What version of the product are you using? On what operating system?
 changeset:   53:ce7137b69e36

Please provide any additional information below.
 Have created an clone with commandTimeout implemented

http://code.google.com/r/mattias-dapper-dot-net-commandtimeout/source/checkout

Original issue reported on code.google.com by mattias%[email protected] on 26 Apr 2011 at 1:45

Managing Dapper scripts

Not sure whether I should post here or on SO. Really its just a general 
question at you guys how you manage your Dapper scripts.

I've taken to exposing my queries as properties on a static class. This makes 
it a bit easier when I need to tweak the query as there's no noise from other 
code - see http://codepaste.net/tfb8so

Within my service layer I can then just call:

cn.Query<PostModel>(BlogQueries.PublicPostsQuery, new { .... } )

Notice that I'm also doing a bit of string concatenation to reduce repeating 
sql such as the fields to retrieve and the criteria needed twice (once for the 
count and once for the select).

Do you guys do something similar or have any other tips?

Original issue reported on code.google.com by [email protected] on 4 Jun 2011 at 1:50

Suggestion for automatic multi-mapping

Is any work planned to optionally auto-multi-map based off the 
AssociationAttribute (or similar)? Or is this completely off the table in order 
to stay with POCO or for some other reason?

Nature of relation would be relatively easy (for 1-1, 1-n at least) to 
determine (check for IEnumerable<T>/T on involved types)

Manually doing this is kind of tedious (foot work, identity tracking, LOC), 
especially for multiple joins. Even though it may be reusable in a limited 
fashion in the simple cases.

Example for the attribute usage:

[Association("OtherEntityName", "ThisKey", "OtherKey")]

A first step would not even need caching as it pays in terms of productivity.

I'll do it myself eventually if no one else will do it. But not in the coming 
months. Just asking :-)



PS:
AssociationAttribute is stashed away in various DLLs and various forms across 
framework versions. But why not just copy it into Dapper for .NET < 4 and keep 
to the System.ComponentModel.DataAnnotations namespace for .NET 4, which seems 
useful in and of itself.

Original issue reported on code.google.com by [email protected] on 20 Jul 2011 at 10:36

Added/changed NHibernate tests

Clone dimon-naumov-dapper-dot-net.
Switched NHibernate to use stateless session.
Added test for Session.Get
Please review and pull.

P.S. Sorry if I used 'Issues' in the wrong way. I found no other options to 
make a 'pull request'.


Original issue reported on code.google.com by [email protected] on 22 Jun 2011 at 9:06

bl-toolkit

Can you please add bl-toolkit (http://bltoolkit.net) into the benchmark.

Serdar

Original issue reported on code.google.com by [email protected] on 6 Apr 2011 at 6:00

Support for ExecuteScalar

I would appreciate support for ExecuteScalar.
This makes it easy to to do inserts and get the generated row id as a result.

I've implemented this based on the ExecuteCommand implementation and it works 
for me so far.




Original issue reported on code.google.com by [email protected] on 9 May 2011 at 10:18

Attachments:

database independence

Can you please make the code database independent like
https://github.com/nberardi/dapper-dot-net


Serdar

Original issue reported on code.google.com by [email protected] on 6 Apr 2011 at 6:02

Index outside the bounds of the array exception when using MultiMapping

> What steps will reproduce the problem?

When I use Multi-Mapping, I get an index outside the bounds of the array 
exception in SqlMapper.cs line 892.  The code is the Add call here:

            var names = new List<string>();
            for (int i = startBound; i < startBound + length; i++)
            {
                names.Add(reader.GetName(i));      <-------- Exception
            }

This loop is executed twice.  The first finishes without error.  On the second, 
StartBound is 15 and length is 1.  However the reader at this point has just 
one row - this an index out of bounds exception.

My calling code is:

var insertMessages = mailDB.Connection.Query<MessageTransfer, AddressTransfer, 
MessageTransfer>("SELECT [messageId] ,[folderId] ,[subject] ,[date] ,[header] 
,[isRead] ,[isReplied] ,[isForwarded] ,CAST([format] as INT) as 
format,[charset] ,[contentSize] ,[content], emailAddress, friendlyName FROM 
Messages m LEFT JOIN Addresses a ON a.addressId = m.fromAddressId WHERE 
messageId IN @MessageIds", (m, a) => { m.FromAddress = a; return m; }, 
insertMessageIdsParameter);

The classes in question are:


    public partial class MessageTransfer : LiveObject 
    {
        public int MessageId { get; set; }
        public int FolderId { get; set; }
        public string Subject { get { return _subject; } set { _subject = value == null ? "" : value; } } private string _subject = "";
        public DateTime Date { get; set; }
        public string Header { get { return _header; } set { _header = value == null ? "" : value; } } private string _header = "";
        public bool IsRead { get; set; }
        public bool IsReplied { get; set; }
        public bool IsForwarded { get; set; }
        public ContentFormat Format { get; set; }
        public string Charset { get { return _charset; } set { _charset = value == null ? "" : value; } } private string _charset = "";
        public int ContentSize { get; set; }
        public string Content { get { return _content; } set { _content = value == null ? "" : value; } } private string _content = "";
        public override int LiveId { get { return MessageId; } }
...
}

and 


    public partial class AddressTransfer : TransferObject 
    {
        public string EmailAddress { get { return _emailAddress; } set { _emailAddress = value == null ? "" : value; } } private string _emailAddress = "";
        public string FriendlyName { get { return _friendlyName; } set { _friendlyName = value == null ? "" : value; } } private string _friendlyName = "";
...
}

The schema is:

CREATE TABLE [dbo].[Messages](
    [messageId] [int] IDENTITY(1,1) NOT NULL,
    [folderId] [int] NOT NULL,
    [subject] [nvarchar](max) NOT NULL,
    [fromAddressId] [int] NOT NULL,
    [date] [smalldatetime] NOT NULL,
    [header] [nvarchar](max) NOT NULL,
    [isRead] [bit] NOT NULL,
    [isReplied] [bit] NOT NULL,
    [isForwarded] [bit] NOT NULL,
    [format] [int] NOT NULL,
    [charset] [varchar](64) NOT NULL,
    [contentSize] [int] NOT NULL,
    [content] [nvarchar](max) NOT NULL
)

and

CREATE TABLE [dbo].[Addresses](
    [addressId] [int] IDENTITY(1,1) NOT NULL,
    [emailAddress] [nvarchar](256) NOT NULL,
    [friendlyName] [nvarchar](256) NOT NULL
)

Please let me know if I can provide any other information.

Original issue reported on code.google.com by [email protected] on 29 May 2011 at 7:07

Three-type multimap with GridReader throws an exception

What steps will reproduce the problem?
1. Create a query to return multiple result sets, one of which will create an 
object with two additional types (see attached test).
2. Run the query, mainly the result set for the three objects

What is the expected output? What do you see instead?

The object with two nested objects should be returned from the Read method.  
Instead, an exception is thrown:

Unable to cast object of type 
'System.Func`4[SqlMapper.Tests+Post,SqlMapper.Tests+User,SqlMapper.Tests+Comment
,SqlMapper.Tests+Post]' to type 
'System.Func`3[SqlMapper.Tests+Post,SqlMapper.Tests+User,SqlMapper.Tests+Post]'.

What version of the product are you using? On what operating system?
v1.4, Windows 7 64bit Ultimate

Please provide any additional information below.
The problem is in the GridReader class, the MultiReadInternal method.  When 
SqlMapper.MultiMapImpl is called, it is ignoring all but the first two type 
parameters, instead using DontMap.  Changing this to use the other types makes 
it function correctly (see attached file).

Original issue reported on code.google.com by [email protected] on 15 Jul 2011 at 2:18

Attachments:

Identity constructor GetHashCode throw Arithmetic Overflow exception

What steps will reproduce the problem?
1. Running simple select code for dynamic object.
2. Code throws an exception of  Arithmetic Overflow
3.

What is the expected output? What do you see instead?


What version of the product are you using? On what operating system?


Please provide any additional information below.
Fixed by changing code 
hashCode = unchecked(hashCode * 23 + (sql == null ? 0 : sql.GetHashCode()));
                hashCode = unchecked(hashCode * 23 + (type == null ? 0 : type.GetHashCode()));
                hashCode = unchecked(hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode()));

Original issue reported on code.google.com by [email protected] on 22 Apr 2011 at 10:09

Implicit casting

Hi guys,

This may be on the same lines as issue 24.

I've got a class that supports implicit casting to/from string.

When dapper tries to do the cast, it falls over. I did try create a custom type 
converter, but it doesn't look like this is being used.

Is there anyway I can tell dapper how to convert from string to MyType?

Original issue reported on code.google.com by [email protected] on 14 Jul 2011 at 8:03

Converting values when mapping results

Dapper should attempt to convert values when the property type and result type 
do not match.

For example these throw exceptions at the moment when expect Dapper would just 
convert them so that they work:
Int16 -> Int32
Int16 -> Bool (i.e. converting 1/0 values to true/false)

Original issue reported on code.google.com by [email protected] on 27 May 2011 at 3:44

Add Simple.Data performance tests for comparison

I've attached the patch for running the same tests but using Simple.Data ORM.

The results aren't brilliant (at least i my machine) but according to Mark 
Rendle the performance tweaks will happen for the 1.0 release 
(https://twitter.com/#!/markrendle/status/55230529175433216)

Original issue reported on code.google.com by [email protected] on 5 Apr 2011 at 12:13

Attachments:

Enable mr fine grain multi map segment / split control for the api user

I found cases where i wanted to use the multimap feature but not able and/or 
wanting to change the select to the same "Id" column name to split on.

I solved this by added an optional Func to the MultiMap Query methods for the 
api user which overides the nextSplit index functionality.

I have created i clone with these changes if any one else should found this 
useful. It can be found here:
http://code.google.com/r/mattias-multimap-fine-grain-segment-control/source/

It also solves the issue when there are no "split" columns on the "child" 
types. Common when you want to map record sets with many fields to groups of 
properties (Like Address ie.)

Sample usage (issues seems not to like lt/gt generics but hopefully you get the 
flow):
conn.Query«WorkOrder, Vehicle, Customer, Technician, Location, Booking»(
"SearchVehicleCurrentBookingTechicianLocation @Search",
(workOrder, vehicle, customer, technician, location) =» 
    new Booking
    {
        WorkOrder = workOrder,
        Vehicle = vehicle,
        Customer = customer,
        Technician = technician,
        Location = location
    },
new { Search },
getNextSplitFunc:(reader,splitOn)=»new Queue«int»(
    new []
        {
            reader.GetOrdinal("OrderId"),
            reader.GetOrdinal("VehicleId"),
            reader.GetOrdinal("CustomerId"),
            reader.GetOrdinal("TechnicianId"),
            reader.GetOrdinal("LocationId"),
            reader.FieldCount
        }.Skip(1)
    ).Dequeue
);

Original issue reported on code.google.com by mattias%[email protected] on 30 Apr 2011 at 11:37

FastExpando enhancement suggestion

I'd like to be able to (dynamically) iterate over the internal IDictionary of a 
FastExpando object.
Implementing a TryConvert method could provide this functionality:


public override bool TryConvert(ConvertBinder binder, out object result)
{
    if (binder.Type == data.GetType())
    {
        result = data;
        return true;
    }
    return base.TryConvert(binder, out result);
} 


Original issue reported on code.google.com by [email protected] on 16 May 2011 at 5:05

List support

I have the following query:

select  t.Title, t.Slug, pt.Post_id as PostId 
from    BlogPostTags pt inner join Tags t on pt.Tag_id = t.TagId 
where   pt.Post_id in @ids order by t.Title

And passing an IEnumerable<Guid> to dapper:

var tags = session.Connection.Query(query,new { ids = postIds });

According to mvc-mini-profiler it's generating the following query, which 
doesn't look right:

select  t.Title, t.Slug, pt.Post_id as PostId from    
BlogPostTags pt inner join Tags t on pt.Tag_id = t.TagId                     
where   pt.Post_id in (@ids, ids, ids, ids, ids5) order by t.Title

Is this an issue with the profiler or dapper?  The query does appear to be 
returning the correct results (in this case it returns the tags for 5 posts) 
but I'm not sure why it shows "ids, ids, ids," ?

Original issue reported on code.google.com by [email protected] on 9 Jun 2011 at 5:44

Deserialization doesn't work for properties with non-public setters that reside in a base class

What steps will reproduce the problem?
1. Create a base class with a property that has a private setter.
2. Create a derived class.
3. Deserialize the derived class, with the base class property included.

What is the expected output? What do you see instead?
The base class property is ignored.

Please provide any additional information below.

I have implemented a fix.  Revision 944d3ebb699b of 
http://code.google.com/r/paulbartrum-dapper-dot-net

Original issue reported on code.google.com by [email protected] on 23 Apr 2011 at 2:05

Oracle ODP.NET connection causes unit tests to fail

What steps will reproduce the problem?
1. download dapper from nuget or by hand
2. open a connection to an Oracle 10 or 11 db
3. run the sample test code
4. exception 

What is the expected output? What do you see instead?

System.ArgumentException was caught
  Message=Value does not fall within the expected range.
  Source=Oracle.DataAccess
  StackTrace:
       at Oracle.DataAccess.Client.OracleParameter.set_DbType(DbType value)
       at ParamInfo6757459b-c923-49af-aba7-97aefc1b85fe(IDbCommand , Object )
       at Dapper.SqlMapper.SetupCommand(IDbConnection cnn, IDbTransaction transaction, String sql, Action`2 paramReader, Object obj, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\code\VisionCore\BE\PPCOM\CoreLogic\Com.BE.BizLib\Com.BuyEfficient.Entity\Dapper\SqlMapper.cs:line 1036
       at Dapper.SqlMapper.<QueryInternal>d__4`1.MoveNext() in C:\code\VisionCore\BE\PPCOM\CoreLogic\Com.BE.BizLib\Com.BuyEfficient.Entity\Dapper\SqlMapper.cs:line 395
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\code\VisionCore\BE\PPCOM\CoreLogic\Com.BE.BizLib\Com.BuyEfficient.Entity\Dapper\SqlMapper.cs:line 351
       at TestProject.UnitTest1.TestDapper() in C:\code\VisionCore\BE\PPCOM\CoreLogic\Com.BE.BizLib\TestProject\UnitTest1.cs:line 41
  InnerException: 



What version of the product are you using? On what operating system?
1.4
Windows 7 64bit Enterprise

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 17 Jun 2011 at 4:28

Tracing/Logging

It would be quite nice if we could log the generated Sql (including params) and 
perhaps execution times (both query and mapping).

Perhaps just passing in an optional Action<string>?

cn.Query("select * from foo where foo.Name = @name", new { name = "Foobar" }, s 
=> Console.WriteLine(s));

Original issue reported on code.google.com by [email protected] on 29 May 2011 at 11:53

Error parsing column 10 (RTP_FLAG=True - Boolean)

What steps will reproduce the problem?
1.return conn.Query<RecordModel,RecordTypeModel,RecordModel>(
                    "SELECT RCD_ID,RCD_RTP_ID,RCD_DT,CRD_TITLE,RCD_AMOUNT,RCD_PLACE,RCD_MEMO,RCD_TYPE,RTP_ID,RTP_NAME,RTP_FLAG FROM BRECORD A LEFT JOIN RRECORDTYPE B ON A.RCD_RTP_ID = B.RTP_ID ORDER BY A.RCD_ID  ",
                    (record, type)
                        =>
                    {
                        record.TYPE = type;
                        return record;
                    },splitOn:"RTP_ID")
                    .ToList<RecordModel>();


2.public static void ThrowDataException(Exception ex, int index, IDataReader 
reader)
        {
            string name = "(n/a)", value = "(n/a)";
            if (reader != null && index >= 0 && index < reader.FieldCount)
            {
                name = reader.GetName(index);
                object val = reader.GetValue(index);
                if (val == null || val is DBNull)
                {
                    value = "<null>";
                }
                else
                {
                    value = Convert.ToString(val) + " - " + Type.GetTypeCode(val.GetType());
                }
            }
            throw new DataException(string.Format("Error parsing column {0} ({1}={2})", index, name, value), ex);-----throw DataException
        }

What is the expected output? What do you see instead?

 System.InvalidCastException: Error parsing column 10 (RTP_FLAG=True - Boolean)
----------------------------------------
Please help me,Thanks!


Original issue reported on code.google.com by [email protected] on 16 Jun 2011 at 11:56

do not use Type information as part for Dictionary key

public static SomeType<T>
{
    public static readonly Object SomeObj = new Object();
}

ReferenceEquals(SomeType<int>.SomeObj, SomeType<int>.SomeObj) => true
ReferenceEquals(SomeType<int>.SomeObj, SomeType<long>.SomeObj) => false

So move targetType/parameterType/otherTypes outside Identity for even faster 
processing:

private static class QueryCacheHolder<TTarget, TParam,TFirst,...TLast>
{
private statict readonly Dictionary<Identity, CacheInfo> = ...
}

Original issue reported on code.google.com by [email protected] on 24 May 2011 at 2:42

Firebird IDbReader is closed when IDbCommand is disposed

What steps will reproduce the problem?
1. Just use a Firebird connection and do a Query(sql)
2. It should throw when executing

What is the expected output? What do you see instead?
Expected is the same result as you would get by doing a manual ExecuteReader. 
Instead you get an InvalidOperation exception

What version of the product are you using? On what operating system?
I used a clone of r4e36b1b7019f.

Please provide any additional information below.
I used Firebird .Net Data Provider v 2.6.0 
(http://www.firebirdsql.org/index.php?op=files&id=netprovider)

Original issue reported on code.google.com by [email protected] on 25 Apr 2011 at 6:04

Inserting a lot of data

This may be not an issue, and might be out of scope of dapper...
I have a list of one million objects - and need to insert them into database.
If I use pure ADO .NET with command.prepare and then loop, insert is done in 
rought 5 minutes. With dapper and command.execute and loop - insert does not 
end within 40 minutes... Would like to read some thoughts and guidance.

Original issue reported on code.google.com by [email protected] on 22 May 2011 at 12:41

Support for customizable data conversion (type coercion)

I posted a question on SO on this topic:

http://stackoverflow.com/q/5898988/64096

Basically what I need is to specify to Dapper how to convert from database data 
to strong typed data.
Alternatively, it would also suffice to have Dapper set a private field or 
property on the data object so that I can implement data conversion within the 
data object (as suggested by Marc Gravell).

The second option is probably what I would use with Linq2Sql.


Original issue reported on code.google.com by [email protected] on 9 May 2011 at 10:06

Multi-mapper fails on SqlCE

using code very similar to what you posted here: http://snipt.org/xgnph I am 
getting the attached exception.

Here are the POCOs:

    public class Post
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }

        public Author Author { get; set; }
    }

    public class Author
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

and here is the execution code:

            using (var cnn = new SqlCeConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyLittleBlog"].ConnectionString))
            {
                cnn.Open();
                var data = cnn.Query<Post, Author>(@"select * from Posts p left join Authors a on a.ID = p.AuthorID order by p.ID", (post, author) => { post.Author = author; });
                var firstPost = data.First();
                var title = firstPost.Title;
                cnn.Close();
            }

running the query against the db returns results just fine:
ID      Title           AuthorID    Body                Expr1   Name
1   Post number one 1       stuff in the body.  1   Sam Moore
2   post another    1       more body stuff.    1   Sam Moore

Lemme know if you want me to zip this up or git it so you can test if yourself.

When I tried to check the reader in the GetClassDeserializer Method the HasRows 
property was returning an exception("SQL Server Compact does not support calls 
to HasRows property if the underlying cursor is not scrollable.").

Original issue reported on code.google.com by [email protected] on 27 Apr 2011 at 12:37

Attachments:

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.