How to create View logic in C # projects

I have three C # projects in my solution. One is a console application that simply calls the class library project. The class library project does all the processing for the application. Then there is the WinForm project, which displays the form, and then when you click the button, it calls the same logic in the class library project. As a result, there are two ways to start the logic, through the console or through the Windows interface (WinForm).

My problem is that part of the path through the class library logic, if a UI application is used, I want a custom WinForm form to appear to ask the user a question.

In the Console application, I want the same place in the logic to simply be written to the Console. In my understanding of architecture, you do not want the class library project to contain WinForm logic and require it to have links to all WinForm links. But how do I make a call to a WinForms project (or something else) to display a custom WinForm form? There will be a circular link where the class library will refer to the main WinForm application, and the WinForm application will refer to the class library project.

What is the standard way to do this?

+5
source share
7 answers

, , , .

...

public interface IProgressReporter
{
       void ReportMessage(string message);
}



public class WinFormsProgressReporter : IProgressReporter
{
    public void ReportMessage(string message)
    {
          MessageBox.SHow(message);
    }
}

public class ConsoleAppProgressReporter : IProgressReporter
{
    public void ReportMessage(string message)
    {
          Console.WriteLine(message);
    }
}

public class LibraryClass
{
    public static void SomeMethod(IProgressReporter rep)
    {
         rep.ReportMessage("Wooooohooooo!");
    }
}
+2

inteface, IOutputHandler, DisplayOutput. 2 , winforms . . , IOutputHandler, .

+2

, / /. , , , . .

+2

. , ​​ , , .

0

- . , , , .

0

, , , , Console WinForm .

0

lib..

public void MyLibMethod(Action<string> callBack)
{
      callBack("Yeh baby...");
}

Class.MyLibMethod(s=> Console.WriteLine(s));
0

All Articles