2

それは以下に関係します:私は互いに多かれ少なかれ独立して存在するはずの2つのプロジェクトを持っています。プロジェクト1は、一種のファイルシステムウォッチャーです。もう1つは私のUIのコニストです。新しいファイルがある場合、ファイルウォッチャーはイベントを発生させます。その後、ファイルのデータをデータベースに追加する必要があります。それは大まかに背景の話です。実際の問題は、ファイルウォッチャーがイベントを発生させた後、データのビューを更新するようにUIに通知したいということです。つまり、イベントはファイルウォッチャーによって発生し、UIの実装にイベントを登録する必要があります。主な問題は、両方のプロジェクトのクラスのインスタンスが必要になることです。明らかに、これは循環依存の問題を引き起こします。もちろん、CP問題のインターフェースの解決策はありますが、これでは問題は解決しません。データの作成とイベントの登録に同じオブジェクトが必要です。うまくいけば、あなたはこの問題で私を助けることができます。ありがとう。

4

2 に答える 2

1

Why do you think you need an UI instance in the business logic assembly?

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 e.g. 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.

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. No circular dependency here.

于 2010-07-29T09:20:35.243 に答える
0

FileWatcherがUIに依存する理由がわかりませんが、そう言うので、2つの間のイベントアグリゲーターとして機能する3番目のプロジェクトを追加できます。これにより、両方のプロジェクトにアグリゲーターへの依存関係が与えられますが、相互の依存関係は削除されます。

于 2010-07-29T09:01:42.843 に答える