<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nati Dobkin</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Rhino mocks 3.5 with Framework 4</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2010/12/18/rhino-mocks-3-5-with-framework-4.aspx</link><pubDate>Sun, 19 Dec 2010 01:34:34 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:758634</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>63</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=758634</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2010/12/18/rhino-mocks-3-5-with-framework-4.aspx#comments</comments><description>&lt;p&gt;I was migrating our projects to Framework 4 and when I tried to run tests I have realized that not all of them are running as expected, for most of the tests the exception was &amp;quot;Ambiguous match found&amp;quot; when Arg&amp;lt;MyType&amp;gt;.Matches(…) been used.&lt;/p&gt;  &lt;p&gt;I have fetch the web an realized the Rhino Arg type is facing some difficulties with Framework 4. The solution is updating Rhino version to 3.6 (download &lt;a href="http://www.ayende.com/projects/rhino-mocks/downloads.aspx"&gt;here&lt;/a&gt;).&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=758634" width="1" height="1"&gt;</description></item><item><title>Logging Linq to SQL operations</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2010/02/01/logging-linq-to-sql-operations.aspx</link><pubDate>Tue, 02 Feb 2010 04:33:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:513088</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>60</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=513088</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2010/02/01/logging-linq-to-sql-operations.aspx#comments</comments><description>&lt;p&gt;Linq to SQL provide build in logger that logs all select, insert, update and delete operations, it executes. Very helpful tool when something goes wrong.&lt;/p&gt;  &lt;p&gt;You can find it under &lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;DataContext &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Log &lt;/span&gt;property that takes &lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;TextWriter &lt;/span&gt;.&lt;/p&gt;  &lt;p&gt;Most of the samples you probably would find about the logger are setting the &lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;Console&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Out &lt;/span&gt;to it, like this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;DataContext &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;dataContext &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;newMyDataContext&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;();
&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;dataContext&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Log &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;Console&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Out&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The more common scenario would be to write it to a text file. Changing this code to open a &lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;StreamWriter &lt;/span&gt;is not so hand:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background:#2e2e2e;color:silver;"&gt;string &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;logFilePath &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#a31515;"&gt;@&amp;quot;c:\log.txt&amp;quot;&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;;
&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileStream &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;fileStream &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:silver;"&gt;new &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileStream&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;(&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;logFilePath&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;, &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileMode&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;OpenOrCreate&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;, &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileAccess&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Write&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;);
&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;TextWriter &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;textWriter &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:silver;"&gt;new &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;StreamWriter&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;(&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;fileStream&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;);
&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;dataContext&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Log &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;textWriter&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Unfortunately this code will not work properly all the time, not if you use Linq to SQL with Async calls or Parallel execution. If you have more then one thread that accessing the same file you probably get the exception &amp;quot;The process cannot access the file because it is being used by another process&amp;quot;.&lt;/p&gt;

&lt;p&gt;To overcome this problem all that need to be done is opening the file with sharing, using &lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileShare&lt;/span&gt; enum, which is the proper way to handle files when running multi threads&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileStream &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;fileStream &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;= &lt;/span&gt;&lt;span style="background:#2e2e2e;color:silver;"&gt;new &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileStream&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;(&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;logFilePath&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;, &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileMode&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;OpenOrCreate&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;, &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileAccess&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;Write&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;, &lt;/span&gt;&lt;span style="background:#2e2e2e;color:#49c8ed;"&gt;FileShare&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;.&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#f8ffc6;"&gt;ReadWrite&lt;/span&gt;&lt;span style="background:#2e2e2e;color:#d2d200;"&gt;);
&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=513088" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+3.0/default.aspx">.net 3.0</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/Linq+TO+SQL/default.aspx">Linq TO SQL</category></item><item><title>Add configuration intelliSense for Unity Application Block</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2009/06/28/add-configuration-intellisense-to-unity-application-block.aspx</link><pubDate>Mon, 29 Jun 2009 02:59:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:347900</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>43</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=347900</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2009/06/28/add-configuration-intellisense-to-unity-application-block.aspx#comments</comments><description>&lt;pre&gt;&lt;a href="http://www.microsoft.com/Downloads/details.aspx?familyid=1643758B-2986-47F7-B529-3E41584B6CE5&amp;amp;displaylang=en"&gt;Unity Application Block&lt;/a&gt; is part of entLib 4.1, unfortunately its configuration section is not editable via entLib configuration editor. The editor probably &lt;/pre&gt;

&lt;pre&gt;will be updated in the next version of entLib 5.0, till then the best that we can do is add intelliSense to the configuration file.&lt;b&gt;&amp;#160;&lt;/b&gt;&lt;/pre&gt;

&lt;pre&gt;First download the xsd file of Unity from &lt;a href="http://www.codeplex.com/unitycontributions/SourceControl/PatchList.aspx"&gt;http://www.codeplex.com/unitycontributions/SourceControl/PatchList.aspx&lt;/a&gt; and copy that&lt;/pre&gt;

&lt;pre&gt;to c:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas [for VS 2008].&lt;/pre&gt;

&lt;pre&gt;After that go to your configuration file, open it for edit, open the properties, go to schemas and open the editor, check the use box for unoty.xsd&lt;/pre&gt;

&lt;pre&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image0026_1BFC5ADA.jpg"&gt;&lt;img title="clip_image002[6]" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="113" alt="clip_image002[6]" src="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image0026_thumb_0B45E35C.jpg" width="455" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;

&lt;p&gt;After that you will be able to auto complete the unit section&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image0046_30F92D74.jpg"&gt;&lt;img title="clip_image004[6]" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="117" alt="clip_image004[6]" src="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image0046_thumb_1C3177B8.jpg" width="272" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The bad news is that you have to do this operation for every new configuration file you use. &lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=347900" width="1" height="1"&gt;</description></item><item><title>Setting up subversion for visual studio</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2009/06/20/setting-up-subversion-for-visual-studio.aspx</link><pubDate>Sun, 21 Jun 2009 00:49:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:343262</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=343262</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2009/06/20/setting-up-subversion-for-visual-studio.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://subversion.tigris.org/"&gt;Subversion&lt;/a&gt; (AKA SVN) is good choice for source control if you don’t want to pay for licenses.&lt;/p&gt;  &lt;p&gt;For setting up the subversion source control for visual studio you need to do the following:&lt;/p&gt;  &lt;p&gt;1) Install the subversion plug in - &lt;a href="http://www.visualsvn.com/visualsvn/download/"&gt;http://www.visualsvn.com/visualsvn/download/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;2) for more operation and user interface install - &lt;a href="http://tortoisesvn.net/downloads"&gt;http://tortoisesvn.net/downloads&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;3) for the SVN repository you have couple of choices:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;a. Install one on your machine. Download and install the SVN server (&lt;a href="http://www.visualsvn.com/server/"&gt;http://www.visualsvn.com/server/&lt;/a&gt;). For this you have to be ready to do some extra install (like web server) and do some configurations. The benefit is that you have full control on the server&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;b. Open a SVN repository on one of the providers on the web. I found and open main at &lt;a href="https://projectlocker.com/"&gt;https://projectlocker.com/&lt;/a&gt;, you can find much more by googling &amp;quot;SVN host/repository&amp;quot;. The differences between the hosts are is like everything in life – what you get in return. Much more you pay you get more security and storage. If you will choose a free one then you get up to 500M (which is more the enough for small projects) but not of those hosts are guaranty that you sources will be secured.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Bottom line, Choose the one that fits your needs.&lt;/p&gt;  &lt;p&gt;After following those steps you will get a VisualSVN menu in your visual studio&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image002_1ED83A30.jpg"&gt;&lt;img title="clip_image002" style="border-right:0px;border-top:0px;display:inline;border-left:0px;border-bottom:0px;" height="332" alt="clip_image002" src="http://blogs.microsoft.co.il/blogs/ndobkin/clip_image002_thumb_1255DD3B.jpg" width="168" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can see there are all the common actions for source control. Update, commit, get and add solution from and to source control and more.&lt;/p&gt;  &lt;p&gt;You can see it in more detail in the following tutorial: &lt;a href="http://www.visualsvn.com/visualsvn/demo/"&gt;http://www.visualsvn.com/visualsvn/demo/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=343262" width="1" height="1"&gt;</description></item><item><title>Two Days Course About WCF</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/03/02/two-days-course-about-wcf.aspx</link><pubDate>Sun, 02 Mar 2008 06:16:16 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:62722</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=62722</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/03/02/two-days-course-about-wcf.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;Last week &lt;a href="http://www.human-debugger.net/"&gt;Shani Raba&lt;/a&gt; and I have performed a two days session about WCF. The presentations from both days are attached.  &lt;p&gt;Code will come soon.  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/files/folders/ndobkin/entry61998.aspx"&gt;WCF Course – First Day&lt;/a&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/files/folders/ndobkin/entry62720.aspx"&gt;WCF Course – Second Day&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=62722" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+3.5/default.aspx">.net 3.5</category></item><item><title>Validating Xml File Against XSD Schema</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/02/19/validating-xml-file-against-xsd-schema.aspx</link><pubDate>Wed, 20 Feb 2008 02:16:19 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:60130</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=60130</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/02/19/validating-xml-file-against-xsd-schema.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;Let the code talk&lt;pre class="csharpcode"&gt;public class XmlXsdValidator
{
    private static bool _isValied = true;
    public static bool ValidateXmlAgainstXsdSchema(string xmlPath, string xsdPath)
    {
        try
        {
            XmlReader xsd = new XmlTextReader(xsdPath);
            XmlSchemaSet schema = new XmlSchemaSet();
            schema.Add(null, xsd);

            XmlReaderSettings xmlReadeSettings = new XmlReaderSettings();
            xmlReadeSettings.ValidationType = ValidationType.Schema;
            xmlReadeSettings.Schemas.Add(schema);
            xmlReadeSettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);

            XmlTextReader xmlTextReader = new XmlTextReader(xmlPath);
            XmlReader xmlReader = XmlReader.Create(xmlTextReader, xmlReadeSettings);

            while(xmlReader.Read());
            xmlReader.Close();
        }
        catch (Exception ex)
        {
            _isValied = false;
            // Write here exception massage to log
        }
        return _isValied;
    }

    private static void ValidationHandler(object sender, ValidationEventArgs args)
    {
        _isValied = false;
    }
}&lt;/pre&gt;
&lt;p&gt;There is one static method that get XML and XSD paths and return true if the xml fit the xsd schema and false otherwise.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=60130" width="1" height="1"&gt;</description></item><item><title>Taking a shot to simplify Configuration Section using</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/01/14/taking-a-shot-to-simplify-configuration-section-using.aspx</link><pubDate>Mon, 14 Jan 2008 06:47:05 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:50134</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=50134</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2008/01/14/taking-a-shot-to-simplify-configuration-section-using.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;  &lt;p&gt;Configuration sections are our ability as developers to interfere in the structure of our configuration file and add some custom value in it. &lt;a href="http://blogs.microsoft.co.il/blogs/yoavmichaeli/"&gt;Yoav Michaely&lt;/a&gt; (my team mate) and I tried to make the use of it a little simpler.  &lt;p&gt;Our goals:&lt;br /&gt;1) Make easy use of configuration section instance&lt;br /&gt;2) To imitate the functionality of AppSetting of key value collection&lt;br /&gt;3) Maybe get some extra functionality in the way – how knows…?  &lt;p&gt;So let see out class declaration:&lt;pre class="csharpcode"&gt;public abstract class ConfigurationSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; : ConfigurationSection
&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;that would be our base configuration section class that will encapsulate some basic logic like instance initialization, type converting and more&lt;/i&gt;&lt;pre class="csharpcode"&gt;public abstract class ConfigurationSingleSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; : ConfigurationSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;where CurrentClass : ConfigurationSingleSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;t hat would be our single section class, which will get us the goals we define earlier. As you can see the generic class gets the derived class as it type&lt;/i&gt; 
&lt;p&gt;The last part that I am missing for handle the job is the configuration section name, which you usually supply hard coded. I decide to pass it as an attribute over deriving class, and there is it definition&lt;pre class="csharpcode"&gt;public sealed class ConfigurationSectionAttribute : Attribute&lt;/pre&gt;
&lt;p&gt;&lt;i&gt;which have only one property of SectionName&lt;/i&gt; 
&lt;p&gt;Let&amp;#39;s see how we are using that mechanize, shell we.&lt;pre class="csharpcode"&gt;[ConfigurationSection(&amp;quot;TestSection&amp;quot;)]
public class TestSection : ConfigurationSingleSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TestSection&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
{
    [ConfigurationProperty(&amp;quot;Prop1&amp;quot;)]
    public string Prop1
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;string&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(&amp;quot;Prop1&amp;quot;); }
    }

    [ConfigurationProperty(&amp;quot;Prop2&amp;quot;)]
    public int Prop2
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;int&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(&amp;quot;Prop2&amp;quot;); }
    }
}

&lt;/pre&gt;
&lt;p&gt;As you can see derived for out base class, add the attribute to indicate what my section name in config file and wrote some properties. As you can see I used GetSectionValue&amp;lt;T&amp;gt; method, which come from our ConfigurationSingleSectionBase class which heples to conver property to it correct type. 
&lt;p&gt;Now we have to register the section to our config file, the usual way,like this 
&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/4%5B5%5D.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="143" src="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/4_thumb%5B3%5D.jpg" width="429" border="0" alt="" /&gt;&lt;/a&gt; 
&lt;p&gt;As you can see we can add properties and some key value collection to our section definition. Now comes the interesting part, how would we access our section instance, as some of you will remember the way we are getting configuration section handlers is by writing code like following: 
&lt;p&gt;&lt;pre class="csharpcode"&gt;object section = ConfigurationManager.GetSection(&amp;quot;MyCoolSectionName&amp;quot;);
if (section != null)
    MyNewSection sec = (MyNewSection)section;
sec.Prop1;
&lt;/pre&gt;
&lt;p&gt;This is not so nice code to write for every and each section, so what would we do? My answer is nothing!&lt;br /&gt;Remember our derive class (TestSection) so let look what it has? 
&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/3%5B4%5D.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="94" src="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/3_thumb%5B2%5D.jpg" width="192" border="0" alt="" /&gt;&lt;/a&gt; 
&lt;p&gt;Two simple properties:&lt;br /&gt;Instance – gives you that ability to access all the properties of the section.&lt;br /&gt;Setting – key value collection, well you can guess what it does. 
&lt;p&gt;Now you can write code like this:&lt;pre class="csharpcode"&gt;TestSection.Instance.Prop1;
TestSection.Settings[&amp;quot;first&amp;quot;];&lt;/pre&gt;
&lt;p&gt;Not bad I think for folowing allmost the same steps of creating a configuration section. But wait there is more (I promised in 3rd point int our goads definition). Now let me take you one stap foraord. Let&amp;#39;s say I have tree environments in my work, dev, test and prod. All three of them have common setting like whether to write a log or not, but also they have uniqe data like user name and password. I want to be able to handle all this in one simple configuration section. 
&lt;p&gt;Let see the solution - class definition:&lt;/p&gt;&lt;pre class="csharpcode"&gt;public abstract class ConfigurationMuiltiSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;, &lt;span class="attr"&gt;ConfigurationCollectionElementClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; : ConfigurationSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
where CurrentClass : ConfigurationMuiltiSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CurrentClass&lt;/span&gt;, &lt;span class="attr"&gt;ConfigurationCollectionElementClass&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;where ConfigurationCollectionElementClass : ConfigurationSingleSectionBase&amp;lt;ConfigurationCollectionElementClass&amp;gt;&lt;br /&gt;&lt;i&gt;that would be our multi section class. As you can see the generic class gets the derived class itself as it type and another class of type &lt;/i&gt;ConfigurationSingleSectionBase&lt;i&gt;.&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Now you can define something like this:&lt;pre class="csharpcode"&gt;[ConfigurationSection(&amp;quot;projectenv&amp;quot;)]
public class ProjectEnv : ConfigurationSingleSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ProjectEnv&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
{
    private const string Prop1Name = &amp;quot;Name&amp;quot;;
    [ConfigurationProperty(Prop1Name)]
    public string Name
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;string&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(Prop1Name); }
    }

    private const string Prop2Name = &amp;quot;User&amp;quot;;
    [ConfigurationProperty(Prop2Name)]
    public string User
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;string&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(Prop2Name); }
    }

    private const string Prop3Name = &amp;quot;Password&amp;quot;;
    [ConfigurationProperty(Prop3Name)]
    public string Password
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;string&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(Prop3Name); }
    }
}

[ConfigurationSection(&amp;quot;environments&amp;quot;)]
public class Environments : ConfigurationMuiltiSectionBase&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Environments&lt;/span&gt;, &lt;span class="attr"&gt;ProjectEnv&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
{
    private const string Prop1Name = &amp;quot;IsWriteConfig&amp;quot;;
    [ConfigurationProperty(Prop1Name)]
    public bool IsWriteConfig
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;bool&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(Prop1Name); }
    }

    private const string Prop2Name = &amp;quot;ActiveSystem&amp;quot;;
    [ConfigurationProperty(Prop2Name)]
    public int ActiveSystem
    {
        get { return GetSectionValue&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;int&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;(Prop2Name); }
    }
} &lt;/pre&gt;
&lt;p&gt;&lt;i&gt;And add the following definition to the config file: &lt;/i&gt;
&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/1%5B5%5D.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="175" src="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/1_thumb%5B3%5D.jpg" width="429" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Now you we write &lt;/p&gt;&lt;pre class="csharpcode"&gt;Environments.Settings[0].Name;&lt;/pre&gt;
&lt;p&gt;or &lt;pre class="csharpcode"&gt;Environments.Settings[Environments.Instance.ActiveSystem].Name;&lt;/pre&gt;
&lt;p&gt;as you can see 
&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/2%5B5%5D.jpg"&gt;&lt;img style="border-top-width:0px;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="56" src="http://blogs.microsoft.co.il/blogs/ndobkin/WindowsLiveWriter/TakingashottosimplifyConfigurationSectio_1832/2_thumb%5B3%5D.jpg" width="459" border="0" alt="" /&gt;&lt;/a&gt; 
&lt;p&gt;The return type of our setting is now a list of our inner section. 
&lt;p&gt;That’s it. &lt;a href="http://blogs.microsoft.co.il/files/folders/ndobkin/entry50132.aspx"&gt;Here is the code&lt;/a&gt;. Play with it a little and let me know if you find it usably. &lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=50134" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category></item><item><title>Visualizer For LINQ Debuging</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/12/22/visualizer-for-linq-debuging.aspx</link><pubDate>Sun, 23 Dec 2007 04:50:19 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:44750</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=44750</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/12/22/visualizer-for-linq-debuging.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://weblogs.asp.net/scottgu/default.aspx"&gt;Scott Guthrie&lt;/a&gt;&amp;nbsp;wrote a very nice &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx"&gt;visualizer&lt;/a&gt; for LINQ to SQL for easier debugging. Take a look on it and think maybe you can add something alike in your LINQ implementations.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=44750" width="1" height="1"&gt;</description></item><item><title>MSTest task for MSBuild</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/12/16/mstest-task-for-msbuild.aspx</link><pubDate>Sun, 16 Dec 2007 05:25:06 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:42706</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>88</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=42706</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/12/16/mstest-task-for-msbuild.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;  &lt;p&gt;I was looking for an ability to run automated tests from MSBuild, my demands are:  &lt;blockquote&gt; &lt;p&gt;1) Be able to run tests from many solutions (that contains many projects and many tests).&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;2) Be able to control the entire test running definition from one file(and not to open each proj file to add a new test every time someone in my team writes one).&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;3) Be able to separate sets of tests that take more time to run from those that run with no time – usually it will be divided into unit and integration tests.&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;4) Do all this without using .vsmdi file – because from some reason this file not work properly within TFS source safe, which me and my team are using.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;MSBuild have no task for running units. Those are the alternatives I found for units to run from MSBuild.  &lt;p&gt;1) Run the MSTest command directly,by using exec task, like this  &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Exec&lt;/span&gt; &lt;span class="attr"&gt;Command&lt;/span&gt;=&amp;#39;&lt;span class="kwrd"&gt;&amp;quot;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\mstest.exe&amp;quot;&lt;/span&gt; /&lt;span class="attr"&gt;testcontainer:c:&lt;/span&gt;\&lt;span class="attr"&gt;projects&lt;/span&gt;\&lt;span class="attr"&gt;MyTests&lt;/span&gt;\&lt;span class="attr"&gt;Sampe&lt;/span&gt;.&lt;span class="attr"&gt;Tests&lt;/span&gt;.&lt;span class="attr"&gt;dll&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;This solution is pretty good if you have one test project to run, if you have more than one you have to specify the &amp;quot;/testcontainer:…&amp;quot; parameter for every project that you want to run. Or add new exec task.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;If you have to run it for, let say five solutions, when each one contains more the one test project, you have already ten projects that you have to maintain, and I don’t want to be the one that doing this.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;2) Running &lt;b&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/aa721750(VS.80).aspx"&gt;TestToolsTask&lt;/a&gt; &lt;/b&gt;task&lt;b&gt; &lt;/b&gt;which comes with TFS and can be found in Microsoft.TeamFoundation.Build.targets file. 
&lt;blockquote&gt;
&lt;p&gt;This one can be used only with .vsmdi files, and this differing number four in my demands.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;3) Writing task of my own, and make all my dreams come true. And this is what I did. 
&lt;blockquote&gt;
&lt;p&gt;The task has all the functionality that MSTest command line tool has.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;How to use the task? 
&lt;p&gt;Import the task in you .proj file:&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UsingTask&lt;/span&gt; &lt;span class="attr"&gt;TaskName&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Dobkin.Common.Tasks.MSTest&amp;quot;&lt;/span&gt;&lt;span class="attr"&gt;AssemblyFile&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;D:\MyFolder\Dobkin.Common.Tasks.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;And call the task:&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;$(ProjectName) == &amp;#39;Project1&amp;#39;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Tests&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;c:\Project1\Project1.BL.Test.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Test&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;c:\Project1\Project1.Dal.Tests.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

 &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;$(ProjectName) == &amp;#39;Project2&amp;#39;&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Tests&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;c:\Project2\Project2.BL.Test.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Test&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;c:\Project2\Project2.Dal.Tests.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Test&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;c:\Project2\Project2.Utils.Tests.dll&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And that’s it. For now on, every new test project or even a whole new solution, you will add, you have to change only this file (for new solution of course you have to import this file first) 
&lt;p&gt;Documentation of how to use the task is included in the attachment.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/files/folders/ndobkin/entry42703.aspx"&gt;MSTest.rar&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=42706" width="1" height="1"&gt;</description></item><item><title>Managing WCF service instances</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/24/managing-wcf-service-instances.aspx</link><pubDate>Sun, 25 Nov 2007 01:35:45 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:36985</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=36985</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/24/managing-wcf-service-instances.aspx#comments</comments><description>&lt;p&gt;When you first time run your WCF service (&lt;a href="http://localhost/myCoolService.svc"&gt;http://localhost/myCoolService.svc&lt;/a&gt;) you get a nice tutorial, made by Microsoft, that explain you how you should use the service.&amp;nbsp;This is not hard to notice that you should close every connection&amp;nbsp;you made to a service. Like this:&lt;pre class="csharpcode"&gt;MyServiceClient client = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyServiceClient();
client.Close();&lt;/pre&gt;
&lt;p&gt;I am using a lot of services in my apps and I don’t want to kip in mind that all the time I am using a service&amp;nbsp;instance I have to remember to close the connection. So I wrote a simple code that handles the closing for me. &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ServicesFactory : IDisposable
{
    List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt; services;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; ServicesFactory()
    {
        services = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; T CreateService&amp;lt;T&amp;gt;() &lt;span class="kwrd"&gt;where&lt;/span&gt; T : ICommunicationObject
    {
        T service = Activator.CreateInstance&amp;lt;T&amp;gt;();
        services.Add(service);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; service;
    }

    &lt;span class="preproc"&gt;#region&lt;/span&gt; IDisposable Members

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Dispose()
    {
        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;object&lt;/span&gt; obj &lt;span class="kwrd"&gt;in&lt;/span&gt; services)
        {
            ICommunicationObject currService = (ICommunicationObject)obj;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (currService.State != CommunicationState.Closed)
                currService.Close();
        }

    }

    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;
}&lt;/pre&gt;
&lt;p&gt;Now every time I will create a service instance I will do it this way.&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; (ServicesFactory factory = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServicesFactory())
{
    WCFServiceEx.Client.Calculate.CalculateClient client = 
                            factory.CreateService&amp;lt;WCFServiceEx.Client.Calculate.CalculateClient&amp;gt;();

    &lt;span class="rem"&gt;// do service logic.&lt;/span&gt;
}&lt;/pre&gt;
&lt;p&gt;That will make shore that the connection of all the instances that I made in the scope will be closed as soon as the code in the scope will finished.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=36985" width="1" height="1"&gt;</description></item><item><title>Cache me if you can</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/22/cache-me-if-you-can.aspx</link><pubDate>Fri, 23 Nov 2007 02:07:10 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:36644</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=36644</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/22/cache-me-if-you-can.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;  &lt;p&gt;WCF provides nice capability of extending the behavior of operations by implementing &lt;a href="http://msdn2.microsoft.com/En-US/library/system.servicemodel.description.ioperationbehavior.aspx"&gt;IOperationBehavior&lt;/a&gt; interface. I want to concentrate on ApplyDispatchBehavior method of that interface, which purpose is to do extention to server side calls. I will show how I use this extensability to provide a nice and easy cache mechanisam.  &lt;p&gt;I will make a class which derives from Attribute class and implements an &lt;a href="http://msdn2.microsoft.com/En-US/library/system.servicemodel.description.ioperationbehavior.aspx"&gt;IOperationBehavior&lt;/a&gt; interface. Now I want to interfire in the invoke process of the method so I need to make another class that implements &lt;a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.dispatcher.ioperationinvoker_methods.aspx"&gt;IOperationInvoker&lt;/a&gt; interface and would hold the cache logic. And then set it to dispatch.Invoker property of the ApplyDispatchBehavior method. Let see some code:&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; CacheMeAttribute: Attribute, IOperationBehavior
{
    &lt;span class="preproc"&gt;#region&lt;/span&gt; IOperationBehavior Members
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
    {
        dispatch.Invoker = &lt;span class="kwrd"&gt;new&lt;/span&gt; CommonInvoker(dispatch.Invoker);
    }
    &lt;span class="rem"&gt;//other interface methods.&lt;/span&gt;
    
    &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;span class="preproc"&gt;&lt;/span&gt;&lt;span class="preproc"&gt;} &lt;/span&gt;&lt;/pre&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; CommonInvoker : IOperationInvoker
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Cache AppCache
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; HttpRuntime.Cache; 
        }
    }

    IOperationInvoker BaseInvoker;
        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; CommonInvoker(IOperationInvoker baseInvoker)
    {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.BaseInvoker = baseInvoker;
    }
        
    
    &lt;span class="preproc"&gt;#region&lt;/span&gt; IOperationInvoker Members

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] AllocateInputs()
    {
        &lt;span class="kwrd"&gt;object&lt;/span&gt;[] ret = &lt;span class="kwrd"&gt;this&lt;/span&gt;.BaseInvoker.AllocateInputs();
        &lt;span class="kwrd"&gt;return&lt;/span&gt; ret;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt; Invoke(&lt;span class="kwrd"&gt;object&lt;/span&gt; instance, &lt;span class="kwrd"&gt;object&lt;/span&gt;[] inputs, &lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] outputs)
    {
        &lt;span class="kwrd"&gt;string&lt;/span&gt; key = GetCacheKey(inputs);
        &lt;span class="kwrd"&gt;object&lt;/span&gt; result = AppCache[key];
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (result == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            result = &lt;span class="kwrd"&gt;this&lt;/span&gt;.BaseInvoker.Invoke(instance, inputs, &lt;span class="kwrd"&gt;out&lt;/span&gt; outputs);
            AppCache[key] = result;
        }
        outputs = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[0];
        &lt;span class="kwrd"&gt;return&lt;/span&gt; result;
     }

     &lt;span class="rem"&gt;// other interface methods.&lt;/span&gt;

      &lt;span class="preproc"&gt;#endregion&lt;/span&gt;

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; GetCacheKey(&lt;span class="kwrd"&gt;object&lt;/span&gt;[] inputs)
    {
        &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;
        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; inputs.Length; i++)
             key += inputs[0].ToString();
        &lt;span class="kwrd"&gt;return&lt;/span&gt; key;
    }
}
&lt;/pre&gt;
&lt;p&gt;The cache algorithm is shown in the Invoke method and cache the return value of the calling method by the parameters given to the method input. I use HttpRuntime.Cache for the caching and it work well for both win and web application as I explained in my last post. 
&lt;p&gt;Now all you have to do in order to cache data is add CacheMe attribute over a method like this:&lt;pre class="csharpcode"&gt;[ServiceContract(Namespace = &lt;span class="str"&gt;&amp;quot;Dobkin.Services&amp;quot;&lt;/span&gt;)]
&lt;span class="kwrd"&gt;interface&lt;/span&gt; IMyService
{
    [OperationContract]
    &lt;span class="kwrd"&gt;string&lt;/span&gt; Mult(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y);
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; MyService : IMyService
{
    [CacheMe]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;long&lt;/span&gt; Mult(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; x * y;
    }
}&amp;nbsp;&lt;/pre&gt;
&lt;p&gt;And that&amp;#39;s it. Now every time that Mult method will be called then the CommonInvoker will check if there is entry in the cache with a &amp;#39;xy&amp;#39; key. If there is, the value will be returned to the caller without executing the method. 
&lt;p&gt;See full example of the code &lt;a href="http://blogs.microsoft.co.il/files/folders/ndobkin/entry36285.aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=36644" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category></item><item><title>Caching with WCF</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/21/caching-with-wcf.aspx</link><pubDate>Wed, 21 Nov 2007 05:59:24 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:35986</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>24</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=35986</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/21/caching-with-wcf.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;  &lt;p&gt;Asp.Net provides us very powerful cache mechanism that can be found under System.Web.Caching. You can access the cache using the folioing syntax:  &lt;p&gt;HttpContext.Current.Cache[&amp;quot;key&amp;quot;] = value;  &lt;p&gt;HttpContext.Current.Cache.Add(params...);  &lt;p&gt;Lately I was looking for similar mechanism to use with WCF. The flax ability of WCF services provide them to live in supported as well not supported web environment. In that case HttpContext.Current would not be available. From curiosity I went and check how the HttpContext.Current.Cache property is implemented and I was surprised to see the all it does is: return &lt;a href="http://msdn2.microsoft.com/EN-US/library/e1f13641.aspx"&gt;HttpRuntime&lt;/a&gt;.Cache. After googling I was more suprized to find out that inspite it very weby name (you know Http…) it is not have to live in web supported envirment. The only check that made by this getter is &amp;#39;if ASPNet is installed on the machine&amp;#39; and as far as I know it come with the installation of FrameWork.  &lt;p&gt;The only explanation that I can see for this one is that the Cache class is places in not appropriate namespace.  &lt;p&gt;So there is no matter how you will host you WCF service, within IIS (web app) or console app, you can use the cache mechanism by accessing &lt;a href="http://msdn2.microsoft.com/EN-US/library/e1f13641.aspx"&gt;HttpRuntime&lt;/a&gt;.Cache.  &lt;p&gt;Some of you may say: &amp;quot;wait a minute, I am writhing a console application, why the hell do I need a reference to System.Web?&amp;quot; and I will agree that usually you don’t. But till Microsoft will come up with some appropriate solution for caching in client apps, I think this deviation from the common sense worth it.  &lt;p&gt;&amp;nbsp;  &lt;p&gt;There are some references on the issue:  &lt;p&gt;&lt;a href="http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx"&gt;http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx&lt;/a&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx"&gt;http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=35986" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category></item><item><title>Testable code – is it worth it?</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/17/testable-code-is-it-worth-it.aspx</link><pubDate>Sun, 18 Nov 2007 04:31:05 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:35066</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=35066</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/11/17/testable-code-is-it-worth-it.aspx#comments</comments><description>&lt;p&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;  &lt;p&gt;Recently I been refactoring some legasy code and writing a new one. I am using Rhino mocks to run my units and for that I have to write my code in a testable way. This approach makes my code looks not exactly the way I designed and want it to be. Eli Lopian wrote an article about this issue – &amp;quot;&lt;a href="http://www.codeproject.com/dotnet/StopDesign4Tests.asp"&gt;Stop Designing for Testability&lt;/a&gt;&amp;quot; and has some points to think of.  &lt;p&gt;In my opinion, if we have to write very simple code to encapsulate some internal logic for our base class and make sure that there is no way some smart ass will override it, but still give an opportunity to reuse this code in the deriving class, like this: &lt;pre class="csharpcode"&gt;public abstract class MyBase
{
    protected void DoSomeVeryImportentLogicThatCanotBeOverriden()
    {
        DoTheSameVeryImportentLogic();
    }

    internal virtual void DoTheSameVeryImportentLogic()
    {
        // logic goes there.
    }
}&lt;/pre&gt;




&lt;p&gt;Only to make the code testeble – we are missing something – this is not the way code should look like. 
&lt;p&gt;There is also the impact on performance of making all our methods as virtual, to be able to override them when we make out mock classes, I agree with Roy Osherove one &lt;a href="http://weblogs.asp.net/rosherove/archive/2005/07/11/418855.aspx"&gt;this on&lt;/a&gt; and we are not dealing here with real-time applications so the impact of the vtable that created when we are performing &lt;a href="http://www.answers.com/topic/type-polymorphism"&gt;polymorphism&lt;/a&gt; (you can read about it from &lt;a href="http://www.krokhmal.com/CommentView,guid,83234cdf-66bc-4a38-aad0-b9a1454af394.aspx#commentstart"&gt;Shimon&amp;#39;s post&lt;/a&gt;) is not so painful. And as Roy explained in his post if this is your pain in the ass you can deal with it. 
&lt;p&gt;I don’t really like the idea of losing the concept of OOP and clear and simple code writing over testable code. 
&lt;p&gt;What do you think?&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=35066" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/Unit+tests/default.aspx">Unit tests</category></item><item><title>TFSBuild – not the right way to do continues integration.</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/10/21/tfsbuild-not-the-right-way-to-do-continues-integration.aspx</link><pubDate>Sun, 21 Oct 2007 04:19:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:28544</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=28544</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/10/21/tfsbuild-not-the-right-way-to-do-continues-integration.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the past few days I been dealing with setting up continues integration (CI) process in my team. The process is not finished yet but be ready for a post at the minute when it will be done. 
&lt;p&gt;CI is dealing with automated builds so I needed some automated build tool to handle the job. No so long ago I have been working with &lt;a href="http://msdn2.microsoft.com/en-us/library/0k6kkbsd.aspx"&gt;MSBuild&lt;/a&gt;, which is very powerful tool to manage your build process. However lately more and more often I run in to TFSBuild (which is a tool for automated build that come with TFS) relative with CI. I have seen tutorials about the tool and it seemed pretty simple. More over it has been written that this tool is based on MSBuild, and MSBuild we familiar with, so what can go wrong?! – In fact many things. 
&lt;p&gt;I have followed the guide lines and with a help of the tutorial I have made a new build type which included getting latest from the TFS source safe into my working directory, building the solution, running the unit tests and running the code analysis. So fare so well. 
&lt;p&gt;After a while I want to add some more tests to my build, which is a common task that every team member has to do at least once a week. Then I realized that there is no easy way to do that, the guide line said – &amp;quot;go to the safe source, check out of the build file, and then change the tests list hard coded&amp;quot; - why someone in my team have to care that there is some kind of build file!? But I said ok, let see where it goes. I have opened the build file and saw this task:&lt;/p&gt;&lt;pre class="code"&gt;&amp;lt;IncludeDataFile Include=&amp;quot;$(SolutionRoot)\MyProject\TestProject.vsmdi&amp;quot;&amp;gt;
    &amp;lt;TestList&amp;gt;MyDalTest&amp;lt;/TestList&amp;gt;
&amp;lt;/IncludeDataFile&amp;gt;&lt;/pre&gt;
&lt;p&gt;When MyDalTest has to be a test list, which is a concept of VS 2005 for testers and not included in common VS 2005. Moreover I don’t want that for every test that someone of my teammate writes, he has to remember to add it to some kind of not understandable test list, and if some one will, one sunny day, forget to do this the test will never run at the CI process. I said &amp;quot;this is not the working process that I been looking for&amp;quot; and went to look for some alternatives. But before this I had to find out how Microsoft runs their test. So I go to the IDE folder in VS root installation folder, where I know Microsoft keep all their goodies, and I have found a command line tool that called MSTest.exe (you can find help about it &lt;a href="http://msdn2.microsoft.com/en-us/library/ms182489(VS.80).aspx"&gt;here&lt;/a&gt;). I was surprised to see that this tool has very powerful ability to run tests in any form I only wish to, form running separate test to running .vsmdi files and entire solutions. &lt;/p&gt;
&lt;p&gt;So as I see it Microsoft was able to make a very powerful tool to run their tests but someway hasn’t included it all features in their build process. 
&lt;p&gt;This disability of the team build running test and some other little thing that I had problem with made me to move to some better and more important more flexible alternatives like &lt;a href="http://confluence.public.thoughtworks.org/display/CCNET/Welcome+to+CruiseControl.NET"&gt;cruise control .net&lt;/a&gt; and standard MSBuild files &lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=28544" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/CI/default.aspx">CI</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/MSbuild/default.aspx">MSbuild</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/Cruise+Control.Net/default.aspx">Cruise Control.Net</category></item><item><title>Mocking internal methods and classes with Rhino.Mocks</title><link>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/10/13/mocking-internal-methods-and-classes-with-rhino-mocks.aspx</link><pubDate>Sat, 13 Oct 2007 04:48:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:27314</guid><dc:creator>Nati Dobkin</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.microsoft.co.il/blogs/ndobkin/rsscomments.aspx?PostID=27314</wfw:commentRss><comments>http://blogs.microsoft.co.il/blogs/ndobkin/archive/2007/10/13/mocking-internal-methods-and-classes-with-rhino-mocks.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;After my trip to Thailand Last month (&lt;a href="http://picasaweb.google.com/ndobkin"&gt;see some photos&lt;/a&gt;) I back and jumped right in to coding. I wrote some code at work and tried to fix some unit tests for it using &lt;a href="http://www.ayende.com/default.aspx"&gt;Rhino Mocks&lt;/a&gt;. I guess you all familiar with that powerful tool and I will not try to explain &lt;a href="http://www.ayende.com/Wiki/(S(pcqrco55t5f20i45d5pzpgar))/Default.aspx?Page=Rhino+Mocks+Documentation"&gt;how to use it&lt;/a&gt;, it been done perfectly by some of my colleagues. 
&lt;p&gt;I wrote a class like this: &lt;pre class="csharpcode"&gt;public class User 
{ 
  private void DoSomeLogic() 
  { } 

  public void Add() 
  { 
    DoSomeLogic(); 
    // ... 
  }
} &lt;/pre&gt;
&lt;div class="csharpcode"&gt;When I came up to write the test I realized that the DoSomeLogic method, which I want to do some mocking and inject custom logic when I about to check the Add method, is not accessible from outside the class. So as good student I go and did what is written to do in such situation and make the DoSomeLogic method internal virtual and of course added: &lt;/div&gt;
&lt;p&gt;[assembly: InternalsVisibleTo(&amp;quot;MyAssembly.Test&amp;quot;)] 
&lt;p&gt;To assemblyInfo file. 
&lt;p&gt;After that I managed to access this method and wrote test that looked like this one: &lt;pre class="csharpcode"&gt;[TestMethod] 
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Add_LogicFlow_Ok() 
{ 
  Rhino.Mocks.MockRepository mocker = &lt;span class="kwrd"&gt;new&lt;/span&gt; MockRepository(); 
  User user = mocker.CreateMock&amp;lt;User&amp;gt;(); 
  &lt;span class="kwrd"&gt;using&lt;/span&gt; (mocker.Record()) 
  { 
    user.DoSomeLogic(); 
  } 
} &lt;/pre&gt;
&lt;p&gt;And my class, after the change, looked like this: &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; User 
{ 
  &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomeLogic() 
  { } 

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddUser() 
  { 
    DoSomeLogic(); 
    &lt;span class="rem"&gt;// ... &lt;/span&gt;
  } 
} &lt;/pre&gt;
&lt;p&gt;I run the test, with hope that it will pass. But, as you know, the life is no so simple and I got an exception that look very strange because it looked like it came from my original DoSomeLogic method - isn’t the user instance is a mock?! 
&lt;p&gt;Before I manage to say &amp;quot;WTF&amp;quot;, which is very sort acronym, I was on debug mode to see what is going on. And in fact the user.DoSomeLogic(); - which is inside my record statement, went directly to my original User.DoSomeLogic() – what is definitely not what I meant to do. 
&lt;p&gt;I try digging and se what is wrong. After a while and with help of &lt;a href="http://blogs.microsoft.co.il/blogs/yoavmichaeli/"&gt;yoav&lt;/a&gt; we came up with conclusion that Rhino can not access this method (because it internal) and cannot override it when it mocking my class. 
&lt;p&gt;A solution to this problem can be made by two ways. 
&lt;p&gt;1. add protected to the definition of the method like this: &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; User 
{ 
  &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DoSomeLogic() 
  { } 

  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddUser() 
  { 
    DoSomeLogic(); 
    &lt;span class="rem"&gt;// ... &lt;/span&gt;
  } 
} &lt;/pre&gt;
&lt;p&gt;2. add [assembly: InternalsVisibleTo(&amp;quot;Rhino.Mocks&amp;quot;)] 
&lt;p&gt;The first solution is missing some basic purpose of what I have intended to do, because in such case you open an opportunity for deriving class to override logic (DoSomeLogic method ) which, as fare as you remember, was private at the beginning. This solution is useful only if the class itself is internal. In that case you anyway need to go to second solution, in order to Rhino could see the class and mock it. So this is what I did. I add the necessary line in to assemblyInfo and added to it the public key of the Rhino assembly (which I get by running command line - &amp;quot;sn –Tp Rhino.mocks.dll&amp;quot;) something simple like this: 
&lt;p&gt;[assembly: InternalsVisibleTo(&amp;quot;Rhino.Mocks, PublicKey=00240000048000009400000006020000002400005253413100040000010001009D1CF4B75B7218B141AC64C15450141B1E5F41F6A302AC717AB9761FA6AE2C3EE0C354C22D0A60AC59DE41FA285D572E7CF33C320AA7FF877E2B7DA1792FCC6AA4EB0B4D8294A2F74CB14D03FB9B091F751D6DC49E626D74601692C99EAB7718ED76A40C36D39AF842BE378B677E6E4EAE973F643D7065241AD86ECC156D81AB&amp;quot;)] 
&lt;p&gt;Unfortunately it hasn&amp;#39;t helped. I still was unable to make a mock of my user type. As you can figure the same problem will appear testing a internal classes. 
&lt;p&gt;After hitting some values at Google I figured that Rhino come with very helpful constant - Rhino.Mocks.RhinoMocks.StrongName which holds the assembly name and public key (which apparently is different from the one that I have found). So after I add this line 
&lt;p&gt;[assembly: InternalsVisibleTo(&amp;quot;DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7&amp;quot;)] 
&lt;p&gt;My user type could be mocked and the test finally passed!&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=27314" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/.net+2.0/default.aspx">.net 2.0</category><category domain="http://blogs.microsoft.co.il/blogs/ndobkin/archive/tags/Unit+tests/default.aspx">Unit tests</category></item></channel></rss>