using System;
using System.Threading;
namespace Events
{
#region Delegates
public delegate void NewEmployeeInsertedEventHandler(object source, EventArgs args);
public delegate void NewEmployeeInsertingEventHandler(object source, EmployeeEventArgs args);
#endregion
class Program
{
static void Main(string[] args)
{
HRDepartment hr = new HRDepartment();
SystemAdmin sysAdmin = new SystemAdmin();
Ceo ceo = new Ceo();
WebSite webSite = new WebSite();
Employee newEmployee = new Employee() {Id = 1000, Name = "John Doe", Age = 25, Salary = 1000, YearsEmployed = 0};
// Subscribing SysAdmin to NewEmployeeInserting event
hr.NewEmployeeInserting += sysAdmin.OnNewEmployeeInserting;
// Subscribing subscribers to NewEmployeeInserted event
hr.NewEmployeeInserted += sysAdmin.OnNewEmployeeInserted;
hr.NewEmployeeInserted += ceo.OnNewEmployeeInserted;
hr.NewEmployeeInserted += webSite.OnNewEmployeeInserted;
// By calling InsertNewEmployee, few thing will happen:
// 1. Process of inserting employee will start
// 2. SysAdmin will be notiffied that process of inserting has started
// 1. Employees information will be inserted in database
// 1. All subscribers will be notified that employee has been inserted
hr.InsertNewEmployee(newEmployee);
Console.ReadKey();
}
}
public class HRDepartment
{
// Events
public event NewEmployeeInsertedEventHandler NewEmployeeInserted;
public event NewEmployeeInsertingEventHandler NewEmployeeInserting;
public void InsertNewEmployee(Employee employee)
{
Console.WriteLine("Inserting new employee in database...");
// Firing NewEmployeeInserting event
OnEmployeeInserting();
// TODO insert new employee in database
// Because there is no logic to insert our employee
// into database we are simulating wait time
Thread.Sleep(3500);
Console.WriteLine("New employee has been successfully inserted.");
// Firing NewEmployeeInserted event
OnNewEmployeeInserted();
}
protected virtual void OnNewEmployeeInserted()
{
// Checking if there are any subscribers to our event
if (NewEmployeeInserted != null)
{
// as parameters whe are sending
// this instance of a class
// and no EventArgs
NewEmployeeInserted(this, EventArgs.Empty);
}
}
protected virtual void OnEmployeeInserting()
{
if (NewEmployeeInserting != null)
{
// as parameters we are sending
// this instance of a class
// and EmployeeEventArgs instance with DateTime value
NewEmployeeInserting(this, new EmployeeEventArgs() {TimeOfInserting = DateTime.Now});
}
}
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int YearsEmployed { get; set; }
public double Salary { get; set; }
}
public class EmployeeEventArgs : EventArgs
{
// DateTime value that will be shown while
// inserting new employee
public DateTime TimeOfInserting { get; set; }
}
public class Ceo
{
// Method signature must match delegate NewEmployeeInsertedEventHandler
public void OnNewEmployeeInserted(object source, EventArgs args)
{
Console.WriteLine("Ceo notified about new employee.");
}
}
public class WebSite
{
// Method signature must match delegate NewEmployeeInsertedEventHandler
public void OnNewEmployeeInserted(object source, EventArgs args)
{
Console.WriteLine("WebSite notified about new employee.");
}
}
// Because SysAdmin is subscribed to two events that consume two diferent delegates
// we need two different methods for subscription
public class SystemAdmin
{
// Method signature must match delegate NewEmployeeInsertingEventHandler
public void OnNewEmployeeInserting(object source, EmployeeEventArgs args)
{
Console.WriteLine("Notification to SysAdmin: A new employee is beeing created {0}", args.TimeOfInserting);
}
// Method signature must match delegate NewEmployeeInsertedEventHandler
public void OnNewEmployeeInserted(object source, EventArgs args)
{
Console.WriteLine("SysAdmin notified about new employee.");
}
}
}