SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
ORMs – Entity Framework and NHibernate




Bob Davidson
Developer - Blend Interactive
About You
Enough about you
   ●   Bob Davidson
   ●   Developer - Blend Interactive
       ● @funnybob

       ● http://gplus.to/bobdavidson

       ● http://www.PageOfBob.com/

   ●   .NET guy since '04
       ● Some ColdFusion – don't want to


         talk about it.
   ●   Nhibernate guy
The Myth
    “With ORM, the database becomes an
    implementation detail, and can be ignored”
                  -Idiots Everywhere
The Myth




           5
The Project




Maintenance Tracking App
Users                                 Events
●   Those who use                     ●   At the service station
                                      ●   Handy man visit
                                      ●   Vet visit

                Has many                                  Many-to-many

Things                                Work
●   Car                               ●   Transmission flush
●   House                  Has many   ●   Gutters cleaned
●   Cat                               ●   Fluid change?
The Project




The Requirements
    ●   Never Delete – Mark Deleted.
    ●   Track Created Date / Last Modified
    ●   Code-First
The Project




Everybody POCO!
The Basics




NHibernate
    ●   Based on Hibernate - Java Project
    ●   LGPL Open Source
    ●   Been around since 2007 (Hibernate since 2001)
    ●   Large community
The Basics




Entity Framework
    ●   From Microsoft
    ●   First released 2008
    ●   First version SUCKED (IMHO)
    ●   Now Open Source - Apache V2
Mapping




Common 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.
Mapping




Other Mapping Options
    ●   NHibernate
        ● XML

          ● Default

          ● Based on Hibernate

          ● Severe Bracket Tax

    ●   Entity Framework
        ● Visual Designer
Gotchas




NHibernate
    ●   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.
Gotchas




Entity Framework
    ●   Do not initialize collections in the constructor (or
        will cause problems when objects from the DB) –
        instead, initialize them manually when created
        new.
    ●   Can't specify foreign-key names.
    ●   Different inheritance strategies require different
        DbContext / Query strategies (NHibernate only
        requires mapping changes)
Identifiers




To Guid or not to Guid?
    ●   Yeah, we're gonna go ahead and Guid
        ● Easier to migrate data

        ● Using NH's 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 NH's built-in caching if used


          within the same Isession.
        ● NH also supports Load<T> for getting proxy


          objects to use in relationships.
Inheritance




Remember this?
Inheritance




Mapping Strategies
    ●   Table Per Type
    ●   Table Per Hierarchy
    ●   Table Per Concrete Type
Inheritance




Mapping 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
Inheritance
Inheritance




DEMO
Inheritance




Mapping 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
Inheritance
Inheritance




DEMO
Inheritance




Mapping 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
Inheritance
Inheritance




DEMO
Inheritance




Entity 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>
Concurrency




Concurrency Strategies
    ●   None
    ●   Optimistic – All
    ●   Optimistic – Dirty
    ●   Versioned
    ●   Pessimistic
Inheritance




DEMO
Query Patterns




NHibernate
    ●   Careful use of QueryOver<T,T> and inheritance =
        re-usable query logic.
    ●   Join tables / queries
    ●   Easier seen than explained.
Query Patterns




Entity 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.
    ●   Can't apply WHERE logic before joining tables.
Performance




Performance 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.
The Big Question




WHICH IS BETTER?
The Big Question
Geeze, wrap it up already




                  Thank you.
Bob Davidson
Developer - Blend Interactive
@funnybob
http://www.pageofbob.com/
https://github.com/mrdrbob

Contenu connexe

En vedette

Entity and NHibernate ORM Frameworks Compared
Entity and NHibernate ORM Frameworks ComparedEntity and NHibernate ORM Frameworks Compared
Entity and NHibernate ORM Frameworks ComparedZoltan Iszlai
 
NHibernate from inside
NHibernate from insideNHibernate from inside
NHibernate from insideAndriy Buday
 
Introduction to NHibernate
Introduction to NHibernateIntroduction to NHibernate
Introduction to NHibernateDublin Alt,Net
 
NHibernate for .NET
NHibernate for .NETNHibernate for .NET
NHibernate for .NETGuo Albert
 
NHibernate (The ORM For .NET Platform)
NHibernate (The ORM For .NET Platform)NHibernate (The ORM For .NET Platform)
NHibernate (The ORM For .NET Platform)Samnang Chhun
 
Руйнуємо .NET Міфи
Руйнуємо .NET МіфиРуйнуємо .NET Міфи
Руйнуємо .NET МіфиSerhiy Kalinets
 
Micro orm для жизни. Кожевников Дмитрий D2D Just.NET
Micro orm для жизни. Кожевников Дмитрий D2D Just.NETMicro orm для жизни. Кожевников Дмитрий D2D Just.NET
Micro orm для жизни. Кожевников Дмитрий D2D Just.NETDev2Dev
 
Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Alan Christensen
 
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”Lviv Startup Club
 
State of entity framework
State of entity frameworkState of entity framework
State of entity frameworkDavid Paquette
 
The Innovation Game: Why & How Businesses are Investing in Innovation Centers
The Innovation Game: Why & How Businesses are Investing in Innovation Centers The Innovation Game: Why & How Businesses are Investing in Innovation Centers
The Innovation Game: Why & How Businesses are Investing in Innovation Centers Capgemini
 

En vedette (15)

Entity and NHibernate ORM Frameworks Compared
Entity and NHibernate ORM Frameworks ComparedEntity and NHibernate ORM Frameworks Compared
Entity and NHibernate ORM Frameworks Compared
 
NHibernate
NHibernateNHibernate
NHibernate
 
NHibernate from inside
NHibernate from insideNHibernate from inside
NHibernate from inside
 
Introduction to NHibernate
Introduction to NHibernateIntroduction to NHibernate
Introduction to NHibernate
 
NHibernate
NHibernateNHibernate
NHibernate
 
NHibernate for .NET
NHibernate for .NETNHibernate for .NET
NHibernate for .NET
 
NHibernate
NHibernateNHibernate
NHibernate
 
ASP.Net MVC
ASP.Net MVCASP.Net MVC
ASP.Net MVC
 
NHibernate (The ORM For .NET Platform)
NHibernate (The ORM For .NET Platform)NHibernate (The ORM For .NET Platform)
NHibernate (The ORM For .NET Platform)
 
Руйнуємо .NET Міфи
Руйнуємо .NET МіфиРуйнуємо .NET Міфи
Руйнуємо .NET Міфи
 
Micro orm для жизни. Кожевников Дмитрий D2D Just.NET
Micro orm для жизни. Кожевников Дмитрий D2D Just.NETMicro orm для жизни. Кожевников Дмитрий D2D Just.NET
Micro orm для жизни. Кожевников Дмитрий D2D Just.NET
 
Domain Driven Design Demonstrated
Domain Driven Design Demonstrated Domain Driven Design Demonstrated
Domain Driven Design Demonstrated
 
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”
Lviv MDDay 2014. Дмитро Андреєв “знайомство з .Net micro framework”
 
State of entity framework
State of entity frameworkState of entity framework
State of entity framework
 
The Innovation Game: Why & How Businesses are Investing in Innovation Centers
The Innovation Game: Why & How Businesses are Investing in Innovation Centers The Innovation Game: Why & How Businesses are Investing in Innovation Centers
The Innovation Game: Why & How Businesses are Investing in Innovation Centers
 

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

Zope is dead - Long live Zope
Zope is dead - Long live ZopeZope is dead - Long live Zope
Zope is dead - Long live ZopeLennart Regebro
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011lusis
 
MapReduce with Hadoop and Ruby
MapReduce with Hadoop and RubyMapReduce with Hadoop and Ruby
MapReduce with Hadoop and RubySwanand Pagnis
 
Deploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremDeploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremKris Buytaert
 
Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Python Ireland
 
Utopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersUtopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersJaime Buelta
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with PuppetKris Buytaert
 
Puppet managed loadays
Puppet managed loadaysPuppet managed loadays
Puppet managed loadaysloadays
 
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...Chauvin Mariot
 
BlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search FeedbackBlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search Feedbacksinfomicien
 
Ruby, the language of devops
Ruby, the language of devopsRuby, the language of devops
Ruby, the language of devopsRob Kinyon
 
Moby is killing your devops efforts
Moby is killing your devops effortsMoby is killing your devops efforts
Moby is killing your devops effortsKris Buytaert
 
Monitoring your VM's at Scale
Monitoring your VM's at ScaleMonitoring your VM's at Scale
Monitoring your VM's at ScaleKris Buytaert
 
Scaling up wso2 bam for billions of requests and terabytes of data
Scaling up wso2 bam for billions of requests and terabytes of dataScaling up wso2 bam for billions of requests and terabytes of data
Scaling up wso2 bam for billions of requests and terabytes of dataWSO2
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsGR8Conf
 
Performance optimization techniques for Java code
Performance optimization techniques for Java codePerformance optimization techniques for Java code
Performance optimization techniques for Java codeAttila Balazs
 
Approaching zero driver overhead
Approaching zero driver overheadApproaching zero driver overhead
Approaching zero driver overheadCass Everitt
 

Similaire à "ORMs – Entity Framework and NHibernate" - Bob Davidson, South Dakota Code Camp 2012 (20)

Zope is dead - Long live Zope
Zope is dead - Long live ZopeZope is dead - Long live Zope
Zope is dead - Long live Zope
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011
 
MapReduce with Hadoop and Ruby
MapReduce with Hadoop and RubyMapReduce with Hadoop and Ruby
MapReduce with Hadoop and Ruby
 
Deploying your SaaS stack OnPrem
Deploying your SaaS stack OnPremDeploying your SaaS stack OnPrem
Deploying your SaaS stack OnPrem
 
Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+
 
Utopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersUtopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K users
 
Automating MySQL operations with Puppet
Automating MySQL operations with PuppetAutomating MySQL operations with Puppet
Automating MySQL operations with Puppet
 
Sensible scaling
Sensible scalingSensible scaling
Sensible scaling
 
Puppet managed loadays
Puppet managed loadaysPuppet managed loadays
Puppet managed loadays
 
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
Mixing Diagram, Tree, Text, Table and Form editors to build a kick-ass modeli...
 
BlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search FeedbackBlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search Feedback
 
Ruby, the language of devops
Ruby, the language of devopsRuby, the language of devops
Ruby, the language of devops
 
Moby is killing your devops efforts
Moby is killing your devops effortsMoby is killing your devops efforts
Moby is killing your devops efforts
 
Monitoring your VM's at Scale
Monitoring your VM's at ScaleMonitoring your VM's at Scale
Monitoring your VM's at Scale
 
rsyslog meets docker
rsyslog meets dockerrsyslog meets docker
rsyslog meets docker
 
Scaling up wso2 bam for billions of requests and terabytes of data
Scaling up wso2 bam for billions of requests and terabytes of dataScaling up wso2 bam for billions of requests and terabytes of data
Scaling up wso2 bam for billions of requests and terabytes of data
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails Projects
 
Performance optimization techniques for Java code
Performance optimization techniques for Java codePerformance optimization techniques for Java code
Performance optimization techniques for Java code
 
Practical domain driven design
Practical domain driven designPractical domain driven design
Practical domain driven design
 
Approaching zero driver overhead
Approaching zero driver overheadApproaching zero driver overhead
Approaching zero driver overhead
 

Plus de Blend Interactive

"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...
"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ..."The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...
"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...Blend Interactive
 
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ..."The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...Blend Interactive
 
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017Blend Interactive
 
"Never Knowing Enough: dealing with the self doubt that hinders your success....
"Never Knowing Enough: dealing with the self doubt that hinders your success...."Never Knowing Enough: dealing with the self doubt that hinders your success....
"Never Knowing Enough: dealing with the self doubt that hinders your success....Blend Interactive
 
"Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con..."Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con...Blend Interactive
 
"Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con..."Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con...Blend Interactive
 
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...Blend Interactive
 
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...Content Measurement and Analytics: Making Positive Change on the Web by Rick ...
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...Blend Interactive
 
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...Blend Interactive
 
"Adaptive Content, Context, and Controversy
"Adaptive Content, Context, and Controversy"Adaptive Content, Context, and Controversy
"Adaptive Content, Context, and ControversyBlend Interactive
 
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017Blend Interactive
 
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017Blend Interactive
 
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017Blend Interactive
 
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...Blend Interactive
 
Content Design for Mobile Devices - Now What? Conference 2017
Content Design for Mobile Devices - Now What? Conference 2017Content Design for Mobile Devices - Now What? Conference 2017
Content Design for Mobile Devices - Now What? Conference 2017Blend Interactive
 
"Making Things Real: Taking content strategy from abstract to functional" - M...
"Making Things Real: Taking content strategy from abstract to functional" - M..."Making Things Real: Taking content strategy from abstract to functional" - M...
"Making Things Real: Taking content strategy from abstract to functional" - M...Blend Interactive
 
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...Blend Interactive
 
"The Self-Directed Strategist: Building a Practice and Managing Organizationa...
"The Self-Directed Strategist: Building a Practice and Managing Organizationa..."The Self-Directed Strategist: Building a Practice and Managing Organizationa...
"The Self-Directed Strategist: Building a Practice and Managing Organizationa...Blend Interactive
 
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...Blend Interactive
 

Plus de Blend Interactive (20)

"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...
"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ..."The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...
"The Accessible Editor Workshop" by Corey Vilhauer, from Now What? Workshops ...
 
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ..."The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...
"The Accessible Editor" by Corey Vilhauer, from DrupalCon 2018 in Nashville, ...
 
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017
"Click to Continue" by Sam Otis, from Content+Design Meetup, Oct. 4, 2017
 
"Never Knowing Enough: dealing with the self doubt that hinders your success....
"Never Knowing Enough: dealing with the self doubt that hinders your success...."Never Knowing Enough: dealing with the self doubt that hinders your success....
"Never Knowing Enough: dealing with the self doubt that hinders your success....
 
"Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con..."Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con...
 
"Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con..."Making things real: Content strategy for realistic content management" - Con...
"Making things real: Content strategy for realistic content management" - Con...
 
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...
Getting Started With User-Centered Content by Emileigh Barnes & Kate Garklavs...
 
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...Content Measurement and Analytics: Making Positive Change on the Web by Rick ...
Content Measurement and Analytics: Making Positive Change on the Web by Rick ...
 
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...
“How Silos Learn: Working in the Idea Factory” by Amanda Costello - Now What?...
 
"Adaptive Content, Context, and Controversy
"Adaptive Content, Context, and Controversy"Adaptive Content, Context, and Controversy
"Adaptive Content, Context, and Controversy
 
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017
"Empathy Behind the Algorithms" by Chris Corak - Now What? Conference 2017
 
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017
“The Beauty of Brevity” by Ravi Jain - Now What? Conference 2017
 
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017
“Why Content Projects Fail” by Deane Barker - Now What? Conference 2017
 
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...
Not Perfect, "Always Better: A Story of Inclusion" by Derek Featherstone - No...
 
Content Design for Mobile Devices - Now What? Conference 2017
Content Design for Mobile Devices - Now What? Conference 2017Content Design for Mobile Devices - Now What? Conference 2017
Content Design for Mobile Devices - Now What? Conference 2017
 
"Making Things Real: Taking content strategy from abstract to functional" - M...
"Making Things Real: Taking content strategy from abstract to functional" - M..."Making Things Real: Taking content strategy from abstract to functional" - M...
"Making Things Real: Taking content strategy from abstract to functional" - M...
 
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...
“Writing for Your Audience — The Message, the Words, the Plan” – Business Sen...
 
Functional concepts in C#
Functional concepts in C#Functional concepts in C#
Functional concepts in C#
 
"The Self-Directed Strategist: Building a Practice and Managing Organizationa...
"The Self-Directed Strategist: Building a Practice and Managing Organizationa..."The Self-Directed Strategist: Building a Practice and Managing Organizationa...
"The Self-Directed Strategist: Building a Practice and Managing Organizationa...
 
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...
Next Level Collaboration: The Future of Content and Design by Rebekah Cancino...
 

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

  • 1. ORMs – Entity Framework and NHibernate Bob Davidson Developer - Blend Interactive
  • 3. Enough about you ● Bob Davidson ● Developer - Blend Interactive ● @funnybob ● http://gplus.to/bobdavidson ● http://www.PageOfBob.com/ ● .NET guy since '04 ● Some ColdFusion – don't want to talk about it. ● Nhibernate guy
  • 4. The Myth “With ORM, the database becomes an implementation detail, and can be ignored” -Idiots Everywhere
  • 6. The Project Maintenance Tracking App Users Events ● Those who use ● At the service station ● Handy man visit ● Vet visit Has many Many-to-many Things Work ● Car ● Transmission flush ● House Has many ● Gutters cleaned ● Cat ● Fluid change?
  • 7. The Project The Requirements ● Never Delete – Mark Deleted. ● Track Created Date / Last Modified ● Code-First
  • 9. The Basics NHibernate ● Based on Hibernate - Java Project ● LGPL Open Source ● Been around since 2007 (Hibernate since 2001) ● Large community
  • 10. The Basics Entity Framework ● From Microsoft ● First released 2008 ● First version SUCKED (IMHO) ● Now Open Source - Apache V2
  • 11. Mapping Common 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. Mapping Other Mapping Options ● NHibernate ● XML ● Default ● Based on Hibernate ● Severe Bracket Tax ● Entity Framework ● Visual Designer
  • 13. Gotchas NHibernate ● 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. Gotchas Entity Framework ● Do not initialize collections in the constructor (or will cause problems when objects from the DB) – instead, initialize them manually when created new. ● Can't specify foreign-key names. ● Different inheritance strategies require different DbContext / Query strategies (NHibernate only requires mapping changes)
  • 15. Identifiers To Guid or not to Guid? ● Yeah, we're gonna go ahead and Guid ● Easier to migrate data ● Using NH's 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 NH's built-in caching if used within the same Isession. ● NH also supports Load<T> for getting proxy objects to use in relationships.
  • 17. Inheritance Mapping Strategies ● Table Per Type ● Table Per Hierarchy ● Table Per Concrete Type
  • 18. Inheritance Mapping 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
  • 21. Inheritance Mapping 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
  • 24. Inheritance Mapping 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
  • 27. Inheritance Entity 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. Concurrency Concurrency Strategies ● None ● Optimistic – All ● Optimistic – Dirty ● Versioned ● Pessimistic
  • 30. Query Patterns NHibernate ● Careful use of QueryOver<T,T> and inheritance = re-usable query logic. ● Join tables / queries ● Easier seen than explained.
  • 31. Query Patterns Entity 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. ● Can't apply WHERE logic before joining tables.
  • 32. Performance Performance 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.
  • 35. Geeze, wrap it up already Thank you. Bob Davidson Developer - Blend Interactive @funnybob http://www.pageofbob.com/ https://github.com/mrdrbob