Methods for distributing value among classes in a program

I use

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\MyProgram"

As a way to store multiple files used by my program. I would like to avoid pasting the same piece of code throughout my application.

I need to make sure that:

  • The path cannot be accidentally changed after installation
  • The classes he needs have access to him.

I thought:

  • Make him single
  • Using constructor dependency injection
  • Using Property Dependency Injection
  • Using AOP to create a path where necessary.

Each has its pros and cons.

Singleton is all his favorite boys. I am not against using one, but there are good reasons to avoid it, if possible.

Castle Windsor. , Windsor . , - , . , .

, , , , , . , , .

AOP , AOP , .

, ? , ?

+3
5

IMyAppConfig, .

0

, Environment , .

MyAppEnvironment.ApplicationFolder

, a) , , b) . , . , , ( / , ).

, , .

+3

, . AOP o , .

. , . ...

public static class GlobalSettings {
  private static string s_path;
  public static string Path { get { return s_path; } }
  public static void UpdatePath(string path) {
    if ( s_path != null || path == null ) { throw ... }
    s_path = path;
  }
}
+1

.net, . .

0

- : ? , ? ? , ?

, , .

, , , , . , . " ", , . , ( ..), , , .

, , . , , . ( ), , , , . .

And classes can be transferred to a new project, regardless of this particular detail.

0
source

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


All Articles