You are previewing NHibernate 2.
O'Reilly logo
NHibernate 2

Book Description

Rapidly retrieve data from your database into .NET objects

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

  • Gain database independence, not tied to any particular technology

  • Avoid spending countless hours developing data access layers

  • Eliminate writing stored procedures

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

  • In Detail

    NHibernate is an open source object-relational mapper, or simply put, a way to retrieve data from your database into standard .NET objects. Quite often we spend hours designing the database, only to go back and re-design a mechanism to access that data and then optimize that mechanism. This book will save you time on your project, providing all the information along with concrete examples about the use and optimization of NHibernate.

    This book is an approachable, detailed introduction to the NHibernate object-relational mapper and how to integrate it with your .NET projects. If you're tired of writing stored procedures or maintaining inline SQL, this is the book for you.

    Connecting to a database to retrieve data is a major part of nearly every project, from websites to desktop applications to distributed applications. Using the techniques presented in this book, you can access data in your own database with little or no code.

    This book covers the use of NHibernate from a first glance at retrieving data and developing access layers to more advanced topics such as optimization and Security and Membership providers. It will show you how to connect to multiple databases and speed up your web applications using strong caching tools. We also discuss the use of third-party tools for code generation and other tricks to make your development smoother, quicker, and more effective.

    This easy-to-follow guide 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 2
      1. NHibernate 2
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. 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. Errata
          2. Piracy
          3. Questions
      6. 1. First Look
        1. What is NHibernate?
        2. Why would I use it?
        3. Where do I get it?
        4. Can I get help using NHibernate?
          1. Have a go hero—looking at some sample files
        5. Database table
        6. The XML mapping file (hbm.xml)
        7. Plain Old CLR Object ( POCO )
        8. Data access
        9. Look how easy it is to use!
        10. Summary
      7. 2. Database Layout and Design
        1. Before you get started
        2. Laying the foundation—table layouts
        3. Time for action—creating an ordering system database
          1. What just happened?
        4. Table layouts
        5. NHibernate assigned IDs
        6. Relationships
          1. Pop quiz—relationships
        7. Normal Form
          1. Have a go hero—looking back
        8. Putting it all together
        9. Time for action—adding some tables to our Ordering system database
          1. What just happened?
          2. Have a go hero—create some more tables
        10. Summary
      8. 3. A Touch of Class
        1. Start up our applications
        2. Creating objects
        3. Time for action—creating our first class
          1. What just happened?
          2. Have a go hero—ramping up
        4. Public properties and private variables
        5. Time for action—adding a few properties
          1. What just happened?
        6. Converting SQL database types to .NET types
        7. Properties for Foreign Keys
          1. Have a go hero—adding more properties (and classes!)
          2. Pop quiz—mapping
        8. Summary
      9. 4. Data Cartography
        1. What is mapping?
        2. Types of mapping
        3. XML mapping
          1. Getting started
          2. Classes
          3. Properties
          4. ID columns
        4. Mapping our types
        5. Time for action—mapping basic types
          1. What just happened?
          2. Pop quiz—class mapping
        6. Relationships
          1. One-to-many relationships
          2. Many-to-many relationships
          3. Getting started
        7. Time for action—mapping relationships
          1. What just happened?
          2. Have a go hero—flushing out the rest of our map
        8. Fluent mapping
          1. Pop quiz—fluent mapping
        9. Summary
      10. 5. The Session Procession
        1. What is an NHibernate session?
        2. Time for action—getting ready
          1. What just happened?
        3. What is a session factory?
        4. Creating your first session
        5. Why do we call .Commit()?
        6. NHibernate session versus database session?
        7. Time for action—creating a session and doing some CRUD
          1. Pop quiz—creating and updating records
          2. Have a go hero—creating a full order
        8. Sessions in ASP.NET
        9. Summary
      11. 6. I'm a Logger
        1. Why do we need to log?
        2. Why log4net?
        3. Getting started
          1. Have a go hero—adding some logging
        4. Configuration
        5. Time for action—adding some logging
          1. What just happened?
        6. NHibernate log messages
        7. Appenders
          1. Pop quiz—creating and updating records
        8. Creating a logger
        9. Time for action—adding some custom logging
          1. What just happened?
          2. Have a go hero—adding some logging
        10. Summary
      12. 7. Configuration
        1. Looking back
        2. The basics of configuration
          1. Pop quiz—basic configuration
        3. Taking a look at the SQL
          1. Have a go hero—using the connection string name
        4. Abstracting the configuration
        5. Time for action—moving our configuration
          1. What just happened?
        6. XML configuration
          1. Have a go hero—using an XML configuration file
        7. Summary
      13. 8. Writing Queries
        1. Using Data Access Objects
        2. The basic Data Access Object
        3. Time for action—creating our basic Data Access Object
          1. What just happened?
        4. Data Access Object methods
        5. Time for action—adding some CRUD methods
          1. What just happened?
        6. Coding some GetX() methods
        7. The FieldNames structure
        8. Time for action—expanding our capabilities
          1. What just happened?
        9. The ICriteria object
          1. Pop quiz—FieldNames and ICriteria
        10. Creating a GetAll() method
        11. Paging and Sorting GetAll() methods
          1. Have a go hero—creating some paging and sorting methods
        12. Filtering ICriteria
        13. Time for action—replacing our inline code
          1. What just happened?
        14. Summary
      14. 9. Binding Data
        1. Why should we use data binding?
        2. Time for action—adding an ASP.NET project
          1. What just happened?
        3. Basic data binding techniques
        4. Common data binding methods
        5. A simple templated control
          1. Pop quiz—basic data binding
        6. Creating a control instance
        7. The <asp:ListView> control
        8. The <asp:ObjectDataSource> control
        9. Time for action—adding our first data bound control
          1. What just happened?
        10. Direct data binding
          1. Have a go hero—give direct data binding a try
        11. One last control—the <asp:FormView>
          1. Have a go hero—creating an <asp:FormView>
        12. Summary
      15. 10. .NET Security
        1. Built-in controls
        2. Time for action—adding a login to our page
          1. What just happened?
        3. Membership providers
        4. Location security
          1. Pop quiz—doing the thing
        5. Configuring our provider
        6. Time for action—create a membership provider
          1. What just happened?
        7. User roles
        8. Role providers
        9. Provider configuration
          1. Have a go hero—using some roles
        10. Summary
      16. 11. It's a Generation Thing
        1. Judging requirements
        2. CodeSmith
        3. nhib-gen
        4. AjGenesis
        5. Visual NHibernate
        6. MyGeneration
        7. Time for action—using MyGeneration
          1. What just happened?
        8. NGen NHibernate Code Generator
        9. NHModeller
        10. Microsoft T4 templates
        11. T4 hbm2net
        12. Summary
      17. 12. Odds and Ends
        1. Unit of Work and Burrow
          1. Pop quiz—doing the thing
        2. How does this relate to NHibernate?
        3. Blog.Net blogging components
        4. maxRequestLength
        5. Converting CSS templates
        6. Time for action—converting a CSS template
          1. What just happened?
          2. Have a go hero—.NETing the master page
        7. XML documentation & GhostDoc
        8. Summary
      18. A. Pop quiz—Answers
        1. Chapter 2 — Database Layout and Design
          1. Relationships
        2. Chapter 3 — A Touch of Class
          1. Mapping
        3. Chapter 4 — Data Cartography
          1. Class mapping
        4. Chapter 5 — The Session Procession
          1. Creating and updating records
        5. Chapter 6 — I'm Logger
          1. Logging
        6. Chapter 7 — Configuration
          1. Basic configuration
        7. Chapter 8 — Writing Queries
          1. Fieldnames and ICriteria
        8. Chapter 9 — Binding Data
          1. Basic data binding
        9. Chapter 10 — .NET Security
          1. Access configuration
        10. Chapter 12 — Odds and Ends
          1. Burrowing in