Learning NHibernate 4
eBook - ePub

Learning NHibernate 4

Suhas Chatekar

  1. 402 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning NHibernate 4

Suhas Chatekar

Book details
Book preview
Table of contents
Citations

About This Book

This book targets .NET developers who have never used an ORM before, developers who have used an ORM before but are new to NHibernate, or have used NHibernate sparingly and want to learn more about NHibernate.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learning NHibernate 4 an online PDF/ePUB?
Yes, you can access Learning NHibernate 4 by Suhas Chatekar in PDF and/or ePUB format, as well as other popular books in Computer Science & Application Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781784393564
Edition
1

Learning NHibernate 4


Table of Contents

Learning NHibernate 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introduction to NHibernate
What is ORM?
What is NHibernate?
What is new in NHibernate 4.0?
NHibernate for users of Entity Framework
Is using ORM a bad idea?
Why ORM is a better bet?
Non-functional features required by the data access layer
Building blocks of NHibernate
Mappings
Configuration
Session
Summary
2. Let's Build a Simple Application
Explaining the approach used in learning NHibernate
The problem statement
The development setup
Visual Studio 2013
SQL Server 2012 Express
ReSharper
NUnit
NuGet
SQLite
The domain model
Employee
Leave
SeasonTicketLoan
SkillEnhancementAllowance
Adding some code
Adding the NuGet packages to projects
Back to our test
Summary
3. Let's Tell NHibernate About Our Database
Important NHibernate terminology
Mapping the prerequisites
Different mechanisms to write the mappings
XML mappings
Fluent mapping
Mapping by code
XML mappings for the Employee class
Getting the development environment ready
Getting IntelliSense to work with the XML mapping files
Unit tests to verify the Employee mappings
The mappings
Hibernate-mapping
Class
Property
Identifier generation
Identity
Sequence
Hilo
Seqhilo
GUID
Guid.comb
Native
Assigned
Mapping associations
Associations and database tables
One-to-many association
Types of collections
Many-to-one association
One-to-one association
Many-to-many association
Mapping inheritance
Unit tests to verify the inheritance mappings
Table per class hierarchy
Table per subclass
Table per concrete class
Choosing the right inheritance mapping strategy
Mapping components
Mapping by code
Identifier
Property
Association
One-to-many
Many-to-one
One-to-one
Many-to-many
Component
Inheritance
Table per class hierarchy
Table per subclass
Table per concrete class
Complete mappings by code for the employee benefits domain
Fluent mapping a.k.a. Fluent NHibernate
Inheritance mappings
Table per class hierarchy
Table per concrete class
Table per subclass
Component mapping
Choosing the right mapping method
Summary
4. NHibernate Warm-up
Warming up NHibernate succinctly
The NHibernate session architecture
XML configuration
Programmatic configuration
Loquacious configuration
Fluent configuration
Configuration options
Connection release mode
Dialect
Driver
Mappings
Programmatic configuration
XML mapping
Mapping by code
Fluent configuration
Connection string
Caching
Session context
Batch size
Command timeout
Show SQL
Generating the database scripts from mappings
The database creation script
The database update scripts
Automatically create/update the database schema
Summary
5. Let's Store Some Data into the Database
Entity states
Transactions and unit of work
Explicit and implicit transactions
Flush modes
Saving entities
Saving entities – complex scenarios
Transitive persistence using cascade styles
Transitive persistence/cascading in action
Saving a transient entity with association to other transient entities
A word on bidirectional associations
Updating associations on a persistent entity
Deleting a persistent entity with one or more associations set
Bidirectional associations and ownership
Ownership in many-to-many associations
Inverse and cascade
Order of operations
Entity equality
Why does equality matter?
Implementing equality
The architecture diagram
Summary
6. Let's Retrieve Some Data from the Database
Querying the workflow
Different querying methods
Native SQL
Hibernate Query Language
Parameter binding
Criteria API
Filtering the criteria queries
The QueryOver API
LINQ
Which querying method should I use?
Joins
Different types of joins
Implicit joins
QueryOver joins explained
JoinAlias
Outer joins
Theta style joins
Lazy loading
Lazy collections
Disabling laziness
Different lazy behaviors
Single-ended lazy associations
Proxy versus NoProxy
Lazy properties
Why use lazy loading?
Lazy loading gotcha's
Keeping the session open
Being aware of the select N+1 problem
Using lazy loading together with the right fetching strategy
Using automatic properties
Eager fetching
HQL
Criteria
QueryOver
LINQ
Pagination and ordering of results
Narrowing down the number of records returned by a query
Ordering the results
Retrieving entities by identifiers
Why use Load<T>?
Polymorphic queries
Summary
7. Optimizing the Data Access Layer
Baseline and optimize
Organization of the chapter
Batching
Batching the write queries using ADO.NET batching
Limitations of batching
Memory consumption and batching
Read batching using future queries
Batching lazy collections
Fetching strategies
Select fetching
Join fetching
Subselect fetching
Choosing the right fetching strategy
Avoiding the select N+1 problem
What is the select N+1 problem?
Why select N+1 is a problem?
How do we fix the select N+1 problem?
Avoiding eager fetching
Future queries to the rescue
Using extra-lazy for lazy collections
Summary
8. Using NHibernate in a Real-world Application
What kind of application are we building?
Architectural principles we will follow
No anaemic domain model
The onion architecture
Dependency inversion
Explicitly declaring the capabilities required
Unit of work
Feature 1 – onboarding an employee
Repository pattern
Where does the repository belong?
Back to onboarding an employee
Unit of work
Scope of unit of work for web applications
Session per request
Contextual sessions
Configuring CurrentSessionContext
Implementing session per request using the contextual sessions
Using the HTTP module
Using hooks in the global.ascx file
Using the action filters of ASP.NET MVC or Web API
WCF
Unit of work implementation
Unit of work spanning the whole HTTP request
Unit of work with custom scope
Feature 2 – updating the details of an employee
Updates using transitive persistence
Updating the detached entities
Why transitive persistence is better
Feature 3 – searching employees
Summary
9. Advanced Data Access Patterns
Problems with the repository pattern
Generalization
Unclear and confusing contract
Leaky abstraction
God object anti-pattern
Specification pattern
Specification pattern in its original form
Specification pattern for NHibernate
Specification chaining
Limitations
Query object pattern
Extending the query object pattern
The limitations of the query object pattern
Summary
10. Working with Legacy Database
Composite IDs
Composite foreign key
Components
Join-mapping to load data from multiple tables
Working with database views
Using subselect instead of views
Working with stored procedures
Using custom DTOs
Right place for DTOs
The conversion layer
Downsides of using custom DTOs
Custom lazy loading retention
Summary
11. A Whirlwind Tour of Other NHibernate Features
Concurrency control
Optimistic concurrency control
Using optimistic-lock
Using the version property
Pessimistic concurrency control
Event system
Event listeners
Adding the audit trail for entities
Caching
Session level cache
Second level cache
Query cache
Stateless sessions
User-defined types
Other user-defined types
Summary
Index

Learning NHibernate 4

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of ...

Table of contents

  1. Learning NHibernate 4