View on GitHub

Domain Events

A Simple Domain Event framework for applications based on work by Udi Dahan

Download this project as a .zip file Download this project as a tar.gz file

Welcome to Domain Events Project.

The Domain Events Project was inspired by work done by Udi Dahan.  The goal of the project is to make adding Domain Event support to your appllication easy. 

Add Domain Events to Your Project

Please use Nuget (http://nuget.org) to add Domain Events to your project.  There are currently two packages available, JaySmith.DomainEvents (The core Domain Events library) and JaySmith.DomainEvents.StructureMap (A package configured to use StructureMap as the Inversion of Control container).  The JaySmith.DomainEvents.StructureMap will automatically pull in JaySmith.DomainEvents.  Instructions for using just the Core Library or configuring your own IoC container are available in the Wiki.

Getting Started

This section will cover a quick start for adding Domain Events to an application.

The Domain Object

public class Person
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
}

Defining a Domain Event

public class PersonLastNameChangedEvent : IDomainEvent
{
	public Person Person { get; set; }
	public string OldLastName { get;  set; }
	public string NewLastName { get; set; }

	public PersonLastNameChangedEvent( Person person, string oldLastName, string newLastName)
	{
		this.Person = person;
		this.OldLastName = oldLastName;
		this.NewLastName = newLastName;
	}
}

Defining a Domain Event Handler

public class PersonLastNameChangedEventHandler : IDomianEventHandler<PersonLastNameChangedEvent>
{
	public void Handle(PersonLastNameChangedEvent args)
	{
		var person = args.Person;
		var oldLastName = args.OldLastName;
		var newLastName = args.NewLastName;

		// Do Something with the data
		...
	}
}

Raising the Domain Event

public class Person
{
	...
	public void ChangeLastName(string newLastName)
	{
		//Capture the current last name
		var oldLastName = this.LastName;
		
		// Change the last Name
		this.LastName = newLastName;

		// Create and Raise the Domain Event
		var lastNameChangedEvent = new PersonLastNameChangedEvent(this, oldLastName, value);
		DomainEventManager.Raise(lastNameChangedEvent);
	}
}

Contributing

If you are interested in contributing to this project please see the projects Wiki.