ORMs – Entity Framework and NHibernateBob DavidsonDeveloper - Blend Interactive
About You
Enough about you   ●   Bob Davidson   ●   Developer - Blend Interactive       ● @funnybob       ● http://gplus.to/bobdavid...
The Myth    “With ORM, the database becomes an    implementation detail, and can be ignored”                  -Idiots Ever...
The Myth           5
The ProjectMaintenance Tracking AppUsers                                 Events●   Those who use                     ●   A...
The ProjectThe Requirements    ●   Never Delete – Mark Deleted.    ●   Track Created Date / Last Modified    ●   Code-First
The ProjectEverybody POCO!
The BasicsNHibernate    ●   Based on Hibernate - Java Project    ●   LGPL Open Source    ●   Been around since 2007 (Hiber...
The BasicsEntity Framework    ●   From Microsoft    ●   First released 2008    ●   First version SUCKED (IMHO)    ●   Now ...
MappingCommon Options   ●   Automap       ● Easy, lazy, dangrous - May default to         nvarchar(max)   ●   Annotations ...
MappingOther Mapping Options    ●   NHibernate        ● XML          ● Default          ● Based on Hibernate          ● Se...
GotchasNHibernate    ●   All properties must be virtual    ●   Uses specific collections        ● Set (HashSet<>) - distin...
GotchasEntity Framework    ●   Do not initialize collections in the constructor (or        will cause problems when object...
IdentifiersTo Guid or not to Guid?    ●   Yeah, were gonna go ahead and Guid        ● Easier to migrate data        ● Usin...
InheritanceRemember this?
InheritanceMapping Strategies    ●   Table Per Type    ●   Table Per Hierarchy    ●   Table Per Concrete Type
InheritanceMapping Strategies    ●   Table Per Type        ● One table for each type, including abstract          base typ...
Inheritance
InheritanceDEMO
InheritanceMapping Strategies    ●   Table Per Type    ●   Table Per Hierarchy        ● All types crammed into 1 table    ...
Inheritance
InheritanceDEMO
InheritanceMapping Strategies    ●   Table Per Type    ●   Table Per Hierarchy    ●   Table Per Concrete Type        ● A t...
Inheritance
InheritanceDEMO
InheritanceEntity Framework Weirdness    ●   Table Per Type    ●   Table Per Hierarchy        ● Use DbSet<BaseObject>    ●...
ConcurrencyConcurrency Strategies    ●   None    ●   Optimistic – All    ●   Optimistic – Dirty    ●   Versioned    ●   Pe...
InheritanceDEMO
Query PatternsNHibernate    ●   Careful use of QueryOver<T,T> and inheritance =        re-usable query logic.    ●   Join ...
Query PatternsEntity Framework    ●   PredicateBuilder was as close as I could get.    ●   Query syntax changed slightly d...
PerformancePerformance Considerations    ●   Get<T> == SELECT *    ●   The (N + 1) problem [Lazy Loading]    ●   Future qu...
The Big QuestionWHICH IS BETTER?
The Big Question
Geeze, wrap it up already                  Thank you.Bob DavidsonDeveloper - Blend Interactive@funnybobhttp://www.pageofbo...
Upcoming SlideShare
Loading in …5
×

"ORMs – Entity Framework and NHibernate" - Bob Davidson, South Dakota Code Camp 2012

4,338 views

Published on

"ORMs – Entity Framework and NHibernate" by Bob Davidson, given November 10, 2012, at South Dakota Code Camp 2012 in Sioux Falls.

1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
4,338
On SlideShare
0
From Embeds
0
Number of Embeds
54
Actions
Shares
0
Downloads
30
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

"ORMs – Entity Framework and NHibernate" - Bob Davidson, South Dakota Code Camp 2012

  1. 1. ORMs – Entity Framework and NHibernateBob DavidsonDeveloper - Blend Interactive
  2. 2. About You
  3. 3. Enough about you ● Bob Davidson ● Developer - Blend Interactive ● @funnybob ● http://gplus.to/bobdavidson ● http://www.PageOfBob.com/ ● .NET guy since 04 ● Some ColdFusion – dont want to talk about it. ● Nhibernate guy
  4. 4. The Myth “With ORM, the database becomes an implementation detail, and can be ignored” -Idiots Everywhere
  5. 5. The Myth 5
  6. 6. The ProjectMaintenance Tracking AppUsers Events● Those who use ● At the service station ● Handy man visit ● Vet visit Has many Many-to-manyThings Work● Car ● Transmission flush● House Has many ● Gutters cleaned● Cat ● Fluid change?
  7. 7. The ProjectThe Requirements ● Never Delete – Mark Deleted. ● Track Created Date / Last Modified ● Code-First
  8. 8. The ProjectEverybody POCO!
  9. 9. The BasicsNHibernate ● Based on Hibernate - Java Project ● LGPL Open Source ● Been around since 2007 (Hibernate since 2001) ● Large community
  10. 10. The BasicsEntity Framework ● From Microsoft ● First released 2008 ● First version SUCKED (IMHO) ● Now Open Source - Apache V2
  11. 11. MappingCommon Options ● Automap ● Easy, lazy, dangrous - May default to nvarchar(max) ● Annotations ● Control, less work, decorates your POCO with DB concerns. ● Fluent ● Control, separation of concerns, manual work.
  12. 12. MappingOther Mapping Options ● NHibernate ● XML ● Default ● Based on Hibernate ● Severe Bracket Tax ● Entity Framework ● Visual Designer
  13. 13. GotchasNHibernate ● All properties must be virtual ● Uses specific collections ● Set (HashSet<>) - distinct, sans-order ● Bag (List<>) - sans-order ● Map (Dictionary<>) ● List (List<>) ● Currently, you can reference IESE collections, or use ICollection<> for Set. ● Usually initialize collections in the contstructor.
  14. 14. GotchasEntity Framework ● Do not initialize collections in the constructor (or will cause problems when objects from the DB) – instead, initialize them manually when created new. ● Cant specify foreign-key names. ● Different inheritance strategies require different DbContext / Query strategies (NHibernate only requires mapping changes)
  15. 15. IdentifiersTo Guid or not to Guid? ● Yeah, were gonna go ahead and Guid ● Easier to migrate data ● Using NHs Guid.Comb to prevent index fragmentation. (No EF analog) ● NHibernate supports Get<T>(object id), whereas EF requires you query Where(x => x.ID = id). ● The Get<T> uses NHs built-in caching if used within the same Isession. ● NH also supports Load<T> for getting proxy objects to use in relationships.
  16. 16. InheritanceRemember this?
  17. 17. InheritanceMapping Strategies ● Table Per Type ● Table Per Hierarchy ● Table Per Concrete Type
  18. 18. InheritanceMapping Strategies ● Table Per Type ● One table for each type, including abstract base types. ● Most normalized ● Least performant (generally) ● Table Per Hierarchy ● Table Per Concrete Type
  19. 19. Inheritance
  20. 20. InheritanceDEMO
  21. 21. InheritanceMapping Strategies ● Table Per Type ● Table Per Hierarchy ● All types crammed into 1 table ● Least normalized, cannot enforce NOT NULL at the DB level ● NHibernate will let you set NOT NULL, and will try to enforce it, causing issues. ● Table Per Concrete Type
  22. 22. Inheritance
  23. 23. InheritanceDEMO
  24. 24. InheritanceMapping Strategies ● Table Per Type ● Table Per Hierarchy ● Table Per Concrete Type ● A table per instantiatable class (base class properties folded into each class) ● Balance of the former two options
  25. 25. Inheritance
  26. 26. InheritanceDEMO
  27. 27. InheritanceEntity Framework Weirdness ● Table Per Type ● Table Per Hierarchy ● Use DbSet<BaseObject> ● Table Per Concrete Type ● Use DbSet<User>, DbSet<Thing>, etc. ● Do not map DbSet<BaseObject>
  28. 28. ConcurrencyConcurrency Strategies ● None ● Optimistic – All ● Optimistic – Dirty ● Versioned ● Pessimistic
  29. 29. InheritanceDEMO
  30. 30. Query PatternsNHibernate ● Careful use of QueryOver<T,T> and inheritance = re-usable query logic. ● Join tables / queries ● Easier seen than explained.
  31. 31. Query PatternsEntity Framework ● PredicateBuilder was as close as I could get. ● Query syntax changed slightly depending on the inheritance strategy chosen. ● Joining tables changes the “shape” of the query, making query logic very difficult to generalize. ● Cant apply WHERE logic before joining tables.
  32. 32. PerformancePerformance Considerations ● Get<T> == SELECT * ● The (N + 1) problem [Lazy Loading] ● Future queries (NH built-in, EF add-on) ● In NH, all queries are run in a transaction – can be beneficial to wrap all queries in 1 transaction, rather than have multiple transactions.
  33. 33. The Big QuestionWHICH IS BETTER?
  34. 34. The Big Question
  35. 35. Geeze, wrap it up already Thank you.Bob DavidsonDeveloper - Blend Interactive@funnybobhttp://www.pageofbob.com/https://github.com/mrdrbob

×