NHibernate.BusinessObjects on Get NHibernate.BusinessObjects at SourceForge.net. Fast, secure and Free Open Source software downloads

NHibernate community site

NHibernate.BusinessObjects - Tutorial Part 2

Introduction

In the first part of the NHibernate.BusinessObjects tutorial, I have explained how to craete a simple data model, how to generate the code, and how to build your first application using NHibernate.BusinessObjects. The second part of the tutorial will focus on parent/child and reference relationships.

There are different kind of relationships used in the object oriented world. In this tutorial, I will explain the differences and show you how to use them with NHibernate.BusinessObjects.

You can either follow the steps described in the tutorial to create the project or you can download the complete sources for this tutorial.

Parent/Child (Composition)

In the Unified Modeling Language (UML), a parent/child relationship is defined as a composition. Composited (composed) objects are often referred to as having a "owns a" relationship. Composition has a strong life cycle dependency between instances of the container class and instances of the contained class. If the container is destroyed, every instance that it contains is destroyed as well.

The multiplicity for the child objects can be 0..1, 1, or 0..*. The multiplicity of the parent object is always 1.

The NHibernate.BusinessObjects generator creates a property with a getter to access the child object if the multiplicity is 0..1 or 1. For multiplicity of 0..*, it creates a property with a getter that returns a typed list of child objects.

References

In the UML, a reference relationship is defined as an aggregation. Aggregation is often referred to as having a "has a" relationship. Aggregation differs from composition in that it does not imply ownership. It does not have strong life cycle dependency between instances of the container class and instances of the contained class. If the container is destroyed, no instance that it contains will be destroyed.

The multiplicity for both, the referencing and the referenced objects can be 0..1, 1, or 0..*.

The NHibernate.BusinessObjects generator creates a property with a getter to access the referenced object if the multiplicity is 0..1 or 1. For multiplicity of 0..*, it creates a property with a getter that returns a typed list of referenced objects.

Getting started

This part of the tutorial is based on the first part. Copy the sources from part 1 of the tutorial into a new folder, e.g. Tutorial Part 2 and open the solution.

Extending the data model

In the DataModel folder, create a new XML file and name it "Tutorial.Address.bo.xml".
Add the following content to the file.

Picture 1

There is one main difference to the Person description file that we have created in the first part of the tutorial: The IsRoot attribute has been set to "false". By setting this attribute to "false", we tell the code generator not to generate the CreateAddress and DeleteAddress methods. Thus, you cannot create or delete a new business object directly. Instead NHibernate.BusinessObjects creates or deletes adresses when needed. We will see this later.

Open the Person description file "Tutorial.Person.bo.xml" and add the following code after the Properties element.

Picture 2

Creating a child object is very easy and the only thing we need to review is the Required attribute. If this attribute is set to "true", the Person object must have a child of type Address. In the description of the Address type, we have set the IsRoot property to "false" so the code generator won't generate the CreateAddress method. So, how can you create an Address object? In fact you don't need to craete the object. NHibernate.BusinessObjects automatically creates an Address object when you create a new Person and it will be deleted together with the Person.

If the Address object shall be optional (the Required attribute is set to "false"), the code generator adds the CreateAddress and DeleteAddress to the Person business object.

Generating the business objects

Now we can regenerate the business objects. Open a console window and change the directory to the bin folder.
Call the NHibernate.BusinessObjects.Generator.exe. As parameter you need to assign the path to the description files and to the target folder.

NHibernate.BusinessObjects.Generator.exe /d:..\Source\DataModel /t:..\Source\Generated

The generated Visual Studio project contains now some additional interface, implementation, and NHibernate mapping files for the Address object.

Updating the database

The new Address table will be added to the database the next time you call Create on the business document.

Accessing the data

In the Console project open the file "Program.cs" and add the following method:

Picture 3

In the Main method add a call to the new method.

Picture 4

References, child lists and reference list can be used in the same way. I will extend this part of the tutorial soon.

Deleting business objects

When you delete a business object, all child objects of the deleted object will be deleted as well. If a child object itself contains children, these children will be deleted too. In fact, the whole tree will be deleted. A deleted object may be referenced by one or more business objects. You don't have to set these references to null.NHibernate.BusinessObjects will find all references and set them to null.

Next...

In the next part of this tutorial, I will show you how to use inheritance with business objects