Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

Property setters bypassed – why?

$
0
0

I struck on that the block property setter is called only once during block “life time”. In case from call within

SetDefaultValues(ContentType contentType)

I performed some tests on this:

1. Create new block

[ContentType(DisplayName = "SetterTestsBlock", GUID = "43ca7e93-6982-4b95-b073-c42af6ad2315", Description = "")]
public class SetterTestsBlock : BlockData
{
  public virtual string SomeVirtualStringProperty
  {
    get => this.GetPropertyValue(t => t.SomeVirtualStringProperty);
    set { this.SetPropertyValue(t => t.SomeVirtualStringProperty, "Ahoj1"); }      
  }
  public string SomeStringProperty
  {
    get => this.GetPropertyValue(t => t.SomeStringProperty);
    set { this.SetPropertyValue(t => t.SomeStringProperty, "Ahoj2"); }    
  }
  public override void SetDefaultValues(ContentType contentType)
  {
    SomeVirtualStringProperty = "Čau1";
    SomeStringProperty = "Čau2";
  }
}

The result is expectable:

Test 1 result

2. Create new block again

[ContentType(DisplayName = "SetterTestsBlock", GUID = "43ca7e93-6982-4b95-b073-c42af6ad2315", Description = "")]
public class SetterTestsBlock : BlockData
{
  public virtual string SomeVirtualStringProperty
  {
    get => this.GetPropertyValue(t => t.SomeVirtualStringProperty);
    //set { this.SetPropertyValue(t => t.SomeVirtualStringProperty, "Ahoj1"); }
    //set { }
    set { throw new Exception(); }
  }
  public string SomeStringProperty
  {
    get => this.GetPropertyValue(t => t.SomeStringProperty);
    //set { this.SetPropertyValue(t => t.SomeStringProperty, "Ahoj2"); }
    //set { }
    set { throw new Exception(); }
  }
  //public override void SetDefaultValues(ContentType contentType)
  //{
  //    SomeVirtualStringProperty = "Čau1";
  //    SomeStringProperty = "Čau2";
  //}
}

This time the result is also quite expectable:
Test 2 result

3. Publish changes to block from test 2

Test 3

This result is not so expectable:


Test 3 result

Tests conclusion:

  1. Property setter is called only from SetDefaultValues(ContentType contentType) method during block first-time creation.
  2. Property setter is never further called.
  3. Observed behaviour does not depend on property virtuality (virtual modifier).

The Problem

Imagine situation illustrated by code underneath.

[ContentType(DisplayName = "RealUsageSimulation", GUID = "12737925-ab51-4f63-9144-cd4632244a1c", Description = "")]
public class RealUsageSimulation : BlockData
{
  public string SomeStrPropWithDependency
  {
    get => this.GetPropertyValue(t => t.SomeStrPropWithDependency);
    set
    {
      this.SetPropertyValue(t => t.SomeStrPropWithDependency, GetDBValue());
      string GetDBValue()
      {  
      return string.Join(",",
        value.Split(new[] { ',', ';', '/'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()));
      }
    }
  }
}

Since issue described in tests this code is useless.

Am I wrong in some part? How to work around this in some good-approach manner?


Viewing all articles
Browse latest Browse all 8020

Trending Articles