Private static fields in a non-stationary class

You have a class like this:

class Application { private Process _process; private static string _applicationPath = string.Empty; public void Start(string arguments) { if (!File.Exists(_applicationPath)) Deploy(); _process = Process.Start(_applicationPath, arguments); } public void SomeMethod() { //method that manipulate _process } private void Deploy() { // copying, installation steps (takes some time) and assign _applicationPath } } 
  • Is it bad that I use a static field? Other application instances are expected to use the same _applicationPath.
  • Is this an example of a violation of the SRP principle (SOLID)? And should I extract the "deployment responsibility" to another class?
+5
source share
2 answers

Is it bad that I use a static field?

It depends on what you use it for. In this case, since you are changing it using a non-static method ( Deploy() ), then yes, this is probably bad. If it should be the same for all instances, set it in a static constructor or property (provided that the application setup installs it).

Is this an example of a violation of the SRP principle (SOLID)?

What are the responsibilities of this class? Can you extract Deploy and Start logically or do you need something else?

+2
source

Is it bad that I use a static field? Other application instances are expected to use the same _applicationPath.

I do not think this is bad. As long as this field is private, you can use it however you want. Just be careful not to change it after you install it during deployment.

Is this an example of a violation of the SRP principle (SOLID)? And should I extract the "deployment responsibility" to another class?

Yes, I would definitely reorganize it and separate the deployment from the application launch. Currently, your class does more than one, it starts the process and deploys the application:

 class Application { private Process _process; private static string _applicationPath; public void Start(string arguments) { var isDeployed = string.IsNullOrEmpty(_applicationPath); if (!isDeployed) { var deployment = new Deployment(); _applicationPath = deployment.Deploy(); } _process = Process.Start(_applicationPath, arguments); } public void SomeMethod() { //method that manipulate _process } } class Deployment { private static string _applicationPath; public string Deploy() { if (IsDeployed) { return _applicationPath; } // copying, installation steps (takes some time) and assign _applicationPath return _applicationPath; } } 
0
source

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


All Articles