Is anything good / bad / unnecessary about this use of a public member?

Here's a quick overview of my code, which is a moderately complex WinForms GUI.

A dependency context is a template for representing a model view.

public class StatSyncherFormView : Form, IView 
{ ... }

public class Presenter 
{
   // Here is the member I made public
   public readonly IView view;

   public Presenter(IView view) 
   {
      this.view = view;
   }    
}

static void Main()
{
   IView view = new View();
   Presenter presenter = new Presenter(view);

   // Here is where I'm accessing the public member
   Application.Run((Form)p.view);   
}

1) I like the fact that the view is set only by the constructor and will not be changed after it. It makes me feel better in the context of multi-threaded GUI development.

2) With public View {get; private set;}I lose (immutability?).

3) private readonly IView viewI also need public View {get {return view;}}one that feels (at least maybe someone can tell me otherwise) redundant.

: , (3) - , .

, , , , .

+3
4

.

( 3), , , "". BTW ,

  • ,

  • getter (, , JITter).

"" .

, Hans Passant, / , , .

+1

Presenter Run().

+4

( ):

  • getter
  • : , , .
  • Encapsulation also means that you can really change the storage field of some other type if it can be converted to IView. This conversion may occur in a getter.
+1
source

If you use a public field, you cannot change it to a property later without recompiling your assembly. Therefore, I believe that it is better to do this correctly using the property.

0
source

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


All Articles