{"id":12,"date":"2007-05-12T10:00:07","date_gmt":"2007-05-12T08:00:07","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=12"},"modified":"2007-05-12T10:00:20","modified_gmt":"2007-05-12T08:00:20","slug":"explicit-interface-implementation","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=12","title":{"rendered":"Explicit interface implementation"},"content":{"rendered":"<p>In C# you have a feature, which java don&#8217;t have. It&#8217;s called explicit interface implementation.<br \/>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:<\/p>\n<p>namespace testpw.ExplicitInterfaces<br \/>{<br \/>&nbsp;&nbsp;&nbsp; public interface IA<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void doAction();<br \/>&nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; public interface IB<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void doAction();<br \/>&nbsp;&nbsp;&nbsp; }<br \/>}<\/p>\n<p>The implementation of both interfaces on the same class look now like this:<\/p>\n<p>namespace testpw.ExplicitInterfaces<br \/>{<br \/>&nbsp;&nbsp;&nbsp; public class TestImpl : IA, IB<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void IA.doAction()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&#8220;Called doAction on interface IA&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void IB.doAction()<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&#8220;Called doAction on interface IB&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp; }<br \/>}  <\/p>\n<p>One of the benefit of the exlicit interface implementation is, that you could solve this a little bit exotic problem. An other possibility to use explicit interface implementation is, when you have an existing object-structure and you have to extend it with some infrastructure-logic. This new logic you want to hidden to the consumer of the object-structure, so you could use the explicit interface implementation to hide this new logic.<\/p>\n<p>But, I saw also other interpretations how to use the explicit interface implementation:<\/p>\n<ul>\n<li>Use an interface as bracket to hold the logic together, which is used in an UseCase\n<li>Implement diffrent UseCases on the same class and the only separation was by implementing the logic through explicit interface implementation.<\/li>\n<\/ul>\n<p>Those interpretation of the explicit interface implementation is from my point wrong. They are anti-patterns.<br \/>For the first interpretation ignores, that for that reason exists classes.<br \/>The second interpretation is even worse. It hurts the encapsulation on an object. If you implement severals inteface in an explicit manner and you add to this class a private metod for one of those interfaces, this private method is also visible for the other interface implementations. This interpretation hurts also the principles&nbsp;&#8220;single-responsibilty&#8221; or &#8220;separation-of-concerns&#8221;. The implemention (the class) has to be modified when one of the interface implementation has to be fixed.<\/p>\n<p>My advice for explicit interface implementation is to use it only in the two described cases. Don&#8217;t try to find an other encapsulation mechanism for classes, use classes for this need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C# you have a feature, which java don&#8217;t have. It&#8217;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{&nbsp;&nbsp;&nbsp; public interface IA&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void doAction();&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; public interface IB&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void doAction();&nbsp;&nbsp;&nbsp; }} The implementation of both interfaces on the same class look now like this: namespace testpw.ExplicitInterfaces{&nbsp;&nbsp;&nbsp; public class TestImpl : IA, IB&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=12\"> 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":[5],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-good-practices"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-c","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":376,"url":"https:\/\/blog.eweibel.net\/?p=376","url_meta":{"origin":12,"position":0},"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":12,"position":1},"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":[]},{"id":677,"url":"https:\/\/blog.eweibel.net\/?p=677","url_meta":{"origin":12,"position":2},"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":160,"url":"https:\/\/blog.eweibel.net\/?p=160","url_meta":{"origin":12,"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":1082,"url":"https:\/\/blog.eweibel.net\/?p=1082","url_meta":{"origin":12,"position":4},"title":"Branch or not to branch","author":"Patrick","date":"22. Aug 2011","format":false,"excerpt":"That is a question? Yes, it is a question which you should ask yourself before to create a new branch or reuse an existing branch. There are other practices or possibilities to create new features in an existing software, which I will show in this blog post. Also there are\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"Fotolia_25836922_S","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_25836922_S_thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":200,"url":"https:\/\/blog.eweibel.net\/?p=200","url_meta":{"origin":12,"position":5},"title":"How to structure code in an unit test","author":"Patrick","date":"14. Jun 2009","format":false,"excerpt":"When you create your unit tests for a method in the SUT (software under test) you will ask yourself how to structure the code in the test method.I saw two kind of syntaxes which help to structure the code in a unit test method (well, actually there are at least\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":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/12","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=12"}],"version-history":[{"count":0,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}