<?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>{ Duane.Wingett } - .Net</title>
    <link>http://www.duanewingett.info/</link>
    <description>.Net Code Monkey</description>
    <language>en-gb</language>
    <copyright>Duane Wingett</copyright>
    <lastBuildDate>Thu, 02 Sep 2010 18:06:44 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>mail@duanewingett.info</managingEditor>
    <webMaster>mail@duanewingett.info</webMaster>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=5967e8bc-786b-4dba-bb90-a68eb3c13017</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,5967e8bc-786b-4dba-bb90-a68eb3c13017.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,5967e8bc-786b-4dba-bb90-a68eb3c13017.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=5967e8bc-786b-4dba-bb90-a68eb3c13017</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Mixed Scope Properties in .Net
</h3>
        <p>
There may be a time when you want to have mixed scope for your "Getters" and "Setters"
in a property. For those of you who are not familiar with the syntax here it is
</p>
        <p>
VB:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Property</span> MyProperty <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">as</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">String</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Get</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Return</span> _myField <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Get</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Set</span> _myField <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> Value <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Set</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Property</span></span>
        </pre>
        <p>
This example allows a public "Getter" and a private "Setter". 
</p>
        <p>
The same can be done in C#:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span> MyProperty
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">get</span> { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span> _myField;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">set</span> {
_myField <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> value;
} }</span>
        </pre>
        <p>
Hope you find that useful.
</p>
        <p>
Regards,
</p>
        <p>
Dib.
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=5967e8bc-786b-4dba-bb90-a68eb3c13017" />
      </body>
      <title>Mixed Scope Properties in .Net</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,5967e8bc-786b-4dba-bb90-a68eb3c13017.aspx</guid>
      <link>http://www.duanewingett.info/2010/09/02/MixedScopePropertiesInNet.aspx</link>
      <pubDate>Thu, 02 Sep 2010 18:06:44 GMT</pubDate>
      <description>&lt;h3&gt;Mixed Scope Properties in .Net
&lt;/h3&gt;
&lt;p&gt;
There may be a time when you want to have mixed scope for your "Getters" and "Setters"
in a property. For those of you who are not familiar with the syntax here it is
&lt;/p&gt;
&lt;p&gt;
VB:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Property&lt;/span&gt; MyProperty &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;as&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;String&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Get&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Return&lt;/span&gt; _myField &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Get&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Set&lt;/span&gt; _myField &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; Value &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Set&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Property&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This example allows a public "Getter" and a private "Setter". 
&lt;/p&gt;
&lt;p&gt;
The same can be done in C#:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt; MyProperty
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;get&lt;/span&gt; { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt; _myField;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;set&lt;/span&gt; {
_myField &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; value;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Hope you find that useful.
&lt;/p&gt;
&lt;p&gt;
Regards,
&lt;/p&gt;
&lt;p&gt;
Dib.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=5967e8bc-786b-4dba-bb90-a68eb3c13017" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,5967e8bc-786b-4dba-bb90-a68eb3c13017.aspx</comments>
      <category>.Net</category>
      <category>Asp.Net</category>
      <category>C#</category>
      <category>Scope</category>
      <category>VB Script</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=3c6c00f1-740c-4bf2-b6ec-3875ad339beb</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,3c6c00f1-740c-4bf2-b6ec-3875ad339beb.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,3c6c00f1-740c-4bf2-b6ec-3875ad339beb.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=3c6c00f1-740c-4bf2-b6ec-3875ad339beb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
VB.Net Single Instance application on Terminal Server 
</p>
        <p>
Yesterday at work I spent nearly the whole day bashing my head against a wall trying
to replicate functionality that our original VB6 application had into the new .Net
conversion. The orginal VB6 application used "<strong>App.PrevInstance</strong>"
to determine if another instance was running or not. This appeared to work for multiple
users on Terminal Servers, so each user could run an instance of the app, but it would
warn if they tried to open another.
</p>
        <p>
VB.Net does not appear to have such direct functionality. Looking on the web provided
various 'solutions' to the problem. However each one seemed to have an 'issue' with
our scenario. By the afternoon I was looking at the <strong>Mutex</strong> objects
which a lot of people seemed to find worked for them. However for me I was getting
an "<span name="subject">Object synchronization method was called from an unsynchronized
block of code</span>" error when closing the second instance onwards.
</p>
        <p>
Looking into the <strong>Mutex</strong> object in a bit more depth it seemed that
maybe it was not the right object for our purpose but maybe the <strong>Semaphore</strong> object
would be better.
</p>
        <p>
In the end the solution I used was to use a named Semaphore. When the application
first instance runs it would attempt to open the named Semaphore and if it did
not exist, assume it was the first instance and create one. The second instance when
run would again look for and open the named Semaphore and if it exists and it manages
to open it then it would assmue it is a second instance of the application.
</p>
        <p>
By prefixing the Semaphorename with "Local\" it will allow each <strong>Terminal Server</strong> session
to have it's own Semaphore so each remote user would not affected by another.
</p>
        <p>
The elements of the code I used are below.
</p>
        <p>
Imports:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Imports</span> System.Threading</span>
        </pre>
        <p>
Declarations:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Private</span> SingleInstanceSemaphore <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span> Semaphore <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Nothing</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
This needs to live for the life of the application</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Const</span> MAX_SESSION_INSTANCES <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Integer</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> 1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Private</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Const</span> SEMAPHORE_NAME <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">String</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"{9A49BB2C-45D2-4bb9-B38E-74F9DB612B96}"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
Unique for each application</span></span>
        </pre>
        <p>
Main Method:
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'''
&lt;summary&gt;</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'''
This is the main application entrance point.</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'''
&lt;/summary&gt;</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Public</span>
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Sub</span> Main() <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
We need a name for the semaphore that is specific to to the Application / User combination</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
We prefix the semaphoreName with "Local\" to allow this to run under terminal services. </span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
The "Local\" prefix forces this into local user space. </span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
If we want to forbid this in TS, use the "Global\" prefix.</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Dim</span> semaphorePrefix <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">String</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Local"</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
Or "Global" if required</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Dim</span> semaphoreName <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">String</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">String</span>.<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Format</span>(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"{0}\{1}{2}"</span>,
semaphorePrefix, SEMAPHORE_NAME, Environment.UserName) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Dim</span> anotherInstanceRunningForUser <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Boolean</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">True</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Dim</span> runAnotherDialogResult <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span> DialogResult <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> DialogResult.No <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Dim</span> mainForm <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span> Form1 <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Nothing</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Try</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Try</span> SingleInstanceSemaphore <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> Semaphore.OpenExisting(semaphoreName) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Catch</span> ex <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span> Exception
anotherInstanceRunningForUser <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">False</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Try</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span> (anotherInstanceRunningForUser <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">False</span>) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Then</span> SingleInstanceSemaphore <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">New</span> Semaphore(0,
MAX_SESSION_INSTANCES, semaphoreName) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span> (anotherInstanceRunningForUser) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Then</span> runAnotherDialogResult <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> MessageBox.Show(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Another
instance is already running. Do you want to run another?"</span>, _<br /><span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Already
Running..."</span>, MessageBoxButtons.YesNo, MessageBoxIcon.Question) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
If this is the first instance of the application, or a further instance but the user
wants more than one...</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span> (anotherInstanceRunningForUser <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">False</span>) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">Or</span> _
((anotherInstanceRunningForUser <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">True</span>) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">And</span> (runAnotherDialogResult <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> DialogResult.Yes)) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Then</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px">'
Run the application</span> mainForm <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">New</span> Form1(mutexName)
Application.Run(mainForm) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">If</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Catch</span> ex <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">As</span> Exception <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">MsgBox</span>(ex.Message) <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">End</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">Try</span></span>
        </pre>
        <p>
I hope that mayhelp someone in the future and save them a day trawling through the
web sticking examples of code together and getting failed results like I did.
</p>
        <p>
Regards,<br />
Dib.
</p>
        <p>
 
</p>
        <font color="#000000" face="Courier New">
        </font>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=3c6c00f1-740c-4bf2-b6ec-3875ad339beb" />
      </body>
      <title>VB.Net Single Instance application on Terminal Server </title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,3c6c00f1-740c-4bf2-b6ec-3875ad339beb.aspx</guid>
      <link>http://www.duanewingett.info/2010/07/30/VBNetSingleInstanceApplicationOnTerminalServer.aspx</link>
      <pubDate>Fri, 30 Jul 2010 07:21:34 GMT</pubDate>
      <description>&lt;p&gt;
VB.Net Single Instance application on Terminal Server 
&lt;/p&gt;
&lt;p&gt;
Yesterday at work I spent nearly the whole day bashing my head against a wall trying
to replicate functionality that our original VB6 application had into the new .Net
conversion. The orginal VB6 application used&amp;nbsp;"&lt;strong&gt;App.PrevInstance&lt;/strong&gt;"
to determine if another instance was running or not. This appeared to work for multiple
users on Terminal Servers, so each user could run an instance of the app, but it would
warn if they tried to open another.
&lt;/p&gt;
&lt;p&gt;
VB.Net does not appear to have such direct functionality. Looking on the web provided
various 'solutions' to the problem. However each one seemed to have an 'issue' with
our scenario. By the afternoon I was looking at the &lt;strong&gt;Mutex&lt;/strong&gt; objects
which a lot of people seemed to find worked for them. However for me I was getting
an "&lt;span name="subject"&gt;Object synchronization method was called from an unsynchronized
block of code&lt;/span&gt;" error when closing the second instance onwards.
&lt;/p&gt;
&lt;p&gt;
Looking into the &lt;strong&gt;Mutex&lt;/strong&gt; object in a bit more depth it seemed that
maybe it was not the right object for our purpose but maybe the &lt;strong&gt;Semaphore&lt;/strong&gt; object
would be better.
&lt;/p&gt;
&lt;p&gt;
In the end the solution I used was to use a named Semaphore. When the application
first instance&amp;nbsp;runs it would attempt to open the named Semaphore and if it did
not exist, assume it was the first instance and create one. The second instance when
run would again look for and open the named Semaphore and if it exists and it manages
to open it then it would assmue it is a second instance of the application.
&lt;/p&gt;
&lt;p&gt;
By prefixing the Semaphorename with "Local\" it will allow each &lt;strong&gt;Terminal Server&lt;/strong&gt; session
to have it's own Semaphore so each remote user would not affected by another.
&lt;/p&gt;
&lt;p&gt;
The elements of the code I used are below.
&lt;/p&gt;
&lt;p&gt;
Imports:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Imports&lt;/span&gt; System.Threading&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Declarations:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Private&lt;/span&gt; SingleInstanceSemaphore &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; Semaphore &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Nothing&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
This needs to live for the life of the application&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Const&lt;/span&gt; MAX_SESSION_INSTANCES &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Integer&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; 1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Private&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Const&lt;/span&gt; SEMAPHORE_NAME &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;String&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"{9A49BB2C-45D2-4bb9-B38E-74F9DB612B96}"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
Unique for each application&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Main Method:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'''
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'''
This is the main application entrance point.&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'''
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Sub&lt;/span&gt; Main() &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
We need a name for the semaphore that is specific to to the Application / User combination&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
We prefix the semaphoreName with "Local\" to allow this to run under terminal services. &lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
The "Local\" prefix forces this into local user space. &lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
If we want to forbid this in TS, use the "Global\" prefix.&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Dim&lt;/span&gt; semaphorePrefix &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;String&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Local"&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
Or "Global" if required&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Dim&lt;/span&gt; semaphoreName &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;String&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;String&lt;/span&gt;.&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Format&lt;/span&gt;(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"{0}\{1}{2}"&lt;/span&gt;,
semaphorePrefix, SEMAPHORE_NAME, Environment.UserName) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Dim&lt;/span&gt; anotherInstanceRunningForUser &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Boolean&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;True&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Dim&lt;/span&gt; runAnotherDialogResult &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; DialogResult &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; DialogResult.No &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Dim&lt;/span&gt; mainForm &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; Form1 &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Nothing&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Try&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Try&lt;/span&gt; SingleInstanceSemaphore &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; Semaphore.OpenExisting(semaphoreName) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Catch&lt;/span&gt; ex &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; Exception
anotherInstanceRunningForUser &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;False&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Try&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; (anotherInstanceRunningForUser &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;False&lt;/span&gt;) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Then&lt;/span&gt; SingleInstanceSemaphore &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;New&lt;/span&gt; Semaphore(0,
MAX_SESSION_INSTANCES, semaphoreName) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; (anotherInstanceRunningForUser) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Then&lt;/span&gt; runAnotherDialogResult &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; MessageBox.Show(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Another
instance is already running. Do you want to run another?"&lt;/span&gt;, _&lt;br&gt;
&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Already
Running..."&lt;/span&gt;, MessageBoxButtons.YesNo, MessageBoxIcon.Question) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
If this is the first instance of the application, or a further instance but the user
wants more than one...&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; (anotherInstanceRunningForUser &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;False&lt;/span&gt;) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;Or&lt;/span&gt; _
((anotherInstanceRunningForUser &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;True&lt;/span&gt;) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;And&lt;/span&gt; (runAnotherDialogResult &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; DialogResult.Yes)) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Then&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: green; FONT-SIZE: 11px"&gt;'
Run the application&lt;/span&gt; mainForm &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;New&lt;/span&gt; Form1(mutexName)
Application.Run(mainForm) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;If&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Catch&lt;/span&gt; ex &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;As&lt;/span&gt; Exception &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;MsgBox&lt;/span&gt;(ex.Message) &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;End&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
I hope that mayhelp someone in the future and save them a day trawling through the
web sticking examples of code together and getting failed results like I did.
&lt;/p&gt;
&lt;p&gt;
Regards,&lt;br&gt;
Dib.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;font color=#000000 face="Courier New"&gt;&lt;/font&gt;&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=3c6c00f1-740c-4bf2-b6ec-3875ad339beb" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,3c6c00f1-740c-4bf2-b6ec-3875ad339beb.aspx</comments>
      <category>.Net</category>
      <category>Mutex</category>
      <category>Semaphore</category>
      <category>Single Instance</category>
      <category>Terminal Server</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I hit hit this error message while filling a typed DataSet Datatable. The odd
thing was I could preview the data in the DataSet designer and the data looked fine.
</p>
        <p>
I had to resort to Google and that returned this page, which if you hit this error,
I would definately recommend. Look specifically for Sanjeys post about six posts down.
</p>
        <p>
        </p>
        <p>
          <a href="http://weblogs.asp.net/rosherove/archive/2004/10/03/DataSet-hell-_2D00_-_2200_Failed-to-enable-constraints.-One-or-more-rows--contain-values_2E002E002E002E002200_.aspx" target="_blank">Roy
Osherove's Blog - Dataset Hell</a>
        </p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be" />
      </body>
      <title>Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be.aspx</guid>
      <link>http://www.duanewingett.info/2010/06/08/FailedToEnableConstraintsOneOrMoreRowsContainValuesViolatingNonnullUniqueOrForeignkeyConstraints.aspx</link>
      <pubDate>Tue, 08 Jun 2010 18:35:03 GMT</pubDate>
      <description>&lt;p&gt;
Today I hit hit this error message while filling a typed DataSet Datatable. The odd
thing was I could preview the data in the DataSet designer and the data looked fine.
&lt;/p&gt;
&lt;p&gt;
I had to resort to Google and that returned this page, which if you hit this error,
I would definately recommend. Look specifically for Sanjeys post about six posts down.&lt;p&gt;
&lt;p&gt;
&lt;a href="http://weblogs.asp.net/rosherove/archive/2004/10/03/DataSet-hell-_2D00_-_2200_Failed-to-enable-constraints.-One-or-more-rows--contain-values_2E002E002E002E002200_.aspx" target="_blank"&gt;Roy
Osherove's Blog - Dataset Hell&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,3dbc6ea6-9e1c-42a5-a075-930d2c9ac0be.aspx</comments>
      <category>.Net</category>
      <category>Asp.Net</category>
      <category>C#</category>
      <category>Database</category>
      <category>Errors</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=c106d15a-bb0f-40d1-84a9-bc443c83c2c1</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,c106d15a-bb0f-40d1-84a9-bc443c83c2c1.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,c106d15a-bb0f-40d1-84a9-bc443c83c2c1.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=c106d15a-bb0f-40d1-84a9-bc443c83c2c1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Since Christmas I have been filling some fo my spare time working on a website for
a game for family and friends that my girlfriend's family play. This has now been
released and can be seen. here:
</p>
        <p>
          <a href="http://www.coi.me.uk/">The Coconut Of Incontinence</a>
        </p>
        <p>
        </p>
        <p>
The site is written in Asp.Net 2.0
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=c106d15a-bb0f-40d1-84a9-bc443c83c2c1" />
      </body>
      <title>Coconut If Incontinence website Launched</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,c106d15a-bb0f-40d1-84a9-bc443c83c2c1.aspx</guid>
      <link>http://www.duanewingett.info/2010/04/06/CoconutIfIncontinenceWebsiteLaunched.aspx</link>
      <pubDate>Tue, 06 Apr 2010 12:36:23 GMT</pubDate>
      <description>&lt;p&gt;
Since Christmas I have been filling some fo my spare time working on a website for
a game for family and friends that my girlfriend's family play. This has now been
released and can be seen. here:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.coi.me.uk/"&gt;The Coconut Of Incontinence&lt;/a&gt;
&lt;p&gt;
&lt;p&gt;
The site is written in Asp.Net 2.0
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=c106d15a-bb0f-40d1-84a9-bc443c83c2c1" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,c106d15a-bb0f-40d1-84a9-bc443c83c2c1.aspx</comments>
      <category>.Net</category>
      <category>Coconut Of Incontinece</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=099967a0-f566-4bc6-a89d-ec89c35450b3</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,099967a0-f566-4bc6-a89d-ec89c35450b3.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,099967a0-f566-4bc6-a89d-ec89c35450b3.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=099967a0-f566-4bc6-a89d-ec89c35450b3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Another discovery today, courtesy of <strong>Google</strong> finding <a href="http://www.wintellect.com/cs/blogs/rrobinson/archive/2008/01/18/a-better-way-to-remove-a-trailing-slash-from-a-path.aspx"><strong>Rik
Robinson's Blog</strong></a> is <strong><font face="Courier New">String.TrimStart()</font></strong> and <strong><font face="Courier New">String.TrimEnd()</font></strong>.
</p>
        <p>
To trim a leading slash of a string or path use:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">myString <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> myString.TrimStart(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">char</span>[]{'\\',
'<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span>'}); </span>
        </pre>
        <p>
 
</p>
        <p>
To trim a trailing slash of a string or path use:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">myString <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> myString.TrimEnd(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">char</span>[]{'\\',
'<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span>'}); </span>
        </pre>
        <p>
 
</p>
        <p>
I had previously been using a much more long-winded approach. Cheers Rik for bringing
that to my attention.
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=099967a0-f566-4bc6-a89d-ec89c35450b3" />
      </body>
      <title>System.String functions: Strip leading or trailing slash of path or string</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,099967a0-f566-4bc6-a89d-ec89c35450b3.aspx</guid>
      <link>http://www.duanewingett.info/2008/11/15/SystemStringFunctionsStripLeadingOrTrailingSlashOfPathOrString.aspx</link>
      <pubDate>Sat, 15 Nov 2008 11:17:15 GMT</pubDate>
      <description>&lt;p&gt;
Another discovery today, courtesy of &lt;strong&gt;Google&lt;/strong&gt; finding &lt;a href="http://www.wintellect.com/cs/blogs/rrobinson/archive/2008/01/18/a-better-way-to-remove-a-trailing-slash-from-a-path.aspx"&gt;&lt;strong&gt;Rik
Robinson's Blog&lt;/strong&gt;&lt;/a&gt;&amp;nbsp;is &lt;strong&gt;&lt;font face="Courier New"&gt;String.TrimStart()&lt;/font&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;font face="Courier New"&gt;String.TrimEnd()&lt;/font&gt;&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
To trim a leading slash of a string or path use:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;myString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; myString.TrimStart(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;char&lt;/span&gt;[]{'\\',
'&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt;'}); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
To trim a trailing slash of a string or path use:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;myString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; myString.TrimEnd(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;char&lt;/span&gt;[]{'\\',
'&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt;'}); &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
I had previously been using a much more long-winded approach. Cheers Rik for bringing
that to my attention.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=099967a0-f566-4bc6-a89d-ec89c35450b3" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,099967a0-f566-4bc6-a89d-ec89c35450b3.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=a1d7e01d-43f6-4d23-9eb2-477a8e94c255</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,a1d7e01d-43f6-4d23-9eb2-477a8e94c255.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,a1d7e01d-43f6-4d23-9eb2-477a8e94c255.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=a1d7e01d-43f6-4d23-9eb2-477a8e94c255</wfw:commentRss>
      <title>Separate database connections for TableAdapter for individual website user roles</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,a1d7e01d-43f6-4d23-9eb2-477a8e94c255.aspx</guid>
      <link>http://www.duanewingett.info/2008/10/26/SeparateDatabaseConnectionsForTableAdapterForIndividualWebsiteUserRoles.aspx</link>
      <pubDate>Sun, 26 Oct 2008 15:34:07 GMT</pubDate>
      <description>&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;It’s been quite a while since I
have written anything (yes I really need to finish the last article!), but this morning
I came across a problem where I need to provide separate database connections for
different website visitors who will be using the same underlying .Net TableAdapters.
So I thought I’d write about it.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;&lt;/font&gt;&amp;nbsp;
&lt;/p&gt;
&lt;h3 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Background:
&lt;/h3&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;I have set up my Sql Server 2005
database to have four roles; DbAdmin, WebAdmin, ProductSeller, ProductBuyer. Each
of these roles has been given different levels of read / write / execute permission
on the various database objects. It is my intention to let the built in .Net 2.0 Membership
and Roles objects handle assigning of roles (mirroring the four database roles) to
all of the website visitors. &lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;As far as I know to make use the
database roles properly I will need four separate database logins and database users,
and hence in my application I will need four different connection strings.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;&lt;/font&gt;&amp;nbsp;
&lt;/p&gt;
&lt;h3 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Problem:
&lt;/h3&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
The problem is that I could not see any exposed properties of a table adapter to allow
the connection to be changed dynamically.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h3 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;My solution:
&lt;/h3&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
My solution is to set up the application to have four connection strings for the four
database logins, and create an overridden constructor for each TableAdapter that requires
role specific database connections.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Setting up the database
&lt;/h4&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
For the purpose of this example I have created the following database logins.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;table class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing=0 cellpadding=0 border=1&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: silver; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 167.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt" valign=top width=223&gt;
&lt;p class=TableHeader style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;strong&gt;&lt;font face=Arial&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;Login&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: silver; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 186pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=248&gt;
&lt;p class=TableHeader style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;strong&gt;&lt;font face=Arial&gt;Login Name&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: silver; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 176.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=236&gt;
&lt;p class=TableHeader style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;strong&gt;&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;font face=Arial&gt;Password&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 167.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=223&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Database administrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 186pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=248&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;MyWesite_DbAdministrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 176.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=236&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;da1&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 167.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=223&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Website administrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 186pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=248&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;MyWesite_WebAdministrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 176.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=236&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;wa1&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 167.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=223&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Product Seller&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 186pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=248&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;MyWesite_ProductSeller&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 176.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=236&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;ps1&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 4; mso-yfti-lastrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 167.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=223&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font color=#000000&gt;Product Buyer&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 186pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=248&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;MyWesite_ProductBuyer&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 176.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=236&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;pb1&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;/font&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt;The database roles I have created
are as follows.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;table class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing=0 cellpadding=0 border=1&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: silver; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 104.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt" valign=top width=139&gt;
&lt;p class=TableHeader style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;strong&gt;&lt;font face=Arial&gt;Role name&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: silver; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 321.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=429&gt;
&lt;p class=TableHeader style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;strong&gt;&lt;font size=2&gt;&lt;font color=#000000&gt;&lt;font face=Arial&gt;Description&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 104.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=139&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;DatabaseAdmin&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 321.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=429&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;font color=#000000&gt;The role assigned to the database administrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 104.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=139&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;WebsiteAdmin&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 321.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=429&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;font color=#000000&gt;The role assigned to the web site administrator&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 104.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=139&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;ProductSeller&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 321.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=429&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;font color=#000000&gt;The role assigned to users who can list a product on the site.&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 4; mso-yfti-lastrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 104.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=139&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;span class=ProgramCode&gt;&lt;span style="FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Times New Roman'"&gt;&lt;font color=#333333&gt;&lt;font face="Courier New"&gt;ProductBuyer&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 321.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=429&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-pagination: widow-orphan lines-together"&gt;
&lt;font color=#000000&gt;The role given to public users of the site&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Application Architecture Overview:&lt;font face="Times New Roman" color=#000000 size=3&gt;
&lt;/h4&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
The application is written in a layered architecture with the web front end having
visibility of the Interfaces layer and the Services layer, and the Services layer
having visibility of the Interfaces layer, the Business Entities layer, and the Data
Access layer. The data access layer contains all the typed datasets and any other
database related objects.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Connection strings:
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
The four connection strings are entered into the &lt;strong&gt;Setting&lt;/strong&gt;s tab of
the &lt;strong&gt;Properties &lt;/strong&gt;page of the &lt;strong&gt;DataAccess &lt;/strong&gt;project.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;table class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellspacing=0 cellpadding=0 border=1&gt;
&lt;tbody&gt;
&lt;tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;&lt;/b&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Name&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Type&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Scope&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;b style="mso-bidi-font-weight: normal"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Value&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/b&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 1"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;&amp;gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;DatabaseAdminCon…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;(Connection…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Application&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Data Source=…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 2"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;WebsiteAdminConn…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;(Connection…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Application&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Data Source=…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 3"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;ProductSellerConne…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;(Connection…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Application&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Data Source=…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 4"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;ProductBuyerConne…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;(Connection…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Application&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;Data Source=…&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr style="mso-yfti-irow: 5; mso-yfti-lastrow: yes"&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 32.4pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=43&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-FAMILY: Arial"&gt;&lt;font color=#000000&gt;*&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 126pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=168&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 97.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=130&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;td style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #ebe9ed; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: #ebe9ed; WIDTH: 85.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" valign=top width=114&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;span style="FONT-FAMILY: Arial"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Creating the connection
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
As we need to provide a role specific connection to the table adapter we need an object
that can create and return a connection.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Data.SqlClient; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; ConnectionStrings &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; MyProject.DataAccess.Properties.Settings; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; MyProject.DataAccess
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; DatabaseConnection
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Returns a connection for use with the role name passed in&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="roleName"&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the name of the role to get connection string for.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Returns a Sql Server connection.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; SqlConnection
GetConnectionForRole(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; roleName)
{ SqlConnection connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;.Empty;
connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetConnectionStringForRole(roleName);
connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; SqlConnection(connectionString); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; connection;
} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Returns a connection string based on the passed in rolename.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="roleName"&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the name of the role to get connection string for.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; GetConnectionStringForRole(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; roleName)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;.Empty; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;switch&lt;/span&gt; (roleName)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"DatabaseAdmin"&lt;/span&gt;:
connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ConnectionStrings.Default.DatabaseAdminConnectionString; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"WebsiteAdmin"&lt;/span&gt;:
connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ConnectionStrings.Default.WebsiteAdminConnectionString; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ProductSeller"&lt;/span&gt;:
connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ConnectionStrings.Default.ProductSellerConnectionString; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"ProductBuyer"&lt;/span&gt;:
connectionString &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ConnectionStrings.Default.ProductBuyerConnectionString; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;default&lt;/span&gt;: &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; ArgumentOutOfRangeException(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"roleName"&lt;/span&gt;, &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unknown
role name encountered."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; connectionString;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Default Constructor:
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
Next we need to look at the TableAdapter and create a constructor that overrides the
default non parametricised constructor. The default constructor for our table adapter
looks like this.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; MyTableAdapter()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.ClearBeforeFill &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;true&lt;/span&gt;;
} &lt;/span&gt;&lt;/pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;New Constructor:
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
To override this constructor we are going to take advantage of .Net 2.0’s partial
classes. Using these we do not have to modify the auto-generated code of the dataset,
and neither do we have to attempt to create a class that inherits from the dataset.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Data.SqlClient; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; MyProject.DataAccess.ApplicationDataSetTableAdapters
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; MyTableAdapter:
System.ComponentModel.Component { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Used to provide an overridden constructor for the MyTableAdapter&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
allowing a connection to be passed in.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="sqlConnection"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt; [System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; MyTableAdapter
(SqlConnection sqlConnection) { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.ClearBeforeFill &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;true&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; sqlConnection;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
We take care to set the connection via the “Connection” property rather than just
setting the _connection fields as the auto-generated "Connection"&amp;nbsp;property ensure
all adapter commands get the correct connection assigned to them, see below.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[System.Diagnostics.DebuggerNonUserCodeAttribute()] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;internal&lt;/span&gt; System.Data.SqlClient.SqlConnection
Connection { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; ((&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;._connection
== &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;))
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.InitConnection();
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;._connection;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;._connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; ((&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.InsertCommand
!&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;))
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.InsertCommand.Connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; ((&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.DeleteCommand
!&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;))
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.DeleteCommand.Connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; ((&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.UpdateCommand
!&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;))
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.Adapter.UpdateCommand.Connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
(i &amp;lt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.CommandCollection.Length);
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;+&lt;/span&gt; 1))
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;if&lt;/span&gt; ((&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.CommandCollection[i]
!&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;))
{ ((System.Data.SqlClient.SqlCommand)(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;this&lt;/span&gt;.CommandCollection[i])).Connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } } } &lt;/span&gt;&lt;/pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Initialising the TableAdapter
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
So now the code for accessing the adapter should look something like this.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Used to add an object that implements the IMyItem interface to the database.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="product"&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the object implementing the IMyItem interface to add to the database.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; IMyItem.AddToDatabase(IMyItem
product) { ApplicationAdapters.MyTableAdapter myTableAdapter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;;
SqlConnection connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;try&lt;/span&gt; {
connection &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; SqlServerConnectionService.GetConnectionForRole(RoleName);
myTableAdapter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; ApplicationAdapters.
MyTableAdapter (connection); MyTableAdapter.Add(product.Id, product.Name); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;catch&lt;/span&gt; (Exception
ex) { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; ex;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;finally&lt;/span&gt; {
SqlServerConnectionService.CloseConnection(connection); connection.Dispose(); myTableAdapter.Dispose();
} } &lt;/span&gt;&lt;/pre&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Connection Handling:
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
This code still allows the adapter to open and close the connection as necessary,
we have just provided the connection we want. This approach has allowed me to ensure
that a user logged in as a ProductBuyer should not have the permission to call procedures
they shouldn’t even if I end up with a bug in the website code that allows a non authorised
user to reach an administration page with add / update / delete functions. 
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
As always your opinions and comments on this subject or the approach taken to overcome
the problem are most welcome. I hope some one may find this useful.
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;
&lt;/p&gt;
&lt;/font&gt; 
&lt;h4 class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;Foot Note:
&lt;/h4&gt;
&lt;font face="Times New Roman" color=#000000 size=3&gt; 
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
Some error handling in code has been omitted for sake of clarity
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;/font&gt;&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=a1d7e01d-43f6-4d23-9eb2-477a8e94c255" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,a1d7e01d-43f6-4d23-9eb2-477a8e94c255.aspx</comments>
      <category>.Net</category>
      <category>Asp.Net</category>
      <category>C#</category>
      <category>Sql Server</category>
      <category>Connection Strings</category>
      <category>Database</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=f40ec3ec-6713-43ec-8007-f3b4ade7b1d8</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,f40ec3ec-6713-43ec-8007-f3b4ade7b1d8.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,f40ec3ec-6713-43ec-8007-f3b4ade7b1d8.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=f40ec3ec-6713-43ec-8007-f3b4ade7b1d8</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm sure many of you are aware that you can override the connection string settings
in your <strong>DataAccess</strong> layer of your web application with connection
string settings in your <strong>Web.Config</strong>. We found this out recently and
found it very useful in an intranet application at work recently. However this week
one of the other developers required that he could override a setting the <strong>BusinessEntity</strong> layer
of his application from the <strong>App.Config</strong> in the console layer.
</p>
        <p>
A quick chat with Mr Google didn't turn up a solution for this, so with this in mind
all three of us quickly tried to return a working solution. I'm going to try to give
an example of what we did, incase it something others may find of use. First we create
a solution with two class library projects within it. The first will simulate the <strong>DataAccess</strong> layer
and the second will simulate the <strong>BusinessEntity</strong> layer in an application.
In the <strong>BusinessEntity</strong> project we will create a reference to the <strong>DataAccess</strong> project.
</p>
        <p>
Starting with the <strong>DataAccess</strong> project we will add an <strong>App.Config</strong> file
by creating a setting called "MySetting" in the <strong>Settings</strong> tab
of the project <strong>Properties</strong>. The setting scope will be set to
Application.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/ConfigurationOverride_Settings.jpg" border="0" />
        </p>
        <p>
This creates a new <strong>sectionGroup</strong> called <strong>applicationSettings</strong> and
a new section in the <strong>App.Config</strong> file and places the new setting in
their too.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: #966633; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;?xml <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">version</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"1.0"</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">encoding</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"utf-8"</span> ?&gt;
&lt;configuration&gt; &lt;configSections&gt; &lt;sectionGroup <font color="#ff0000">name</font>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"applicationSettings"</span><br /><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">type</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"System.Configuration.ApplicationSettingsGroup,
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span> &gt;
&lt;section <font color="#ff0000">name</font>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Playground.ConfigurationOverride.DataAccess.Properties.Settings"</span><br /><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">type</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"System.Configuration.ClientSettingsSection,
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span><br /><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">requirePermission</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"false"</span> /&gt;
&lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;applicationSettings&gt; &lt;Playground.ConfigurationOverride.DataAccess.Properties.Settings&gt;
&lt;setting <font color="#ff0000">name</font>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"MySetting"</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">serializeAs</span>=<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"String"</span>&gt;
&lt;value&gt;Setting in DataAccess&lt;/value&gt; &lt;/setting&gt; &lt;/Playground.ConfigurationOverride.DataAccess.Properties.Settings&gt;
&lt;/applicationSettings&gt; &lt;/configuration&gt;</span>
        </pre>
        <p>
Next we will add a simple class to the DataAccess project with a single static method
that will return the value of the setting we have just created.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ConfigurationOverride.DataAccess
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> SettingsHelper
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> GetMySetting()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> Properties.Settings.Default.MySetting;
} } }</span>
        </pre>
        <p>
Next we will create anothe simple class, <strong>SettingsGetter</strong>, in
the but in the <strong>BusinessEntity</strong> layer this time. It will just return
the value returned from the <strong>SettingsHelper</strong> class in the <strong>DataAccess</strong> layer.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ConfigurationOverride.BusinessEntity
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> SettingsGetter
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> GetDataAccessSettingValue()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> DataAccess.SettingsHelper.GetMySetting();
} } }</span>
        </pre>
Next we need to create a presentation layer. So first off we will create a small web
application and add it to the solution, and grab a reference to the <strong>BusinessEntity</strong> layer.
Set the web site as the startup project. We will also add a <strong>Label</strong> to
the default webform and a <strong>Button</strong>. In the <strong>Page_Load</strong> handler
method populate the label text from the business entity method.<pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Page_Load(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
EventArgs e) { uxSettingsLabel.Text <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Playground.ConfigurationOverride.BusinessEntity.SettingsGetter.GetDataAccessSettingValue();
}</span></pre><p>
...and run the application, and allow the <strong>Web.Config</strong> file to be created.
The value of the setting in the <strong>DataAccess</strong><strong>App.Config</strong> file
should be displayed.
</p><p>
 
</p><p><font size="1">More to follow soon...</font></p><img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=f40ec3ec-6713-43ec-8007-f3b4ade7b1d8" /></body>
      <title>Override App.Config in the DataAccess layer with Web.Config or App.Config in the presentation layer.</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,f40ec3ec-6713-43ec-8007-f3b4ade7b1d8.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/12/OverrideAppConfigInTheDataAccessLayerWithWebConfigOrAppConfigInThePresentationLayer.aspx</link>
      <pubDate>Sat, 12 Apr 2008 19:03:24 GMT</pubDate>
      <description>&lt;p&gt;
I'm sure many of you are aware that you can override the connection string settings
in your &lt;strong&gt;DataAccess&lt;/strong&gt; layer of your web application with connection
string settings in your &lt;strong&gt;Web.Config&lt;/strong&gt;. We found this out recently and
found it very useful in an intranet application at work recently. However this week
one of the other developers required that he could override a setting the &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer
of his application from the &lt;strong&gt;App.Config&lt;/strong&gt; in the console layer.
&lt;/p&gt;
&lt;p&gt;
A quick chat with Mr Google didn't turn up a solution for this, so with this in mind
all three of us quickly tried to return a working solution. I'm going to try to give
an example of what we did, incase it something others may find of use. First we create
a solution with two class library projects within it. The first will&amp;nbsp;simulate&amp;nbsp;the &lt;strong&gt;DataAccess&lt;/strong&gt; layer
and the second will simulate the &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer in an application.
In the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project we will create a reference to the &lt;strong&gt;DataAccess&lt;/strong&gt; project.
&lt;/p&gt;
&lt;p&gt;
Starting with the &lt;strong&gt;DataAccess&lt;/strong&gt; project we will add an &lt;strong&gt;App.Config&lt;/strong&gt; file
by&amp;nbsp;creating a setting called "MySetting"&amp;nbsp;in the &lt;strong&gt;Settings&lt;/strong&gt; tab
of the project &lt;strong&gt;Properties&lt;/strong&gt;. The setting&amp;nbsp;scope will be set to
Application.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ConfigurationOverride_Settings.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
This creates a new &lt;strong&gt;sectionGroup&lt;/strong&gt; called &lt;strong&gt;applicationSettings&lt;/strong&gt; and
a new section in the &lt;strong&gt;App.Config&lt;/strong&gt; file and places the new setting in
their too.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: #966633; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;lt;?xml &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;version&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"1.0"&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;encoding&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"utf-8"&lt;/span&gt; ?&amp;gt;
&amp;lt;configuration&amp;gt; &amp;lt;configSections&amp;gt; &amp;lt;sectionGroup &lt;font color=#ff0000&gt;name&lt;/font&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"applicationSettings"&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;type&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"System.Configuration.ApplicationSettingsGroup,
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&lt;/span&gt; &amp;gt;
&amp;lt;section &lt;font color=#ff0000&gt;name&lt;/font&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Playground.ConfigurationOverride.DataAccess.Properties.Settings"&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;type&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"System.Configuration.ClientSettingsSection,
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;requirePermission&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"false"&lt;/span&gt; /&amp;gt;
&amp;lt;/sectionGroup&amp;gt; &amp;lt;/configSections&amp;gt; &amp;lt;applicationSettings&amp;gt; &amp;lt;Playground.ConfigurationOverride.DataAccess.Properties.Settings&amp;gt;
&amp;lt;setting &lt;font color=#ff0000&gt;name&lt;/font&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"MySetting"&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;serializeAs&lt;/span&gt;=&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"String"&lt;/span&gt;&amp;gt;
&amp;lt;value&amp;gt;Setting in DataAccess&amp;lt;/value&amp;gt; &amp;lt;/setting&amp;gt; &amp;lt;/Playground.ConfigurationOverride.DataAccess.Properties.Settings&amp;gt;
&amp;lt;/applicationSettings&amp;gt; &amp;lt;/configuration&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Next we will add a simple class to the DataAccess project with a single static method
that will return the value of the setting we have just created.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ConfigurationOverride.DataAccess
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; SettingsHelper
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; GetMySetting()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; Properties.Settings.Default.MySetting;
} } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Next we will create anothe simple class, &lt;strong&gt;SettingsGetter&lt;/strong&gt;,&amp;nbsp;in
the but in the &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer this time. It will just return
the value returned from the &lt;strong&gt;SettingsHelper&lt;/strong&gt; class in the &lt;strong&gt;DataAccess&lt;/strong&gt; layer.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ConfigurationOverride.BusinessEntity
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; SettingsGetter
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; GetDataAccessSettingValue()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; DataAccess.SettingsHelper.GetMySetting();
} } }&lt;/span&gt;&lt;/pre&gt;
Next we need to create a presentation layer. So first off we will create a small web
application and add it to the solution, and grab a reference to the &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer.
Set the web site as the startup project. We will also add a &lt;strong&gt;Label&lt;/strong&gt; to
the default webform and a &lt;strong&gt;Button&lt;/strong&gt;. In the &lt;strong&gt;Page_Load&lt;/strong&gt; handler
method populate the label text from the business entity method.&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Page_Load(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
EventArgs e) { uxSettingsLabel.Text &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Playground.ConfigurationOverride.BusinessEntity.SettingsGetter.GetDataAccessSettingValue();
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
...and run the application, and allow the &lt;strong&gt;Web.Config&lt;/strong&gt; file to be created.
The value of the setting in the &lt;strong&gt;DataAccess&lt;/strong&gt; &lt;strong&gt;App.Config&lt;/strong&gt; file
should be displayed.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;More to follow soon...&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=f40ec3ec-6713-43ec-8007-f3b4ade7b1d8" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,f40ec3ec-6713-43ec-8007-f3b4ade7b1d8.aspx</comments>
      <category>.Net</category>
      <category>Asp.Net</category>
      <category>C#</category>
      <category>Config Files</category>
      <category>XML</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Back to <strong><a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx">Part
2</a></strong></p>
        <h3>Review Of Progress So Far
</h3>
        <p>
So, to date we have created an interface for our <strong>ShapeFactory</strong> and
any other shape factory to implement, an enumeration that represents all the available
shapes we may wish to build, and a factory class that can build any of our two
current shapes. The next step is to create a graphical user interface that will call
the factory and receive a couple of shape objects for it's trouble.
</p>
        <h3>The Client Gets What They Deserve
</h3>
        <p>
Next we need a client to display the shapes, so for simplicity we are going to create
a small Windows Forms project called <strong>WindowsApplication </strong>and add it
to the solution. The project will have a single form called <strong>Main</strong> (700x550)
which will have a picture box (<strong>uxPictureBox</strong>) and a button, <strong>uxDrawButton</strong>.
</p>
        <p>
We now need to add a handler for the Draw <strong>Button</strong> and declare a <strong>Bitmap</strong> and
a <strong>Graphics</strong> object at the top of the class. In the form constructor
we will initialise the bitmap to a new instance which will be the size of the <strong>PictureBox</strong>.
We will then create the <strong>Graphics</strong> object from the <strong>Bitmap</strong>.
</p>
        <p>
In the button handler method we will clear the graphics object to a white background.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> partial <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Main
: Form { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Bitmap</font> _bitmap; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Graphics</font> _graphics; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{ InitializeComponent(); _bitmap <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Bitmap</font>(uxPictureBox.Width,
uxPictureBox.Height); _graphics <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><font color="#7fffd4">Graphics</font>.FromImage(_bitmap);
uxPictureBox.Image <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> _bitmap;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> uxDrawButton_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
EventArgs e) { _graphics.Clear(Color.White); } }</span>
        </pre>
        <p>
We now need a collection to hold our shapes, so we will declare a list of objects
that implement IShape at the top of the class, and to do this we will need to provide
a reference to the <strong>ShapeInterfaces</strong> project. At the same time add
a reference to the <strong>Factories</strong> project and import the namespaces for
both projects at the top of the form code.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Text; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Windows.Forms; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.Factories; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.WindowsApplication
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> partial <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Main
: Form { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Bitmap</font> _bitmap; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Graphics</font> _graphics; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> List&lt;<font color="#7fffd4">IShape</font>&gt;
_shapes; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{</span>
        </pre>
        <p>
Next we need to add two method stubs that are void of return values, <strong>AddShapes</strong>()
and <strong>DrawShapes</strong>(). Surprisingly, <strong>AddShapes</strong>() will take
responsibility for adding new shapes to our list and <strong>DrawShapes</strong>()
will draw those shapes. We will add a line to create an instance of our list in the
constructor and below that we will place a call to the <strong>AddShapes</strong>()
method. We will place a call in the button handler to the <strong>DrawShapes</strong>()
method, below the line that clears the graphics to a white background. Follow this
with a refresh of the PictureBox.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{ InitializeComponent(); _bitmap <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Bitmap(uxPictureBox.Width,
uxPictureBox.Height); _graphics <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Graphics.FromImage(_bitmap);
uxPictureBox.Image <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> _bitmap;
_shapes <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> List&lt;IShape&gt;();
AddShapes(); } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Handles the draw button pressed event.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="sender"&gt;&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="e"&gt;&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> uxDrawButton_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
EventArgs e) { _graphics.Clear(Color.White); DrawShapes();<br />
uxPictureBox.Refresh(); }</span>
        </pre>
        <p>
In the <strong>AddShapes</strong>() method we need to create a new instance of our <strong>ShapeFactory</strong> object.
Then using the <strong>ShapeFactory</strong> object add two shapes, a circle and a
rectangle to the list.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Adds shapes to the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> AddShapes()
{ <font color="#7fffd4">ShapeFactory</font> shapeFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">ShapeFactory</font>();
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Circle));
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Rectangle));
}</span>
        </pre>
        <p>
Because we know that the shapes Implement the <strong>IShape</strong> interface we
have a contract that says we can call the <strong>Draw</strong>() methods on any of
these shapes. These objects will then take the responsibility for drawing them selves.
So in the <strong>DrawShapes</strong>() method all we need to do is iterate through
all the shapes in the list and call each shapes <strong>Draw</strong>() method.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Draws the shapes in the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> DrawShapes()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span>(IShape
shape <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> _shapes)
{ shape.Draw(); } }</span>
        </pre>
        <p>
And that would be it... apart from we haven't added any working code into the Draw()
methods of each concrete object yet!
</p>
        <h3>A Change In The Contract
</h3>
        <p>
Apart from the fact there is no code in the concrete objects to actually draw them
selves, I have just noticed the need to be able to pass the <strong>Graphics</strong> object
into each shape object, so we need to go back to the interfaces and change the signature
for the <strong>Draw</strong>() method.
</p>
        <p>
So if we take a look in the <strong>IShape</strong> interface we need to be able to
pass in a <strong>Graphics</strong> object as a method parameter.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShape
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the center of the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Point Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The method used to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="graphics"&gt;A reference to the Graphics object to draw the shape
on.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics); }</span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          </span>
        </pre>
        <p>
And while we are in this project I think we will take the opportunity to extend the <strong>CreateShape</strong>()
method signature in the <strong>IShapeFactory</strong> interface, too. We are going
to add a center point parameter; this will allow us to position the shape where we
like.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShapeFactory
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the type of shape to be made.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="center"&gt;Indicates the center point of the shape.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape, <font color="#7fffd4">Point</font> center);
}</span>
        </pre>
        <p>
A quick build of the project will highlight where our change in interface has broken
the contract between it and the objects that implement it so in the <strong>BaseShape</strong> object
we need to add a <strong>Graphics</strong> parameter into the <strong>Draw</strong>()
method. This will be the same in the <strong>Rectangle</strong> and the <strong>Circle</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(<font color="#7fffd4">Graphics</font> graphics);</span>
        </pre>
        <p>
In the <strong>ShapeFactory</strong> class we need to extend the <strong>CreateShape</strong>(), <strong>CreateCircle</strong>()
and <strong>CreateRectangle</strong>() methods signatures to include the center point
parameter. Then in the <strong>CreateShape</strong>() method we need to pass the center
point parameter on to the worker method calls.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IShape.&lt;/returns&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> IShape
CreateShape(Shape shape, Point center) {</span>
        </pre>
        <p>
In the two worker methods, CreateCircle() and CreateRectangle() we will populate the
object's Center property with the value from the center parameter passed in.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <font color="#7fffd4">ICircle</font> CreateCircle(Point
center) { <font color="#7fffd4">Circle</font> circle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Circle</font>();
circle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> center;
circle.Diameter <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 50; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">ICircle</font>)circle;
} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle(Point
center) { BusinessEntity.<font color="#7fffd4">Rectangle</font> rectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> BusinessEntity.<font color="#7fffd4">Rectangle</font>();
rectangle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> center;
rectangle.Size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Size(400,
200); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">IRectangle</font>)rectangle;
}</span>
        </pre>
        <p>
Then back in the form code we need to alter the calls to the <strong>ShapeFactory</strong><strong>CreateShape</strong>()
methods to include a center position
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Adds shapes to the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> AddShapes()
{ <font color="#7fffd4">ShapeFactory</font> shapeFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">ShapeFactory</font>();
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Circle, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(250,250)));
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Rectangle, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(350,
200))); } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Draws the shapes in the list</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> DrawShapes()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span>(<font color="#7fffd4">IShape</font> shape <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> _shapes)
{ shape.Draw(_graphics); } }</span>
        </pre>
        <h3>Back to the Drawing Board
</h3>
        <p>
So finally we can get back into the concrete shape classes and write the code that
will draw the shape.
</p>
        <p>
So let us look at the Circle's <strong>Draw</strong>() method first. We'll add
the code to allow the Circle to draw it's self.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics) { Pen pen <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Pen(Color.DarkGray,
0); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> left <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.X <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Diameter/2); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> top <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.Y <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Diameter/2);
System.Drawing.Rectangle outlineRectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> System.Drawing.Rectangle(left,
top, Diameter, Diameter); graphics.DrawEllipse(pen, outlineRectangle); }</span>
        </pre>
        <p>
And the same for the Rectangle.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics) { Pen pen <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Pen(Color.Red,
0); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> left <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.X <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Size.Width <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span> 2); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> top <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.Y <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Size.Height <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span> 2);
System.Drawing.Rectangle outlineRectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> System.Drawing.Rectangle(left,
top, Size.Width, Size.Height); graphics.DrawRectangle(pen, outlineRectangle); }</span>
        </pre>
        <p>
Now we need to make sure we have set the start up project to be the windows application
and we should be good to go!
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/ShapeMaker_Form_01.jpg" border="0" />
        </p>
        <h3>Conclusions
</h3>
        <p>
Now I'm certain that the application architecture and the code may not be laid out
to best practises, and I'm aware there are plenty of parts that could have been better
thought out and implemented, but really I just wanted to get a simple factory based
application down on paper, so to speak, to get it a little clearer in my mind.
</p>
        <p>
If any of these articles have helped you or planted the seed of inquisitiveness
in your mind to find out more about factories then that can only be a good thing.
If there is anything I have done in this article that is quite clearly incorrect or
bad practise, please feel free to leave a comment. Any constructive feed back is appreciated.
</p>
        <p>
The next step will be to make the objects prinatable. But until then, have fun!
</p>
        <p>
          <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx">
            <strong>Part
1</strong>
          </a> | <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"><strong>Part
2</strong></a></p>
        <a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip">2008-04-11_ShapeMaker.zip
(103.25 KB)</a> &lt;- Solution files all zipped up. <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb" /></body>
      <title>I can see a pattern forming: First Day At The Factory... Part 3</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx</link>
      <pubDate>Fri, 11 Apr 2008 20:44:42 GMT</pubDate>
      <description>&lt;p&gt;
Back to &lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;Part
2&lt;/a&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;h3&gt;Review Of Progress So Far
&lt;/h3&gt;
&lt;p&gt;
So, to date&amp;nbsp;we have created an interface for&amp;nbsp;our &lt;strong&gt;ShapeFactory&lt;/strong&gt;&amp;nbsp;and
any other shape factory to implement, an enumeration that represents all the available
shapes we may wish to build,&amp;nbsp;and a factory class that can build any of our two
current shapes. The next step is to create a graphical user interface that will call
the factory and receive a couple of shape objects for it's trouble.
&lt;/p&gt;
&lt;h3&gt;The Client Gets What They Deserve
&lt;/h3&gt;
&lt;p&gt;
Next we need a client to display the shapes, so for simplicity we are going to create
a small Windows Forms project called &lt;strong&gt;WindowsApplication &lt;/strong&gt;and add it
to the solution. The project will have a single form called &lt;strong&gt;Main&lt;/strong&gt; (700x550)
which will have a picture box (&lt;strong&gt;uxPictureBox&lt;/strong&gt;) and a button, &lt;strong&gt;uxDrawButton&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
We now need to add a handler for the Draw &lt;strong&gt;Button&lt;/strong&gt; and declare a &lt;strong&gt;Bitmap&lt;/strong&gt; and
a &lt;strong&gt;Graphics&lt;/strong&gt; object at the top of the class. In the form constructor
we will initialise the bitmap to a new instance which will be the size of the &lt;strong&gt;PictureBox&lt;/strong&gt;.
We will then create the &lt;strong&gt;Graphics&lt;/strong&gt; object from the &lt;strong&gt;Bitmap&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
In the button handler method we will clear the graphics object to a white background.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Main
: Form { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt; _bitmap; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; _graphics; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{ InitializeComponent(); _bitmap &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt;(uxPictureBox.Width,
uxPictureBox.Height); _graphics &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt;.FromImage(_bitmap);
uxPictureBox.Image &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; _bitmap;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; uxDrawButton_Click(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
EventArgs e) { _graphics.Clear(Color.White); } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We now need a collection to hold our shapes, so we will declare a list of objects
that implement IShape at the top of the class, and to do this we will need to provide
a reference to the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project. At the same time add
a reference to the &lt;strong&gt;Factories&lt;/strong&gt; project and import the namespaces for
both projects at the top of the form code.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Text; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Windows.Forms; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.Factories; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.WindowsApplication
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Main
: Form { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt; _bitmap; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; _graphics; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; List&amp;lt;&lt;font color=#7fffd4&gt;IShape&lt;/font&gt;&amp;gt;
_shapes; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Next we need to add two method stubs that are void of return values, &lt;strong&gt;AddShapes&lt;/strong&gt;()
and &lt;strong&gt;DrawShapes&lt;/strong&gt;(). Surprisingly, &lt;strong&gt;AddShapes&lt;/strong&gt;() will&amp;nbsp;take
responsibility for&amp;nbsp;adding new shapes&amp;nbsp;to our list and &lt;strong&gt;DrawShapes&lt;/strong&gt;()
will draw those shapes. We will add a line to create an instance of our list in the
constructor and below that we will place a call to the &lt;strong&gt;AddShapes&lt;/strong&gt;()
method. We will place a call in the button handler to the &lt;strong&gt;DrawShapes&lt;/strong&gt;()
method, below the line that clears the graphics to a white background. Follow this
with a refresh of the PictureBox.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{ InitializeComponent(); _bitmap &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Bitmap(uxPictureBox.Width,
uxPictureBox.Height); _graphics &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Graphics.FromImage(_bitmap);
uxPictureBox.Image &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; _bitmap;
_shapes &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; List&amp;lt;IShape&amp;gt;();
AddShapes(); } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Handles the draw button pressed event.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="sender"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="e"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; uxDrawButton_Click(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
EventArgs e) { _graphics.Clear(Color.White); DrawShapes();&lt;br&gt;
uxPictureBox.Refresh(); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the &lt;strong&gt;AddShapes&lt;/strong&gt;() method we need to create a new instance of our &lt;strong&gt;ShapeFactory&lt;/strong&gt; object.
Then using the &lt;strong&gt;ShapeFactory&lt;/strong&gt; object add two shapes, a circle and a
rectangle to the list.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Adds shapes to the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; AddShapes()
{ &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; shapeFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt;();
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle)); _shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Because we know that the shapes Implement the &lt;strong&gt;IShape&lt;/strong&gt; interface we
have a contract that says we can call the &lt;strong&gt;Draw&lt;/strong&gt;() methods on any of
these shapes. These objects will then take the responsibility for drawing them selves.
So in the &lt;strong&gt;DrawShapes&lt;/strong&gt;() method all we need to do is iterate through
all the shapes in the list and call each shapes &lt;strong&gt;Draw&lt;/strong&gt;() method.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Draws the shapes in the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; DrawShapes()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;foreach&lt;/span&gt;(IShape
shape &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; _shapes)
{ shape.Draw(); } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
And that would be it... apart from we haven't added any working code into the Draw()
methods of each concrete object yet!
&lt;/p&gt;
&lt;h3&gt;A Change In The Contract
&lt;/h3&gt;
&lt;p&gt;
Apart from the fact there is no code in the concrete objects to actually draw them
selves, I have just noticed the need to be able to pass the &lt;strong&gt;Graphics&lt;/strong&gt; object
into each shape object, so we need to go back to the interfaces and change the signature
for the &lt;strong&gt;Draw&lt;/strong&gt;() method.
&lt;/p&gt;
&lt;p&gt;
So if we take a look in the &lt;strong&gt;IShape&lt;/strong&gt; interface we need to be able to
pass in a &lt;strong&gt;Graphics&lt;/strong&gt; object as a method parameter.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShape
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the center of the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Point Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The method used to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="graphics"&amp;gt;A reference to the Graphics object to draw the shape
on.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics); }&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;/pre&gt;
&gt; 
&lt;p&gt;
And while we are in this project I think we will take the opportunity to extend the &lt;strong&gt;CreateShape&lt;/strong&gt;()
method signature in the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface, too. We are going
to add a center point parameter; this will allow us to position the shape where we
like.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShapeFactory
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the type of shape to be made.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="center"&amp;gt;Indicates the center point of the shape.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape, &lt;font color=#7fffd4&gt;Point&lt;/font&gt; center);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
A quick build of the project will highlight where our change in interface has broken
the contract between it and the objects that implement it so in the &lt;strong&gt;BaseShape&lt;/strong&gt; object
we need to add a &lt;strong&gt;Graphics&lt;/strong&gt; parameter into the &lt;strong&gt;Draw&lt;/strong&gt;()
method. This will be the same in the &lt;strong&gt;Rectangle&lt;/strong&gt; and the &lt;strong&gt;Circle&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(&lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; graphics);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the &lt;strong&gt;ShapeFactory&lt;/strong&gt; class we need to extend the &lt;strong&gt;CreateShape&lt;/strong&gt;(), &lt;strong&gt;CreateCircle&lt;/strong&gt;()
and &lt;strong&gt;CreateRectangle&lt;/strong&gt;() methods signatures to include the center point
parameter. Then in the &lt;strong&gt;CreateShape&lt;/strong&gt;() method we need to pass the center
point parameter on to the worker method calls.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IShape.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; IShape
CreateShape(Shape shape, Point center) {&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the two worker methods, CreateCircle() and CreateRectangle() we will populate the
object's Center property with the value from the center parameter passed in.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle(Point
center) { &lt;font color=#7fffd4&gt;Circle&lt;/font&gt; circle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Circle&lt;/font&gt;();
circle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; center;
circle.Diameter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 50; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;ICircle&lt;/font&gt;)circle;
} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle(Point
center) { BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt; rectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt;();
rectangle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; center;
rectangle.Size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Size(400,
200); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt;)rectangle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Then back in the form code we need to alter the calls to the &lt;strong&gt;ShapeFactory&lt;/strong&gt; &lt;strong&gt;CreateShape&lt;/strong&gt;()
methods to include a center position
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Adds shapes to the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; AddShapes()
{ &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; shapeFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt;();
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(250,250)));
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(350,
200))); } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Draws the shapes in the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; DrawShapes()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;foreach&lt;/span&gt;(&lt;font color=#7fffd4&gt;IShape&lt;/font&gt; shape &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; _shapes)
{ shape.Draw(_graphics); } }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Back to the Drawing Board
&lt;/h3&gt;
&lt;p&gt;
So finally we can get back into the concrete shape classes and write the code that
will draw the shape.
&lt;/p&gt;
&lt;p&gt;
So let us look at the Circle's &lt;strong&gt;Draw&lt;/strong&gt;() method&amp;nbsp;first. We'll add
the code to allow the Circle to draw it's self.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics) { Pen pen &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Pen(Color.DarkGray,
0); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; left &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.X &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Diameter/2); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; top &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.Y &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Diameter/2);
System.Drawing.Rectangle outlineRectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; System.Drawing.Rectangle(left,
top, Diameter, Diameter); graphics.DrawEllipse(pen, outlineRectangle); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
And the same for the Rectangle.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics) { Pen pen &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Pen(Color.Red,
0); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; left &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.X &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Size.Width &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt; 2); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; top &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.Y &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Size.Height &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt; 2);
System.Drawing.Rectangle outlineRectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; System.Drawing.Rectangle(left,
top, Size.Width, Size.Height); graphics.DrawRectangle(pen, outlineRectangle); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now we need to make sure we have set the start up project to be the windows application
and we should be good to go!
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ShapeMaker_Form_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;h3&gt;Conclusions
&lt;/h3&gt;
&lt;p&gt;
Now I'm certain that the application architecture and the code may not be laid out
to best practises, and I'm aware there are plenty of parts that could have been better
thought out and implemented, but really I just wanted to get a simple factory based
application down on paper, so to speak, to get it a little clearer in my mind.
&lt;/p&gt;
&lt;p&gt;
If any of these articles have helped you or planted&amp;nbsp;the seed of inquisitiveness
in your mind to find out more about factories then that can only be a good thing.
If there is anything I have done in this article that is quite clearly incorrect or
bad practise, please feel free to leave a comment. Any constructive feed back is appreciated.
&lt;/p&gt;
&lt;p&gt;
The next step will be to make the objects prinatable. But until then, have fun!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"&gt;&lt;strong&gt;Part
1&lt;/strong&gt;&lt;/a&gt; | &lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;&lt;strong&gt;Part
2&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip"&gt;2008-04-11_ShapeMaker.zip
(103.25 KB)&lt;/a&gt;&amp;nbsp;&amp;lt;- Solution files all zipped up. &lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=81f8a739-ee84-4f69-8642-e47546c4ec52</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=81f8a739-ee84-4f69-8642-e47546c4ec52</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Back to <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"><strong>Part
1</strong></a></p>
        <h3>Review Of Progress So Far
</h3>
        <p>
So far we have two concrete shape classes, <strong>Circle</strong> and <strong>Rectangle</strong> that
derive from a base shape class. The base shape class implements the <strong>IShape</strong> interface
and the concrete classes implement in turn the <strong>IShape</strong> interface as
well as their respective shape interfaces. The next step is to create a factory class
to create our shapes.
</p>
        <p align="center">
          <img src="http://www.duanewingett.info/content/binary/ShapeMaker_ClassDiagram_01.jpg" border="0" />
        </p>
        <h3>Working In The Factory
</h3>
        <p>
So, we need to add to the solution, another new class library project which
we will call <strong>Factories</strong>. This will house our <strong>ShapeFactory</strong> class. The <strong>ShapeFactory</strong> class will
be responsible for creating our shape objects. It will be able to create and
return an object that implements the <strong>IShape</strong> interface. So far we
have ensured that both our circle and rectangle inherently implement the <strong>IShape</strong> interface
for two reasons. One, because they both derive from the BaseShape object
and we know that implements <strong>IShape</strong>, and two, because they implement <strong>ICircle</strong> and <strong>IRectangle</strong> which
in turn implements <strong>IShape</strong>, as well. (<em>Maybe some one with a little
more experience than I, could advise if this is an acceptable practise. -&gt; I can't
see why not!</em>)
</p>
        <p>
But before we create our <strong>ShapeFactory</strong> we have one more thing to do.
Because we may want to use more than one factory in the future to create our shapes
we are first going to create another interface, which will describe what a <strong>ShapeFactory</strong> need
to create some shape objects. This will be the <strong>IShapeFactory</strong> interface,
and we are going to add that for the sake of simplicity to our <strong>ShapeInterfaces</strong> project
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span><font color="#7fffd4">IShapeFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape();
} }</span>
        </pre>
        <p>
As you can see we have determined that any factory that implements <strong>IShapeFactory</strong> will
have to contain a method that returns an object implementing <strong>IShape</strong>.
The problem is, how do we determine what sort of shape we want to get? Well even though
we only have two shapes at the moment for future expansion we will need an enumeration
to represent these. As enumeration is to be used in every class that implements <strong>IShapeFactory</strong> and
will need to be a parameter in the <strong>CreateShape</strong> method, it seems logical
to me to place the enumeration in the <strong>ShapeInterfaces</strong> project. If
any one can advise whether this is good practise or not, please do! So, let us add
the <strong>Shape</strong> enumeration.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the types of shapes that can be made</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">enum</span><font color="#7fffd4">Shape</font> { <font color="#a9a9a9"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Indicates the shape is to be a circle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span></font> Circle, <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Indicates the shape is to be a rectangle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Rectangle } }</span>
        </pre>
We can now modify the <strong>CreateShape</strong> method of the <strong>IShapeFactory</strong> interface
to accept a <strong>Shape</strong> enumerated value.<pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span><font color="#7fffd4">IShapeFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the type of shape to be made.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape);
}</span></pre><p>
So it's back to the <strong>Factories</strong> project now to create the <strong>ShapeFactory</strong> class
that will provide us with some shapes. To create some shapes we need to add a reference
to both the <strong>BusinessEntity</strong> and the <strong>ShapeInterfaces</strong> projects
into the <strong>Factories</strong> project. After adding our new <strong>ShapeFactory</strong> class
into the <strong>Factories</strong> project we need to import the <strong>BusinessEntity</strong> and
the <strong>ShapeInterfaces</strong> namespaces into the top of it. We now need to
make the <strong>ShapeFactory</strong> class implement <strong>IShapeFactory</strong>.
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.Factories
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span><font color="#7fffd4">ShapeFactory</font> :
IShapeFactory { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShapeFactory
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape)
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Exception</font>(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span> }
}</span></pre><p>
We are now going to create a pair of private methods that will create our two shapes.
Although these methods will create two concrete objects the methods will return not
the objects but their interfaces. First though we will just create the method stubs.
Then we will modify the <strong>CreateShape</strong>() method to call one or the other
of these two methods depending upon the enumerated value passed in.
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShapeFactory
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Creates a shape and returns it.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the shape to create.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IShape.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape)
{ <font color="#7fffd4">IShape</font> createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">switch</span> (shape)
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">case</span><font color="#7fffd4">Shape</font>.Circle:
createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> CreateCircle(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">break</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">case</span><font color="#7fffd4">Shape</font>.Rectangle:
createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> CreateRectangle(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">break</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">default</span>: <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Unknown
shape encountered in CreateShape() method."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> createdShape;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">ICircle</font> CreateCircle()
{ } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle()
{ }</span></pre><p>
We have used switch / case to select which method will create our shape object. Notice
that we use the default case to throw an exception if we encounter a shape that
we haven't catered for. This is in case at any time the shape enumeration is extended
to provide more shapes. Notice also that although the <strong>CreateShape</strong>()
method returns an object that implements <strong>IShape</strong>, the two worker
methods return <strong>ICircle</strong> and <strong>IRectangle</strong>. This works
because both <strong>ICircle</strong> and <strong>IRectangle</strong> both implement
the <strong>IShape</strong> interface, and that is what <strong>CreateShape</strong>()
will return.
</p><p>
Anyway let us crack on and 'flesh out' the <strong>CreateCircle</strong>() worker
method! Because our shapes using the <strong>System.Drawing</strong> namespace, our
factory that is going to create these will need to also. So we need to add a reference
to and import that namespace for the <strong>ShapeFactory</strong>. (<em>Incidentally
have you noticed the more you look at the word "circle" the more wrong it looks!</em>)
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">ICircle</font> CreateCircle()
{ <font color="#7fffd4">Circle</font> circle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Circle</font>();
circle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(250,250);
circle.Diameter <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 50; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">ICircle</font>)circle;
}</span></pre><p>
Now I would normally use constructor in my code, but this example isn't to show best
practise of constructing objects, so I am going to construct my objects using the
properties today. You will probably also notice that I am casting the newly constructed <strong>Circle</strong> object
to the type of <strong>ICircle</strong> before returning it. You don't need to
do this but I believe it is good practise. (<em>Can any one confirm this please?</em>)
</p><p>
At this point I have just realised an error with choosing a rectangle for a shape,
due to a conflict with the <strong>System.Drawing.Rectangle</strong> object. Well
we could change the shape to a square or a triangle.... but we have got this far,
so I am going to stick with it. It will however mean there will be some ugly code
with the BusinessEntity namespace forced in here and there!!!
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle()
{ BusinessEntity.<font color="#7fffd4">Rectangle</font> rectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> BusinessEntity.<font color="#7fffd4">Rectangle</font>();
rectangle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Point(300,
300); rectangle.Size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Size(400,
200); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">IRectangle</font>)rectangle;
}</span></pre><p>
Now that should be it for our ShapeFactory for the moment. If all has gone well for
you then your project should build without errors.
</p><p>
To sum up we have created an interface for this and any other shape factory to implement,
an enumeration that represents all the available shapes we may wish to build, and
a factory class that can build any of our two current shapes. the next step is to
create a graphical user interface that will call the factory and receive a couple
of shape objects for it's trouble.
</p><p><strong><a href="http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx">Part
3</a></strong></p><img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=81f8a739-ee84-4f69-8642-e47546c4ec52" /></body>
      <title>I can see a pattern forming: First Day At The Factory... Part 2</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx</link>
      <pubDate>Thu, 10 Apr 2008 18:28:13 GMT</pubDate>
      <description>&lt;p&gt;
Back to &lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"&gt;&lt;strong&gt;Part
1&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h3&gt;Review Of Progress So Far
&lt;/h3&gt;
&lt;p&gt;
So far we have two concrete shape classes, &lt;strong&gt;Circle&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt;&amp;nbsp;that
derive from a base shape class. The base shape class implements the &lt;strong&gt;IShape&lt;/strong&gt; interface
and the concrete classes implement in turn the &lt;strong&gt;IShape&lt;/strong&gt; interface as
well as their respective shape interfaces. The next step is to create a factory class
to create our shapes.
&lt;/p&gt;
&lt;p align=center&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ShapeMaker_ClassDiagram_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;h3&gt;Working In The Factory
&lt;/h3&gt;
&lt;p&gt;
So, we need&amp;nbsp;to add to the solution, another new class library project&amp;nbsp;which
we will&amp;nbsp;call &lt;strong&gt;Factories&lt;/strong&gt;. This will house our &lt;strong&gt;ShapeFactory&lt;/strong&gt; class.&amp;nbsp;The &lt;strong&gt;ShapeFactory&lt;/strong&gt; class&amp;nbsp;will
be&amp;nbsp;responsible for creating our shape objects. It will be able to create and
return an object that implements the &lt;strong&gt;IShape&lt;/strong&gt; interface. So far we
have ensured that both our circle and rectangle inherently implement the &lt;strong&gt;IShape&lt;/strong&gt; interface
for two reasons.&amp;nbsp;One,&amp;nbsp;because they both derive from the BaseShape object
and we know that implements &lt;strong&gt;IShape&lt;/strong&gt;, and two, because they implement &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt; which
in turn implements &lt;strong&gt;IShape&lt;/strong&gt;, as well. (&lt;em&gt;Maybe some one with a little
more experience than I, could advise if this is an acceptable practise. -&amp;gt; I can't
see why not!&lt;/em&gt;)
&lt;/p&gt;
&lt;p&gt;
But before we create our &lt;strong&gt;ShapeFactory&lt;/strong&gt; we have one more thing to do.
Because we may want to use more than one factory in the future to create our shapes
we are first going to create another interface, which will describe&amp;nbsp;what a&amp;nbsp;&lt;strong&gt;ShapeFactory&lt;/strong&gt; need
to create some shape objects. This will be the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface,
and we are going to add that for the sake of simplicity to our &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IShapeFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape();
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see we have determined that any factory that implements &lt;strong&gt;IShapeFactory&lt;/strong&gt; will
have to contain a method that returns an object implementing &lt;strong&gt;IShape&lt;/strong&gt;.
The problem is, how do we determine what sort of shape we want to get? Well even though
we only have two shapes at the moment for future expansion we will need an enumeration
to represent these. As enumeration is to be used in every class that implements &lt;strong&gt;IShapeFactory&lt;/strong&gt; and
will need to be a parameter in the &lt;strong&gt;CreateShape&lt;/strong&gt; method, it seems logical
to me to place the enumeration in the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project. If
any one can advise whether this is good practise or not, please do! So, let us add
the &lt;strong&gt;Shape&lt;/strong&gt; enumeration.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the types of shapes that can be made&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;enum&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt; { &lt;font color=#a9a9a9&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Indicates the shape is to be a circle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt; Circle, &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Indicates the shape is to be a rectangle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Rectangle } }&lt;/span&gt;&lt;/pre&gt;
We can now modify the &lt;strong&gt;CreateShape&lt;/strong&gt; method of the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface
to accept a &lt;strong&gt;Shape&lt;/strong&gt; enumerated value.&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IShapeFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the type of shape to be made.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So it's back to the &lt;strong&gt;Factories&lt;/strong&gt; project now to create&amp;nbsp;the &lt;strong&gt;ShapeFactory&lt;/strong&gt; class
that will provide us with some shapes. To create some shapes we need to add a&amp;nbsp;reference
to both&amp;nbsp;the &lt;strong&gt;BusinessEntity&lt;/strong&gt; and the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; projects
into the &lt;strong&gt;Factories&lt;/strong&gt; project. After adding our new &lt;strong&gt;ShapeFactory&lt;/strong&gt; class
into&amp;nbsp;the &lt;strong&gt;Factories&lt;/strong&gt; project we need to import the &lt;strong&gt;BusinessEntity&lt;/strong&gt; and
the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; namespaces into the top of it. We now need to
make the&amp;nbsp;&lt;strong&gt;ShapeFactory&lt;/strong&gt; class implement &lt;strong&gt;IShapeFactory&lt;/strong&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.Factories
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; :
IShapeFactory { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShapeFactory
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Exception&lt;/font&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We are now going to create a pair of private methods that will create our two shapes.
Although these methods will create two concrete objects the methods will return not
the objects but their interfaces. First though we will just create the method stubs.
Then we will modify the &lt;strong&gt;CreateShape&lt;/strong&gt;() method to call one or the other
of these two methods depending upon the enumerated value passed in.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShapeFactory
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Creates a shape and returns it.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the shape to create.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IShape.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape)
{ &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;switch&lt;/span&gt; (shape)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle:
createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; CreateCircle(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle:
createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; CreateRectangle(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;default&lt;/span&gt;: &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unknown
shape encountered in CreateShape() method."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; createdShape;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle()
{ } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle()
{ }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We have used switch / case to select which method will create our shape object. Notice
that we use the default case to throw an exception&amp;nbsp;if we encounter a shape that
we haven't catered for. This is in case at any time the shape enumeration is extended
to provide more shapes. Notice also that although the &lt;strong&gt;CreateShape&lt;/strong&gt;()
method&amp;nbsp;returns an object that implements &lt;strong&gt;IShape&lt;/strong&gt;, the two worker
methods return &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt;. This works
because both &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt; both implement
the &lt;strong&gt;IShape&lt;/strong&gt; interface, and that is what &lt;strong&gt;CreateShape&lt;/strong&gt;()
will return.
&lt;/p&gt;
&lt;p&gt;
Anyway let us crack on and 'flesh out' the &lt;strong&gt;CreateCircle&lt;/strong&gt;() worker
method! Because our shapes using the &lt;strong&gt;System.Drawing&lt;/strong&gt; namespace, our
factory that is going to create these will need to also. So we need to add a reference
to and import that namespace for the &lt;strong&gt;ShapeFactory&lt;/strong&gt;.&amp;nbsp;(&lt;em&gt;Incidentally
have you noticed the more you look at the word "circle" the more wrong it looks!&lt;/em&gt;)
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle()
{ &lt;font color=#7fffd4&gt;Circle&lt;/font&gt; circle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Circle&lt;/font&gt;();
circle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(250,250);
circle.Diameter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 50; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;ICircle&lt;/font&gt;)circle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now I would normally use constructor in my code, but this example isn't to show best
practise of constructing objects, so I am going to construct my objects using the
properties today. You will probably also notice that I am casting the newly constructed &lt;strong&gt;Circle&lt;/strong&gt; object
to the type of&amp;nbsp;&lt;strong&gt;ICircle&lt;/strong&gt; before returning it. You don't need to
do this but I believe it is good practise. (&lt;em&gt;Can any one confirm this please?&lt;/em&gt;)
&lt;/p&gt;
&lt;p&gt;
At this point I have just realised an error with choosing a rectangle for a shape,
due to a conflict with the &lt;strong&gt;System.Drawing.Rectangle&lt;/strong&gt; object. Well
we could change the shape to a square or a triangle.... but we have got this far,
so I am going to stick with it. It will however mean there will be some ugly code
with the BusinessEntity namespace forced in here and there!!!
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle()
{ BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt; rectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt;();
rectangle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Point(300,
300); rectangle.Size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Size(400,
200); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt;)rectangle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now that should be it for our ShapeFactory for the moment. If all has gone well for
you then your project should build without errors.
&lt;/p&gt;
&lt;p&gt;
To sum up we have created an interface for this and any other shape factory to implement,
an enumeration that represents all the available shapes we may wish to build,&amp;nbsp;and
a factory class that can build any of our two current shapes. the next step is to
create a graphical user interface that will call the factory and receive a couple
of shape objects for it's trouble.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx"&gt;Part
3&lt;/a&gt;&lt;/strong&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=81f8a739-ee84-4f69-8642-e47546c4ec52" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=45331794-5e79-41ae-a2be-f799238f61f0</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=45331794-5e79-41ae-a2be-f799238f61f0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Background
</h3>
        <p>
Recently I have been reading quite a bit about Factory Patterns and Inversion of Control
(IOC). However like most principles that are new to me I often find them very hard
to visualise at first, and even harder to put them into practise. So I am going to
try a simple application using what I believe to be a Factory Pattern. The <em>aim
of the game</em> will be to use a factory to produce shapes that will be drawn using
.Net's GDI classes in a Windows form application.
</p>
        <p>
This article assumes that the reader is already comfortable with the use of interfaces
and <a href="http://support.microsoft.com/kb/307205">inheritance</a> in classes.
</p>
        <p>
The files created in this article can be downloaded from here: <a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip">2008-04-11_ShapeMaker.zip
(103.25 KB)</a> 
</p>
        <h3>Interfaces
</h3>
        <p>
The first thing I'm going to do is to create a Blank Solution, using Visual Studio
2005, which I am going to call <strong>ShapeMaker</strong>. To this solution I am
going to add a <strong>New Project</strong> which will be a Visual C# <strong>Class
Library</strong> which I am going to call <strong>ShapeInterfaces</strong>.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_01.jpg" border="0" />
        </p>
        <p>
I'll close and delete the default <strong>Class1.cs</strong> class file before I proceed.
I shall now add a new Interface called <strong>IShape</strong> and remove the two
unused using statements. To the <strong>IShape</strong> interface I am going to add
a <strong>System.Drawing.Point</strong> property called <strong>Center</strong> and
import it's namespace after first adding a reference to the <strong>System.Drawing</strong> assembly.
I shall then add a method that is void of a return type and call it <strong>Draw()</strong>. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShape
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the center of the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Point Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The method used to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw();
} }</span>
        </pre>
        <p>
All shapes that I intend to create will implement this interface. The <strong>Center</strong> property
will define the center point of the shape, and the <strong>Draw()</strong> method
will allow any user interface to know that when it is required to draw the shape it
can call this method and the shape will be responsible for drawing it self.
</p>
        <p>
I'm now going to add two more interfaces which will both implement the <strong>IShape</strong> interface,
the <strong>ICircle</strong> which will also have a integer <strong>Diameter</strong> property...
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> ICircle
: IShape { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the diameter of the circle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Diameter
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;
} } }</span>
        </pre>
        <p>
...and the <strong>IRectangle</strong> which will have a <strong>Size</strong> property.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IRectangle
: IShape { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the size of the rectangle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Size Size { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;}
} }</span>
        </pre>
        <h3>Base Classes
</h3>
        <p>
I'm now going to add another class library project to the solution which will be my <strong>BusinessEntity</strong> layer,
from which I will also delete the default <strong>Class1</strong> class. To the <strong>BusinessEntity</strong> project
I am going to add a folder called <strong>BaseClasses</strong>. I would probably normally
use a separate project for my base classes but for clarity of this example I shall
place them in this folder.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_02.jpg" border="0" />
        </p>
        <p>
To the <strong>BaseClassses</strong> folder I am going to add a class that all my
shape entities will inherit from. I shall call this <strong>BaseShape</strong>. As
I intend for <strong>BaseShape</strong> to implement the interface <strong>IShape</strong> I
need to add a reference to the <strong>ShapeInterfaces</strong> project from the <strong>BusinessEntity</strong> project,
and then import the namespace into the <strong>BaseShape</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity.BaseClasses
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseShape
: IShape { } }</span>
        </pre>
        <p>
We have declared the class as abstract as we do not want this class to be able to
be constructed. It will just be used a s a base for other classes to derive (inherit)
from. Right clicking on the interface name and selecting <strong>Implement Interface</strong> from
the context menu...
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/ImplementInterface.jpg" border="0" />
        </p>
        <p>
... should inject the following code into the class. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShape
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> System.Drawing.Point
Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span></span>
        </pre>
        <p>
We will now need to add a reference to the <strong>System.Drawing</strong> assembly
and import the name space before we can build or we will receive an error. The next
task to do is to declare a private field, <strong>_center</strong>, which will hold
our center point data and then we will modify the <strong>Center</strong> property
to access that field. Then we will declare the draw method as <strong>abstract</strong> so
it must be overridden by any derived class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> Point
_center; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShape
Members <font color="#a9a9a9"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;<font color="#a9a9a9">summary</font>&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
Gets or sets the center point of the shape.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/<font color="#a9a9a9">summary</font>&gt;</span></font><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Point
Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _center;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> {
_center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
&lt;summary&gt;</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
All inherited classes will contain this method.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
The method will be used to draw it's self.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
&lt;/summary&gt;</font></span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span></span>
        </pre>
        <h3>Derived classes
</h3>
        <p>
Next up is to create two shape classes that will derive from the base class. The first
shape we will create will be a <strong>Circle</strong>. We will add it to the root
of the <strong>BusinessEntity</strong> project, and make it both derive from the <strong>BaseShape</strong> class
and implement the <strong>ICircle</strong> interface. To make this class derive
correctly from the <strong>BaseShape</strong> class we will need to declare a <strong>Draw()</strong> method
and mark it as an <strong>override</strong>.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity.BaseClasses; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Circle
: BaseShape, ICircle { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> ICircle
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Diameter
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ } } }</span>
        </pre>
        <p>
We can now create a private field <strong>_diameter</strong> and set the <strong>Diameter</strong> property
to access it fully. The next shape we will add will be the <strong>Rectangle</strong>,
which we will do in much the same way, except that we will create a field to
hold the size data and access it from the <strong>Size</strong> property.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity.BaseClasses; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Rectangle
: BaseShape, IRectangle { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Size</font> _size; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IRectangle
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets the size of the rectangle.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">Size</font> Size
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _size;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> {
_size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ } } }</span>
        </pre>
        <h3>Summing up so far...
</h3>
        <p>
So far we have two concrete shape classes, <strong>Circle</strong> and <strong>Rectangle</strong> that
derive from a base shape class. The base shape class implements the <strong>IShape</strong> interface
and the concrete classes implement in turn the <strong>IShape</strong> interface as
well as their respective shape interfaces.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_03.jpg" border="0" />
        </p>
        <p>
          <strong>
            <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx">Part
2</a>
          </strong> will show the next step, which is to build a factory class
which will be used to create and return our shape objects at runtime.
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=45331794-5e79-41ae-a2be-f799238f61f0" />
      </body>
      <title>I can see a pattern forming: First Day At The Factory... Part 1</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx</link>
      <pubDate>Thu, 10 Apr 2008 04:03:42 GMT</pubDate>
      <description>&lt;h3&gt;Background
&lt;/h3&gt;
&lt;p&gt;
Recently I have been reading quite a bit about Factory Patterns and Inversion of Control
(IOC). However like most principles that are new to me I often find them very hard
to visualise at first, and even harder to put them into practise. So I am going to
try a simple application using what I believe to be a&amp;nbsp;Factory Pattern. The &lt;em&gt;aim
of the game&lt;/em&gt; will be to use a factory to produce shapes that will be drawn using
.Net's GDI classes in a Windows form application.
&lt;/p&gt;
&lt;p&gt;
This article assumes that the reader is already comfortable with the use of interfaces
and &lt;a href="http://support.microsoft.com/kb/307205"&gt;inheritance&lt;/a&gt; in classes.
&lt;/p&gt;
&lt;p&gt;
The files created in this article can be downloaded from here: &lt;a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip"&gt;2008-04-11_ShapeMaker.zip
(103.25 KB)&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;h3&gt;Interfaces
&lt;/h3&gt;
&lt;p&gt;
The first thing I'm going to do is to create a Blank Solution, using Visual Studio
2005, which I am going to call &lt;strong&gt;ShapeMaker&lt;/strong&gt;. To this solution I am
going to add a &lt;strong&gt;New Project&lt;/strong&gt; which will be a Visual C# &lt;strong&gt;Class
Library&lt;/strong&gt; which I am going to call &lt;strong&gt;ShapeInterfaces&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
I'll close and delete the default &lt;strong&gt;Class1.cs&lt;/strong&gt; class file before I proceed.
I shall now add a new Interface called &lt;strong&gt;IShape&lt;/strong&gt; and remove the two
unused using statements. To the &lt;strong&gt;IShape&lt;/strong&gt; interface I am going to add
a &lt;strong&gt;System.Drawing.Point&lt;/strong&gt; property called &lt;strong&gt;Center&lt;/strong&gt;&amp;nbsp;and
import it's namespace after first adding a reference to the &lt;strong&gt;System.Drawing&lt;/strong&gt; assembly.
I shall then add a method that is void of&amp;nbsp;a return type and call it&amp;nbsp;&lt;strong&gt;Draw()&lt;/strong&gt;. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShape
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the center of the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Point Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The method used to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw();
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
All shapes that I intend to create will implement this interface. The &lt;strong&gt;Center&lt;/strong&gt; property
will define the center point of the shape, and the &lt;strong&gt;Draw()&lt;/strong&gt; method
will allow any user interface to know that when it is required to draw the shape it
can call this method and the shape&amp;nbsp;will&amp;nbsp;be responsible for drawing it self.
&lt;/p&gt;
&lt;p&gt;
I'm now going to add two more interfaces which will both implement the &lt;strong&gt;IShape&lt;/strong&gt; interface,
the &lt;strong&gt;ICircle&lt;/strong&gt; which will&amp;nbsp;also have a integer &lt;strong&gt;Diameter&lt;/strong&gt; property...
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; ICircle
: IShape { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the diameter of the circle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Diameter
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;
} } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
...and the &lt;strong&gt;IRectangle&lt;/strong&gt; which will have a &lt;strong&gt;Size&lt;/strong&gt; property.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IRectangle
: IShape { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the size of the rectangle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Size Size { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;}
} }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Base Classes
&lt;/h3&gt;
&lt;p&gt;
I'm now going to add another class library project to the solution which will be my &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer,
from which I will also delete the default &lt;strong&gt;Class1&lt;/strong&gt; class. To the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project
I am going to add a folder called &lt;strong&gt;BaseClasses&lt;/strong&gt;. I would probably normally
use a separate project for my base classes but for clarity of this example I shall
place them in this folder.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_02.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
To the &lt;strong&gt;BaseClassses&lt;/strong&gt; folder I am going to add a class that all my
shape entities will inherit from. I shall call this &lt;strong&gt;BaseShape&lt;/strong&gt;. As
I intend for &lt;strong&gt;BaseShape&lt;/strong&gt;&amp;nbsp;to implement the interface &lt;strong&gt;IShape&lt;/strong&gt; I
need to add a reference to the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project from the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project,
and then import the namespace into the &lt;strong&gt;BaseShape&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseShape
: IShape { } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We have declared the class as abstract as we do not want this class to be able to
be constructed. It will just be used a s a base for other classes to derive (inherit)
from. Right clicking on the interface name and selecting &lt;strong&gt;Implement Interface&lt;/strong&gt; from
the context menu...
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ImplementInterface.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
...&amp;nbsp;should inject the following code into the class. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShape
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; System.Drawing.Point
Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We will now need to add a reference to the &lt;strong&gt;System.Drawing&lt;/strong&gt; assembly
and import the name space before we can build or we will receive an error. The next
task to do is to declare a private field, &lt;strong&gt;_center&lt;/strong&gt;, which will hold
our center point data and then we will modify the &lt;strong&gt;Center&lt;/strong&gt; property
to access that field. Then we will declare the draw method as &lt;strong&gt;abstract&lt;/strong&gt; so
it must be overridden by any derived class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; Point
_center; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShape
Members &lt;font color=#a9a9a9&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;&lt;font color=#a9a9a9&gt;summary&lt;/font&gt;&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
Gets or sets the center point of the shape.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/&lt;font color=#a9a9a9&gt;summary&lt;/font&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Point
Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _center;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; {
_center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
&amp;lt;summary&amp;gt;&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
All inherited classes will contain this method.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
The method will be used to draw it's self.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
&amp;lt;/summary&amp;gt;&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Derived classes
&lt;/h3&gt;
&lt;p&gt;
Next up is to create two shape classes that will derive from the base class. The first
shape we will create will be a &lt;strong&gt;Circle&lt;/strong&gt;. We will add it to the root
of the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project, and make it both derive from the &lt;strong&gt;BaseShape&lt;/strong&gt; class
and implement the &lt;strong&gt;ICircle&lt;/strong&gt; interface. To make this class&amp;nbsp;derive
correctly from the &lt;strong&gt;BaseShape&lt;/strong&gt; class we will need to declare a &lt;strong&gt;Draw()&lt;/strong&gt; method
and mark it as an &lt;strong&gt;override&lt;/strong&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Circle
: BaseShape, ICircle { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; ICircle
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Diameter
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ } } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We&amp;nbsp;can now create a private field &lt;strong&gt;_diameter&lt;/strong&gt; and set the &lt;strong&gt;Diameter&lt;/strong&gt; property
to access it fully. The next&amp;nbsp;shape we will add will be the &lt;strong&gt;Rectangle&lt;/strong&gt;,
which we will do in much the same way, except that we will create a&amp;nbsp;field to
hold the size data and access it from the &lt;strong&gt;Size&lt;/strong&gt; property.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Rectangle
: BaseShape, IRectangle { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Size&lt;/font&gt; _size; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IRectangle
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets the size of the rectangle.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;Size&lt;/font&gt; Size
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _size;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; {
_size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ } } }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Summing up so far...
&lt;/h3&gt;
&lt;p&gt;
So far we have two concrete shape classes, &lt;strong&gt;Circle&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt;&amp;nbsp;that
derive from a base shape class. The base shape class implements the &lt;strong&gt;IShape&lt;/strong&gt; interface
and the concrete classes implement in turn the &lt;strong&gt;IShape&lt;/strong&gt; interface as
well as their respective shape interfaces.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_03.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;Part
2&lt;/a&gt;&lt;/strong&gt; will show the next step, which&amp;nbsp;is to&amp;nbsp;build a factory class
which will be used to create and return&amp;nbsp;our shape objects at runtime.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=45331794-5e79-41ae-a2be-f799238f61f0" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=f3307cd3-5f39-47cb-8560-80d80878caf1</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,f3307cd3-5f39-47cb-8560-80d80878caf1.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,f3307cd3-5f39-47cb-8560-80d80878caf1.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=f3307cd3-5f39-47cb-8560-80d80878caf1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Tonight, I stumbled upon, and then tested out, this useful article by the Wrox Team.
It's all about printing WinForms in C#.
</p>
        <p>
          <a href="http://www.devarticles.com/c/a/C-Sharp/Printing-Using-C-sharp/">http://www.devarticles.com/c/a/C-Sharp/Printing-Using-C-sharp/</a>
        </p>
        <p>
Hopefully you will find it as useful as I did. :-)
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=f3307cd3-5f39-47cb-8560-80d80878caf1" />
      </body>
      <title>Printing forms in C#</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,f3307cd3-5f39-47cb-8560-80d80878caf1.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/04/PrintingFormsInC.aspx</link>
      <pubDate>Fri, 04 Apr 2008 23:14:04 GMT</pubDate>
      <description>&lt;p&gt;
Tonight, I stumbled upon, and then tested out, this useful article by the Wrox Team.
It's all&amp;nbsp;about printing WinForms in C#.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.devarticles.com/c/a/C-Sharp/Printing-Using-C-sharp/"&gt;http://www.devarticles.com/c/a/C-Sharp/Printing-Using-C-sharp/&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Hopefully you will find it as useful as I did. :-)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=f3307cd3-5f39-47cb-8560-80d80878caf1" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,f3307cd3-5f39-47cb-8560-80d80878caf1.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=4d4be93f-a7dd-4988-a966-23872fc4651b</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,4d4be93f-a7dd-4988-a966-23872fc4651b.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,4d4be93f-a7dd-4988-a966-23872fc4651b.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=4d4be93f-a7dd-4988-a966-23872fc4651b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of my on-going projects uses a light weight client to call a web service, which
runs a fairly long running process, part of which will take at minimum five minutes.
Testing has proved that the request to the webserver will cause a timeout. I belive
the default is about 90 seconds. There are probably many ways to circumvent this problem,
but the two that i am concidering are:
</p>
        <ul>
          <li>
Increase the time out settings to cover the longest anticipated duration. 
</li>
          <li>
To implement fire and forget by the client, and let the webservice spawn an additional
thread to carry out the long process, returning the request almost imeadiatley.</li>
        </ul>
        <h3>Increasing Timeout settings
</h3>
        <h4>Solution
</h4>
        <p>
From a little web reasearch it appears that there are three settings that may be able
to assist me.
</p>
        <ul>
          <li>
The timeout property on the webservice proxy 
</li>
          <li>
The http-runtime setting in App.Config of the calling assembly 
</li>
          <li>
The http-runtime setting in Web.Config of the webservice.</li>
        </ul>
        <h4>Pros
</h4>
        <ul>
          <li>
Allows trapping of exceptions during the process and passing them back to
the client in the form of a <font color="#7fffd4">SoapException</font>.</li>
        </ul>
        <h4>Cons
</h4>
        <ul>
          <li>
Process time may vary over operational lifetime of project requiring changing of configuration
files, and possibly recompliling everytime the threshold is exceeded. 
</li>
          <li>
If the request times out there is no way of knowing if the process is still running.</li>
        </ul>
        <h3>Fire and Forget
</h3>
        <h4>Solution
</h4>
        <p>
My second solution would be to use the BackgroundWorker to spawn a new thread
in the code called the from the WebService's WebMethod.
</p>
        <h4>Pros
</h4>
        <ul>
          <li>
Allows imediate return of the WebMethod request so very little risk of time out, and
therefore virtually limit less total process time. 
</li>
          <li>
Because the client needs to request whether the process has finished any way, if an
exception occurs this can be handled and the status relayed back to the client at
the next request.</li>
        </ul>
        <h4>Cons
</h4>
        <ul>
          <li>
Requires state of progress or completion to be stored by webservice (in database,
text file, or similar). 
</li>
          <li>
Requires the calling client to keep "asking" the webservice if the task
has finished. This will involve more coding and the client having to "know" that it
has to keep asking for the progress and or status of completion of the task. 
</li>
          <li>
Requires extra coding to provide client with information if an exception occurs during
the process.</li>
        </ul>
        <p>
So which is the best method to choose? Well this is probably going to sound like a
bit of a cop-out, but it really does depend upon the application and how it is to
be used. Each solution has it merits and its drawbacks as you can see.
</p>
        <p>
For the project that I am concerned with I will no doubt use the fire and forget solution
so I am not limited to time outs, as one of the processes (Archiving) will gradually
take longer and longer to complete over the working life of the application, and I
do not wish to be having to change the timeout and recompile frequently. I am also
concerned that the timeout required couls easily surpass acceptable limits.
</p>
        <p>
So how would <em>you</em> choose to carry out this task? Do you have any other solutions?
Can you see any more advanatages or disadvantages of using either of the two solutions
presented? your thoughts are welcom as always.
</p>
        <p>
          <font size="1">To be continued...</font>
        </p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4d4be93f-a7dd-4988-a966-23872fc4651b" />
      </body>
      <title>Long running processes within a webservice. Extend request timeout vs fire and forget?</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,4d4be93f-a7dd-4988-a966-23872fc4651b.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/03/LongRunningProcessesWithinAWebserviceExtendRequestTimeoutVsFireAndForget.aspx</link>
      <pubDate>Thu, 03 Apr 2008 12:24:24 GMT</pubDate>
      <description>&lt;p&gt;
One of my on-going projects uses a light weight client to call a web service, which
runs a fairly long running process, part of which will take at minimum five minutes.
Testing has proved that the request to the webserver will cause a timeout. I belive
the default is about 90 seconds. There are probably many ways to circumvent this problem,
but the two that i am concidering are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Increase the time out settings to cover the longest anticipated duration. 
&lt;li&gt;
To implement fire and forget by the client, and let the webservice spawn an additional
thread to carry out the long process, returning the request almost imeadiatley.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Increasing Timeout settings
&lt;/h3&gt;
&lt;h4&gt;Solution
&lt;/h4&gt;
&lt;p&gt;
From a little web reasearch it appears that there are three settings that may be able
to assist me.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The timeout property on the webservice proxy 
&lt;li&gt;
The http-runtime setting in App.Config of the calling assembly 
&lt;li&gt;
The http-runtime setting in Web.Config of the webservice.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Pros
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
Allows trapping of exceptions&amp;nbsp;during the process&amp;nbsp;and passing them back to
the client in the form of a &lt;font color=#7fffd4&gt;SoapException&lt;/font&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Cons
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
Process time may vary over operational lifetime of project requiring changing of configuration
files, and possibly recompliling everytime the threshold is exceeded. 
&lt;li&gt;
If the request times out there is no way of knowing if the process is still running.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Fire and Forget
&lt;/h3&gt;
&lt;h4&gt;Solution
&lt;/h4&gt;
&lt;p&gt;
My second solution would be to&amp;nbsp;use the BackgroundWorker to spawn a new thread
in the code called the from the WebService's WebMethod.
&lt;/p&gt;
&lt;h4&gt;Pros
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
Allows imediate return of the WebMethod request so very little risk of time out, and
therefore virtually limit less total process time. 
&lt;li&gt;
Because the client needs to request whether the process has finished any way, if an
exception occurs this can be handled and the status relayed back to the client at
the next request.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Cons
&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
Requires state of progress or completion to be stored by webservice (in database,
text file, or similar). 
&lt;li&gt;
Requires&amp;nbsp;the calling client to keep "asking" the webservice if the&amp;nbsp;task
has finished. This will involve more coding and the client having to "know" that it
has to keep asking for the progress and or status of completion of the task. 
&lt;li&gt;
Requires extra coding to provide client with information if an exception occurs during
the process.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So which is the best method to choose? Well this is probably going to sound like a
bit of a cop-out, but it really does depend upon the application and how it is to
be used. Each solution has it merits and its drawbacks as you can see.
&lt;/p&gt;
&lt;p&gt;
For the project that I am concerned with I will no doubt use the fire and forget solution
so I am not limited to time outs, as one of the processes (Archiving) will gradually
take longer and longer to complete over the working life of the application, and I
do not wish to be having to change the timeout and recompile frequently. I am also
concerned that the timeout required couls easily surpass acceptable limits.
&lt;/p&gt;
&lt;p&gt;
So how would &lt;em&gt;you&lt;/em&gt; choose to carry out this task? Do you have any other solutions?
Can you see any more advanatages or disadvantages of using either of the two solutions
presented? your thoughts are welcom as always.
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;To be continued...&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4d4be93f-a7dd-4988-a966-23872fc4651b" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,4d4be93f-a7dd-4988-a966-23872fc4651b.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Long Running Processes</category>
      <category>Web Services</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=e52ed85e-a815-451e-862d-710c6113e52a</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=e52ed85e-a815-451e-862d-710c6113e52a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm sure most of us endeaver to keep the tightest scope possible for the members of
classes we write. But I have a situation where I just can't seem to be able to keep
the scope of members of my inherited class as tight as I would like.
</p>
        <p>
What I am intending to create is a <strong>Job</strong> object that will inherit from
a <strong>BaseJob</strong> object that will implement the <strong>IJob</strong> interface.
While doing this I want to ensure the <strong>JobFactory</strong> member of the <strong>Job</strong> object
is kept private and the <strong>Id</strong> member is public. Both of these members
belong in the <strong>BaseJob</strong> object.
</p>
        <p>
So I'll start with new assembly that will contain three interfaces; <strong>IJob,
IIdentifyable</strong> and <strong>IJobFactory</strong>. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
            <font color="#7fffd4">IJob</font> : <font color="#7fffd4">IIdentifyable</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the factory that created the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#7fffd4">IJobFactory</font> JobFactory { <font color="#0000ff">get</font>; <font color="#0000ff">set</font>;
} }</span>
        </pre>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the signature for an object that is identifyable<br /></span>
              </span>
            </span>
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> public</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
              <font color="#7fffd4">IIdentifyable</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the identity of the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#0000ff">int</font> Id { <font color="#0000ff">get</font>; <font color="#0000ff">set</font>;
} }</span>
          </span>
        </pre>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            </span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the signature required for a job factory.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
            <font color="#7fffd4">IJobFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Creates an object that implements the IJob interface.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;Returns an object that implements the IJob interface&lt;/returns&gt;</span><font color="#7fffd4">IJob</font> Create(); <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Saves the current job</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Save(<font color="#7fffd4">IJob</font> job);
}</span>
        </pre>
        <p>
          <strong>IJob</strong> has a member which will be used to hold a reference to an
object (that implements the <strong>IJobFactory</strong> inteface) that created it.
It will also have a Id member implemented through the <strong>IIdentifyable</strong> interface. <strong>IJobFactory</strong> has
a method for returning a newly created object that implements the <strong>IJob</strong> interface
and a method to save an object that implements the <strong>IJob</strong> interface.
Both interfaces must be <font color="#0000ff">public</font> as they need to be seen from
outside the assembly. As far as I am aware you canot declare the scope of the methods
in an interface so each will be of the same scope as the interface.
</p>
        <p>
I now need to another new assembly which will contain a base class (<strong>BaseJob</strong>)
for my <strong>Job</strong> object to inherit from. I create a reference to the interfaces
assembly, and allow the <strong>BaseJob</strong> class will implement the <strong>IJob</strong> interface
from it.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The base class that all Job objects should inherit from.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseJob
: IJob { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> _id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IJobFactory</font> _jobFactory; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IIdentifyable
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the identity of an Job</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Id
{ <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _id
; } <font color="#0000ff">set</font> { _id <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IJob
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the factory that created the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#0000ff">public</font><font color="#7fffd4">IJobFactory</font> JobFactory
{ <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _jobFactory;
} <font color="#0000ff">set</font> { _jobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span> }</span>
        </pre>
        <p>
Now, I would like to set the scope of the <strong>BaseJob</strong>'s <strong>Id</strong> and <strong>Jobfactory</strong> members
as <font color="#0000ff">protected</font>, do they can only be seen by objects that
inherit from the <strong>BaseJob</strong> object. However if I try and make the
scope any tighter then VS advises me of an error when I try to build.
</p>
        <p>
          <font face="Courier New" color="#ff0000">Error 1 'BaseJob' does not implement
interface member 'IJob.JobFactory'. 'BaseJob.JobFactory' is either static, not public,
or has the wrong return type.</font>
        </p>
        <p>
So as far as I can see it has to stay <font color="#0000ff">public</font>. Any way
on to my <strong>Job</strong> object now. In a third assembly, which has a reference
to both the base class assembly and the interfaces assembly,  I create the <strong>Job</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Job
: BaseJob { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Job(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> id, <font color="#7fffd4">IJobFactory</font> jobFactory)
: <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.Id <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.JobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> jobFactory;
} }</span>
        </pre>
        <p>
The <strong>Job</strong> class has a contructor which allows me to pass in the identity
and the JobFactory which is used to create it ( I hope to write an post / question
on dependency injection next!). My problem is I don't intend the <strong>JobFactory</strong> property
of the <strong>Job</strong> object to be visiable outside the class code, so ideally
I would like to scope it as <font color="#0000ff">private</font>. However it is an
inherited member that already has a scope of <font color="#0000ff">public</font>.
I can quite clearly see the member like so...
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> TestClass
{ <font color="#7fffd4">Job</font> _job; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> TestClass()
{ _job <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Job</font>();
_job.JobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> ...;
} }</span>
        </pre>
        <p>
The only way I can see to hide the member is by overriding the <font color="#7fffd4">JobFactory</font> member
to the <font color="#7fffd4">Job</font> object with the <font color="#0000ff">new</font> modifier
like so.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> IJobFactory
JobFactory { <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.JobFactory;
} }</span>
        </pre>
        <p>
Is this really the best way to go about this, or am I doing something fundementally
wrong? Your comments appreciated!
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=e52ed85e-a815-451e-862d-710c6113e52a" />
      </body>
      <title>Interfaces, Inheritance and Scope - Am I being some kind of fool? It is April, afterall.</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/01/InterfacesInheritanceAndScopeAmIBeingSomeKindOfFoolItIsAprilAfterall.aspx</link>
      <pubDate>Tue, 01 Apr 2008 20:35:00 GMT</pubDate>
      <description>&lt;p&gt;
I'm sure most of us endeaver to keep the tightest scope possible for the members of
classes we write. But I have a situation where I just can't seem to be able to keep
the scope of members of my inherited class as tight as I would like.
&lt;/p&gt;
&lt;p&gt;
What I am intending to create is a &lt;strong&gt;Job&lt;/strong&gt; object that will inherit from
a &lt;strong&gt;BaseJob&lt;/strong&gt; object that will implement the &lt;strong&gt;IJob&lt;/strong&gt; interface.
While doing this I want to ensure the &lt;strong&gt;JobFactory&lt;/strong&gt; member of the &lt;strong&gt;Job&lt;/strong&gt; object
is kept private and the &lt;strong&gt;Id&lt;/strong&gt; member is public. Both of these members
belong in the &lt;strong&gt;BaseJob&lt;/strong&gt; object.
&lt;/p&gt;
&lt;p&gt;
So I'll start with new assembly that will contain&amp;nbsp;three interfaces; &lt;strong&gt;IJob,
IIdentifyable&lt;/strong&gt;&amp;nbsp;and &lt;strong&gt;IJobFactory&lt;/strong&gt;. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IJob&lt;/font&gt; : &lt;font color=#7fffd4&gt;IIdentifyable&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the factory that created the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; JobFactory { &lt;font color=#0000ff&gt;get&lt;/font&gt;; &lt;font color=#0000ff&gt;set&lt;/font&gt;;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the signature for an object that is identifyable&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IIdentifyable&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the identity of the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#0000ff&gt;int&lt;/font&gt; Id { &lt;font color=#0000ff&gt;get&lt;/font&gt;; &lt;font color=#0000ff&gt;set&lt;/font&gt;;
} }&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the signature required for a job factory.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Creates an object that implements the IJob interface.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;Returns an object that implements the IJob interface&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IJob&lt;/font&gt; Create(); &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Saves the current job&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Save(&lt;font color=#7fffd4&gt;IJob&lt;/font&gt; job);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;IJob&lt;/strong&gt; has a member which will be used to hold a reference to&amp;nbsp;an
object (that implements the &lt;strong&gt;IJobFactory&lt;/strong&gt; inteface) that created&amp;nbsp;it.
It will also have a Id member implemented through the &lt;strong&gt;IIdentifyable&lt;/strong&gt; interface.&amp;nbsp;&lt;strong&gt;IJobFactory&lt;/strong&gt; has
a method for returning a newly created object that implements the&amp;nbsp;&lt;strong&gt;IJob&lt;/strong&gt; interface
and a method to save an object that implements the &lt;strong&gt;IJob&lt;/strong&gt; interface.
Both interfaces must be &lt;font color=#0000ff&gt;public&lt;/font&gt; as they need to be seen&amp;nbsp;from
outside the assembly. As far as I am aware you canot declare the scope of the methods
in an interface so each will be of the same scope as the interface.
&lt;/p&gt;
&lt;p&gt;
I now need to another new assembly which will contain a base class (&lt;strong&gt;BaseJob&lt;/strong&gt;)
for my &lt;strong&gt;Job&lt;/strong&gt; object to inherit from. I create a reference to the interfaces
assembly, and allow the &lt;strong&gt;BaseJob&lt;/strong&gt; class will implement the &lt;strong&gt;IJob&lt;/strong&gt; interface
from it.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The base class that all Job objects should inherit from.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseJob
: IJob { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; _id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; _jobFactory; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IIdentifyable
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the identity of an Job&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Id
{ &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _id
; } &lt;font color=#0000ff&gt;set&lt;/font&gt; { _id &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IJob
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the factory that created the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#0000ff&gt;public&lt;/font&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; JobFactory
{ &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _jobFactory;
} &lt;font color=#0000ff&gt;set&lt;/font&gt; { _jobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now, I would like to set the scope of the &lt;strong&gt;BaseJob&lt;/strong&gt;'s&amp;nbsp;&lt;strong&gt;Id&lt;/strong&gt; and &lt;strong&gt;Jobfactory&lt;/strong&gt; members
as &lt;font color=#0000ff&gt;protected&lt;/font&gt;, do they can only be seen by objects that
inherit from the &lt;strong&gt;BaseJob&lt;/strong&gt; object. However if I try and&amp;nbsp;make&amp;nbsp;the
scope any tighter then VS advises me of an error when I try to build.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" color=#ff0000&gt;Error&amp;nbsp;1&amp;nbsp;'BaseJob' does not implement
interface member 'IJob.JobFactory'. 'BaseJob.JobFactory' is either static, not public,
or has the wrong return type.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
So as far as I can see it has to stay &lt;font color=#0000ff&gt;public&lt;/font&gt;. Any way on
to my &lt;strong&gt;Job&lt;/strong&gt; object now. In a third assembly, which has a reference
to both the base class assembly and the interfaces assembly,&amp;nbsp;&amp;nbsp;I create the &lt;strong&gt;Job&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Job
: BaseJob { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Job(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; id, &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; jobFactory)
: &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.Id &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.JobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; jobFactory;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The &lt;strong&gt;Job&lt;/strong&gt; class has a contructor which allows me to pass in the identity
and the JobFactory which is used to create it ( I hope to write an post / question
on dependency injection next!). My problem is I don't intend the &lt;strong&gt;JobFactory&lt;/strong&gt; property
of the &lt;strong&gt;Job&lt;/strong&gt; object to be visiable outside the class code, so ideally
I would like to scope it as &lt;font color=#0000ff&gt;private&lt;/font&gt;. However it is an inherited
member that already has a scope of &lt;font color=#0000ff&gt;public&lt;/font&gt;. I can quite
clearly see the member like so...
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; TestClass
{ &lt;font color=#7fffd4&gt;Job&lt;/font&gt; _job; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; TestClass()
{ _job &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Job&lt;/font&gt;();
_job.JobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ...;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The only way I can see to hide the member is by&amp;nbsp;overriding the&amp;nbsp;&lt;font color=#7fffd4&gt;JobFactory&lt;/font&gt; member
to the &lt;font color=#7fffd4&gt;Job&lt;/font&gt; object with the &lt;font color=#0000ff&gt;new&lt;/font&gt; modifier
like so.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; IJobFactory
JobFactory { &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.JobFactory;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Is this really the best way to go about this, or am I doing something fundementally
wrong? Your comments appreciated!
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=e52ed85e-a815-451e-862d-710c6113e52a" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
      <category>Scope</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=eeefddc3-6c61-4adb-aad3-d43f78c94121</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=eeefddc3-6c61-4adb-aad3-d43f78c94121</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h2>Background
</h2>
        <p>
As part of an ongoing project where a light weight client application needs to communicate
with a database via a webservice I have been trying to get my head around a the best
way to encapsulate data within custom objects and pass it from the client 's GUI to
the Web Service's Data Access layer without replicating too much code or having the
the client hold a reference to the same assembly as the webservice.
</p>
        <p>
The intention is for the client application to have 3 layers, the presentation layer,
a Business Entity / Business Logic layer and an Webservice Adapter layer. The Webservice
will have the WebService layer, it's own Business Entity / Business Logic layer and
a Data Access layer. 
</p>
        <h2>Initial Thoughts
</h2>
        <p>
My initial thoughts were to create an assembly which would hold a set of interfaces
that will describe the business entities that both parts of the application would
use. For example the interface fot the job object would be:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IJob
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> CreatedBy; <font color="#7fffd4">DateTime</font> CreatedOn;
. . <font color="#7fffd4">DateTime</font> LastAccessed; }</span>
        </pre>
        <p>
This would require that both pairs of business entity assemblies would need to have
a reference to the assembly. My plan was then to return from the webservice's webmethods
the interface rather than the object. For example.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[WebMethod] <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> IJob
GetJobById(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> id)
{ . . }</span>
        </pre>
        <p>
          <font size="1">
            <font size="2">My plan was then bind the form controls to the properties
of the interface but among a couple of minor flaws in my plan, there was <strong>one
big</strong> one... It appears that you <em>can not</em> return an interface from
a web service. I assume do to it not being serialisable. A little research later
it appears that a better way would be to would be to use a shared base class for the
business entities on both sides of the void, so to speak. So back to the drawing board
slightly..</font>
          </font>
        </p>
        <h2>Second Thoughts
</h2>
        <p>
So now I'm thinking I need to create an abstract base class for the objects
in the business entitiy assemblies in both the client and the webservice to inherit
from. If I ensure the base classes implement the interfaces, then I can still bind
my controls to the interfaces properties. So ignoring the various properties of the
class I now have a bas class that implements the previosu
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseJob
: IJob { . . }</span>
        </pre>
        <p>
So now the Job class in each business entity layer will inherit from the BaseJob abstract
class which implements the IJob interface.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Job
: BaseJob { . . }</span>
        </pre>
        <p>
          <font size="1">
            <font size="2">So this means that now rather than just having a reference
to the assembly with the interfaces in for each business entity assembly I now have
a reference to the base class assembly too. Is this really the way I want to go?</font>
          </font>
        </p>
        <p>
          <font size="1">To be continued...</font>
        </p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=eeefddc3-6c61-4adb-aad3-d43f78c94121" />
      </body>
      <title>Same objects on opposite sides of the void...</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</guid>
      <link>http://www.duanewingett.info/2008/03/31/SameObjectsOnOppositeSidesOfTheVoid.aspx</link>
      <pubDate>Mon, 31 Mar 2008 15:30:16 GMT</pubDate>
      <description>&lt;h2&gt;Background
&lt;/h2&gt;
&lt;p&gt;
As part of an ongoing project where a light weight client application needs to communicate
with a database via a webservice I have been trying to get my head around a the best
way to encapsulate data within custom objects and pass it from the client 's GUI to
the Web Service's Data Access layer without replicating too much code or having the
the client hold a reference to the same assembly as the webservice.
&lt;/p&gt;
&lt;p&gt;
The intention is for the client application to have 3 layers, the presentation layer,
a Business Entity / Business Logic layer and an Webservice Adapter layer. The Webservice
will have the WebService layer, it's own Business Entity / Business Logic layer and
a Data Access layer. 
&lt;/p&gt;
&lt;h2&gt;Initial Thoughts
&lt;/h2&gt;
&lt;p&gt;
My initial thoughts were to create an assembly which would hold a set of interfaces
that will describe the business entities that both parts of the application would
use. For example the interface fot the job object would be:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IJob
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; CreatedBy; &lt;font color=#7fffd4&gt;DateTime&lt;/font&gt; CreatedOn;
. . &lt;font color=#7fffd4&gt;DateTime&lt;/font&gt; LastAccessed; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This would require that both pairs of business entity assemblies would need to have
a reference to the assembly. My plan was then to return from the webservice's webmethods
the interface rather than the object. For example.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[WebMethod] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; IJob
GetJobById(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; id)
{ . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;font size=2&gt;My plan was then bind the form controls to the properties
of the interface but among a couple of minor flaws in my plan, there was &lt;strong&gt;one
big&lt;/strong&gt; one... It appears that you &lt;em&gt;can not&lt;/em&gt; return an interface from
a web service. I assume do to it not being serialisable.&amp;nbsp;A little research later
it appears that a better way would be to would be to use a shared base class for the
business entities on both sides of the void, so to speak. So back to the drawing board
slightly..&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;Second Thoughts
&lt;/h2&gt;
&lt;p&gt;
So now I'm thinking I&amp;nbsp;need to create an abstract&amp;nbsp;base class for the objects
in the business entitiy assemblies&amp;nbsp;in both the client and the webservice to inherit
from. If I ensure the base classes implement the interfaces, then I&amp;nbsp;can still&amp;nbsp;bind
my controls to the interfaces properties. So ignoring the various properties of the
class I now have a bas class that implements the previosu
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseJob
: IJob { . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So now the Job class in each business entity layer will inherit from the BaseJob abstract
class which implements the IJob interface.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Job
: BaseJob { . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;font size=2&gt;So this means that now rather than just having a reference
to the assembly with the interfaces in for each business entity assembly I now have
a reference to the base class assembly too. Is this really the way I want to go?&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;To be continued...&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=eeefddc3-6c61-4adb-aad3-d43f78c94121" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
      <category>Web Services</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=728e6c3c-172c-4292-9d99-3e53560fb6d8</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,728e6c3c-172c-4292-9d99-3e53560fb6d8.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,728e6c3c-172c-4292-9d99-3e53560fb6d8.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=728e6c3c-172c-4292-9d99-3e53560fb6d8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
As part of the preliminary research and design process for a prospective
new project at work one of the requirements will be for the application to provide
a webservice that can be consumed from a third party's Java application.
</p>
        <p>
So without further ado a quick .Net webservice was nocked up in C# with a single WebMethod.
The WebMethod would return a formatted string containing within it the value
passed in as a string parameter. The Webservice was deployed to localhost and tested
using the simple webform test that is present on a local instal.
</p>
        <p>
All good so far.
</p>
        <p>
Next is to build a small Java web project that can consume the webservice. So having
never written a Java application before, it's a quick trip to see Mr Google for a
working example of how to call a webservice from Java. ( <a href="http://www.duanewingett.info/2008/03/31/MyFirstJavaApp.aspx">See
My First Java App</a> ) Google thankfully turned up <a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=257552">David
Hobbs</a>' example "<span class="ArticleTopTitle" id="ctl00_ArticleTopHeader_ArticleTitle"><a href="http://www.codeproject.com/KB/XML/WSfromJava.aspx">How
to consume an ASP.NET webservice from Java via SOAP</a>"</span> on CodeProject. After
a an hour or after nocking up a quick Java webpage with an embedded applet
that will call the webservice and display the result, it should be a case of running
it and seeing it work like clockwork.... After all the whole idea of a webservice
is to platform unspecific, isn't it?....
</p>
        <p>
Well, I shouldn't really have expected miracles, should I? After a whole afternoon
of checking and double checking that the code was correct and using a free trial of <a href="http://www.prestosoft.com/edp_examdiffpro.asp">ExamDiff
Pro</a> to ensure that XML that the java code was out putting was exactly what
the XML that the server was expecting to receive I came to the conclusion that it
must be something perculiar to our network. The XML output was identical except for
the physical value of the parameter.
</p>
        <p>
So at the end of the day I was at a loss. I'm not sure if it is an error in the code,
or if the webservice expects different XML than what the description page offers up.
Or if there is a network security issue which isn't allowing communication between
the two applications. One of my .Net Windows applications calls a .Net webservice
which is built in the same way as this test one perfectly, and has done for the last
year! Event the third party representative was at a bit of a loss.
</p>
        <p>
So I am at a bit of a loss with this one. May be a fresh look in a new week will shed
some light. Who knows? If any one else has come accross this issue, and resolved or
not resolved it, please post and let me know your findings!
</p>
        <p>
          <hr />
        </p>
        <p>
        </p>
        <p>
          <font size="1">
            <strong>Postscript: #1</strong>
            <br />
          </font>I have just noticed on the botttom of David's CodeProject artical the following:
</p>
        <p>
          <em>
            <font color="#808080">
              <strong>Notes</strong>
              <br />
Some Java Virtual Machines (like the Microsoft one) only allow you to make a socket
connection to the same machine that hosts the Java class files. Therefore, if you're
using an applet like me, you will need to host the Java class files on the same machine
where the webservice resides.</font>
          </em>
        </p>
        <p>
I wonder if this has anything to do with the problem... Oh well, maybe tomorrow I'll
waste another fruitless afternoon trying to find out!! ;-)
</p>
        <p>
          <font size="1">
            <strong>Postscript: #2</strong>
          </font>
          <br />
Well after further investigation I haven't managed to determin the fault, but today
we tried a different approach and used the "Web Service proxy" object from the <strong>Business
Tier</strong> &gt; <strong>Web Services</strong> category in the New Gallery.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/NewGalleryWebService.jpg" border="0" />
        </p>
        <p>
We then followed the wizard and let it generate the code. The outputted code was tested
and hey presto!!
</p>
        <p>
So I guess this topic is closed for me now!
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=728e6c3c-172c-4292-9d99-3e53560fb6d8" />
      </body>
      <title>"Invalid Parameter" error when attempting to consume a .Net WebService from a Java application over network.</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,728e6c3c-172c-4292-9d99-3e53560fb6d8.aspx</guid>
      <link>http://www.duanewingett.info/2008/03/31/InvalidParameterErrorWhenAttemptingToConsumeANetWebServiceFromAJavaApplicationOverNetwork.aspx</link>
      <pubDate>Mon, 31 Mar 2008 14:07:10 GMT</pubDate>
      <description>&lt;p&gt;
As part of the&amp;nbsp;preliminary research&amp;nbsp;and design process for a prospective
new project at work one of the requirements will be for the application to provide
a webservice that can be consumed from a third party's&amp;nbsp;Java application.
&lt;/p&gt;
&lt;p&gt;
So without further ado a quick .Net webservice was nocked up in C# with a single WebMethod.
The WebMethod would return a&amp;nbsp;formatted string&amp;nbsp;containing within it the value
passed in as a string parameter. The Webservice was deployed to localhost and tested
using the simple webform test that is present on a local instal.
&lt;/p&gt;
&lt;p&gt;
All good so far.
&lt;/p&gt;
&lt;p&gt;
Next is to build a small Java web project that can consume the webservice. So having
never written a Java application before, it's a quick trip to see Mr Google for a
working example of how to call a webservice from Java. ( &lt;a href="http://www.duanewingett.info/2008/03/31/MyFirstJavaApp.aspx"&gt;See
My First Java App&lt;/a&gt; ) Google thankfully turned up &lt;a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=257552"&gt;David
Hobbs&lt;/a&gt;' example "&lt;span class=ArticleTopTitle id=ctl00_ArticleTopHeader_ArticleTitle&gt;&lt;a href="http://www.codeproject.com/KB/XML/WSfromJava.aspx"&gt;How
to consume an ASP.NET webservice from Java via SOAP&lt;/a&gt;"&lt;/span&gt; on CodeProject. After
a&amp;nbsp;an hour or after nocking up a quick Java webpage&amp;nbsp;with an embedded applet
that will call the webservice and display the result, it should be a case of running
it and seeing it work like clockwork....&amp;nbsp;After all the whole idea of a webservice
is to platform unspecific, isn't it?....
&lt;/p&gt;
&lt;p&gt;
Well, I shouldn't really have expected miracles, should I? After a whole afternoon
of checking and double checking that the code was correct and using a free trial of &lt;a href="http://www.prestosoft.com/edp_examdiffpro.asp"&gt;ExamDiff
Pro&lt;/a&gt; to ensure that XML that&amp;nbsp;the java code was out putting was exactly what
the XML that the server was expecting to receive I came to the conclusion that it
must be something perculiar to our network. The XML output was identical except for
the physical value of the parameter.
&lt;/p&gt;
&lt;p&gt;
So at the end of the day I was at a loss. I'm not sure if it is an error in the code,
or if the webservice expects different XML than what the description page offers up.
Or if there is a network security issue which isn't allowing communication between
the two applications. One of my .Net Windows applications calls a .Net webservice
which is built in the same way as this test one perfectly, and has done for the last
year! Event the third party representative was at a bit of a loss.
&lt;/p&gt;
&lt;p&gt;
So I am at a bit of a loss with this one. May be a fresh look in a new week will shed
some light. Who knows? If any one else has come accross this issue, and resolved or
not resolved it, please post and let me know your findings!
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;strong&gt;Postscript: #1&lt;/strong&gt;
&lt;br&gt;
&lt;/font&gt;I have just noticed on the botttom of David's CodeProject artical the following:
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;&lt;font color=#808080&gt;&lt;strong&gt;Notes&lt;/strong&gt;
&lt;br&gt;
Some Java Virtual Machines (like the Microsoft one) only allow you to make a socket
connection to the same machine that hosts the Java class files. Therefore, if you're
using an applet like me, you will need to host the Java class files on the same machine
where the webservice resides.&lt;/font&gt;&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I wonder if this has anything to do with the problem... Oh well, maybe tomorrow I'll
waste another fruitless afternoon trying to find out!! ;-)
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;strong&gt;Postscript: #2&lt;/strong&gt;&lt;/font&gt;
&lt;br&gt;
Well after further investigation I haven't managed to determin the fault, but today
we tried a different approach and used the "Web Service proxy" object from the &lt;strong&gt;Business
Tier&lt;/strong&gt; &amp;gt; &lt;strong&gt;Web Services&lt;/strong&gt; category&amp;nbsp;in the New Gallery.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/NewGalleryWebService.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
We then followed the wizard and let it generate the code. The outputted code was tested
and hey presto!!
&lt;/p&gt;
&lt;p&gt;
So I guess this topic is closed for me now!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=728e6c3c-172c-4292-9d99-3e53560fb6d8" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,728e6c3c-172c-4292-9d99-3e53560fb6d8.aspx</comments>
      <category>.Net</category>
      <category>Java</category>
      <category>SOAP</category>
      <category>Web Services</category>
      <category>XML</category>
      <category>JDeveloper</category>
    </item>
  </channel>
</rss>