0

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 ?

devanalyst
  • 1,348
  • 4
  • 28
  • 55

1 Answers1

0

So then how will you use events in this scenario?

The short answer is: you won't :)

You are in a situation where you have a hammer and you are looking for a nail. Rather determine why you need to do this.

Typically you will have very little use within a domain of knowing when something happened in the domain itself. You could take a look at domain events. But once again, a particular domain will probably be more interested in something happening in another domain.

If you do find yourself needing to know on the web-server that an employee was added then I would go with domain events anyway. A domain event would typically be used to publish the event on a service bus that other systems subscribe to. For instance, once you add an employee in the HR system the access control system would like to know so that it can start issuing a card. In this case the access control system would have it's endpoint subscribe to the Company.HR.Messages.EmployeeAddedEvent message.

Eben Roux
  • 12,983
  • 2
  • 27
  • 48