I am trying to use events as a form of communication between the tiers in a 3 tier application since events are recommended way to avoid coupling between tiers. However I am not able to figure how to use events when synchronous response is needed in the result like in case of a MVC web application. Consider below example
//1. Repository for Employee (Database Tier)
interface IEmployeeRepository
{
//event to signal add complete in repository
public event EventHandler event_EmployeeAdded_To_Repository;
void AddEmployee(Employee emp);
}
public class EmployeeRepository: IEmployeeRepository
{
//implementation of event
public event EventHandler event_EmployeeAdded_To_Repository;
public void AddEmployee(Employee emp)
{
//Save to database
//Fire event to signal add complete in repository
event_EmployeeAdded_To_Repository(null,e);
}
}
//2. Business Tier for Employee
interface IEmployee
{
//event to signal add complete in business tier
public event EventHandler event_EmployeeAdded_To_Entity;
public void AddEmployee();
}
public class Employee : IEmployee
{
public event EventHandler event_EmployeeAdded_To_Entity;
IEmployeeRepository _empRepository;
//constructor injection
public Employee(IEmployeeRepository empRepository)
{
this._empRepository = empRepository;
//Add event handler for the repository event
this._empRepository.event_EmployeeAdded_To_Repository += OnEmployeeAddedToRepository;
}
public void AddEmployee()
{
//Call repository method for adding to database
_empRepository.AddEmployee(this);
}
//Event handler for the repository event
public void OnEmployeeAddedToRepository(Object sender, EventArgs e)
{
//Fire event to signal add complete in business tier
event_EmployeeAdded_To_Entity(null, e);
}
}
//3. Presentation Tier
public class EmployeeController : Controller
{
IEmployee _emp;
//constructor injection
public EmployeeController(IEmployee emp)
{
this._emp = emp;
//Add event handler for business tier event
this._emp.event_EmployeeAdded_To_Entity += OnEmployeeAddedToEntity;
}
//This is an Ajax method call
public ActionResult Add()
{
//Call add method of business tier
_emp.AddEmployee();
//What do I do here ?
}
//Event handler for the business tier event
public void OnEmployeeAddedToEntity(Object sender, EventArgs e)
{
//What do I do here ?
}
}
In above example, I have defined event "event_EmployeeAdded_To_Repository" in the Repository (Database tier) to notify addition completion in repository. In the business tier I have defined event handler "OnEmployeeAddedToRepository" that handles this event. The "OnEmployeeAddedToRepository" event handler in turn fires the event "event_EmployeeAdded_To_Entity". Finally in the presentation tier I have defined event handler "OnEmployeeAddedToEntity" for handling the business tier event.
But in the controller I have an action "Add" that needs to return a response synchronously (after "AddEmployee" has completed) to notify user (or maybe call another ajax action). Firing an event will simply change the flow of control from the action to the event handler.
So then how will you use events in this scenario ?