Common functionality between C # console applications

I have two console applications, Query and Update, which have some features. I wanted the two classes to inherit from a common base class, but the problem is that for a console application I have to have a function static Main. I currently have the following:

namespace Utils
{
    public class ConsoleBase
    {
        protected const int ERROR_EXIT_CODE = 1;
        protected static void printWarning(string msg) {...}
        public ConsoleBase(IEnumerable<string> args) { ... }
...

namespace Update
{
    class Update : ConsoleBase
    {
        private static ConsoleBase _consoleBase;
        public static void Main(string[] args) { ... }
...

namespace Query
{
    class Query : ConsoleBase
    {
        private static ConsoleBase _consoleBase;
        public static void Main(string[] args) { ... }
...

It seems to me that the design problem for me is both inherited from ConsoleBaseand has an instance of it as a variable staticin each derived class. The reason I am doing this is as follows:

  • I can have methods protected staticdefined in ConsoleBasethat are available to other methods staticin derived classes.
  • ConsoleBase, , public ConsoleBase.

, / ConsoleBase, .

_consoleBase.UseDebugMode()

, ConsoleBase,

printWarning(CONST_MSG_IN_BASE_CLASS);

? , ?

+3
3

, .

, , , . , .

, Update Query ConsoleBase - ?

+1

main, BaseClass.Main(args) Main .

:

public class BaseApp
{
    public static Main(String[] args)
    {
        // TODO: ...
    }
}

public class App1 : BaseApp // Same for App2
{
    // There is no need to keep a reference of the base class
    // if you are accessing static methods only

    public static Main(String[] args)
    {
        BaseApp.Main(args); // Access via class, not via instance
    }
}
+1

, . ConsoleBase.Main() ?

.

0

Source: https://habr.com/ru/post/1720725/


All Articles