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?
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!");
}
}
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.
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.
While the interface answers are probably better solutions, if it's just one method you could just use a delegate to pass the Console or WinForm method to the class library.
Another solution
In your class lib..
public void MyLibMethod(Action<string> callBack)
{
callBack("Yeh baby...");
}
Then call
Class.MyLibMethod(s=> Console.WriteLine(s));
精彩评论