You are previewing NHibernate 3 Beginner's Guide.
O'Reilly logo
NHibernate 3 Beginner's Guide

Book Description

Rapidly retrieve data from your database into .NET objects

  • Incorporate robust, efficient data access into your .Net projects

  • Reduce hours of application development time and get better application architecture and performance

  • Create your domain model first and then derive the database structure automatically from the model

  • Test, profile, and monitor data access to tune the performance and make your applications fly

  • Clear, precise step-by-step directions to get you up and running quickly

  • In Detail

    Ideally, we would want to persist the objects our application uses and produces AS IS, without having to first transform them in complex ways. NHibernate is a framework that provides us with an object-oriented access to a relational database without having to write SQL and with little or no database-specific data access code. Definitely, if you are a .NET developer, knowing more about NHibernate will simplify and reduce your efforts in developing .Net applications.NHibernate 3 Beginner's Guide introduces Nhibernate with step-by-step examples, and is the easiest way to learn about bridging the gap between object-oriented .NET applications and the relational database that stores the application's data. It is a beginner's guide to NHibernate that starts from scratch. Successive chapters build upon earlier concepts, while the sample code presents various ways to accomplish typical data access tasks.

    NHibernate 3 Beginner's Guide examines all of the topics required to get a functional data access layer implemented while writing the least amount of code possible, presenting options along the way to handle particular edge cases or situations as they arise. The book begins with an introduction to NHIbernate 3 and then moves on to creating the development environment. Then it teaches you how to create a model, define a database schema, and map the model to the database then covers sessions and transactions. This is succeeded by testing, profiling, and configuration, validation of data and writing queries. Finally, we wrap up with notes on the common pitfalls that you should avoid.

    Once you complete reading the book, you will have gained the skills and knowledge to incorporate NHibernate into your own applications.

    An easy-to-follow guide that will show you how to connect the NHibernate object-relational mapper to your projects to create a rich, efficient, object-oriented data access layer with little or no additional work

    Table of Contents

    1. NHibernate 3
      1. NHibernate 3
      2. Credits
      3. About the Authors
      4. About the Reviewers
      5. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      6. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Time for action heading
          1. What just happened?
          2. Pop quiz heading
          3. Have a go hero heading
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      7. 1. First Look
        1. What is NHibernate
        2. What is new in NHibernate 3.0
        3. Why would I use it
        4. Where do I get it
        5. Is there documentation available
        6. Can I get help using NHibernate
        7. Is there commercial support available
        8. Who uses it
        9. Summary
      8. 2. A First Complete Sample
        1. Prepare our development environment
        2. Defining a model
        3. Time for action Creating the product inventory model
          1. What just happened?
        4. Mapping our model
        5. Time for action Creating the mapping classes
          1. What just happened?
        6. Creating the database schema
        7. Time for action Creating the database
          1. What just happened?
        8. Creating a session factory
        9. Time for action Creating a session factory
          1. What just happened?
        10. Opening a session
        11. Time for action Opening a session to the database
          1. What just happened?
        12. Persisting objects to the database
        13. Time for action Adding a new category to the database
          1. What just happened?
        14. Reading from the database
        15. Time for action Loading the list of all categories from the database
          1. What just happened?
          2. Have a go hero
        16. Doing the same without NHibernate using ADO.NET only
          1. Pop quiz
        17. Summary
      9. 3. Creating a Model
        1. What is a model
        2. Model first versus data first
        3. Elements of a model
          1. Entity
          2. Value object
        4. Time for action Creating a Name value object
          1. What just happened?
        5. Creating an entity
        6. Time for action Creating a base entity
          1. What just happened?
        7. Time for action Creating a Customer entity
          1. What just happened?
        8. Defining relations between entities
          1. Owns or contains
          2. One-to-many
          3. One-to-one
          4. Many-to-many
        9. The order entry model
        10. Time for action Implementing an order entry model
          1. What just happened?
          2. Pop quiz Identifying entities
          3. Have a go hero Extending the model
        11. Summary
      10. 4. Defining the Database Schema
        1. What is a database schema?
          1. Who owns the database?
        2. Time for action Creating the OrderingSystem database
          1. What just happened?
        3. Laying the foundation table layouts
        4. Time for action Creating the Categories table
          1. What just happened?
        5. Time for action Defining a script to create the Products table
          1. What just happened?
        6. Table columns
          1. Data types of table columns
        7. Relations, constraints, and indices
          1. Relations
          2. Constraints
        8. Time for action Adding a constraint to the Product table
          1. What just happened?
        9. Time for action Creating a script to add a check constraint
          1. What just happened?
          2. Indices
        10. Time for action Adding an index using the designer
          1. What just happened?
        11. Time for action Creating a script to add an index
          1. What just happened?
        12. Normal form
        13. Putting it all together
        14. Time for action Creating a schema for the order entry system
          1. What just happened?
          2. Have a go hero Adding an additional table to the schema
          3. Have a go hero Authoring a DDL script to generate the schema
        15. Do not use database-generated IDs
        16. Views
        17. What about stored procedures and triggers?
          1. Pop quiz
        18. Summary
      11. 5. Mapping the Model to the Database
        1. What is mapping?
        2. Types of mapping
          1. XML-based mapping
          2. Attribute-based mapping
          3. Fluent mapping
          4. Mapping by convention
        3. A word about lazy loading
          1. Loading data on demand
          2. Proxies
          3. Virtual properties and methods
        4. Creating database schema creation scripts
        5. Fluent mapping
          1. Expression trees please explain
          2. Getting started
          3. Classes
          4. Entity level settings
          5. ID columns
          6. Properties
          7. References
          8. Collections
          9. Mapping many-to-many relations
          10. Mapping value objects
        6. Time for action Mapping our domain
          1. What just happened?
        7. Use mapping conventions
          1. ID conventions
          2. Property conventions
          3. Foreign key conventions
          4. Have a go hero Implement your own convention
        8. No mapping; is that possible?
          1. Auto-mapping with Fluent NHibernate
        9. Time for action Using auto-mapping
          1. What just happened?
          2. Using ConfORM
        10. Time for action Using ConfORM to map our domain
          1. What just happened?
        11. XML mapping
          1. Getting started
          2. Classes
          3. Properties
          4. ID columns
          5. One-to-many relations
          6. Many-to-many relations
          7. Mapping value objects
        12. Time for action Mapping a simple domain using XML
          1. What just happened?
          2. Pop quiz XML mapping
        13. Summary
      12. 6. Sessions and Transactions
        1. What are sessions and transactions
          1. Session
          2. Transaction
        2. The session factory
        3. Creating your first session
          1. Why do we call Commit?
          2. Adding new data to the database
          3. Reading data from the database
          4. Get versus Load
          5. Updating existing data
          6. Deleting data
        4. First level cache or identity map
          1. Clearing the cache
          2. Refreshing entities in the cache
        5. No database operation without a transaction
          1. Should I use transactions when querying data?
        6. NHibernate session versus database session
        7. Time for action Creating a session and doing some CRUD
          1. What just happened?
          2. Pop quiz Creating, updating, and deleting data
        8. Session management
          1. Web-based applications
        9. Time for action Implementing session management for a web application
          1. What just happened?
          2. WinForm or WPF applications
          3. Windows services
        10. Unit of Work
        11. Handling exception
        12. Second level cache
          1. Cache regions
          2. Second level cache implementations
        13. Time for action Using a second level cache
          1. What just happened?
        14. Summary
      13. 7. Testing, Profiling, Monitoring, and Logging
        1. Why do we need tests?
        2. What should we test?
        3. What about the database?
          1. Download SQLite
          2. Pop quiz
        4. Preparing our environment for testing
          1. Testing the mapping
          2. Testing the mapping with Fluent NHibernate
        5. Time for action Creating the base for testing
          1. What just happened?
          2. Have a go hero
        6. Time for action Using SQLite in our tests
          1. What just happened?
          2. Testing queries
        7. Logging
          1. Why do we need to log?
          2. Logging with Log4Net
        8. Time for action Adding logging to our application
          1. What just happened?
          2. Setting up logging for NHibernate
        9. Time for action Enable logging in NHibernate
          1. What just happened?
        10. Monitoring and profiling
          1. Analyzing log files
          2. Using SQL Server Profiler
          3. Monitoring and profiling with NHibernate Profiler
        11. Time for action Adding NHibernate Profiler support
          1. What just happened?
        12. Summary
      14. 8. Configuration
        1. Why do we need a configuration?
        2. Elements of the configuration
          1. Which database do we want to use?
          2. What byte code provider and proxy factory?
          3. Where are our mappings?
          4. Do we use second level caching?
          5. Do we want to extend NHibernate?
        3. XML configuration
        4. Time for action Configuring NHibernate using XML
          1. What just happened?
        5. Configuring NHibernate in code
        6. Time for action Configuring NHibernate in code
          1. What just happened?
        7. Fluent configuration
          1. Configuring NHibernate with Loquacious
        8. Time for action Using Loquacious to configure NHibernate
          1. What just happened?
          2. Pop quiz
          3. Configuring NHibernate with Fluent NHibernate
          4. Have a go hero
        9. Convention over configuration
        10. Summary
      15. 9. Writing Queries
        1. How can we get to our data?
        2. The LINQ to NHibernate provider
          1. Defining the root of our query
          2. Limiting the number of records returned
          3. Filtering a set of records
          4. Mapping a set of records
          5. Sorting the resulting set
          6. Grouping records
          7. Forcing a LINQ query to execute immediately
          8. Changing from querying the database to querying in-memory objects
        3. Creating a report using LINQ to NHibernate
        4. Time for action Preparing the system
          1. What just happened?
        5. Time for action Creating the reports
          1. What just happened?
        6. Criteria queries
          1. Untyped criteria queries
          2. Strongly-typed criteria queries
        7. Time for action Using QueryOver to retrieve data
          1. What just happened?
          2. Have a go hero
        8. Hibernate Query Language
        9. Lazy loading properties
          1. Have a go hero
        10. Executing multiple queries in a batch
        11. Eager loading versus lazy loading
        12. Bulk data changes
          1. Pop quiz Some remarkable points
        13. Summary
      16. 10. Validating the Data to Persist
        1. What is validation and why is it so important?
        2. Who owns the database?
        3. Why, what, and where do we validate?
          1. Why validate?
          2. What data?
          3. Where to validate?
        4. Validating single properties
          1. Configuring the validator
          2. Defining validation rules
          3. Fluently configure validation rules
          4. Enforcing validation
        5. Time for action Using property validation
          1. What just happened?
          2. Have a go hero
        6. Validating complex business rules
          1. Enforcing always valid entities
          2. Using validation classes
        7. Time for action Validating user input
          1. What just happened?
          2. Have a go hero
        8. What to do if we don't own the database?
          1. Pop quiz
        9. Summary
      17. 11. Common Pitfalls—Things to Avoid
        1. Requesting the obvious
        2. Wrong mapping for read-only access
        3. Blindly relying on NHibernate
        4. Using implicit transactions
        5. Using database-generated IDs
        6. Using LINQ to NHibernate the wrong way
        7. The trouble with lazy loading
          1. The select (n+1) problem
          2. Accessing lazy loaded parts after the session is closed
          3. Did I just load the whole database?
        8. Using one model for read and write operations
          1. CQRS
        9. Phantom updates
        10. Time for action Causing a phantom update
          1. What just happened?
        11. Using NHibernate to persist any type of data
        12. Summary
      18. 12. Pop Quiz Answers
        1. Chapter 2
          1. A First Complete Sample
        2. Chapter 3
            1. Creating a Model
        3. Chapter 4
          1. Defining the Database Schema
        4. Chapter 5
          1. Mapping the Model to the Database
        5. Chapter 6
          1. Sessions and Transactions
        6. Chapter 7
          1. Testing, Profiling, Monitoring, and Logging
        7. Chapter 8
          1. Configuration
        8. Chapter 9
          1. Writing Queries
        9. Chapter 10
          1. Validating the Data to Persist