简体   繁体   English

如何在分层架构中使用 FileSystemWatcher?

[英]How to use a FileSystemWatcher in a layered architecture?

Perhaps I am looking into the wrong architecture all together.也许我正在一起研究错误的架构。 If we assume that we have the UI, application, domain and infrastructure layers, what would be a good way to represent a file system watcher?如果我们假设我们有 UI、应用程序、域和基础设施层,那么表示文件系统观察程序的好方法是什么? My application essentially wants to get new files from an input folder and move them to an output folder.我的应用程序本质上想从输入文件夹中获取新文件并将它们移动到 output 文件夹中。 This is of course simplified but I can't seem to find good resources on how to structure such a thing.这当然是简化的,但我似乎无法找到关于如何构建这样一个东西的好资源。

The use cases from the UI are to add and remove input->output pairs and add new ones. UI 中的用例是添加和删除输入->输出对并添加新的。 When a new one is added, I'd want a file system watcher to be listening in another thread and call its callbacks from another thread (such that it will not block).当添加一个新的时,我希望一个文件系统观察者在另一个线程中监听并从另一个线程调用它的回调(这样它就不会阻塞)。 It's callbacks would be setup by the application layer use case handler, where a domain object's function would be plugged into the interface (maybe adapted as well if needed).它的回调将由应用层用例处理程序设置,其中域对象的 function 将插入接口(如果需要也可以调整)。 I would use a factory to create the IFileSystemWatcher from the input string.我会使用工厂从输入字符串创建 IFileSystemWatcher。 When one is removed, I would want to stop all callback threads and the file system watcher thread.当一个被删除时,我想停止所有回调线程和文件系统观察者线程。 A threadpool for handling callbacks would probably be a good idea.用于处理回调的线程池可能是一个好主意。

Im mostly unsure where the threads should be made and managed, what layer a file system watcher would belong and where the implementation details of the things described above belong.我主要不确定应该在哪里创建和管理线程,文件系统观察程序属于哪个层以及上述事物的实现细节属于哪里。 I can't seem to find much information about these kinds of event producing long running tasks that call callbacks, where the events aren't really domain events (as far as I can see)我似乎无法找到有关此类事件的太多信息,这些事件会产生调用回调的长时间运行的任务,其中事件并不是真正的域事件(据我所知)

I believe the interface would be in the domain as the domain's purpose is to detect files, but perhaps thats a naive view and it is an application level thing.我相信接口将在域中,因为域的目的是检测文件,但也许这是一个幼稚的观点,它是一个应用程序级别的东西。 Regardless, I would see the interface as something like无论如何,我会看到界面类似于

public interface IFileSystemWatcher {
    void StartWatching();
    void StopWatching();
    void RegisterCallback(Action<FileSystemEvent> callback);
}

I've been stuck on this for a really long time and don't really have anyone to ask.我已经被困在这个问题上很长时间了,真的没有人可以问。 I've tried to make it generic enough such that it is useful for others as well while also helping me specifically.我试图使它足够通用,以便对其他人也有用,同时也专门帮助我。

TL;DR How to work with and manage event producing background threads. TL;DR 如何使用和管理产生事件的后台线程。

In my view threading issues are infrastructure layer, that can take care of bootstrap the file watcher, and as soon as it is notified it can transform that event into a "domain" one.在我看来,线程问题是基础设施层,它可以负责引导文件观察程序,并且一旦收到通知,它就可以将该事件转换为“域”事件。 Why you do you think these events are not domain ones?为什么你认为这些事件不是领域事件? Can they be part of a different subdomain?它们可以是不同子域的一部分吗?

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

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