Chapter 3. Let's Tell NHibernate About Our Database

You must be excited to start your NHibernate learning tour. Well, we are just about to do that. In this chapter, we are going to talk about declaring mappings for your domain model. You declare mappings to tell NHibernate how classes in your domain model match to database tables. If you have built your database to suit to the domain model then mappings would look very simple. But if database and domain models are not compatible with each other, then mappings start getting complex. Having a thorough knowledge of mappings would help in such situations. There are multiple ways of declaring mappings, from XML-based ones to code-based ones. We will cover three most widely used methods, namely, XML mappings, mapping by code, and fluent mappings.

Mapping is the most important concept in NHibernate. The way NHibernate interacts with database largely depends on how mappings are written. In this chapter, we will cover the basics of mapping. As we go deeper into the other NHibernate features in the upcoming chapters, we would revisit mappings to see how slightly tweaking a mapping can bring about big changes in the way NHibernate interacts with your database. This chapter should give you a basic understanding of how to declare mappings and make you ready for taking the plunge into the more advanced NHibernate topics.

Important NHibernate terminology

From this chapter onwards, we will be using some terms. These terms have special meanings when used in context of NHibernate or data access layers of applications. It would help to know what these terms mean and how we are going to use them in this chapter and in rest of the book. It is possible that some of these terms are already known to you.

POCO, Plain Old CLR Object. An alternate term for a class in C# which:

  • Does not inherit from classes defined in any framework
  • Does not implement interfaces defined in any framework
  • Does not use any attributes defined in any framework

We will just use the word class most of the time in this book but if I may have to use POCO in a few places, you will know what I mean.

  • Entity: A class having an identifier that identifies an instance at least in the entity graph maintained by NHibernate. This may be slightly confusing, but as we progress through the chapter you will know more about NHibernate to understand this clearly. Till that time, whenever I say entity, assume that I am referring to a class/POCO.
  • Persistent Class: Any class which NHibernate is able to persist in one or more database tables using the mappings defined.
  • Attribute/property: When used in reference to POCO/class, this means getter/setter properties or fields on the class. When used in reference to XML, this would mean usual XML attributes. I may refer to class attributes as properties in some places to avoid confusion.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.200.14