Skip to content

Getting Started

tobyrogers edited this page Jul 25, 2013 · 6 revisions

This example takes you through creating a simple application using Petrify and MongoDB

Before you start

This example assumes you have a suitable starting project (this can just be an empty console project) and an existing instance of MongoDB.

For instructions on installing MongoDB see:...

Install the package from NuGet

To install Petrify for MongoDB, run the following command in the Package Manager Console

PM> Install-Package Petrify.MongoDB

This will install and reference Petrify.Core.dll, Petrify.MongoDB.dll and the Mongo C# Driver (MongoDB.Driver.dll and MongoDB.Bson.dll)

Create your object model

We will create a simple model consisting of a Person and their Address. We are going to model both Person and Address and aggregates.

Aggregates must have global identity and we have modelled this by inheriting both classes from an Entity base class that has a single Id member, here using a Guid to give global identity.

public abstract class Entity
{
	public virtual Guid Id { get; set; }
}

public class Person : Entity
{
	public virtual string FirstName { get; set; }
	public virtual string LastName { get; set; }
	public virtual Address Address { get; set; }
}

public class Address : Entity
{
	public virtual string Street { get; set; }
	public virtual string Town { get; set; }
	public virtual string Postcode { get; set; }
}

Remember, how you structure your aggregates should be based on your domain. Just because this example has separate aggregates for Person and Address does not mean that should always be the case.

Persisting your aggregate

Add the required namespaces.

using Petrify.Core;
using Petrify.MongoDB;

Use the RepositoryConfiguration object to create a repository.

var repository = new RepositoryConfiguration ()
	.ConnectTo.MongoDb ("mongodb://localhost:27017/myDatabase")
	.BuildRepository ();

Create a Person and associated Address and and save them to the repository.

var address = new Address () { Street = "221B Baker Street", Town = "London", Postcode = "NW1 6XE" };
var person = new Person () { FirstName = "Sherlock", LastName = "Holmes", Address = address };

var id = repository.Save (person);

This will persist the Person object in a person collection along with a reference to the Address. The Address object will be saved in an address collection. Note that this happens in reverse order as associations are saved first.

The save method returns the uniquely assigned identify of the person aggregate. This can be used to load the aggregate. By default, the address reference will be lazy loaded when required.

var person2 = repository.Load<Person> (id);
Console.WriteLine (person2.Address.Street); // Lazy load address

Identifying the aggregate root

Petrify automatically identified Person and Address as aggregate roots by convention, in this case by identifying the Id member on the common base class.

See ??? for more details on conventions and customisations.

Mapping aggregates to collections

Petrify will persist each aggregate type in a separate collection. In this case Person will be persisted to a person collection and Address to an address collection.

This default behaviour can be customised, see ...