Book Image

Learning NHibernate 4

Book Image

Learning NHibernate 4

Overview of this book

Table of Contents (18 chapters)
Learning NHibernate 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mapping by code


By now, you should have got a very good idea of what purpose the mappings serve. This knowledge will make understanding this section and the next very easy. At the beginning of the chapter, we discussed what is mapping by code and why it is built. So without much ado, let me get straight into the "mapping by code" feature of NH.

In mapping by code, mappings for each class can be declared in a separate and own class. It is also possible to declare the mappings in a single place by using the ModelMapper class. We would prefer to declare mappings for each entity in its own class. This class must inherit from another class provided by NHibernate called NHibernate.Mapping.ByCode.ClassMapping<T>. Whatever mappings you want to declare should be declared in the constructor of the class. Following code snippet shows how this looks for the Employee class:

using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using Domain;

namespace Persistence.Mappings.ByCode...