Object Relational Mapping with Dapper (Micro ORM)

Post on 15-Apr-2017

896 views 4 download

Transcript of Object Relational Mapping with Dapper (Micro ORM)

Welcome Everyone!

Today's Topic

Object Relational Mapping with Dapper (a micro ORM)

Road map

Application Architecture↓

Databases↓

ORM↓

Dapper

Web App Architecture

Databases(A quick overview)

Types of Databases

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

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)

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

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.

Solution for Incompatibility?

What is an ORM?

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

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.

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

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

Boring???

Dapper(A micro ORM)

Why Dapper ?

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

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.

Dapper Performance Measures?

Dapper in Action

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

(1) Query with Strongly Typed Result

(2) Query with Dynamic Object Result

(3) Command with No Result

Execute a Command multiple times

Execute a Stored Procedure

Multiple Results in Single Query

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.

Final words on Dapper(Finally towards ending!)

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.

Applications

● Stack Overflow

● Helpdesk

● And many more

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.

Conclusion

● Simple and lightweight to implement / use.

● Provide enough helpers/ support to do ORM activities.

● Doesn't suppress developers SQL skills.

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.

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