如何通过服务将事件从存储库鼓泡到UI?

时间:2012-04-26 11:46:59

标签: c# events repository repository-pattern

我有一个使用报告事件的组件的存储库。

我想在前端显示报告的事件。

这是存储库:

public interface IXmlRepository
{
    //irrelevant stuff removed
    event EventHandler TraceEventHandler;
}

public class XmlPanelRepository : IXmlRepository
{
    public XmlPanelRepository()
    {
        public event EventHandler TraceEventHandler;

        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    // I want to bubble the trace events up to my UI.
        TraceEventHandler += TraceEventHandler_Tracing;
    }

    private void TraceEventHandler_Tracing(object sender, EventArgs e)
    {
        // what do I do here?
    }

    void panelCom_Trace(string message)
    {
        if (TraceEventHandler!= null) TraceEventHandler.Invoke(this, new EventArgs());
    }
}

我的UI使用服务与存储库进行交互。该服务定义为:

public interface IXmlConfigurationService
{
    //irrelevant stuff removed

    event EventHandler TraceEventHandler;
}

public class XmlConfigurationService : IXmlConfigurationService
{
    public event EventHandler TraceEventHandler;

    public XmlConfigurationService(IXmlRepository configurationRepository)
    {
        _configurationRepository.TraceEventHandler += ConfigurationRepository_TraceEventHandler;
    }

    void ConfigurationRepository_TraceEventHandler(object sender, EventArgs e)
    {
        // this never gets hit.
        if (TraceEventHandler != null) TraceEventHandler.Invoke(sender, e);
    }
}

如果我可以使用它,我认为我可以按照相同的步骤来显示事件报告的UI。

如何让服务报告存储库中发生的事件?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要完成您的活动。这类似于Observable在观察者模式中履行所有监听观察者。但对于这样的事件:

public XmlPanelRepository()
    {
        public event EventHandler TraceEventHandler;

        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    // I want to bubble the trace events up to my UI.
        TraceEventHandler += TraceEventHandler_Tracing;
    }

    private void TraceEventHandler_Tracing(object sender, EventArgs e)
    {
        if (TraceEventHandler != null)
        {
              TraceEventHandler(this, e);
        }

    }

但是,您可能应该将事件命名为TraceEventHandler以外的事件,因为您现在正在公开事件(通常以过去时态表示)。

值得注意的是,您可以在冒泡时更改事件参数(如果您愿意)。 EventHandler(TEventArgs)可以为此提供帮助。

答案 1 :(得分:-1)

你忘了使用代表...... 尝试这样的事情:

public interface IXmlRepository
{
   //irrelevant stuff removed
   event EventHandler TraceEventHandler;
}

public class XmlPanelRepository : IXmlRepository
{
    public delegate void EventHandler(string parameter1, string parameter2);

    public event EventHandler TraceEventHandler;

    public XmlPanelRepository()
    {
        var panelCom = new PanelCom(); // this is a COM object that connects to a device

        // when something happens in the COM object it reports it.
        panelCom.Trace += panelCom_Trace; 

    }

    void panelCom_Trace(string message)
    {
        if (EventHandler != null)
          EventHandler("Event was hit, here's the message:", message);
    }
}

public interface IXmlConfigurationService
{
    //irrelevant stuff removed
}

public class XmlConfigurationService : IXmlConfigurationService
{
    public XmlConfigurationService(IXmlRepository configurationRepository)
    {
        _configurationRepository.TraceEventHandler += ConfigurationRepository_EventHandler;
    }

    void ConfigurationRepository_EventHandler(string parameter1, string parameter2);)
    {
        // Do something with your parameters.
        Response.Write(parameter1 + parameter2);
    }
}