{"id":166,"date":"2007-01-25T12:56:52","date_gmt":"2007-01-25T19:56:52","guid":{"rendered":"http:\/\/kennyw.com\/indigo\/166"},"modified":"2007-01-25T12:56:52","modified_gmt":"2007-01-25T19:56:52","slug":"writing-clone-friendly-binding-elements","status":"publish","type":"post","link":"https:\/\/kennyw.com\/?p=166","title":{"rendered":"Writing Clone-friendly Binding Elements"},"content":{"rendered":"<p>When writing a custom binding element, you will notice two constructors on the abstract base class:<\/p>\n<blockquote><p><code class=\"keyword\">protected<\/code><code> BindingElement()<\/code><br \/>\n<code class=\"keyword\">protected<\/code><code> BindingElement(<\/code><code class=\"classname\">BindingElement<\/code><code> elementToBeCloned)<\/code><\/p><\/blockquote>\n<p>The usage of the first (default) constructor is straightforward.  The second one, which takes a <code class=\"classname\">BindingElement<\/code> is a little more subtle. Its purpose is to facilitate proper implementations of <code class=\"classname\">BindingElement<\/code><code>.Clone()<\/code>. The recommended pattern is that all non-sealed binding elements expose such a protected ctor, and then call that ctor from their Clone() method. This allows subclasses of your binding element to be able to implement Clone in a chained manner (since you can\u00e2\u20ac\u2122t just call base.Clone()).  In other words, if I was writing a FooBindingElement, my Clone() method would leverage this constructor as follows:<\/p>\n<p><code class=\"keyword\">protected override<\/code><code> BindingElement Clone()<br \/>\n{<br \/>\n&nbsp;&nbsp;<\/code><code class=\"keyword\">return new <\/code><code class=\"classname\">FooBindingElement<\/code><code>(<\/code><code class=\"keyword\">this<\/code><code>);<br \/>\n}<\/code><\/p>\n<p>If you then set a breakpoint on your Clone() method, you will see it called a bunch of times when you open a Client or Service. The reasons behind this have to do with the tension between the assurances we can (or cannot make) about side-effects of certain methods and needing to ensure the integrity of the original configured binding.  Reading back over that last sentence, it probably sounds a bit obtuse. I&#8217;ll try and clarify\/go into more details around our CanBuild, Build and GetPropery processes (prime users of Clone) in a future post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When writing a custom binding element, you will notice two constructors on the abstract base class: protected BindingElement() protected BindingElement(BindingElement elementToBeCloned) The usage of the first (default) constructor is straightforward. The second one, which takes a BindingElement is a little more subtle. Its purpose is to facilitate proper implementations of BindingElement.Clone(). The recommended pattern is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-indigo"],"_links":{"self":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=166"}],"version-history":[{"count":0,"href":"https:\/\/kennyw.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kennyw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}