35
Welcome Everyone!

Object Relational Mapping with Dapper (Micro ORM)

Embed Size (px)

Citation preview

Page 1: Object Relational Mapping with Dapper (Micro ORM)

Welcome Everyone!

Page 2: Object Relational Mapping with Dapper (Micro ORM)

Today's Topic

Object Relational Mapping with Dapper (a micro ORM)

Page 3: Object Relational Mapping with Dapper (Micro ORM)

Road map

Application Architecture↓

Databases↓

ORM↓

Dapper

Page 4: Object Relational Mapping with Dapper (Micro ORM)

Web App Architecture

Page 5: Object Relational Mapping with Dapper (Micro ORM)

Databases(A quick overview)

Page 6: Object Relational Mapping with Dapper (Micro ORM)

Types of Databases

● Relational Database Management Systems (RDBMS)● NoSQL and Object-Oriented (OODBMS)

Page 7: Object Relational Mapping with Dapper (Micro ORM)

RDBMS

Data Storage

– Rows and columns (Tabular Form)

Features

– Simple, better performance of managing data

– Mathematical foundation (Relational Algebra)

– Flattening and scattering -> unnatural

– Limitations: everything doesn't fit in relations

– Conversion needed (impedance mismatch)

Page 8: Object Relational Mapping with Dapper (Micro ORM)

NoSQL and OODBMS

Data Storage

– Object Form

Features

– No conversion needed (no impedance mismatch)

– Relationships are directly represented, rather than requiring join tables/operations

– Better at modeling complex objects

– No mathematical foundation

– Don’t have maturity of RDBMS yet

Page 9: Object Relational Mapping with Dapper (Micro ORM)

Incompatible Type Systems

● During systems collaboration if systems found having different type systems, they are said to be Incompatible Type Systems and can't interact with each other without an interface. E.g. OO Applications and RDBMS.

● A mechanism/solution is needed to overcome this incompatibility gap for proper collaboration.

Page 10: Object Relational Mapping with Dapper (Micro ORM)

Solution for Incompatibility?

Page 11: Object Relational Mapping with Dapper (Micro ORM)

What is an ORM?

● It is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

Page 12: Object Relational Mapping with Dapper (Micro ORM)

Why to use ORM?

● Many popular database products such as structured query language database management systems (SQL DBMS) can only store and manipulate scalar values such as integers and strings organized within tables. The programmer must convert the object values for storage in the database into groups of simpler values (and convert them back upon retrieval).

● For most of the data-access code that developers usually need to write, it eliminates.

Page 13: Object Relational Mapping with Dapper (Micro ORM)

Problems with ORM

● Object-Relational Impedance Mismatch (paradigm mismatch) is a way of saying that object models and relational models do not work very well together. Loading and storing graphs of objects using a tabular relational database exposes 5 mismatch problems.

– Granularity– Subtypes (inheritance)– Identity– Associations– Data navigation

Page 14: Object Relational Mapping with Dapper (Micro ORM)

Types of ORM

● Entity-based relational mapping– Change tracking, Lazy-loading, Eager fetching, Cascades, Identity map, Unit of work

tracking

● Result-set-based relational mapping– Map straight to DTOs and skip needing to configure mapping

● DML-based relational mapping (micro ORMs)– SQL is brought to the forefront, mapping on the fly and only need a connection

● Bulk loading tools– Limited to INSERT and SELECT

Page 15: Object Relational Mapping with Dapper (Micro ORM)

Boring???

Page 16: Object Relational Mapping with Dapper (Micro ORM)

Dapper(A micro ORM)

Page 17: Object Relational Mapping with Dapper (Micro ORM)

Why Dapper ?

● Simple object mapper for .Net● Performance is a key feature

Page 18: Object Relational Mapping with Dapper (Micro ORM)

Why Dapper is Simple/ Lightweight?

● 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 life cycle, it assumes the connection it gets is open AND has no existing data readers enumerating.

Page 19: Object Relational Mapping with Dapper (Micro ORM)

Dapper Performance Measures?

Page 20: Object Relational Mapping with Dapper (Micro ORM)

Dapper in Action

Page 21: Object Relational Mapping with Dapper (Micro ORM)

Dapper in Action

● Dapper is a “single file” (SqlMapper.cs) that will extend your IDbConnection interface.

● It provides 3 helpers:

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

– Execute a query and map it to a list of dynamic objects– Execute a Command that returns no results

Page 22: Object Relational Mapping with Dapper (Micro ORM)

(1) Query with Strongly Typed Result

Page 23: Object Relational Mapping with Dapper (Micro ORM)

(2) Query with Dynamic Object Result

Page 24: Object Relational Mapping with Dapper (Micro ORM)

(3) Command with No Result

Page 25: Object Relational Mapping with Dapper (Micro ORM)

Execute a Command multiple times

Page 26: Object Relational Mapping with Dapper (Micro ORM)

Execute a Stored Procedure

Page 27: Object Relational Mapping with Dapper (Micro ORM)

Multiple Results in Single Query

Page 28: Object Relational Mapping with Dapper (Micro ORM)

Limitations of Dapper?● 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 life cycle, it assumes the connection it gets is open.

Page 29: Object Relational Mapping with Dapper (Micro ORM)

Final words on Dapper(Finally towards ending!)

Page 30: Object Relational Mapping with Dapper (Micro ORM)

Configurations (Most hectic activity)?

Surprisingly!!! No configuration needed in project, just add reference to the library and you are done.

– Reasons● It uses existing db connection (extends IDbConnection).● It just does object mapping, nothing else as mentioned in

features.

Page 31: Object Relational Mapping with Dapper (Micro ORM)

Applications

● Stack Overflow

● Helpdesk

● And many more

Page 32: Object Relational Mapping with Dapper (Micro ORM)

Dapper and DB providers

● Dapper has no DB specific implementation details, it works across all .Net ADO providers including sqlite, sqlce, firebird, oracle, MySQL, PostgreSQL and SQL Server.

Page 33: Object Relational Mapping with Dapper (Micro ORM)

Conclusion

● Simple and lightweight to implement / use.

● Provide enough helpers/ support to do ORM activities.

● Doesn't suppress developers SQL skills.

Page 34: Object Relational Mapping with Dapper (Micro ORM)

Recommendation (Hybrid Approach)

● Dapper was written with speed (not features) as a priority.

● Using Dapper felt like writing SQL, which majority of developers avoid when possible.

● For Entity Framework slowness issue, replace queries with either a view or a stored procedure, depending on the situation.

● Hybrid approach: Use EF for normal cases due to ease and use Dapper where performance boost in needed.

Page 35: Object Relational Mapping with Dapper (Micro ORM)

References

● http://en.wikipedia.org/wiki/Object-relational_mapping

● http://hibernate.org/orm/what-is-an-orm/

● https://github.com/StackExchange/dapper-dot-net

● https://www.cl.cam.ac.uk/~fms27/db/tr-98-2.pdf

● http://searchcio.techtarget.com/opinion/Relational-databases-are-far-from-dead-just-ask-Facebook

● https://www.quora.com/What-is-LinkedIn-s-database-architecture-like

● http://www.infoq.com/news/2010/08/linkedin-data-infrastructure

● http://blog.dontpaniclabs.com/post/2014/05/01/Speed-Comparison-Dapper-vs-Entity-Framework

● http://lostechies.com/jimmybogard/2012/07/20/choosing-an-orm-strategy/

● http://forums.asp.net/t/1792276.aspx?which+is+better+to+use+dapper+or+entity