{"id":1182,"date":"2012-02-21T21:53:12","date_gmt":"2012-02-21T20:53:12","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=1182"},"modified":"2012-02-22T12:43:25","modified_gmt":"2012-02-22T11:43:25","slug":"anti-pattern-validation-by-execute-n-rollback","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=1182","title":{"rendered":"Anti-Pattern &#8216;Validation by Execute &#8216;n&#8217; Rollback&#8217;"},"content":{"rendered":"<p>Recently in some reviews I saw an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Anti-pattern\" target=\"_blank\">anti-pattern<\/a>. First you have to know, in the code, there was a validation of the data before it was stored in the database. So far so good. But when I looked at the validation code, I saw the following:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:4784647f-555d-445a-b026-eff18742640e\" class=\"wlWriterEditableSmartContent\">\n<pre class=\"brush: c#;\">public void Validate()\r\n{\r\n\tDoProcess(true);\r\n}<\/pre>\n<\/div>\n<p>And the persist logic (with some business logic) looked like this:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:2984d7b9-08dc-4564-b9ca-ec0282a83dea\" class=\"wlWriterEditableSmartContent\">\n<pre class=\"brush: c#;\">public void Process()\r\n{\r\n\tDoProcess(false);\r\n}<\/pre>\n<\/div>\n<p>So, I asked myself, what the Boolean means. Well, here is the method signature:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f3969625-29f1-43dc-9d12-6aa1c2b25bc3\" class=\"wlWriterEditableSmartContent\">\n<pre class=\"brush: c#;\">public void DoProcess(bool bValidate)<\/pre>\n<\/div>\n<p>Hmm, the code looks quite magic so far and the argument (and the method name) itself is more or less an anti-pattern already. But when I checked the DoProcess method, I saw something more or less like this:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:99c75cc4-33a5-4803-a995-3b49ff14fcb3\" class=\"wlWriterEditableSmartContent\">\n<pre class=\"brush: c#;\">public void DoProcess(bool bValidate)\r\n{\r\n\tusing (SqlConnection con = new SqlConnection(connectionString))\r\n\t{\r\n\t\tcon.Open();\r\n\t\tSqlTransaction transaction = con.BeginTransaction();\r\n\t\t\r\n\t\t\/\/ Some business logic here...\r\n\r\n\t\tif(bValidate)\r\n\t\t{\r\n\t\t\ttransaction.Rollback();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\ttransaction.Commit();\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<\/div>\n<p><a href=\"http:\/\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_20233238_S1.jpg\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Fotolia_20233238_S\" border=\"0\" alt=\"Fotolia_20233238_S\" align=\"right\" src=\"http:\/\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_20233238_S_thumb1.jpg\" width=\"103\" height=\"103\" \/><\/a>The idea behind this code is, if it could be executed successfully, that means a successful validation. If there are any problems during the execution, those problems are the result of the validation. The execution itself wouldn&#8217;t be a problem after a successful validation (now I need a drink&#8230;).<\/p>\n<p><strong>Doing it better<\/strong><\/p>\n<p>It&#8217;s easy to find bad things, but it&#8217;s sometimes harder to do things right. But in this case, the better way is easy. First, you have to separate your business logic from the data access logic. This means following the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Separation_of_concerns\" target=\"_blank\">separation of concern principle<\/a>.<\/p>\n<p>The next point is, that you&#8217;re able to validate your model. So put the logic where it should be. If you use a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Domain_model\" target=\"_blank\">domain model<\/a>, the logic to validate an entity has to be on that entity. Or if you have a <a href=\"http:\/\/martinfowler.com\/eaaCatalog\/tableModule.html\" target=\"_blank\">table module approach<\/a>, the logic to validate an entity has to be on the corresponding table module.<\/p>\n<p><strong>Side effects<\/strong><\/p>\n<p>There are also other problems with this anti-pattern. There are some side effects with the data.<\/p>\n<p>As you know, transactions should be as short as possible to prevent lock problems with your data. But if you have combined the business logic and data access logic, your transactions are much longer than needed. To make it worse, this anti-pattern forces you to do it at least twice.<\/p>\n<p>There are reasonable use cases for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Isolation_%28database_systems%29\" target=\"_blank\">dirty reads<\/a>: For example fast searches. In this case you accept dirty reads and you don&#8217;t want any locks on your data in the database. Normally, the chance to have a dirty read isn&#8217;t that high, but with this anti-pattern the chances to get a dirty read are significantly higher.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently in some reviews I saw an anti-pattern. First you have to know, in the code, there was a validation of the data before it was stored in the database. So far so good. But when I looked at the validation code, I saw the following: public void Validate() { DoProcess(true); } And the persist logic (with some business logic) looked like this: public void Process() { DoProcess(false); } So, I asked myself, what the Boolean means. Well, here is&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=1182\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8,14],"tags":[],"class_list":["post-1182","post","type-post","status-publish","format-standard","hentry","category-anti-patterns","category-software-engineering"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-j4","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":677,"url":"https:\/\/blog.eweibel.net\/?p=677","url_meta":{"origin":1182,"position":0},"title":"Round-up of a data centric architecture","author":"Patrick","date":"11. Apr 2010","format":false,"excerpt":"In my last big project we had to use a data centric architecture. There was a learning curve which architecture was the most appropriate one. The result is visible in the picture bellow: Lets explaining the diagram. The data (or state) is managed by the database layer and the common\u2026","rel":"","context":"In &quot;Software architecture&quot;","block_context":{"text":"Software architecture","link":"https:\/\/blog.eweibel.net\/?cat=4"},"img":{"alt_text":"Architektur","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Architektur_thumb.jpg?resize=525%2C300 1.5x"},"classes":[]},{"id":12,"url":"https:\/\/blog.eweibel.net\/?p=12","url_meta":{"origin":1182,"position":1},"title":"Explicit interface implementation","author":"Patrick","date":"12. May 2007","format":false,"excerpt":"In C# you have a feature, which java don't have. It's called explicit interface implementation.The main use of this feature is to solve problems when you have to implement for example two interfaces which have both unfortunatly a same method signature: namespace testpw.ExplicitInterfaces{\u00a0\u00a0\u00a0 public interface IA\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 void doAction();\u00a0\u00a0\u00a0 }\u00a0\u00a0\u00a0\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1033,"url":"https:\/\/blog.eweibel.net\/?p=1033","url_meta":{"origin":1182,"position":2},"title":"Data quality as a business value","author":"Patrick","date":"3. Jun 2011","format":false,"excerpt":"It could happen, that you have to do some data migrations from time to time. If you are familiar with data migrations you know that it isn\u2019t an easy job. There are several concerns: Needed time to do the effective migration Cleaning up data Validate current data (consistency) Transform existing\u2026","rel":"","context":"In &quot;Software engineering&quot;","block_context":{"text":"Software engineering","link":"https:\/\/blog.eweibel.net\/?cat=14"},"img":{"alt_text":"DataQuality","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/DataQuality_thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":160,"url":"https:\/\/blog.eweibel.net\/?p=160","url_meta":{"origin":1182,"position":3},"title":"When to use stored procedures","author":"Patrick","date":"13. May 2009","format":false,"excerpt":"Recently I discussed with a colleague when to use stored procedures. As exptected it was quite a religious conversation. A few days later I found the following screencast: The Pros and Cons of Stored Procedures Based on the discussion and the screencast I tried to summarize my Pros and Cons:\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":376,"url":"https:\/\/blog.eweibel.net\/?p=376","url_meta":{"origin":1182,"position":4},"title":"Mocking frameworks in .Net","author":"Patrick","date":"30. Mar 2010","format":false,"excerpt":"A few month ago I played with some mocking frameworks in .Net. There are already some comparisons available (here, here or here). In this blog post I want to show which frameworks are available and which one fits best for agile development. You could download the source code from github.com.\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":745,"url":"https:\/\/blog.eweibel.net\/?p=745","url_meta":{"origin":1182,"position":5},"title":"ConfORM &ndash; Another NHibernate mapping possibility","author":"Patrick","date":"1. Sep 2010","format":false,"excerpt":"I recently hold two presentations at the .Net User Group Bern (DNUG) with Ren\u00e9 Leupold about object relational mapping in the .Net world. We showed Entity Framework 4.0 and NHibernate. My part was NHibernate. You could download the slides and samples from the DNUG website. In the two presentations I\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"ConfOrmBigTransparent","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/ConfOrmBigTransparent_thumb.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/1182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1182"}],"version-history":[{"count":7,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1191,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/1182\/revisions\/1191"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}