Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning NHibernate 4
Learning NHibernate 4

Learning NHibernate 4: Explore the full potential of NHibernate to build robust data access code

Arrow left icon
Profile Icon Suhas H Chatekar
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6 (9 Ratings)
Paperback Jul 2015 402 pages 1st Edition
eBook
zł59.99 zł177.99
Paperback
zł221.99
Subscription
Free Trial
Arrow left icon
Profile Icon Suhas H Chatekar
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6 (9 Ratings)
Paperback Jul 2015 402 pages 1st Edition
eBook
zł59.99 zł177.99
Paperback
zł221.99
Subscription
Free Trial
eBook
zł59.99 zł177.99
Paperback
zł221.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Learning NHibernate 4

Chapter 2. Let's Build a Simple Application

The title of the chapter says Let's Build a Simple Application, but that is not entirely true. What is true is that we are going to start building an application. This chapter sets the foundation for that. So, in this chapter, I am going to introduce you to the application that we are going to build as we progress through the book.

However, just knowing the application is not enough in my opinion. In order to make sure that you make most out of this book and stay on track while your training shoes are still on, I am going to take you through my development setup and talk about various tools that I will be using. If you try out the sample code from this book and stick to the versions of tools mentioned in this chapter, then you can be assured that things will go smoothly. However, this does not mean that you must use the tools that I am using and the versions that I have installed in my environment. Most tools in the .NET ecosystem...

Explaining the approach used in learning NHibernate

Before we actually talk about the application, I thought I will write about the overall approach that I will take to teach you NHibernate.

In this chapter, we will define a simple problem statement. The aim is to implement an efficient data access layer for our problem using NHibernate as we progress through the chapters in this book. In this process, you will learn about important NHibernate features and apply them to an actual software problem at the same time. When we finish the book, we may not have addressed the complete data access requirements of our problem, but we will have implemented the solutions for important aspects of the problem with a certain level of detail. This level of understanding and some experience should set up to build any kind of data access solution using NHibernate.

It may seem disconnected to jump from the problem statement to the data access layer directly, so my first step is to come up with a domain model...

The problem statement

The problem that we are going to solve is related to an employee benefit management system. This is a system where the employees of an organization can log in and see the benefits that they are entitled to and utilize the benefits. A very simple example of an employee benefit is leave. Every employee of an organization is entitled to get paid or unpaid leave. Employees have the right to take days off when they need by using their leave entitlement, but there are rules around how many leaves a particular employee is entitled to and how he/she can utilize his/her leave.

There are two main actors or end users of this system. The first is evidently an employee, and the second is an administrator user. An employee can see only his/her benefits. An administrator user on the other hand can see everyone else's benefits and can add/delete benefits. An administrator can be an employee himself/herself.

This is a brief of our problem statement. As you can see, this can be a...

The development setup

In order to learn NHibernate most effectively, it is important that you follow the concepts and code samples from this book and try them yourself. While the code from this book should work on any general development environment, it would be prudent to mention which tools I am going to use in particular. If any of the code samples from this book does not work for you, you can always confirm first if it is due to tooling incompatibility. There are a couple of optional tools that I will be referring to in some chapters. These tools help working with NHibernate but are not necessary as there are alternatives available. These tools are not listed here. I will introduce them when they are referred to for the first time in this book.

So, here is the list of tools that we will use:

Visual Studio 2013

I will primarily be using Visual Studio as my code editor. I will be using VS 2013, which is the latest version of Visual Studio at the time of writing of this book. Lately, Visual...

The domain model

Now that our development environment is in place, let's put together a domain model for our problem. A domain model describes the important concepts in the problem domain in a way that can be implemented in software. There are different techniques of preparing a domain model and there are different ways of representing a domain model. The most commonly used representation of a domain model is a class diagram. We will prepare a class diagram and define our classes and their attributes and relations on the basis of the knowledge that we have so far about our problem. I am going to take a slightly agile approach at this. What I mean by this is that I am not going to overly analyze anything here. We will prepare a very simple domain model that covers most of my requirements to a certain degree of comfort. As I go on implementing the features, I will uncover a lot of hidden domain knowledge, which will help me shape my domain model better.

Tip

Classes in a class diagram will...

Adding some code

It is time to implement all the classes from the preceding class diagram and look at our overall source and project structure.

Let's open Visual Studio and create a blank solution named EmployeeBeneftis.sln. In this solution, let's add a project of the Class Library type named Domain.

Tip

To add a blank solution, you need to go to File | New | Project. On the New Project dialog that opens, inside the pane on the left side, locate a tree node named Other Project Types. Inside this node, you will find an item named Visual Studio Solutions. Click on this item, and then, you can choose Blank Solution in the middle pane.

We will now add all the classes to the Domain project. In order to minimize the compilation errors, we will start with the class that does not refer to other classes. Right-click on the Domain project in Visual Studio and add a class named Address. Use the following code for the body of the class:

namespace Domain
{
  public class Address
  {
    public...

Explaining the approach used in learning NHibernate


Before we actually talk about the application, I thought I will write about the overall approach that I will take to teach you NHibernate.

In this chapter, we will define a simple problem statement. The aim is to implement an efficient data access layer for our problem using NHibernate as we progress through the chapters in this book. In this process, you will learn about important NHibernate features and apply them to an actual software problem at the same time. When we finish the book, we may not have addressed the complete data access requirements of our problem, but we will have implemented the solutions for important aspects of the problem with a certain level of detail. This level of understanding and some experience should set up to build any kind of data access solution using NHibernate.

It may seem disconnected to jump from the problem statement to the data access layer directly, so my first step is to come up with a domain model...

Left arrow icon Right arrow icon
Download code icon Download Code

Description

This book targets .NET developers who have never used an ORM before, developers who have used an ORM before but are new to NHibernate, or have used NHibernate sparingly and want to learn more about NHibernate.

Who is this book for?

This book targets .NET developers who have never used an ORM before, developers who have used an ORM before but are new to NHibernate, or have used NHibernate sparingly and want to learn more about NHibernate.

What you will learn

  • Map domain entities to a database schema using the different mapping mechanisms available
  • Configure NHibernate through XML configuration
  • Save, update, and delete entities in the database and query data from a database using different querying methods
  • Optimize database operations for speed and memory consumption
  • Use NHibernate in reallife software projects
  • Get to know about data access patterns such as repository, specification, and query object
  • Use NHibernate with legacy databases

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 31, 2015
Length: 402 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393564
Category :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 31, 2015
Length: 402 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393564
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just zł20 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 641.97
NHibernate 4.x Cookbook
zł221.99
Learning NHibernate 4
zł221.99
Entity Framework Tutorial (Update)
zł197.99
Total 641.97 Stars icon
Banner background image

Table of Contents

12 Chapters
1. Introduction to NHibernate Chevron down icon Chevron up icon
2. Let's Build a Simple Application Chevron down icon Chevron up icon
3. Let's Tell NHibernate About Our Database Chevron down icon Chevron up icon
4. NHibernate Warm-up Chevron down icon Chevron up icon
5. Let's Store Some Data into the Database Chevron down icon Chevron up icon
6. Let's Retrieve Some Data from the Database Chevron down icon Chevron up icon
7. Optimizing the Data Access Layer Chevron down icon Chevron up icon
8. Using NHibernate in a Real-world Application Chevron down icon Chevron up icon
9. Advanced Data Access Patterns Chevron down icon Chevron up icon
10. Working with Legacy Database Chevron down icon Chevron up icon
11. A Whirlwind Tour of Other NHibernate Features Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6
(9 Ratings)
5 star 33.3%
4 star 33.3%
3 star 0%
2 star 22.2%
1 star 11.1%
Filter icon Filter
Top Reviews

Filter reviews by




M. Kvist Oct 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I had been using NHibernate for over a year before reading this book. Yet in every chapter I found something very useful that I didn't know.You are taught configuration by XML which is awkward, but it's an effective way of learning all the configuration options. You are however encouraged to use Fluent NHibernate for the mappings, which is a modern and popular method for mapping by code.Older constructs such as HQL, Criteria API and to some extent, QueryOver are just lightly touched upon, and the book instead promotes the modern LINQ method.One of the most rewaring chapters for me was how to use NHibernate in a real life application, which the author does really cleanly as he introduces the onion architecture and builds on IoC. You are also shown how to concretely build a repository and extend it using Specification pattern and Query object pattern.I came out of this wondering how to combine eager fetching with the Query object pattern, which seems like a reasonable extension in real life projects. I would have liked to read more about that in the book.I thoroughly enjoyed this book and would recommend it any time.
Amazon Verified review Amazon
Leonhard Sep 05, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Fand das Buch sehr gut strukturiert und gelungen aufgebaut. Ohne jedgliche Vorkenntnisse zu den Eigenheiten von NHibernate gibt es einen guten, praxistauglichen Einblick. Neben der Konfiguration, den unterschiedlichen Möglichkeiten der Abfragen wird u.a. auch behandelt wie spezielle Wünsche wie Lazy Loading oder Updates mit/ohne Locks konfiguriert werden können.Zusammengefasst:Für mich als Einstieg in eine neue Welt trotz Hintergrundwissen über andere O/R Mapper vieles neues gelernt. Klare Kaufempfehlung - auch für erfahrene Entwickler geeignet
Amazon Verified review Amazon
Amazon Customer Dec 03, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I must have in your bookshelf
Amazon Verified review Amazon
Samir Aguiar Jan 06, 2016
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I have just finished this book and I'm glad I bought it. This is the NHibernate book I was looking for.I decided to learn more about NH because I'm using it at my current job and I was tired of asking for help when something complex was needed. But although I was not a beginner, I was not an expert either so the official documentation never helped me much. This book though is an in-depth tutorial, a guide, that you can easily follow along from the beginning to the very end while understanding every piece of it.Chatekar's writing is clear and straightforward. It's detailed enough to give you an understanding of the topic and summarized enough so you don't get tired. The first half of the book is more about teaching you the basics: there's a simple problem and the author slowly builds a solution that uses NHibernate. The examples are easy to follow and the chapters are divided so that NHibernate configuration, entities mapping and database querying/writing are all covered separately. The second half of the book advances into more complex topics such as project organization (software architecture), NHibernate usage on web projects, design patterns, dependency injection, performance (caching, lazy loading, future queries) and so on. Also, along the road the author gives a handful of valuable advice about best practices and common pitfalls/caveats so that in the end you are not just a person with a new complex skill and no idea of where to begin to apply it.The only two things that prevented me from giving it 5 stars were the lack of a code revision and more explanation of entities mappings.In the first half, the author goes into implementing a solution to the problem proposed. If you try to follow along, you won't be able to either compile or run the code as there are a lot of tweaks required. I've only managed to run it after changing NHibernate's configuration and checking the book source (which also didn't compile). The book webpage could at least contain some errata or guidance for that (I did submit some to Packt but it never got published).Secondly, I still think that there could have been more discussion about mappings and relationships, which are the heart of NHibernate and the source of many Stack Overflow questions. Again, the documentation is not very helpful here. For instance, the author made all relationships bidirectional and never got much into unidirectional relationships. Why one-to-one relationships cannot be unidirectional? What about mapping an FK Id into its own property to avoid loading an associated entity and mimic ISession.Load() behavior? What about nested relationships with relationships between children? Those are a few questions that remained after reading (and which of course I'll be exploring myself) but could have been clarified in the first chapters.Anyway, despite the above critics the book is definitely worth it and will help anyone to overcome the initial learning curve of NHibernate. It's not meant to cover all the features but rather just kick start the reader -- which it has successfully accomplished.
Amazon Verified review Amazon
julian Nov 12, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Very good book! 100% recomended, clear and complete, the content is very well explained. I dont rate 5 stars because i think that there are 2 subjects missing:1) Many to many relationships with additional parameters2) Logging
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.