Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thank you to our AWESOME sponsors!

Similar presentations


Presentation on theme: "Thank you to our AWESOME sponsors!"— Presentation transcript:

1 Thank you to our AWESOME sponsors!

2 Dejan Dular, Dejan dular s.p.
dapper adjective (of a man) neat and trim in dress and appearance. "he looked very dapper in a dark silk suit" Dapper the Micro-ORM Dejan Dular, Dejan dular s.p.

3 About me Dejan Dular Dejan Dular s.p. .NET Developer SQL enthusiast
Twitter: @dejandular Mail: LinkedIn: dejandular Dejan Dular Dejan Dular s.p. .NET Developer SQL enthusiast Dynamics CRM specialist IoT fan DIY & eco maniac

4 What are we looking at? High performance Micro-ORM Very light
POCO support Easy to implement – extension of IDbConnection interface Works across all .NET ADO providers very performance more easy such lightweight Dynamic method generation ExpandoObject POCO Wow much providers

5 Demo Difference between using SqlDataReader and Dapper

6 IDbConnection extension methods
connection.Query public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true) public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true) connection.Execute public static int Execute(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null) connection.ExecuteScalar public static T ExecuteScalar<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)

7 Parameterized queries
Parameters are passed in as anonymous classes. connection.Query("SELECT * FROM theTable WHERE a and b new {A = 1, B = "b"});

8 Demo 1, 2, 3 Parametrized queries

9 List Support IEnumerable will automatically parameterize your query. connection.Query("select * from theTable where Id new { Ids = new int[] { 1, 2, 3 }}); …where

10 Run query multiple times
conn.Execute("INSERT INTO tmpTest(Id, Val) new [] { new { Id = 1, Val = "a" }, new { Id = 2, Val = "b" }, new { Id = 3, Val = "c" } });

11 Demo 4 Bulk loading

12 Multi Mapping Load data from a single row to multiple objects.
If the identity column is not named ID, use SplitOn attribute Supports up to 7 joined tables IEnumerable<TReturn> Query<TFirst, TSecond, …, TReturn>(this IDbConnection cnn, string sql, Func<TFirst, TSecond, …, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)

13 Demo 5 Multi mapping

14 Multiple results var sql select * from Customers where CustomerId select * from Orders where CustomerId select * from Returns where CustomerId 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(); ... }

15 Demo 6 Multiple results

16 Stored procedures var p = new Dapper.DynamicParameters(); 11); dbType: DbType.Int32, direction: ParameterDirection.Output); dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); conn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); int b = int c = Instead of conn.Execute you can call conn.Query if the stored procedure returns a record set.

17 Demo 8 Stored procedures

18 Also worth mentioning (IDataReader) ExcuteReader
AsTableValuedParameter(this DataTable…) Query cache management functions Buffering async / await support

19 Demo 9 Asynchronous functions

20 Be careful! By default Dapper buffers the entire reader on return. Set the “buffered” parameter to false if you want to load objects as needed. If the connection is closed, Dapper opens it and closes it at the end of the command execution. Opened connections are left open. If all values can be mapped, object is created even if all are NULL. Read multiple results in the same order as defined in the SQL query.

21 Be careful … more varchar VS nvarchar: To query non Unicode use ANSI : Query<Thing>("select * from Thing where Name new {Name = new DbString{Value="abcde", IsFixedLength=true, Length=10, IsAnsi=true }); Due to query information caching you can hit memory issues if you are generating SQL strings on the fly without using parameters. Further versions may fix this. Class must have a default constructor or a construction that will exactly match the type and order of the columns in the query.

22 What is not there No identity map, there are no helpers for update / select, … Hey! It’s micro ORM

23 Micro ORM vs ORM Features Entity Framework Dapper POCO Yes
Second level cache No Relationships No* Designer Migration Learning curve Slow Fast Performance Meh* Awww yeah! Second-level caches are instrumental in solving this type of problem. These caches exist outside of the transaction—often outside of the application—and therefore are available to any context instance. And second-level caching is a commonly used coding pattern for caching data for various uses.

24 Micro ORM vs ORM Speed Entity Framework Dapper Run First Second
Load 1 record by ID 335 13 72 7 Load many by string 75 34 8 Performing JOIN 134 37 23 19 Inserting 264 12 86

25 Micro ORM vs ORM Speed SELECT mapping over 500 iterations (POCO serialization) Method Duration Hand coded (using a SqlDataReader) 47ms Dapper ExecuteMapperQuery 49ms PetaPoco 52ms NHibernate SQL 104ms Linq 2 SQL ExecuteQuery 181ms Entity Framework ExecuteStoreQuery 631ms

26 https://noamlewis. wordpress

27

28 What to use? EF Dapper Small projects, demo applications, tests
Working with relationships Data layer performance Legacy code with SqlDataReader Big projects with complex repository Bulk data transfers You have a DBA living inside you (a hidden personality) and want to optimize transactions SqlBulkCopy

29 Demo Developing a Dapper extension

30 Links Dapper: https://github.com/StackExchange/dapper-dot-net/
Dapper Contrib: Dapper Rainbow: Dapper Extensions: Dapper SimpleSave: Dapper SimpleLoad: Some micro ORM alternatives: Massive PetaPoco FluentData Simple.Data SubSonic 5 reasons not to use micro ORM: Contrib – CRUD, change tracking Rainbow – CRUD, Extensions – CRUD, Paging SimpleSave, SimpleLoad – Complex objects Massive, PetaPoco – only dynamic objects FluentData – contexts, CRUD, dynamic, but own mappers Simple.Data – NoSQL support Subsonic – Batman Ustility Belt for Data Layer

31 try { EndPresentation(); } catch(QuestionsException q) AnswerQuestions(q); Twitter: @dejandular Mail: LinkedIn: dejandular finally { var attendees = connection.Query("SELECT Name FROM Attendees"); attendees.All(a => { Console.WriteLine($"Thank you {a.Name}!"); return true; }); }


Download ppt "Thank you to our AWESOME sponsors!"

Similar presentations


Ads by Google