vendredi 8 janvier 2016

Is it possible to minimize wrapper pattern to one-liner?

Consider such scenario, you have some data, but you don't want to expose it directly, besides there is some logic involved. So you write something like this:

private string field;
public string Field
{
  get { return this.field; }
  set 
  {
     // some logic with previous value
     this.field = value;
     // some logic with new value
  }
}

Ok, cool, but once you write this for N-th time you have enough, so you write a class -- wrapper for it. Since you don't want to expose the implementation you write this:

private Wrapper<string> field = new Wrapper<string>();
public string Field
{
  get { return this.field.value;}
  set { this.field.value = value; }
}

So, ok, common code is hidden, implementation not exposed, but still there is a lot of repetitions.

The question -- is it possible to squeeze it further?

Above it is my main question about the pattern in C#, but I leave this for comment -- is there a language which supports this pattern to a degree that only single line is required. Something like this:

public string Field { get; set; } <- new Wrapper<string>(); // pseudo code

Aucun commentaire:

Enregistrer un commentaire