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

<channel>
	<title>Atlantic BT &#187; Jeremy Wiggins</title>
	<atom:link href="http://www.atlanticbt.com/blog/author/jeremyw/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atlanticbt.com/blog</link>
	<description>Internet Marketing and Web Development in Raleigh</description>
	<lastBuildDate>Wed, 08 Feb 2012 02:40:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tip: Choose which browser to debug with in Visual Studio</title>
		<link>http://www.atlanticbt.com/blog/tip-choose-which-browser-to-debug-with-in-visual-studio/</link>
		<comments>http://www.atlanticbt.com/blog/tip-choose-which-browser-to-debug-with-in-visual-studio/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 14:10:22 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=2027</guid>
		<description><![CDATA[By default, Visual Studio uses Internet Explorer for debugging. If you&#8217;re looking for a way to change that, perhaps to harness the power of developer tools in Firefox or other browsers (or perhaps just because you don&#8217;t like IE), it&#8217;s pretty easy to do. Right-click your start-up project in Studio and click Browse With&#8230; Click [...]]]></description>
			<content:encoded><![CDATA[<p>By default, Visual Studio uses Internet Explorer for debugging.  If you&#8217;re looking for a way to change that, perhaps to harness the power of <a href="http://www.atlanticbt.com/blog/the-essential-firefox-add-ons-for-your-web-development-toolbox/" target="blank">developer tools</a> in Firefox or other browsers (or perhaps just because you don&#8217;t like IE), it&#8217;s pretty easy to do.</p>
<ol>
<li>Right-click your start-up project in Studio and click Browse With&#8230;</li>
<li>Click Add</li>
<li>Click the Browse button beside Program Name</li>
<li>Select the browser you wish to use</li>
<li>Click Ok</li>
<li>Select the browser you just added and click Set as Default</li>
</ol>
<p>Here are the install locations of some of the more common browsers:</p>
<ul>
<li>Firefox: C:\Program Files\Mozilla Firefox</li>
<li>Chrome: C:\Documents and Settings\UserName\Local Settings\Application Data\Google\Chrome\Application</li>
<li>Safari: C:\Program Files\Safari</li>
<li>Opera: C:\Program Files\Opera</li>
<li>IE: C:\Program Files\Internet Explorer</li>
</ul>
<p>I find myself using Firefox the most, followed by Chrome.  And as a best practice, don&#8217;t forget to test your projects in <em>all</em> browsers before putting them into production!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/tip-choose-which-browser-to-debug-with-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Database Performance Tuning, Part 1 &#8211; Keys and Data Types</title>
		<link>http://www.atlanticbt.com/blog/database-performance-tuning-part-1/</link>
		<comments>http://www.atlanticbt.com/blog/database-performance-tuning-part-1/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 14:55:16 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Foreign Key]]></category>
		<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Primary Key]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=1971</guid>
		<description><![CDATA[Database performance tuning is a pretty strong interest of mine, and I&#8217;ve been lucky enough to get to work on some really cool database-intensive projects in my time at Atlantic BT. I&#8217;ll be the first to admit that I&#8217;m still not an expert on the topic and probably won&#8217;t be for some time. But having [...]]]></description>
			<content:encoded><![CDATA[<p>Database performance tuning is a pretty strong interest of mine, and I&#8217;ve been lucky enough to get to work on some really cool database-intensive projects in my time at Atlantic BT.  I&#8217;ll be the first to admit that I&#8217;m still not an expert on the topic and probably won&#8217;t be for some time.  But having said that, I&#8217;ve had to pick up on at least a few things to get by, and I&#8217;d like to share some of those over the course of my next few blog posts.</p>
<h2>Two Common, But Easily Correctable Problems</h2>
<p>Two of the most common problems I&#8217;ve seen in databases are a lack of primary and foreign key constraints and poorly typed data.  These problems can and will lead to performance issues, and even worse, bad data.  Luckily, these are also two of the easiest problems to correct.</p>
<h3>Keys</h3>
<p><img class="alignright size-full wp-image-1985" src="http://d1rvlzmuzboe2s.cloudfront.net/blog/wp-content/uploads/2010/03/sql1_1.JPG" alt="Set Primary Key" width="196" height="250" />If you want to increase the performance of your database, then here&#8217;s a great starting point.  Make sure that your tables have keys.  EVERY table should have at least a primary key.  But you shouldn&#8217;t stop there; you should also have foreign key constraints on many of your tables (keep in mind we call them &#8220;relational databases&#8221; for a reason &#8211; so create some relationships between the tables!).  This has two benefits.</p>
<p>The first is the preservation of data integrity.  Consider a simple example for a moment &#8211; two tables, Members and Address.  The Members table has 2 columns, Id and MemberName.  The Address table has a MemberId column and all the expected fields for addresses.  Let&#8217;s suppose there is an undetected bug in an application that updates or inserts to these tables.  Now think about the application that would consume the data.  What happens when two members have the same ID?  What about when the MemberId in the Address table doesn&#8217;t correspond to an actual Id in the Members table?</p>
<p>In software development, bugs are an unfortunate reality.  Having strict key constraints in your database will add an additional degree of safety to your applications.  In our example above, the Members table should have the Id column marked as a primary key and the Address table should have it&#8217;s MemberId column marked as a foreign key to the Member table&#8217;s Id field.</p>
<p>The second benefit is increased performance.  Foreign keys will help the optimizer establish relationships between tables and primary keys will auto-generate a clustered index on the table.  I&#8217;ll touch on this in more detail later in this post as well as in my next post but these both offer gains in performance.</p>
<h3>Data Types</h3>
<p>Another common problem is poorly typed data.  An extreme example of this is every column in a table being labeled as varchar(max) and allowing nulls, but lesser offenses can still cause problems.  Pay attention to the type, max length, and nullability of your columns and make wise decisions in setting these properties.  In general, your decisions should demonstrate simple logic &#8211; use varchar(2) instead of varchar(max) if you&#8217;re storing State abbreviations, use int instead of varchar if you&#8217;re storing a number, etc.</p>
<h2>An Example</h2>
<p>Both of these concepts are easy to demonstrate.  If you have access to a testing environment, run the following script to create a couple of test tables.</p>
<pre><span style="color: blue">CREATE TABLE</span> [dbo].[WidgetDescription]
(
    [Id] [varchar](255) <span style="color: blue">COLLATE</span> SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>,
    [ItemName] [varchar](255) <span style="color: blue">COLLATE</span>  SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>,
    [Description] varchar(255) <span style="color: blue">COLLATE</span>  SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>
)
GO

<span style="color: blue">CREATE TABLE</span> [dbo].[WidgetCount]
(
    [Id] [varchar](255) <span style="color: blue">COLLATE</span> SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>,
    [WidgetId] [varchar](255) <span style="color: blue">COLLATE</span> SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>,
    [NumberProduced] varchar(255) <span style="color: blue">COLLATE</span> SQL_Latin1_General_CP1_CI_AS <span style="color: gray">NOT NULL</span>
)

<span style="color: blue">DECLARE</span> @i <span style="color: blue">int</span>
<span style="color: blue">SET</span> @i = 0

<span style="color: blue">WHILE</span> @i &lt; 100000
<span style="color: blue">BEGIN</span>
    <span style="color: blue">INSERT INTO</span> [WidgetDescription] (Id, ItemName, Description)
    <span style="color: blue">VALUES</span> (<span style="color: purple">cast</span>(@i <span style="color: blue">as varchar</span>), <span style="color: red">'Widget'</span> + <span style="color: purple">cast</span>(@i <span style="color: blue">as varchar</span>), <span style="color: red">'Description of the Widget'</span>)

    <span style="color: blue">INSERT INTO</span> [WidgetCount] (Id, WidgetId, NumberProduced)
    <span style="color: blue">VALUES</span> (<span style="color: purple">cast</span>(@i <span style="color: blue">as varchar</span>), <span style="color: purple">cast</span>(@i <span style="color: blue">as varchar</span>), <span style="color: purple">cast</span>(@i <span style="color: blue">as varchar</span>))

    <span style="color: blue">SET</span> @i = @i <span style="color: gray">+</span> 1
<span style="color: blue">END</span></pre>
<p>We&#8217;ve created 2 (poorly designed) tables that contain information about widgets.  Now let&#8217;s examine some simple queries.  Note that I&#8217;m using STATISTICS TIME to monitor the run-time of the queries.  To see this information, click the Messages tab in the output when the query is complete.</p>
<pre><span style="color: blue">SET STATISTICS TIME ON</span>
<span style="color: blue">SELECT</span> NumberProduced <span style="color: blue">FROM</span> WidgetCount <span style="color: blue">WHERE</span> WidgetId = 1
<span style="color: blue">SET STATISTICS TIME OFF</span>

<span style="color: blue">SET STATISTICS TIME ON</span>
<span style="color: blue">SELECT</span> Description <span style="color: blue">FROM</span> WidgetDescription <span style="color: blue">WHERE</span> Id = 1
<span style="color: blue">SET STATISTICS TIME OFF</span></pre>
<p>My average run-time for the queries is in the 24 ms and 26 ms range, respectively.</p>
<div id="attachment_2010" class="wp-caption alignright" style="width: 334px"><img class="alignright size-full wp-image-1998" src="http://d1rvlzmuzboe2s.cloudfront.net/blog/wp-content/uploads/2010/03/sql1_2.JPG" alt="Run Time" width="324" height="172" /><p class="wp-caption-text">Run Time of our 2nd query before any optimizations</p></div>
<p>Now let&#8217;s take some simple measures to improve the tables and see the difference it makes.  First we&#8217;ll just make some simple data-type changes to the WidgetCount table.  Our columns are meant to represent numbers, so they should all be of type int.  For the WidgetDescription table, we&#8217;ll make similar data type changes and add a primary key.  Make the Id column an int, set it to Identity with auto-increment, and make it a primary key.  Shorten the ItemName column to only allow 25 characters and shorten the Description column to allow 100.  (In a production environment, we would also set up a foreign key constraint between the WidgetId column of the WidgetCount table and the Id column of the WidgetDescription table, but this isn&#8217;t necessary for this example.)</p>
<div id="attachment_2009" class="wp-caption alignright" style="width: 322px"><img class="size-full wp-image-2009" src="http://d1rvlzmuzboe2s.cloudfront.net/blog/wp-content/uploads/2010/03/sql1_3.JPG" alt="The same query after applying primary key and data type changes" width="312" height="165" /><p class="wp-caption-text">The same query after applying primary key and data type changes</p></div>
<p>Now run the queries again, and you should see run-times averaging around 8 ms and 1 ms respectively.  As you can see, changing the data types boosted performance, and adding a primary key additionally boosted performance.  If a difference of a few milliseconds doesn&#8217;t seem like a big deal, try thinking of it this way.  In the second query, <strong>making a few simple changes to our table resulted in a performance boost of 2500%</strong>.  That is indeed a big deal.  It might not be so noticeable when dealing with milliseconds, but when you bring a query that takes 10 seconds to run down to under 1, it will be.</p>
<p>So how do we explain this boost?  Two things.  As for the data types, ints use less disc space for storage than varchars, and varchar(25) and varchar(100) use less than varchar(255).  So there is simply less physical data to actually inspect and return.  But the bigger difference is the primary key.  Without the key, the optimizer can&#8217;t know that the values in the Id column are unique.  So even though it finds a match after examining one row of data, it has to look at the other 99,999 to make sure there isn&#8217;t another match.  However, once we add the key constraint, the optimizer knows it can stop looking after it finds a single match.  The optimizer is doing a table scan (slow) in one instance and an index seek (fast) in the other.  Again, I&#8217;ll cover this in more detail in my next post.</p>
<p>To summarize, I&#8217;ve outlined two very easy rules to follow to ensure that your databases are well designed.  You should also start to see some performance boosts as a result of enforcing these rules.  In my next post, I&#8217;ll show you how to examine the optimizer&#8217;s execution plan to create indexes on your tables in order to further improve your database&#8217;s performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/database-performance-tuning-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updating a .NET Page in Real-Time Using jQuery and AJAX</title>
		<link>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/</link>
		<comments>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 18:40:50 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[postbacks]]></category>
		<category><![CDATA[real time]]></category>
		<category><![CDATA[real time updates]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=1705</guid>
		<description><![CDATA[Here&#8217;s a problem I bet you&#8217;ve run into before. You have a long-running function on one of your .NET pages and you want to provide real-time feedback to the user while they&#8217;re waiting on the function to complete (by function, I mean a method or a set of methods in the code-behind). So you put [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a problem I bet you&#8217;ve run into before.  You have a long-running function on one of your .NET pages and you want to provide real-time feedback to the user while they&#8217;re waiting on the function to complete (by function, I mean a method or a set of methods in the code-behind).  So you put a variable somewhere on your page and you update it as the function runs.  Then you realize that the variable value won&#8217;t actually change on the front-end until the postback is complete, which in turn means that it will get updated only once and say &#8220;Finished.&#8221;  Just in time to tell the user what they already know.</p>
<p>To get around this conundrum, I present to you jquery and ajax, my two favorite web technologies ever.  <abbr title="Asynchronous JavaScript + XML">AJAX</abbr> is a group of technologies that allows you to interact with the server from the client-side without affecting the behavior of the existing page.  What this means is that you can post to a page without getting the associated postback from .NET (and subsequent page refresh).  If that doesn&#8217;t sound important, trust me, it is.  If we can post to a page without getting a postback, then we&#8217;re already pretty close to real-time updates.  All we need to do now is use jQuery to update the <acronym title="Document Object Model">DOM</acronym> as we do posts.  Sound complicated?  It isn&#8217;t.</p>
<p>Back to our example, suppose a user clicks a button and that button fires a postback and runs 3 methods in the code-behind, DoStep1(), DoStep2(), and DoStep3().  The first thing we need to do is simply move these methods to a new page.  If your original page was called <strong>DoWork.aspx</strong>, then a good name for the new page would be <strong>DoWork_Ajax.aspx</strong>.</p>
<p>Now, back on <strong>DoWork.aspx</strong>, you probably have an asp element on your page that you&#8217;d like to update as the functions progress.  You can replace this with a standard HTML element.  <code>DoWork()</code> is a javascript function that will update this element.  It looks like this:</p>
<pre>
function DoWork(step)
{
    if (step == 1)
    {
        $("#update-msg").html("Starting Work");
    }

    if (step == 4)
    {
        $("#update-msg").html("Finished");
        return false;
    }        

    $("#update-msg").load(
        "DoWork_Ajax.aspx #results span",
        { workStep: step },
        function(){ DoWork(step + 1); }
    );
}<span></span>
</pre>
<p>Assume that <code>#update-message</code> is a span on our page, i.e. a placeholder for the status of our worker functions.</p>
<p>The logic flows as such:  If we&#8217;re on step 1, tell the user we&#8217;re starting.  If we make it past step 3, we&#8217;re done, so tell the user that too and return from this function.  Finally, the meat of the function &#8211; Do an asyncronous post to <strong>DoWork_Ajax.aspx</strong>, telling it what step we&#8217;re on, and injecting a span from it into the DOM for the current page.</p>
<p>The <code>PageLoad</code> method for <strong>DoWork_Ajax.aspx</strong> looks like this:</p>
<pre>
switch (Request["workStep"])
{
    case "1":
        DoStep1();
        break;
    case "2":
        DoStep2();
        break;
    case "3":
        DoStep3();
        break;
}
</pre>
<p>Each method updates an element on the page, just like before.  This is the element that gets injected into the DOM by our jQuery function on <strong>DoWork.aspx.</strong></p>
<p>You&#8217;re doing the exact same work as before, but now you can update the page in real time.  I&#8217;ve included a <a href="/downloads/AjaxJQSampleApp.zip">sample application</a> that demonstrates everything I&#8217;ve talked about.  For those that are interested, you can download the project and run it locally to see this in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/updating-a-net-page-in-real-time-using-jquery-and-ajax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamic Body ID and Class properties on ASP.NET Master Pages</title>
		<link>http://www.atlanticbt.com/blog/dynamic-body-id-and-class-properties-on-asp-net-master-pages/</link>
		<comments>http://www.atlanticbt.com/blog/dynamic-body-id-and-class-properties-on-asp-net-master-pages/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 18:34:10 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[body class]]></category>
		<category><![CDATA[body id]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[master page]]></category>
		<category><![CDATA[masterpage]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=1630</guid>
		<description><![CDATA[Master pages are a great feature of ASP.NET. However, they do have some drawbacks, one being they do not easily offer the flexibility of dynamic body ids and classes that our design team here needs. Luckily a solution exists, and best of all it isn’t all that difficult to implement. The solution is built around [...]]]></description>
			<content:encoded><![CDATA[<p><dfn title="Master pages allow you to create a consistent layout for the pages in your application. (Source: MSDN)">Master pages</dfn> are a great feature of ASP.NET.  However, they do have some drawbacks, one being they do not easily offer the flexibility of dynamic body ids and classes that our design team here needs.</p>
<p>Luckily a solution exists, and best of all it isn’t all that difficult to implement.  The solution is built around the idea that child pages can access their master page, as well as any publicly exposed properties on the master.  For example,  in the <code>Page_Load</code> method of a child page, you can write:</p>
<pre>MyMasterPage masterPage = Master as MyMasterPage;
masterPage.Property = “property value”;</pre>
<p>This is not ideal though, because we are now committed to never changing our child’s master page; if we do, it will break our build.  The correct implementation of this concept is to create an interface that all of our master pages will implement, then use that in our child pages’ <code>Page_Load</code> method:</p>
<pre>IMasterPage masterPage = Master as IMasterPage;
masterPage.Property = “property value”;</pre>
<p>We can still set our property this way, only now we can later change what master page we use, as long as the new one still implements the same interface.  Good stuff.  So good, that it puts us most of the way there towards dynamic body ids and classes.</p>
<p>To get these, we need a couple of properties on our interface:</p>
<pre>public interface IMasterPage
{
     String BodyId { get; set; }
     String BodyClass { get; set; }
}<span> </span></pre>
<p>Then, in the code-behind file for our actual master page:</p>
<pre>public partial class MasterPagesDefault : MasterPage, IMasterPage
{

    private string _bodyId;
    private string _bodyClass;

    public string BodyId
    {
        get { return _bodyId; }
        set { _bodyId = value; }
    }

    public string BodyClass
    {
        get { return _bodyClass; }
        set { _bodyClass = value;}
    }
}<span> </span></pre>
<p>Finally, in the master page’s <code>body</code> tag:</p>
<pre>&lt;body id="<span>&lt;%=</span> BodyId %&gt;" class="<span>&lt;%=</span> BodyClass %&gt;"&gt;</pre>
<p>Now, to set these dynamically, use the child page’s <code>Page_Load</code> method just like before:</p>
<pre>IMasterPage masterPage = Master as IMasterPage;
if (masterPage != null)
{
    masterPage.BodyId = “index”;
    masterPage.BodyClass = “index two-col”;
}<span> </span></pre>
<p>By using publicly accessible properties, we give child pages the ability to set values on their master page.  With this, it is easy to create dynamic body classes and ids in .NET.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/dynamic-body-id-and-class-properties-on-asp-net-master-pages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>.NET Online Payment Gateways: A Factory Pattern Model</title>
		<link>http://www.atlanticbt.com/blog/net-online-payment-gateways-a-factory-pattern-model/</link>
		<comments>http://www.atlanticbt.com/blog/net-online-payment-gateways-a-factory-pattern-model/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 13:30:02 +0000</pubDate>
		<dc:creator>Jeremy Wiggins</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[factory pattern]]></category>
		<category><![CDATA[payment gateway]]></category>
		<category><![CDATA[resusable]]></category>
		<category><![CDATA[reuse]]></category>

		<guid isPermaLink="false">http://www.atlanticbt.com/blog/?p=1167</guid>
		<description><![CDATA[One thing that any good development company should do is strive to create reusable code.  Thinking back to projects I’ve worked on in my first year here, it occurred to me that quite a few of them have involved some form of online payment processing.  However, each client has used a different payment processor that [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that any good development company should do is strive to create reusable code.  Thinking back to projects I’ve worked on in my first year here, it occurred to me that quite a few of them have involved some form of online payment processing.  However, each client has used a different payment processor that has required custom programming.  I began thinking how nice it would be to submit a generic payment object to a gateway with large disregard to what particular vendor the payment is being submitted to.   Early on in developing this, it occurred to me that a factory pattern would be the way to go; essentially what I want is a “factory” that will figure out what type of gateway I need and construct that for me.</p>
<p>To start things, I made a simple payment gateway interface to make sure my gateway objects will do the one thing I need them to:</p>
<pre><code>
public interface IPaymentGateway
{
    string SubmitPayment(Payment payment);
}<span></span>
</code></pre>
<p>Essentially what I’m saying here is that no matter what type of gateway I’m using, I’ll need to submit a payment.</p>
<p>Next, we have a simple factory pattern to return some type of gateway:</p>
<pre><code>public class PaymentGatewayFactory
{
    public IPaymentGateway CreatePaymentGateway()
    {
        PaymentGatewayRepository paymentGatewayRepository = new PaymentGatewayRepository();
        string gatewayType = paymentGatewayRepository.GetGatewayType();

        switch(gatewayType)
        {
            case "Authnet":
                return new Authnet();
            case "Paypal":
                return new Paypal();
            case "Payflow":
                return new PayflowPro();
            default:
                throw new NotSupportedException("This payment gateway is not supported.");
        }<span></span>
    }<span></span>
}<span></span></code></pre>
<p>Here we have a call to a PaymentGatewayRepository class (not included), which will read a config file and return a string representing what type of payment gateway the client is using.  Next, a simple switch is done that will return the appropriate payment gateway class (or a custom exception if the gateway is not yet supported).  Each of the gateway classes implements the IPaymentGateway interface.</p>
<p>The beauty of this is that once this project is built, submitting payments is a breeze.  Just include the assembly in a project, reference it, and then follow these three easy steps:</p>
<ol>
<li>Add the necessary config keys.
<pre><code>&lt;!-- Gateway types | Possible Values: Authnet, Paypal, Payflow --&gt;
&lt;add key="GatewayType" value="Authnet"&gt;

&lt;!-- Authnet Keys --&gt;
&lt;add key="Anet_URL" value="https://test.authorize.net/gateway/transact.dll"/&gt;
&lt;add key="payment_login_key" value="123456789"/&gt;
&lt;add key="payment_tran_key" value="987654321"/&gt;
&lt;add key="payment_auth_name" value="Example"/&gt;</code></pre>
</li>
<li>Build the payment object.
<pre><code>Payment payment = new Payment();
payment.BillingFirstName = "Jeremy";
payment.BillingLastName = "Wiggins";

etc…</code></pre>
</li>
<li>Submit the payment.
<pre><code>IPaymentGateway gateway = new PaymentGatewayFactory().CreatePaymentGateway();
gateway.SubmitPayment(payment);</code></pre>
</li>
</ol>
<p>And that’s all there is to it.  This will be a nice addition to a reusable component library.  Once the logic has been implemented for a new payment gateway, it never has to be thought about again.  Just add a few lines to a config file and submit payments in a couple of easy steps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atlanticbt.com/blog/net-online-payment-gateways-a-factory-pattern-model/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching using disk: basic
Object Caching 433/514 objects using disk: basic
Content Delivery Network via d1rvlzmuzboe2s.cloudfront.net

Served from: www.atlanticbt.com @ 2012-02-08 06:07:25 -->
