Skip to content

Configuration

tobyrogers edited this page Jul 25, 2013 · 2 revisions

Configuration is via a fluent C# api. The starting point for configuration is the RepositoryConfiguration object.

Database

The database is set using the ConnectTo property

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

Only a single database can be set. Subsequent calls to ConnectTo will result in an exception.

Identifying aggregate roots

Aggregate roots can either be defined by inheriting from a common class or interface.

By default, Petrify will try and identify the common class or interface by looking for the Id or Key property. This also assumes that the same class or interface is used throughout the object model to identify an aggregate root.

This behaviour can be customised using the WithRoot property.

var repository = new RepositoryConfiguration ()
	.ConnectTo.MongoDb ("mongodb://localhost:27017/myDatabase")
	.WithRoot.IdentifiedBy<Entity>()
	.BuildRepository ();

By default this assumes that the class or interface declares an Id or Key property to store the unique identifier. A different identifier property can be defined as follows:

	.WithRoot.IdentifiedBy<Entity>(x => x.MyId)

Mapping aggregates to collections

Each aggregate type is stored in a separate collection.

By default the name of the collection is taken from the aggregate root (i.e. the class that inherits from, or implements the root identifier).

This behaviour can be customised using the MapClass<T>() property.

The following configuration will save all Address objects (and those inheriting from Address) to a collection called "addresses".

var repository = new RepositoryConfiguration ()
	.ConnectTo.MongoDb ("mongodb://localhost:27017/myDatabase")
	.MapClass<Address>().ToCollection("addresses")
	.BuildRepository ();

Multiple mappings can be specified as follows:

var repository = new RepositoryConfiguration ()
	.ConnectTo.MongoDb ("mongodb://localhost:27017/myDatabase")
	.MapClass<Address>().ToCollection("addresses")
	.MapClass<AnotherClass>().ToCollection("anotherTable")
	.BuildRepository ();

Lazy Loading

Lazy loading of associations is enabled by default. This default implementation is based on Castle Dynamic Proxy.

To disable lazy loading use the .ReferenceLoading property.

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

Clone this wiki locally