{"id":1037,"date":"2010-09-05T10:11:13","date_gmt":"2010-09-05T09:11:13","guid":{"rendered":"http:\/\/www.minddriven.de\/?p=1037"},"modified":"2010-09-05T15:42:48","modified_gmt":"2010-09-05T14:42:48","slug":"property-memento-followup","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/development\/design-patterns\/property-memento-followup","title":{"rendered":"Follow-up: Property Memento pattern"},"content":{"rendered":"<p>In a <a href=\"http:\/\/www.minddriven.de\/index.php\/technology\/development\/design-patterns\/property-memento-pattern\">previous blog post<\/a> I introduced the <strong>Property Memento pattern<\/strong> as way to temporarily save the value of an entity&#8217;s properties and restoring the initial values when the memento disposes.<\/p>\n<p>While I like the pattern itself I think the syntax can be done better. Here comes an implementation that uses just one type parameter for the property type and misses the reflection helper. The <code>static class PropertyMemento<\/code> helps to generate <code>PropertyMemento&lt;T&gt;<\/code> instances without specifying the type parameter (it&#8217;s inferred by the compiler):<\/p>\n<pre class=\"brush:csharp\">public class PropertyMemento&lt;TProperty&gt; : IDisposable\r\n{\r\n    private readonly TProperty _originalPropertyValue;\r\n    private readonly object _classInstance;\r\n    private readonly string _propertyName;\r\n\r\n    public PropertyMemento(object classInstance,\r\n        Expression&lt;Func&lt;TProperty&gt;&gt; propertySelector)\r\n    {\r\n        _classInstance = classInstance;\r\n        _propertyName = ((MemberExpression)propertySelector.Body).Member.Name;\r\n        _originalPropertyValue = GetPropertyValue();\r\n    }\r\n\r\n    public PropertyMemento(object classInstance,\r\n        Expression&lt;Func&lt;TProperty&gt;&gt; propertySelector, TProperty tempValue)\r\n        : this(classInstance, propertySelector)\r\n    {\r\n        SetPropertyValue(tempValue);\r\n    }\r\n\r\n    public void Dispose()\r\n    {\r\n        SetPropertyValue(_originalPropertyValue);\r\n    }\r\n\r\n    private TProperty GetPropertyValue()\r\n    {\r\n        return (TProperty)_classInstance\r\n            .GetType()\r\n            .GetProperty(_propertyName)\r\n            .GetValue(_classInstance, null);\r\n    }\r\n\r\n    private void SetPropertyValue(TProperty value)\r\n    {\r\n        _classInstance\r\n            .GetType()\r\n            .GetProperty(_propertyName)\r\n            .SetValue(_classInstance, value, null);\r\n    }\r\n}\r\n\r\npublic static class Memento\r\n{\r\n    public static PropertyMemento&lt;TProperty&gt; From&lt;TProperty&gt;(object classInstance,\r\n        Expression&lt;Func&lt;TProperty&gt;&gt; propertySelector)\r\n    {\r\n        return new PropertyMemento&lt;TProperty&gt;(classInstance, propertySelector);\r\n    }\r\n\r\n    public static PropertyMemento&lt;TProperty&gt; From&lt;TProperty&gt;(object classInstance,\r\n        Expression&lt;Func&lt;TProperty&gt;&gt; propertySelector, TProperty tempValue)\r\n    {\r\n        return new PropertyMemento&lt;TProperty&gt;(classInstance, propertySelector, tempValue);\r\n    }\r\n}<\/pre>\n<p>And now its usage in the same scenario shown in the original blog post:<\/p>\n<pre class=\"brush:csharp\">private void SomeMethod(...)\r\n{\r\n    int oldValSumHeight = _valSum.Height;\r\n    int newValSumHeight = 0;\r\n\r\n    AnchorStyles newAnchor = AnchorStyles.Top | AnchorStyles.Left;\r\n    using (PropertyMemento.From(_valSum, () =&gt; _valSum.AutoSize, true))\r\n    using (PropertyMemento.From(_valSum, () =&gt; _valSum.Anchor, newAnchor))\r\n    using (PropertyMemento.From(_detailContent, () =&gt; _detailContent.Anchor, newAnchor))\r\n    {\r\n        _valSum.SetValidationMessages(validationMessages);\r\n\r\n        newValSumHeight = _valSum.Height;\r\n        Height = Height - oldValSumHeight + newValSumHeight;\r\n    }\r\n\r\n    _valSum.Height = newValSumHeight;\r\n}<\/pre>\n<p>As said before: just a little improvement to the original code which slightly improves the implementation and usage of the pattern&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous blog post I introduced the Property Memento pattern as way to temporarily save the value of an entity&#8217;s properties and restoring the initial values when the memento disposes. While I like the pattern itself I think the syntax can be done better. Here comes an implementation that uses just one type parameter &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/development\/design-patterns\/property-memento-followup\" class=\"more-link\"><span class=\"screen-reader-text\">Follow-up: Property Memento pattern<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[264,268,267,277],"class_list":["post-1037","post","type-post","status-publish","format-standard","hentry","category-design-patterns","tag-design-pattern","tag-memento","tag-property","tag-property-memento"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":6,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"predecessor-version":[{"id":1056,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions\/1056"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}