<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dennis&#039; blog &#187; encapsulation</title>
	<atom:link href="http://dennispiccioni.com/wordpress/archives/tag/encapsulation/feed" rel="self" type="application/rss+xml" />
	<link>http://dennispiccioni.com/wordpress</link>
	<description></description>
	<lastBuildDate>Mon, 19 Apr 2010 22:54:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Encapsulating Code for Easier Maintenance</title>
		<link>http://dennispiccioni.com/wordpress/archives/274</link>
		<comments>http://dennispiccioni.com/wordpress/archives/274#comments</comments>
		<pubDate>Sat, 08 Aug 2009 16:41:54 +0000</pubDate>
		<dc:creator>Dennis</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[encapsulation]]></category>

		<guid isPermaLink="false">http://dennispiccioni.com/wordpress/?p=274</guid>
		<description><![CDATA[Code encapsulation simplifies maintenance. The more abstracted and isolated a routine is from a larger code base, the easier it will be to adjust, test, and if needed, replace later.
This came to mind recently when I wrote a short routine that determines if a mapped drive is a local drive:
[DllImport("mpr.dll")]
static extern uint WNetGetConnection(string lpLocalName, StringBuilder [...]]]></description>
			<content:encoded><![CDATA[<p>Code encapsulation simplifies maintenance. The more abstracted and isolated a routine is from a larger code base, the easier it will be to adjust, test, and if needed, replace later.</p>
<p>This came to mind recently when I wrote a short routine that determines if a mapped drive is a local drive:</p>
<pre>[DllImport("mpr.dll")]
static extern uint WNetGetConnection(string lpLocalName, StringBuilder lpRemoteName, ref int lpnLength);

// returns whether the current drive is local
internal static bool IsLocalDrive(String driveName)
{
    bool isLocal = true;  // assume local until disproved

    // strip trailing backslashes from driveName
    driveName = driveName.Substring(0, 2);

    int length = 256; // to be on safe side
    StringBuilder networkShare = new StringBuilder(length);
    uint status = WNetGetConnection(driveName, networkShare, ref length);

    // does a network share exist for this drive?
    if (networkShare.Length != 0)
    {
        // now networkShare contains a UNC path in format \\MachineName\ShareName
        // retrieve the MachineName portion
        String shareName = networkShare.ToString();
        string[] splitShares = shareName.Split('\\');
        // the 3rd array element now contains the machine name
        if (Environment.MachineName == splitShares[2])
            isLocal = true;
        else
            isLocal = false;
    }

    return isLocal;
}</pre>
<p>There is a <a href="http://stackoverflow.com/questions/1088752/how-to-programmatically-discover-mapped-network-drives-on-system-and-their-server/1224167" target="_blank">post on stackoverflow</a> asking how to do this, and, like many others, it has several answers showing several ways to do this. So, is my method correct? This code seems to work, but I&#8217;ve been programming long enough to know that code that &#8220;seems to work&#8221; doesn&#8217;t necessarily work in every circumstance and could have other negatives, such as performance issues.</p>
<p>So, knowing that this code I may have to tweak or replace at some future point in time, I will try to encapsulate it as much as possible to make it easier to work with in an isolated manner later. By keeping the code in a separate method, I can easily use a unit test to try it in various test scenarios without affecting any other code.</p>
<p>Of course, this is a very simple scenario, which made it perfect for this post. It can be much tougher to abstract and isolate more complex code, but that doesn&#8217;t mean we shouldn&#8217;t strive to do so whenever possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://dennispiccioni.com/wordpress/archives/274/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
