Nhibernate
PIERRE HENRI KUATÉ TOBIN HARRIS CHRISTIAN BAUER GAVIN KING
MANNING
Greenwich (74° w. long.)
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. Sound View Court 3Bfax: (609) 877-8256 Greenwich, CT 06830 email: orders@manning.com ©2009 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers andsellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best effortsto that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15% recycled and processed without the use of elemental chlorine.
Manning Publications Co. Sound View Court 3B Greenwich, CT 06830
Development Editor: Copyeditor: Typesetter: Cover designer:
Cynthia Kane Tiffany Taylor Gordan Salinovic Leslie HaimesISBN 978-1-932394-92-4 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 14 13 12 11 10 09
contents
foreword xvii preface xxi acknowledgments xxiii about this book xxv
PART 1 DISCOVERING ORM WITH NHIBERNATE ................ 1
1
Object/relational persistence in .NET 3
1.1 What is persistence? 5
■ ■ ■
Relational databases 5 Understanding SQL 6 Using SQL in.NET applications 6 Persistence in object-oriented applications 6 Persistence and the layered architecture 7
■
1.2
Approaches to persistence in .NET
■
9
■
Choice of persistence layer 9 Implementing the entities 11 Displaying entities in the user interface 13 Implementing CRUD operations 14
1.3
Why do we need NHibernate?
■
15
The paradigm mismatch 15 Units of work andconversations 16 Complex queries and the ADO.NET Entity Framework 18
ix
x
CONTENTS
1.4 1.5
Object/relational mapping
What is ORM? 21
■
21
Why ORM? 21
Summary
23
2
Hello NHibernate! 24
2.1 “Hello World” with NHibernate
■ ■
25
Installing NHibernate 25 Create a new Visual Studio project 25 Creating the Employee class 26 Setting up the database 27 Creating anEmployee and saving to the database 27 Loading an Employee from the database 29 Creating a mapping file 29 Configuring your application 31 Updating an Employee 32 Running the program 33
■ ■ ■ ■
2.2
Understanding the architecture
■
33
■
The core interfaces 35 Callback interfaces 36 Extension interfaces 37
Types 37
2.3
Basic configuration
38
■
Creating a SessionFactory38 database access 41
Configuring the ADO.NET
2.4 2.5
Advanced configuration settings Summary 48
44
■
Using the application configuration file 44
Logging 47
PART 2 NHIBERNATE DEEP DIVE ..................................... 49
3
Writing and mapping classes 51
3.1 The CaveatEmptor application
Analyzing the business domain 52 domain model 53
■
52
The CaveatEmptor3.2
Implementing the domain model
■ ■
55
Addressing leakage of concerns 55 Transparent and automated persistence 55 Writing POCOs 56 Implementing POCO associations 58 Adding logic to properties 61
■
3.3
Defining the mapping metadata
Mapping using XML 63
■
63
Attribute-oriented programming 65
CONTENTS
xi
3.4
Basic property and class mappings
■ ■
66...
Regístrate para leer el documento completo.