.NET class refactoring dilemma

So, I refactored the legacy code base that I inherited, and in this process I found a static class that encapsulates the logic of launching third-party applications. Essentially it looks (short for brevity, to show only one application):

using System.IO;
using System.Configuration;
public static class ExternalApplications
{

   public string App1Path
   {
      get
      {
         if(null == thisApp1Path)
            thisApp1Path = Configuration.AppSettings.Get("App1Path");
         return thisApp1Path;
      }
   }
   private string thisApp1Path = null;

   public bool App1Exists() 
   {
      if(string.IsNullOrEmpty(App1Path))
         throw new ConfigurationException("App1Path not specified.");
      return File.Exists(App1Path);
   }

   public void ExecuteApp1(string args) 
   {
       // Code to launch the application.
   }

}

A good attempt to separate external applications from the rest of the code, but it seems to me that this could be reorganized further. What I mean is something like this:

using System.IO;
public abstract class ExternalApplicationBase
{

   protected ExternalApplicationBase()
   {
      InitializeFromConfiguration();
   }

   public string Path { get; protected set; }

   public bool Exists() 
   {
      if(string.IsNullOrEmpty(this.Path))
         throw new ConfigurationException("Path not specified.");
      return File.Exists(this.Path);
   }

   public virtual void Execute(string args)
   {
      // Implementation to launch the application
   } 

   protected abstract InitializeFromConfiguration();

}

public class App1 : ExternalApplicationBase
{

   protected virtual void InitializeFromConfiguration()
   {
      // Implementation to initialize this application from
      // the application configuration file.
   }

 }

 public class App2 : ExternalApplicationBase
 {

   protected virtual void InitializeFromConfiguration()
   {
      // Implementation to initialize this application from
      // the application configuration file.
   }

 }

My problems are as follows:

  • There may be a class, interface, or other construct that does this, and I just haven't stumbled upon it.

  • , . , , , ( ).

  • . ( , , , , , ).

  • , ( ) App.Config , ; , , ( ).

, , . , , .

P.S. , , , ; , . , , , , , , . !

+3
3

:

using System.IO;
public class ExternalApplication
{
   public ExternalApplication(string path)
   {
      this.Path = path;
   }

   public string Path { get; protected set; }

   public bool Exists() 
   {
      if(string.IsNullOrEmpty(this.Path))
         throw new ConfigurationException("Path not specified.");
      return File.Exists(this.Path);
   }

   public void Execute(string args)
   {
      // Implementation to launch the application
   } 
}

public class AppFactory
{
   public ExternalApplication App1()
   {
      // Implementation to initialize this application from
      // the application configuration file.
   }

   public ExternalApplication App2()
   {
      // Implementation to initialize this application from
      // the application configuration file.
   }

   public ExternalApplication AppFromKey(string key)
   {
      // get from somewhere
   } 
 }

ExternalApplication factory, , .

+2

.

- , . .

0

@Grauenwolf, .

( / ) (Sync ASync).

  • , , .
  •    . ,    .
  • AppLauncherBase AppExecutorBase?        . , - , , / .
  • You might want to consider the convention used to determine configuration information / application data in App.Config, and then implement the logic to retrieve it in the default base class.

Good luck and I hope this helps.

0
source

All Articles