【问题标题】:How to use events for communication between tiers in an MVC application如何在 MVC 应用程序中的层之间使用事件进行通信
【发布时间】:2012-07-16 05:52:13
【问题描述】:

我正在尝试使用事件作为 3 层应用程序中层之间的一种通信形式,因为建议使用事件来避免层之间的耦合。但是,当结果中需要同步响应时,我无法弄清楚如何使用事件,例如 MVC Web 应用程序。考虑下面的例子

//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 ?
    }

}

在上面的示例中,我在存储库(数据库层)中定义了事件“event_EmployeeAdded_To_Repository”以通知存储库中的添加完成。在业务层中,我定义了处理此事件的事件处理程序“OnEmployeeAddedToRepository”。 “OnEmployeeAddedToRepository”事件处理程序依次触发事件“event_EmployeeAdded_To_Entity”。最后,在表示层中,我定义了事件处理程序“OnEmployeeAddedToEntity”来处理业务层事件。

但在控制器中,我有一个动作“Add”,它需要同步返回响应(在“AddEmployee”完成后)以通知用户(或者可能调用另一个 ajax 动作)。触发事件只会将控制流从操作更改为事件处理程序。

那么在这种情况下你将如何使用事件呢?

【问题讨论】:

    标签: asp.net-mvc-3 events event-handling repository-pattern 3-tier


    【解决方案1】:

    那么在这种情况下你将如何使用事件呢?

    简短的回答是:你不会:)

    你的情况是你有一把锤子,你正在寻找一个钉子。而是确定为什么您需要这样做。

    通常情况下,您几乎不会在某个域中知道该域本身发生了什么事情。您可以查看域事件。但是再一次,一个特定的域可能会对另一个域中发生的事情更感兴趣。

    如果您确实发现自己需要在网络服务器上知道添加了员工,那么无论如何我都会处理域事件。域事件通常用于在其他系统订阅的服务总线上发布事件。例如,一旦您在 HR 系统中添加了一名员工,访问控制系统就会想知道以便它可以开始发卡。在这种情况下,访问控制系统将使其端点订阅Company.HR.Messages.EmployeeAddedEvent 消息。

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多