<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>MICHEL'S WEBLOG - VB.NET</title>
    <link>http://www.michelvandervlugt.net/</link>
    <description>MICHELVANDERVLUGT.NET</description>
    <language>en-us</language>
    <copyright>Michel van der Vlugt</copyright>
    <lastBuildDate>Mon, 18 Jun 2007 18:15:04 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>blog@michelvandervlugt.net</managingEditor>
    <webMaster>blog@michelvandervlugt.net</webMaster>
    <item>
      <trackback:ping>http://www.michelvandervlugt.net/Trackback.aspx?guid=cc02d07d-d429-4ade-9ee1-2dc3c4d00eea</trackback:ping>
      <pingback:server>http://www.michelvandervlugt.net/pingback.aspx</pingback:server>
      <pingback:target>http://www.michelvandervlugt.net/PermaLink,guid,cc02d07d-d429-4ade-9ee1-2dc3c4d00eea.aspx</pingback:target>
      <dc:creator>Michel van der Vlugt</dc:creator>
      <wfw:comment>http://www.michelvandervlugt.net/CommentView,guid,cc02d07d-d429-4ade-9ee1-2dc3c4d00eea.aspx</wfw:comment>
      <wfw:commentRss>http://www.michelvandervlugt.net/SyndicationService.asmx/GetEntryCommentsRss?guid=cc02d07d-d429-4ade-9ee1-2dc3c4d00eea</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Test Driven Development is a great thing in software development as it makes it possible
to verify your code very easily during development and when you have to make
changes. However, it is not always easy to write solid unit tests when external influences
are involved. 
</p>
        <p>
For example, in case of Web Services you might want to avoid the actual Web Service
calls in your unit tests for reasons like availability, performance, predictability,
... of the Web Service. In my case I had to avoid the Web Services because
I couldn't rely on the information they returned (the content of the underlying ERP database
was out of my control).
</p>
        <p>
To solve this problem for the unit tests and get a predictable response that
can be tested there are basically two solutions:
</p>
        <ul>
          <li>
Create a dummy Web Service that replaces the actual Web Service; 
</li>
          <li>
Replace the proxy of Web Service in the project by a dummy proxy.</li>
        </ul>
        <p>
In my project I choose for the latter on, based on a technique that is described in
a blog post by <a href="http://blogs.msdn.com/davidwaddleton/archive/2006/08/03/687841.aspx" target="_blank">David
Waddleton</a>. The trick that makes this technique possible and fairly easy is that
the generated proxy is a partial class (.NET 2.0). This makes it possible to define
an interface on the generated proxy (without changing it) by writing your own partial
class. The service consumer can now communicate with the Web Service using the interface
instead of directly to the Web Service. At this point you are free to replace the
Web Service implementation for a mock object during the unit tests using some form
of dependency injection.
</p>
        <p>
          <br />
        </p>
        <center>
          <a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WebServiceMockTechnique_EF73/ClassDiagram_2.jpg" atomicselection="true">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="384" alt="ClassDiagram" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WebServiceMockTechnique_EF73/ClassDiagram_thumb_2.jpg" width="619" border="0" />
          </a>
        </center>
        <p>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
Note: In the picture above the class MyService is displayed as two classes for clarity
of the concept, in reality this is only one class implemented in two cs files.
</p>
        <p>
Creating the interface might seem to be a lot of work, especially for larger
Web Services, but it can easily be generated using the refactor tool [Extract
Interface] on the proxy class defined in Reference.cs. The implementation of the partial
MyService class stays empty, it only defines the interface IMyService (the implementation
of IMyService is in the generated proxy).
</p>
        <p>
For dependency injection I've used a simple factory class that either instantiates
the Web Service or, in case of the unit tests, an instance of a mock object defined
in the web.config.
</p>
        <p>
However, as easy this is in C# 2.0 it cannot be implemented the same way in VB.NET.
The reason is that VB.NET doesn't support implicit interface implementation, you have
to define the interface implementation explicitly using the Implements keyword:
</p>
        <div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1a82eaec-3b4d-4537-9f58-a28ad927b5f2" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 441px; padding-top: 0px">
          <pre style="background-color:White;">
            <div>
              <!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
              <span style="color: #0000FF; ">Public</span>
              <span style="color: #000000; ">
              </span>
              <span style="color: #0000FF; ">Function</span>
              <span style="color: #000000; "> DoSomething() </span>
              <span style="color: #0000FF; ">Implements</span>
              <span style="color: #000000; "> IMyService.DoSomething </span>
              <span style="color: #0000FF; ">End
Function</span>
            </div>
          </pre>
        </div>
        <p>
This is too bad because you either have to alter Reference.cs (which you shouldn't
because when you update the web reference you loose the changes) or implement new
properties/functions in the otherwise empty class MyService and map them to the Web
Service, which is a lot more work. 
</p>
        <br />
        <p>
The source code can be downloaded from here: <a href="http://www.michelvandervlugt.net/content/binary/WebServiceMockTechnique.zip">WebServiceMockTechnique.zip
(62,52 KB)</a></p>
        <img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=cc02d07d-d429-4ade-9ee1-2dc3c4d00eea" />
      </body>
      <title>Web Service Mock Technique</title>
      <guid isPermaLink="false">http://www.michelvandervlugt.net/PermaLink,guid,cc02d07d-d429-4ade-9ee1-2dc3c4d00eea.aspx</guid>
      <link>http://www.michelvandervlugt.net/PermaLink,guid,cc02d07d-d429-4ade-9ee1-2dc3c4d00eea.aspx</link>
      <pubDate>Mon, 18 Jun 2007 18:15:04 GMT</pubDate>
      <description>&lt;p&gt;
Test Driven Development is a great thing in software development as it makes it possible
to verify your code very easily during development and when&amp;nbsp;you have to make
changes. However, it is not always easy to write solid unit tests when external influences
are involved.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
For example, in case of Web Services you might want to avoid the actual Web Service
calls in your unit tests for reasons like availability, performance, predictability,
...&amp;nbsp;of the Web Service.&amp;nbsp;In my case I had to avoid the Web Services because
I couldn't rely on the information they returned (the content of the underlying ERP&amp;nbsp;database
was out of my control).
&lt;/p&gt;
&lt;p&gt;
To solve this problem for the unit tests and get&amp;nbsp;a predictable response&amp;nbsp;that
can be tested there are basically two solutions:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Create a dummy Web Service that replaces the actual Web Service; 
&lt;li&gt;
Replace the proxy of Web Service&amp;nbsp;in the project by a dummy proxy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
In my project I choose for the latter on, based on a technique that is described in
a blog post&amp;nbsp;by&amp;nbsp;&lt;a href="http://blogs.msdn.com/davidwaddleton/archive/2006/08/03/687841.aspx" target="_blank"&gt;David
Waddleton&lt;/a&gt;. The trick that makes this technique possible and fairly easy is that
the generated proxy is a partial class (.NET 2.0). This makes it possible to define
an interface on the generated proxy (without changing it) by writing your own partial
class. The service consumer can now communicate with the Web Service using the interface
instead of directly to the Web Service. At this point you are free to replace the
Web Service implementation for a mock object during the unit tests using some&amp;nbsp;form
of dependency injection.
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
&lt;center&gt;&lt;a href="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WebServiceMockTechnique_EF73/ClassDiagram_2.jpg" atomicselection="true"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="384" alt="ClassDiagram" src="http://www.michelvandervlugt.net/content/binary/WindowsLiveWriter/WebServiceMockTechnique_EF73/ClassDiagram_thumb_2.jpg" width="619" border="0"&gt;&lt;/a&gt;
&lt;/center&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Note: In the picture above the class MyService is displayed as two classes for clarity
of the concept, in reality this is only one class implemented in two cs files.
&lt;/p&gt;
&lt;p&gt;
Creating the&amp;nbsp;interface might seem to be a lot of work, especially&amp;nbsp;for larger
Web Services, but&amp;nbsp;it can easily be generated using the refactor tool [Extract
Interface] on the proxy class defined in Reference.cs. The implementation of the partial
MyService class stays empty, it only&amp;nbsp;defines the interface IMyService (the implementation
of IMyService is&amp;nbsp;in the generated proxy).
&lt;/p&gt;
&lt;p&gt;
For dependency injection I've used a simple factory class that either instantiates
the Web Service or, in case of the unit tests,&amp;nbsp;an instance of a mock object defined
in the web.config.
&lt;/p&gt;
&lt;p&gt;
However, as easy this is in C# 2.0&amp;nbsp;it cannot be implemented the same way in&amp;nbsp;VB.NET.
The reason is that VB.NET doesn't support implicit interface implementation, you have
to define the interface implementation explicitly using the Implements keyword:
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1a82eaec-3b4d-4537-9f58-a28ad927b5f2" contenteditable="false" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 441px; padding-top: 0px"&gt;&lt;pre style="background-color:White;"&gt;
&lt;div&gt;
&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF; "&gt;Public&lt;/span&gt;&lt;span style="color: #000000; "&gt; &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;Function&lt;/span&gt;&lt;span style="color: #000000; "&gt; DoSomething() &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;Implements&lt;/span&gt;&lt;span style="color: #000000; "&gt; IMyService.DoSomething &lt;/span&gt;&lt;span style="color: #0000FF; "&gt;End
Function&lt;/span&gt;
&lt;/div&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This is too bad because you either have to alter Reference.cs (which you shouldn't
because when you update the web reference you loose the changes) or implement new
properties/functions in the otherwise empty class MyService and map them to the Web
Service, which is a lot more work.&amp;nbsp;
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;
The source code can be downloaded from here: &lt;a href="http://www.michelvandervlugt.net/content/binary/WebServiceMockTechnique.zip"&gt;WebServiceMockTechnique.zip
(62,52 KB)&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.michelvandervlugt.net/aggbug.ashx?id=cc02d07d-d429-4ade-9ee1-2dc3c4d00eea" /&gt;</description>
      <comments>http://www.michelvandervlugt.net/CommentView,guid,cc02d07d-d429-4ade-9ee1-2dc3c4d00eea.aspx</comments>
      <category>.NET;C#;VB.NET</category>
    </item>
  </channel>
</rss>