Understanding Domain Events
<p>I am trying to understand the domain events. Let's say I have a customer object and it raises the OnCustomerInserted event. I want my email service class to subscribe to that event so that whenever the event is raised an email is sent. </p>

<pre><code>public class Customer
    {
        public delegate void CustomerInsertedHandler();

        public event CustomerInsertedHandler CustomerInserted; 

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public void OnCustomerInserted()
        {
            if(CustomerInserted != null)
            {
                CustomerInserted(); 
            }
        }
    }
</code></pre>

<p>And here is the CustomerRepository: </p>

<pre><code>public class CustomerRepository
    {
        public void Add(Customer customer)
        {
            // add the customer 

            customer.Id = 2;

            if(customer.Id &gt; 0)
            {
                // raise the event! 
            }
        }
    }
</code></pre>

<p>Finally, here is the simple EmailService class: </p>

<pre><code> public class EmailService
    {
        public EmailService()
        {

        }

        public void Send()
        {
            Console.WriteLine("Email has been sent!");
        }
    }
</code></pre>

<p>UPDATE: </p>

<p>Still the code below is bit ugly! </p>

<pre><code>public class CustomerRepository
    {
        public delegate void CustomerInsertedHandler(Customer newCustomer);
        public event CustomerInsertedHandler CustomerInserted; 

        public void OnCustomerInserted(Customer newCustomer)
        {
            if (CustomerInserted != null)
                CustomerInserted(newCustomer); 
        }

        public void Add(Customer customer)
        {
            // add the customer 

            customer.Id = 2;


            if(customer.Id &gt; 0)
            {
                var emailService = new EmailService();
                CustomerInserted += emailService.Send; 

                // raise the event!
                OnCustomerInserted(customer);
            }
        }


}
</code></pre>

<p>I updated my repository and is using StructorMap to create instance of the Repository </p>

<pre><code>public class CustomerRepository : ICustomerRepository
    {
        public delegate void CustomerInsertedHandler(Customer newCustomer);
        public event CustomerInsertedHandler CustomerInserted;
        private IEmailService _emailService; 

        public CustomerRepository(IEmailService emailService)
        {
            _emailService = emailService;
            CustomerInserted += _emailService.Send; 
        }
</code></pre>

<p>The code is still not good since now the CustomerRepository constructor is getting crowded. The EmailService is clean as follows: </p>

<pre><code> public class EmailService : IEmailService
    {
        public EmailService()
        {

        }

        public void Send(Customer customer)
        {
            Console.WriteLine("Email Sent!"); 
        }
    }
</code></pre>
