简体   繁体   English

在多项目中引发自定义事件

[英]Raise custom event in multi-project

it concerns following: I have two projects which should exists more or less independently from each other. 它涉及以下方面:我有两个项目应该或多或少相互独立存在。 Project one is a kind File System Watcher. 项目之一是一种文件系统监视程序。 The other one cosnists of my UI. 我的UI的其他一位专家。 The file watcher raises an event, if there is a new file. 如果有新文件,文件监视程序将引发一个事件。 After that, the data from the file should be added to a database. 之后,应将文件中的数据添加到数据库中。 That's coarsely the background story. 那大概是背景故事。 The actual problem is that after the file watcher raised an event, I want to notify the UI to update the view of the data. 实际的问题是在文件监视程序引发事件后,我想通知UI更新数据视图。 That means, the event should be raised by the file watcher and the event should be registered in the implementation of the UI. 这意味着,该事件应由文件监视程序引发,并且该事件应在UI的实现中注册。 The main problem ist now that I need instances of classes from both projects. 现在的主要问题是,我需要两个项目中的类实例。 Obviuosly this results in the circular dependency problem. 显然这会导致循环依赖问题。 Of course there is the solution of interfaces for the CP problem, but this won't solve the problem, that I need the same object for data creation and event registration. 当然,有解决CP问题的接口的解决方案,但这并不能解决问题,我需要相同的对象进行数据创建和事件注册。 Hopefully you can help me with this problem. 希望你能帮助我解决这个问题。 Thanks. 谢谢。

Why do you think you need an UI instance in the business logic assembly? 您为什么认为在业务逻辑程序集中需要一个UI实例?

To register an event handler, you usually need only the instance from the calling assembly (observer, already contained in the calling assembly) and an instance of the referenced assembly (your assembly containing the filesystem watcher). 要注册事件处理程序,通常只需要调用程序集中的实例(观察者,已包含在调用程序集中)和引用程序集的实例(您的包含文件系统监视程序的程序集)。

Then you have eg the following structure: 然后,您将具有以下结构:

Assembly with logic 逻辑组装

public class MyCustomWatcher
{   
    public event EventHandler Event;

    private void RaiseEventForWhateverReason()
    {
        if (Event != null)
        {
            Event(this, new Args());
        }
    }
   public Data GetData()
   {
    //return the data
   }
}

Assembly with UI: - both form and the controller types are declared here. 使用UI进行组装: -在此声明窗体和控制器类型。

class Form : System.Windows.Forms.Form
{
 public void DisplayNotification(Data data)
 {
   //actual code here
 }
}

class Controller 
{
    private Form form;
    private MyCustomWatcher watcher;

    public void Init()
    {
      this.watcher = CreateWatcher();
      RegisterEvents();
      ShowForm();
    }
    void ShowForm()
    {
     //show
    }
    void RegisterEvents()
    {
        this.watcher.Event += HandleChange;
    }

    void HandleChange(object sender /*this will be the instance that raised the event*/, SomeEventArgs e)
    {
        //BTW: this.watcher == sender; //the same instance

        form.DisplayNotification(this.watcher.GetData());
    }
}

Assembly with UI references the assembly with logic. 具有UI的程序集引用具有逻辑的程序集。 No circular dependency here. 这里没有循环依赖。

I'm not sure I understand why the FileWatcher would have any dependency on you UI, but since you say it does you could add a third project to work as an event aggregator between the two. 我不确定我为什么理解FileWatcher为何对您的UI有依赖性,但是由于您说了做到,因此您可以添加第三个项目作为两者之间的事件聚合器 This would give both projects a dependency on the aggregator, but will remove the dependencies on each other. 这将使两个项目都对聚合器具有依赖性,但是将消除彼此之间的依赖性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM