简体   繁体   中英

How do I design View logic into c# projects

I have three c# projects in my solution. One is a console app that simply calls into a class library project. The class library project does all the processing for the application. Then there is a WinForm project that displays a form and then when a button is pressed, calls the same logic in the class library project. As a result, there are two ways to run the logic, via the Console or via a Windows UI (WinForm).

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

In the Console app, I want the same place in the logic to simply write out to the Console. In my understanding of architecture, you don't want the class library project to contain WinForm logic and require it to have references to all the WinForm references. But how do I make a call to the WinForms project (or something else) to display the custom WinForm form? There would be a circular reference where the class library would reference the main WinForm app and the WinForm app would reference the class library project.

What is the standard way of doing this?

Why not define an inteface, IOutputHandler, that has a method called DisplayOutput. You would have 2 implementations of it, one for your winforms app and one for the console. You'd call the correct version of it at runtime. You could modify your class library to have a private field instance for the IOutputHandler, and then plug in the proper one at runtime.

You could raise an event in the class library that is listened to/registered from whatever your UI/Console layer is. That way it can decide to act on the event if it is deemed necessary in as many places as you desire. It really depends on how your architecture is setup.

You could create an interface that your library defines to communicate back to the caller, then have both your calling apps define their own implementaions of this interface, the library calls the methods on this interface and knows nothing of the implmentation.

The caller processes the methods accordingly...

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!");
    }
}

This is where you need clear definition between your logic layer and your UI layer. It's perfectly acceptable and normal to put this sort of logic in your UI, since that bit can't reasonably live within the logic layer, as it is UI dependent.

Your logic should never, ever refer to any kind of UI component. If it does, it's wrong, and you need to redesign it to remove UI dependencies completely.

虽然接口答案可能是更好的解决方案,但如果仅是一种方法,则可以只使用委托将Console或WinForm方法传递给类库。

Another solution

In your class lib..

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

Then call

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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