-2

I'm developing a small app in c# .net. I would like to use it in different ways. As windows forms app and as command line app. So i have interface projects and i have internal core dll project. In some procedures in that dll i want to comunicate to user and ask if i should continue with my operation. So... Which would be the better way to communicate with user through my interface projects? Would it be some kinds of delegate functions passed to my dll class or through some service reference?

4

1 回答 1

0

您已经使用了以下术语:

  1. 接口项目
  2. 内部核心dll 项目

为什么 DLL 有任何业务要​​求用户提供某些东西?我会重新考虑整个设计,以便 DLL 只做实际工作。其他一切,比如询问用户要做什么,都应该只在用户界面项目中完成。

您可以创建 DLL 可以调用的回调来询问要做什么,但您绝不应该假设这些调用提供了用户交互。这意味着:您应该以某种方式对其进行设计,以便 DLL 不需要知道如何将信息返回给它们,只需要知道将信息返回给它们。

例如:假设您的一个 DLL 包含将文件从文件夹复制A到文件夹的功能B。如果一个文件的复制失败,您希望用户决定他是要中止还是继续所有其他文件。你可以像这样创建一个事件:

public class QueryContinueEventArgs : EventArgs
{
    public QueryContinueEventArgs(string failedFile, Exeption failure)
    {
        FailedFile = failedFile;
        Failure = failure;
        Continue = false;
    }

    public string FailedFile { get; private set; }
    public Exception Failure { get; private set; }
    public Continue { get; set; }
}


public event EventHandler<QueryContinueEventArgs> QueryContinueAfterCopyFailure;

protected bool OnQueryContinueAfterCopyFailure(string fileName, Exception failure)
{
    if (QueryContinueAfterCopyFailure != null)
    {
        QueryContinueEventArgs e = new QueryContinueEventArgs(fileName, failure);
        QueryContinueAfterCopyFailure(this, e);
        return e.Continue;
    }
    return false;
}

分配的事件处理程序可以提供用户交互并Continue相应地设置标志。

于 2013-10-01T10:46:36.583 回答