NHibernate 4.x Cookbook - Second Edition

Book description

Over 90 incredible and powerful recipes to help you efficiently use NHibernate in your application

About This Book

  • Master the full range of NHibernate features through detailed example recipes that you can quickly apply to your own applications
  • Reduce hours of application development time and get a better application architecture and improved performance
  • Create, maintain, and update your database structure automatically with the help of NHibernate

Who This Book Is For

This book is written for .NET developers who want to use NHibernate and those who want to deepen their knowledge of the platform. Examples are written in C# and XML. Some basic knowledge of SQL is assumed. If you build .NET applications that use relational databases, this book is for you.

What You Will Learn

  • Create a persistent object model to move data in and out of your database
  • Build the database from your model automatically
  • Configure NHibernate for use with WebForms, MVC, WPF, and WinForms applications
  • Create database queries using a variety of methods
  • Improve the performance of your applications using a variety of techniques
  • Build an infrastructure for fast, easy, test-driven development of your data access layer
  • Implement entity validation, auditing, full-text search, horizontal partitioning (sharding), and spatial queries using NHibernate Contrib projects

In Detail

NHibernate is a mature, flexible, scalable, and feature-complete open source project for data access. Although it sounds like an easy task to build and maintain database applications, it can be challenging to get beyond the basics and develop applications that meet your needs perfectly. NHibernate allows you to use plain SQL and stored procedures less and keep focus on your application logic instead. Learning the best practices for a NHibernate-based application will help you avoid problems and ensure that your project is a success.

The book will take you from the absolute basics of NHibernate through to its most advanced features, showing you how to take full advantage of each concept to quickly create amazing database applications. You will learn several techniques for each of the four core NHibernate tasks - configuration, mapping, session and transaction management, and querying - and which techniques fit best with various types of applications. In short, you will be able to build an application using NHibernate by the end of the book.

You will also learn how to best implement enterprise application architecture patterns using NHibernate, leading to clean, easy-to-understand code and increased productivity. In addition to new features, you will learn creative ways to extend the NHibernate core, as well as gaining techniques to work with the NHibernate search, shards, spatial, envers, and validation projects.

Style and approach

This book contains recipes with examples organized in functional areas, each containing step-by-step instructions on everything necessary to execute a particular task. The book is designed so you can read it from start to end or just open up any chapter and start following the recipes.

Table of contents

  1. NHibernate 4.x Cookbook Second Edition
    1. Table of Contents
    2. NHibernate 4.x Cookbook Second Edition
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why Subscribe?
    7. Customer Feedback
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    9. 1. The Configuration and Schema
      1. Introduction
      2. Installing NHibernate
        1. Getting ready
        2. How to do it...
        3. There's more…
      3. Configuring NHibernate with hibernate.cfg.xml
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. NHibernate architecture
          2. NHibernate properties
          3. Dialects and drivers
        5. See also
      4. Configuring NHibernate with App.config or Web.config
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Configuring NHibernate with code
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more...
        5. See also
      6. Configuring NHibernate with Fluent NHibernate
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Configuring NHibernate logging
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Using logger to troubleshoot NHibernate
          2. Using other log providers
        5. See also
      8. Generating the database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Scripting the database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      10. Updating the database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      11. Using NHibernate schema tool
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    10. 2. Models and Mappings
      1. Mapping a class with XML
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. What happens to these mappings?
          2. Surrogate keys and natural IDs
          3. ID generator selection
        5. See also
      2. Creating class hierarchy mappings
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Table per class
          2. Table per concrete class
        5. See also
      3. Creating class components
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Mapping a one-to-many relationship
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Lazy loading collections
          2. Lazy loading proxies
          3. Collections
            1. Bags
            2. Lists
            3. Sets
            4. Map
        5. See also
      5. Mapping a many-to-many relationship
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Mapping collections of elements and components
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      7. Setting up a base entity class
        1. How to do it…
        2. How it works…
        3. There's more…
        4. See also
      8. Handling versioning and concurrency
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Mapping by code
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      10. Mapping by convention
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      11. Creating mappings fluently
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      12. Bidirectional one-to-many class relationships
        1. How to do it…
        2. How it works…
        3. There's more…
      13. Mapping enumerations
        1. How to do it…
        2. How it works…
          1. Unnecessary updates
        3. See also
      14. Immutable entities
        1. Getting ready
        2. How to do it…
        3. How it works…
      15. Mapping relations to non-primary keys
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      16. Using lazy properties
        1. Getting ready
        2. How to do it…
        3. How it works…
      17. Mapping joins
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      18. Using calculated properties
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      19. Using serializable values
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      20. Dynamic components
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      21. Mapping <subselect>
        1. Getting ready
        2. How to do it…
        3. How it works…
    11. 3. Sessions and Transactions
      1. Introduction
      2. Setting up session-per-web request
        1. Getting ready
          1. Option 1: Using the companion library
          2. Option 2: Manual setup
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Setting up session-per-presenter
        1. How to do it…
        2. How it works…
        3. There's More…
        4. See also
      4. Creating a session ASP.NET MVC action filter
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. What about Web API?
        4. There's more…
          1. View models
        5. See also
      5. Creating a transaction ASP.NET MVC action filter
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Save entities to the database
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Save
        4. Update
        5. SaveOrUpdate
        6. Persist
        7. Using different flush modes
          1. Commit
          2. Auto
          3. Always
          4. Never
      7. Using session.Merge
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      8. Using session.Refresh
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Handle concurrency using session.Lock
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. None
          2. Read
          3. Upgrade
          4. UpgradeNoWait
      10. There's more…
      11. Using dictionaries as entities
        1. Getting ready
        2. How to do it...
        3. How it works…
        4. There's more…
          1. Partially dynamic
      12. Using NHibernate with transaction scope
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    12. 4. Queries
      1. Introduction
        1. Getting ready
      2. Query entities by ID
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Session.Get
          2. Session.Load
          3. The session cache jumps in
      3. Using LINQ to NHibernate
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Using CriteriaQueries
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Using QueryOver
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Using QueryOver projections and aggregates
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Using the Hibernate Query Language
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      8. Using native SQL
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Eager loading with LINQ
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Limiting the result set doesn't work
          2. Eager loading multiple collections should be avoided
          3. Use batching as an alternative solution
        5. See also
      10. Eager loading with Criteria
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Eager loading with QueryOver
        1. Getting ready
        2. How to do it…
        3. How it works…
      12. Eager loading with HQL
        1. Getting ready
        2. How to do it…
        3. How it works…
      13. Eager loading with SQL
        1. Getting ready
        2. How to do it…
        3. How it works…
      14. Using named queries
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Named SQL queries
        4. There's more…
        5. See also
      15. Using detached queries
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      16. Using HQL for bulk data changes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Bulk inserts
        5. See also
      17. Filtering collections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      18. Using result transformers
        1. How to do it…
        2. How it works…
        3. There's more…
          1. DistinctRootEntity
          2. AliasToEntityMap
          3. PassThrough
          4. RootEntity
          5. ToList
          6. AliasToBean
          7. AliasToBeanConstructor
          8. Creating your own transformer
      19. Extra lazy collections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    13. 5. Improving Performance
      1. Reducing application startup time
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      2. Using MultiCriteria
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      3. Using MultiQuery
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Using Futures
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Eager loading child collections
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. Problem 1 – limiting the result set doesn't work
          2. Problem 2 – only the queried class' properties can be eager loaded
          3. Problem 3 – eager loading multiple collections should be avoided
        4. There's more…
          1. Criteria
          2. QueryOver
          3. See also
      6. Using stateless sessions
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      7. Using read-only entities
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. Setting the session to be read-only
          2. Setting a query to load entities as read-only
          3. Making a specific entity read-only
        4. There's more...
      8. Use the second-level cache
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Entity cache
          2. Query cache
          3. Update timestamp cache
          4. The rules
        5. See also
      9. Configuring the second-level cache with code
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more…
        5. See also
      10. Sharding databases for performance
        1. Getting ready
        2. How to do it...
        3. How it works...
    14. 6. Testing
      1. Introduction
      2. Using NHibernate Profiler
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Profiling NHibernate with Glimpse
        1. Getting ready
        2. How to do it...
        3. How does it work...
        4. See also
      4. Fast testing with the SQLite in-memory database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Preloading data with SQLite
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Using Fluent NHibernate persistence testing
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Using the Ghostbusters test
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    15. 7. Data Access Layer
      1. Introduction
      2. Transaction auto-wrapping for the data access layer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Setting up an NHibernate repository
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Using named queries in the data access layer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Using ICriteria in the data access layer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Using paged queries in the data access layer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Using LINQ specifications in the data access layer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Specification composition
        5. See also
    16. 8. Extending NHibernate
      1. Introduction
      2. Creating an encrypted string type
        1. How to do it…
        2. How it works…
        3. There's more…
        4. See also
      3. Creating a money type
        1. How to do it…
        2. How it works…
        3. There's more…
        4. See also
      4. Using well-known instance types
        1. How to do it…
        2. How it works…
        3. See also
      5. Using dependency injection with entities
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Bland passwords need salt
        5. See also
      6. Creating an audit-event listener
        1. How to do it…
        2. How it works…
        3. There's more…
        4. See also
      7. Creation and change stamping of entities
        1. How to do it…
        2. How it works…
        3. See also
      8. Generating trigger-based auditing
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      9. Implementing a soft-delete pattern
        1. How to do it…
        2. How it works…
        3. There's more…
        4. See also
      10. Setting Microsoft SQL's Context_Info
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      11. Using dynamic connection strings
        1. How to do it…
        2. How it works…
        3. See also
      12. Using custom dialect functions
        1. How to do it…
        2. How it works…
        3. See also
      13. Using custom functions in LINQ
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      14. Extending the LINQ provider
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
    17. 9. NHibernate Contribution Projects
      1. Introduction
      2. Property validation with attributes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Creating validator classes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. See also
      4. Setting up full-text searches
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      5. Auditing data with Envers
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. See also
      6. Using NHibernate Spatial
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Geography or geometry?
          2. What's this SRID?
          3. Spatial data types
    18. Index

Product information

  • Title: NHibernate 4.x Cookbook - Second Edition
  • Author(s): Gunnar Liljas, Alexander Zaytsev, Jason Dentler
  • Release date: January 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781784396428