<?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>Pearl Tech &#187; C#</title>
	<atom:link href="http://blog.pearltechnology.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pearltechnology.com</link>
	<description></description>
	<lastBuildDate>Thu, 05 Jan 2012 14:47:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XNA Tutorial Series 1: Golf (Patience) Solitaire Part II – Deck Class</title>
		<link>http://blog.pearltechnology.com/xna-tutorial-series-1-golf-patience-solitaire-part-ii-%e2%80%93-deck-class/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/xna-tutorial-series-1-golf-patience-solitaire-part-ii-%e2%80%93-deck-class/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 19:46:32 +0000</pubDate>
		<dc:creator>Chad Ferguson</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[XNA; Solitaire]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=1283</guid>
		<description><![CDATA[Golf Solitaire as described by wikipedia:
Golf is a Patience game where players try to earn the lowest number of points (as in golf, the sport) over the course of nine deals (or &#8220;holes&#8221; to further use golfing teminology). It has a tableau of 35 face-up cards and a higher ratio of skill to luck than [...]]]></description>
			<content:encoded><![CDATA[<p>Golf Solitaire as described by wikipedia:</p>
<p>Golf is a Patience game where players try to earn the lowest number of points (as in golf, the sport) over the course of nine deals (or &#8220;holes&#8221; to further use golfing teminology). It has a tableau of 35 face-up cards and a higher ratio of skill to luck than most other solitaire card games.</p>
<p>My attempts in this tutorial series will be able to keep the code reusable and hopefully build onto this series with other Card/Solitaire games. </p>
<p><strong>XNA Tutorial Series 1: Golf (Patience) Solitaire Part III &#8211; Deck Class</strong><br />
For this tutorial we will focus on the class that will handle the logic for a group of cards. Start by right clicking on your classes folder and adding a new class called Deck.cs. The first thing we will need to do is setup are using&#8217;s as follows:<br />
<code><br />
        using System;<br />
        using System.Collections.Generic;<br />
        using System.Linq;<br />
        using Microsoft.Xna.Framework;<br />
        using Microsoft.Xna.Framework.Content;<br />
        using Microsoft.Xna.Framework.Graphics;<br />
        using Microsoft.Xna.Framework.Input;<br />
</code></p>
<p>Next set your class to be public.</p>
<p>Now we can begin by adding a Enum to our class to represent a deck type:<br />
<code><br />
        public enum DeckType<br />
        {<br />
            User = 1,<br />
            Source,<br />
            Waste,<br />
            Target<br />
        };<br />
</code></p>
<p>Now we can setup the individual properties that a Card will have, I will show the code first and then proceed to explain what each property is for:<br />
<code><br />
        protected List cards = new List();<br />
        protected Point pos;<br />
        protected int deckID;<br />
        protected DeckType deckType;<br />
        Texture2D background;</p>
<p>        public virtual Point Position<br />
        {<br />
            get<br />
            {<br />
                return pos;<br />
            }<br />
            set<br />
            {<br />
                pos = value;<br />
            }<br />
        }</p>
<p>        public DeckType Type<br />
        {<br />
            get<br />
            {<br />
                return deckType;<br />
            }<br />
        }<br />
</code><br />
cards will be a list of the cards in the deck, pos is a point that is used for offsetting a card image so you can have one card on top of another and see the values for both. deckID is an ID for keeping track of the decks. DeckType to indicate the enum for which type of deck it is, and a texture to store the decks background.</p>
<p>Next we have our constructors:<br />
<code><br />
        public Deck()<br />
        {<br />
            deckType = DeckType.User;<br />
        }</p>
<p>        public Deck(int id)<br />
        {<br />
            deckID = id;<br />
            deckType = DeckType.User;<br />
        }<br />
</code></p>
<p>Since this deck class is going to be a base type it will default to being a user deck, other deck types (waste, source, etc..) will implement the desk class. Next we will need some basic methods for maintaining the list of cards that are in the deck.</p>
<p>First we will need to be able to retrieve the number of cards in the deck:<br />
<code><br />
        public virtual int CardCount()<br />
        {<br />
            return cards.Count();<br />
        }</p>
<p>Get the last card(top) of the deck<br />
        public virtual Card GetLast()<br />
        {<br />
            return cards.Last();<br />
        }</p>
<p>Remove the last card<br />
        public virtual Card RemoveLast()<br />
        {<br />
            Card card = cards.Last();<br />
            cards.Remove(card);<br />
            return card;<br />
        }</p>
<p>And finally get a card<br />
        public virtual Card GetCard(int index)<br />
        {<br />
            return cards[index];<br />
        }<br />
</code></p>
<p>In the second part of the deck class we will look at Adding cards, handling input, and drawing the cards.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/xna-tutorial-series-1-golf-patience-solitaire-part-ii-%e2%80%93-deck-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use of TransactionScope in .NET</title>
		<link>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 12:53:50 +0000</pubDate>
		<dc:creator>Geer</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MSDTC]]></category>
		<category><![CDATA[TransactionScope]]></category>

		<guid isPermaLink="false">http://blog.pearltechnology.com/?p=79</guid>
		<description><![CDATA[In one of the projects I’m currently working on, I have to handle multiple SQL operations in one transaction, so if anything goes wrong in a particular operation, things done in previous operations can be rolled back automatically.  This can be done by using Transaction in a Stored Procedure directly, but you might lose some [...]]]></description>
			<content:encoded><![CDATA[<p>In one of the projects I’m currently working on, I have to handle multiple SQL operations in one transaction, so if anything goes wrong in a particular operation, things done in previous operations can be rolled back automatically.  This can be done by using Transaction in a Stored Procedure directly, but you might lose some flexibility that way.  For example, when you want to insert an object with multiple children in database, it’s not easy to pass an array or a list of items (children object) to the Stored Procedure.  So, I considered using TransactionScope provided by .NET to handle this.</p>
<p>In order to get away from code being escalated to MSDTC (Microsoft Distributed Transaction Coordinator), I decided to use single SqlConnection per TransactionScope.  Also, to maximize code reusability, we ended up with the following development pattern.  For simplicity reason, error handling is not added in the example below.</p>
<pre style="font-family:Courier New;line-height:14px"><span style="color:blue">public</span> <span style="color:blue">static</span> <span style="color:blue">void</span> CreateOrder(<span style="color:#006080">Order</span> myOrder)
{
    <span style="color:blue">using</span> (<span style="color:#006080">TransactionScope</span> scope =
            <span style="color:blue">new</span> <span style="color:#006080">TransactionScope</span>(<span style="color:#006080">TransactionScopeOption</span>.RequiresNew))
    {
        <span style="color:blue">using</span> (<span style="color:#006080">SqlConnection</span> cn = <span style="color:#006080">DB</span>.GetConnection())
        {
            cn.Open();
            <span style="color:#006080">SqlCommand</span> cmd = <span style="color:blue">new</span> <span style="color:#006080">SqlCommand</span>();
            cmd.Connection = cn;

            InsertOrderTransaction(myOrder, cmd);
        }
        scope.Complete();
    }
}

<span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderTransaction(<span style="color:#006080">Order</span> myOrder, <span style="color:#006080">SqlCommand</span> cmd)
{
    cmd.CommandText = "dbo.spCOrder";
    cmd.<span style="color:#006080">CommandType</span> = <span style="color:#006080">CommandType</span>.StoredProcedure;

    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@CustomerID", myOrder.CustomerID);
    <span style="color:green">// ...</span>

    myOrder.ID = <span style="color:#006080">Convert</span>.ToInt64(cmd.ExecuteScalar());

    InsertOrderTransactionPostProcessing(myOrder, cmd);
}</pre>
<p>As you can see, the method InsertOrderTransaction takes the Order object and a SqlCommand instance. It assumes that the SqlConnection has been established for the SqlCommand in the calling method. It doesn’t care if the SqlCommand needs to be executed within a transaction or not. It simply executes a SQL statement or a Stored Procedure. In its calling method CreateOrder, it defines the TransactionScope and defines and opens a SqlConnection. It can call multiple transactional methods (in this case InsertTransaction) and keep them all in one transaction. However, you don’t have to define a TranscationScope in the calling method if you are not dealing with multiple SQL operations. You can also add pre-processing and post-processing functions if needed.</p>
<pre style="font-family:Courier New;line-height:14px"><span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderTransactionPostProcessing(<span style="color:#006080">Order</span> myOrder, <span style="color:#006080">SqlCommand</span> cmd)
{
    <span style="color: #0000ff">if</span> (myOrder.OrderItems != <span style="color:blue">null</span>)
    {
        <span style="color: #0000ff">foreach </span>(<span style="color:#006080">OrderItem</span> myItem<span style="color:blue"> in </span>myOrder.OrderItems)
        {
            myItem.OrderID = myOrder.ID;
            InsertOrderItemTransaction(myItem, cmd);
        }
    }
}

<span style="color:blue">internal</span> <span style="color:blue">static</span> <span style="color:blue">void</span> InsertOrderItemTransaction(<span style="color:#006080">OrderItem</span> myItem, <span style="color:#006080">SqlCommand</span> cmd)
{
    cmd.CommandText = "dbo.spCOrderItem";
    cmd.<span style="color:#006080">CommandType</span> = <span style="color:#006080">CommandType</span>.StoredProcedure;

    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@ItemID", myItem.ItemID);
    <span style="color:green">// ...</span>
    myItem.OrderItemID = <span style="color:#006080">Convert</span>.ToInt64(cmd.ExecuteScalar());
}</pre>
<p>One thing you have to be aware of is that TransactionScope may not be a good choice if these operations take too long to complete. The reason is obvious. Transaction may result in temporary locking on certain objects (such as tables) in your database. If the transaction needs 5 minutes to complete, it may lock a table for that long and your users may be unable to access data in the table for that long. If anything goes wrong at the very last second in the transaction, it may take another 5 minutes for the database to roll back previous operations in this transaction, and make the table inaccessible to your users for even longer. So in cases like this, one transaction may need to be broken into multiple segments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pearltechnology.com/use-of-transactionscope-in-net-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

