Skip to content

aggregate references

vijay@envivo edited this page Jun 8, 2026 · 7 revisions

Aggregate References

It is good practice to keep Aggregates apart, but there are often times when an Aggregate is related to another Aggregate.

Some common scenarios where you might need an Aggregate Reference (ARef for short):

  • Your Aggregate refers to another Aggregate in a different Bounded Context
  • Your Aggregate refers to a parent Aggregate (i.e. it has an OwnedBy relationship type).

In these scenarios, use ARefs to indirectly link them.

In the diagram below, the graph on the left shows Aggregate Roots that are explicitly linked. The graph on the right shows how ARefs break the explicit links:

ARefs are considered part of the Aggregate. Fresnel provides an AggregateReference<T> class to support this concept.

In the following example, our Order class has a reference to the Customer's Account:

/// <summary>
/// An Order placed by a customer
/// </summary>
public class Order : IAggregateRoot
{
	/// <summary>
	/// <inheritdoc/>
	/// </summary>
	public Order(IAggregateReference<Account> account) //👈
		: this()
	{
		this.Account = account;
	}

	/// <summary>
	/// <inheritdoc/>
	/// </summary>
	[Key]
	public Guid Id { get; init; }

	/// <summary>
	/// The Account this order belongs to
	/// </summary>
	[Relationship(RelationshipType.OwnedBy)]
	[UI(renderOption: UiRenderOption.InlineSimple)]
	public IAggregateReference<Account> Account { get; internal set; } //👈
	
	...
}

Fresnel takes care of detecting ARefs, and will load the Aggregate from the correct Repository when the user requests it.

Aggregate References are immutable records

When an IAggregateReference<T> is created, it takes a snapshot of key values from the Aggregate Root. This description provides a friendly value to show which Aggregate is being referenced.

For this reason, ensure the Aggregate Root's ToString() remains clear and accurate, regardless of any future changes made to the Aggregate itself.

Clone this wiki locally