简体   繁体   English

跨类共享TextBox.AppendText()方法的最干净方法是什么?

[英]What is the cleanest way to share a TextBox.AppendText() method across classes?

Let's say I have a Controller object that launches a simple Windows Form. 假设我有一个Controller对象,它将启动一个简单的Windows窗体。 Inside the Form is a text box to which I want to log my program's output. 表单内是一个文本框,我要将程序的输出记录到该文本框。 Right now, I have a Controller method that calls a method in the Form which then puts the logged message in the text box. 现在,我有一个Controller方法,该方法在Form中调用一个方法,然后将记录的消息放入文本框中。 Then I reference the Controller when creating the objects I want to log stuff and call "Controller.Log(str)", but this approach seems dirty to me. 然后,在创建我要记录的对象并调用“ Controller.Log(str)”时引用控制器,但是这种方法对我来说似乎很脏。

In what way would you guys tackle this problem, and keep the code loosely coupled? 你们将以什么方式解决这个问题,并使代码保持松散耦合?

Thanks in advance! 提前致谢!

I'd go for a Listener structure similar to the .net Trace classes. 我将选择类似于.net Trace类的Listener结构。 The UI can pass a callback into a centralised logging service (perhaps itself created through an IOC like Unity) and the logger sends its message to that centralised logging service. UI可以将回调传递到集中式日志记录服务(可能自己通过像Unity这样的IOC创建),然后记录器将其消息发送到该集中式日志记录服务。 Nice thing there is that there can be many readers of the message allowing both UI and disk based logging at the same time, with no added complexity. 令人高兴的是,消息的读者很多,可以同时进行基于UI和基于磁盘的日志记录,而不会增加复杂性。

The .net Trace architecture is a good place to start. .net Trace体系结构是一个不错的起点。 I can call Trace.WriteLine anywhere in my code without coupling to the listener. 我可以在代码中的任何位置调用Trace.WriteLine,而无需耦合到侦听器。

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

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