<?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>Jason Mitchell</title>
	<atom:link href="http://jason-mitchell.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason-mitchell.com</link>
	<description></description>
	<lastBuildDate>Sat, 24 Mar 2012 21:29:19 +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>Quick Tip: Fixing Incorrectly Sized Bounding Sphere for 3DS Model in XNA</title>
		<link>http://jason-mitchell.com/quick-tip/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/</link>
		<comments>http://jason-mitchell.com/quick-tip/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 12:00:47 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[Quick Tip]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[3D Graphics]]></category>
		<category><![CDATA[quick tip]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=478</guid>
		<description><![CDATA[Recently I&#8217;ve been spending a lot of time working on a 3D game engine using XNA and wanted to implement some model measuring functionality.  I noticed that my measurements weren&#8217;t coming out quite right so with the help of the App Hub&#8217;s shape renderer sample I was able to visualise the bounding sphere for the model and [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Recently I&#8217;ve been spending a lot of time working on a 3D game engine using XNA and wanted to implement some model measuring functionality.  I noticed that my measurements weren&#8217;t coming out quite right so with the help of the App Hub&#8217;s <a href="http://create.msdn.com/en-US/education/catalog/sample/shape_rendering" target="_blank">shape renderer sample</a> I was able to visualise the bounding sphere for the model and noticed it was significantly smaller than my model.  After some poking around and googling I discovered that <strong>I needed to set the units in my install of 3DS Max to be centimeters</strong> &#8211; once this was done and I exported my model again the bounding spheres were correctly sized and my model measurements were returning the correct results!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/quick-tip/quick-tip-fixing-incorrectly-sized-bounding-sphere-for-3ds-model-in-xna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started With Fluent NHibernate and Auto Mapping in C#</title>
		<link>http://jason-mitchell.com/asp-net-programming/getting-started-with-fluent-nhibernate-and-auto-mapping-in-c/</link>
		<comments>http://jason-mitchell.com/asp-net-programming/getting-started-with-fluent-nhibernate-and-auto-mapping-in-c/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 21:10:38 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[data access]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[sample]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=457</guid>
		<description><![CDATA[I always hear good things about NHibernate and how it is better than alternatives such as Entity Framework so I decided to try it out for myself; not being a fan of large XML configuration files I went the route of using Fluent NHibernate which provides a fluent interface for configuration purposes.  Even though I&#8217;ve recently [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I always hear good things about NHibernate and how it is better than alternatives such as Entity Framework so I decided to try it out for myself; not being a fan of large XML configuration files I went the route of using Fluent NHibernate which provides a <a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_blank">fluent interface</a> for configuration purposes.  Even though I&#8217;ve recently become a big fan of ASP.NET MVC 3 I decided to try out NHibernate in an ASP.NET Web Forms application since that&#8217;s what I work with every day.</p>
<p style="text-align: justify;"><span id="more-457"></span></p>
<p style="text-align: justify;"><strong>Note: I&#8217;ve provided a download link to my code at the end of this article.</strong></p>
<p style="text-align: justify;">I started out by creating an empty ASP.NET project and added a basic web form which creates a GridView to display some results from the database (which I&#8217;ve included in the App_Data folder).  Next using <a href="http://nuget.org/" target="_blank">NuGet</a> (one of the best tools ever!) I installed the Fluent NHibernate package and all of its dependencies in my web application.</p>
<p style="text-align: justify;">I created the following class to represent a single record from tUser in my application:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> User
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">int</span> ID <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> EmailAddress <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> Password <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Note the use of the virtual keyword here.  Without it we would get an NHibernate.InvalidProxyTypeException with an error message similar to:</p>
<blockquote><p>The following types may not be used as proxies:<br />
WebApplication1.Entities.User: method get_Password should be &#8216;public/protected virtual&#8217; or &#8216;protected internal virtual&#8217;<br />
WebApplication1.Entities.User: method set_Password should be &#8216;public/protected virtual&#8217; or &#8216;protected internal virtual&#8217;</p></blockquote>
<p style="text-align: justify;">The next step was to set up and configure NHibernate for my application.  For database access using NHibernate we need two objects; ISessionFactory and ISession.  The ISessionFactory interface is created during the fluent configuration of NHibernate and allows us to &#8220;open&#8221; a session by calling mySessionFactory.OpenSession().  This method call returns an instance of ISession which can then be used to create queries, handle transactions and add/remove/update items in the database.</p>
<p style="text-align: justify;">I wanted to only create a single session factory for the entire application and a open a single session per request so I added a Global.asax file to my project in order to take advantage of the Application_Start and Application_BeginRequest events.  This is where I configured my application using the code below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Global <span style="color: #008000;">:</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">HttpApplication</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Start<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Application<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSessionFactory&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> CreateSessionFactory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> ISessionFactory CreateSessionFactory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> Fluently<span style="color: #008000;">.</span><span style="color: #0000FF;">Configure</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;">&#40;</span>MsSqlConfiguration<span style="color: #008000;">.</span><span style="color: #0000FF;">MsSql2008</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">ConnectionString</span><span style="color: #008000;">&#40;</span>ConfigurationManager<span style="color: #008000;">.</span><span style="color: #0000FF;">AppSettings</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;ConnectionString&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0000FF;">Mappings</span><span style="color: #008000;">&#40;</span>m <span style="color: #008000;">=&gt;</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">AutoMappings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>CreateAutomappings<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0000FF;">BuildSessionFactory</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> AutoPersistenceModel CreateAutomappings<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> AutoMap
            <span style="color: #008000;">.</span><span style="color: #0000FF;">AssemblyOf</span><span style="color: #008000;">&lt;</span>AutomappingConfiguration<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AutomappingConfiguration<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Override</span><span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>u <span style="color: #008000;">=&gt;</span> u<span style="color: #008000;">.</span><span style="color: #0000FF;">Table</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;tUser&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_BeginRequest<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ISessionFactory sessionFactory <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ISessionFactory<span style="color: #008000;">&#41;</span> Application<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSessionFactory&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSession&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> sessionFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">OpenSession</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_EndRequest<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ISession session <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ISession<span style="color: #008000;">&#41;</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSession&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        session<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_End<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ISessionFactory sessionFactory <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ISessionFactory<span style="color: #008000;">&#41;</span>Application<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSessionFactory&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        sessionFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Dispose</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;" lang="csharp">The Application_Start method is responsible for creating my session factory by calling the CreateSessionFactory method and storing it at the application level.  The CreateSessionFactory method sets the connection string and sets up the auto mapping by calling the CreateAutomappings.  This method sets the auto mapping configuration which defines what classes to automatically and overrides the table name that my User class should map to.  The auto mapping configuration provides quite a lot of functionality not covered in this article but you can find some examples of configuration at <a href="http://wiki.fluentnhibernate.org/Auto_mapping">http://wiki.fluentnhibernate.org/Auto_mapping</a>.</p>
<p style="text-align: justify;" lang="csharp">In the Application_BeginRequest method I retrieve the my application&#8217;s session factory, open a new session and attach it to the current requests context so it can be accessed in my ASP.NET pages:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_Load<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ISession session <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>ISession<span style="color: #008000;">&#41;</span> Context<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;NHSession&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
    CreateUsers<span style="color: #008000;">&#40;</span>session<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var users <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> u <span style="color: #0600FF; font-weight: bold;">in</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> u<span style="color: #008000;">;</span>
&nbsp;
    grdUsers<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> users<span style="color: #008000;">;</span>
    grdUsers<span style="color: #008000;">.</span><span style="color: #0000FF;">DataBind</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> CreateUsers<span style="color: #008000;">&#40;</span>ISession session<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>session<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&lt;</span>User<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span><span style="color: #008000;">&#40;</span>ITransaction transaction <span style="color: #008000;">=</span> session<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginTransaction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                User user <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> User
                <span style="color: #008000;">&#123;</span>
                    EmailAddress <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;user{0}@test.com&quot;</span>, i<span style="color: #008000;">&#41;</span>,
                    Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>
                <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
                session<span style="color: #008000;">.</span><span style="color: #0000FF;">Save</span><span style="color: #008000;">&#40;</span>user<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            transaction<span style="color: #008000;">.</span><span style="color: #0000FF;">Commit</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;" lang="csharp">The first line of this method retrieves the session from the request context and can then be used to query the database.  The &#8220;CreateUsers&#8221; method shows a simple insert into the database using a transaction.  This is followed by a really simple Linq expression which queries the database for all of the User records.  I should point out that in order to use Linq expressions for queries I needed add a using statement for the NHibernate.Linq namespace.</p>
<p style="text-align: justify;" lang="csharp">This basic set up of Fluent NHibernate is okay but it could be improved upon so I&#8217;m going to write a follow up article on using Fluent NHibernate with dependency injection to improve the structure of the code.  And as promised the download link for the code can be found below; in order to build this project <strong>you will need to have NuGet installed</strong> as I&#8217;m using the <a href="http://nuget.org/packages/NuGetPowerTools" target="_blank">NuGetPowerTools</a> package to retrieve all of the required DLLs when the project is built.</p>
<p style="text-align: justify;" lang="csharp"><strong>Download Link:</strong> <a href="http://www.jason-mitchell.com/Uploads/Fluent_NHibernate_Getting_started.zip">http://www.jason-mitchell.com/Uploads/Fluent_NHibernate_Getting_started.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/asp-net-programming/getting-started-with-fluent-nhibernate-and-auto-mapping-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA and C#: Calling DateTime.Now is Expensive</title>
		<link>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/</link>
		<comments>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/#comments</comments>
		<pubDate>Sat, 07 Jan 2012 20:39:29 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[XNA]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=429</guid>
		<description><![CDATA[While working on a new game engine using XNA I decided to run Visual Studios profiling tools against a basic demo game to measure CPU usage.  The results showed that the update process used approximately 65% of the CPU time while the render process used roughly 25%.  This ratio was not what I had expected [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">While working on a new game engine using XNA I decided to run Visual Studios profiling tools against a basic demo game to measure CPU usage.  The results showed that the update process used approximately 65% of the CPU time while the render process used roughly 25%.  This ratio was not what I had expected at all because the updating was fairly simple so I drilled down into report to find the culprit.  It turns out the code using up the most of the CPU time in the update was a call to DateTime.Now to use as a time stamp.</p>
<p><span id="more-429"></span></p>
<p style="text-align: justify;">First I want to provide a bit of context on how DateTime.Now is being called.  My game engine includes a messaging system so that objects can create and publish messages to the game so they can be consumed by subscribed objects.  Each message generates a time stamp for itself using DateTime.Now.  The demo game has been purposefully set up to create 800 objects each of which send out new messages every frame.</p>
<p style="text-align: justify;">The following screenshots are from the profiler run with the DateTime.Now call still in the code.  This first image shows the CPU usage ratio between the full update and draw processes:</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_Poor.png"><img class="size-full wp-image-437  alignnone" title="Game.Run_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_Poor.png" alt="" width="715" height="351" /></a></p>
<p>The following image shows the call to the constructor containing the usage of DateTime.Now.  Note that the usage for the function calling this code (the blue box on the left) is 35.8%.</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor1.png"><img class="alignnone size-full wp-image-436" title="AttributeChangedMessage.Ctor_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor1.png" alt="" width="716" height="348" /></a></p>
<p>Lastly, this image shows the CPU percentage used for the DateTime.Now call.</p>
<p><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Message.Ctor_Poor.png"><img class="alignnone size-full wp-image-438" title="Message.Ctor_Poor" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Message.Ctor_Poor.png" alt="" width="709" height="337" /></a></p>
<p style="text-align: justify;">I was really quite surprised to see how much CPU time the DateTime.Now call was using.  I then removed the call and ran the CPU profiler to see how the results changed.  The first image is of the Game.Run() call again showing the ratio between the update and draw methods.  Already it&#8217;s clear that removing the DateTime.Now call has improved things since the CPU usage is split more evenly.</p>
<p style="text-align: justify;"><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_.png"><img class="alignnone size-full wp-image-433" title="Game.Run_Improved" src="http://jason-mitchell.com/wp-content/uploads/2012/01/Game.Run_.png" alt="" width="710" height="340" /></a></p>
<p style="text-align: justify;">The following image shows the call to the constructor which <strong>used to</strong> contain the call to DateTime.Now.  Notice that the usage for the code calling this function has dropped from 35.8% to 0.9% which is a massive change!</p>
<p style="text-align: justify;"><a href="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor.png"><img class="alignnone size-full wp-image-434" title="AttributeChangedMessage.Ctor_Improved" src="http://jason-mitchell.com/wp-content/uploads/2012/01/AttributeChangedMessage.Ctor_Poor.png" alt="" width="717" height="342" /></a></p>
<p style="text-align: justify;">Because the results of this performance test were so surprising I decided to do a bit of googling around the performance of DateTime.Now and it didn&#8217;t take very long to find an article confirming that this call is expensive.  The article also states that using DateTime.UtcNow is much faster however I plan on staying away from using DateTime in my game engine.  You can read the article at <a href="http://bit.ly/zTJaaT" target="_blank">http://bit.ly/zTJaaT</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-and-c-calling-datetime-now-is-expensive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Data Access Using a Generic Repository in C#</title>
		<link>http://jason-mitchell.com/c/data-access-using-a-generic-repository-in-c/</link>
		<comments>http://jason-mitchell.com/c/data-access-using-a-generic-repository-in-c/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 21:30:40 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[data access]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=407</guid>
		<description><![CDATA[The repository pattern is an abstraction layer which provides a well-organised approach to maintaining a separation between an applications data access and business logic layers.  This gives us the important advantages of making code more maintainable and readable and improving the testability of our code.  It also works great with dependency injection! When I started [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The repository pattern is an abstraction layer which provides a well-organised approach to maintaining a separation between an applications data access and business logic layers.  This gives us the important advantages of making code more maintainable and readable and improving the testability of our code.  It also works great with dependency injection!</p>
<p style="text-align: justify;">When I started looking at the repository pattern I found that a lot of the samples on the internet used explicitly typed repositories such as ICustomerRepository and IOrderRepository.  However for a website I&#8217;m currently working on all of my CRUD operations were pretty much the same and I wanted to reduce the amount of code I needed to write so I implemented a basic generic repository which would work with any of my data model classes.</p>
<p style="text-align: justify;"><span id="more-407"></span>The following code snippet  is the interface for the basic repository I&#8217;ve been using:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IRepository
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    IQueryable Query<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> filter <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    T SingleOrDefault<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> predicate<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Insert<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">void</span> Delete<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">This interface only defines the most basic data access operations and should probably be expanded on to provide support for beginning, committing and rolling back transactions.</p>
<p style="text-align: justify;">The following class is an implementation of IRepository which has been implemented to abstract access to an Entity Framework Code First data context:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> EntityFrameworkRepository <span style="color: #008000;">:</span> IRepository
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> SiteDataContext dataContext<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> EntityFrameworkRepository<span style="color: #008000;">&#40;</span>SiteDataContext dataContext<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">dataContext</span> <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Save<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> IQueryable Query<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> filter <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        IQueryable query <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>filter <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            query <span style="color: #008000;">=</span> query<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>filter<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> query<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> T SingleOrDefault<span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, <span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&gt;&gt;</span> predicate<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span>predicate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Insert<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Update<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        DbEntityEntry entityEntry <span style="color: #008000;">=</span> dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Entry</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>entityEntry<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">==</span> EntityState<span style="color: #008000;">.</span><span style="color: #0000FF;">Detached</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Attach</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            entityEntry<span style="color: #008000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;">=</span> EntityState<span style="color: #008000;">.</span><span style="color: #0000FF;">Modified</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Delete<span style="color: #008000;">&#40;</span>T entity<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        dataContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>entity<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">There&#8217;s nothing particularly complicated about this class so  I&#8217;m just going to give a quick example on how to use it and leave it at that!  If you have the following class defined in an Entity Framework Code First data context:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>Key<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> ID <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p style="text-align: justify;">You could query your customer data really easily using a LINQ expression:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var customers <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> c <span style="color: #0600FF; font-weight: bold;">in</span> repository<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">where</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Jason&quot;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> c<span style="color: #008000;">;</span></pre></div></div>

<p style="text-align: justify;">or a lambda expression:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var customer <span style="color: #008000;">=</span> repository<span style="color: #008000;">.</span><span style="color: #0000FF;">Query</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Jason&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p lang="csharp">For more information about the repository pattern see: <a href="http://msdn.microsoft.com/en-us/library/ff649690.aspx">http://msdn.microsoft.com/en-us/library/ff649690.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/c/data-access-using-a-generic-repository-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quick Tip: Turn off workflows when importing large amounts of data to SharePoint</title>
		<link>http://jason-mitchell.com/powershell/quick-tip-turn-off-workflows-when-importing-large-amounts-of-data-to-sharepoint/</link>
		<comments>http://jason-mitchell.com/powershell/quick-tip-turn-off-workflows-when-importing-large-amounts-of-data-to-sharepoint/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 21:49:24 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Quick Tip]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[quick tip]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/?p=381</guid>
		<description><![CDATA[Recently at work I’ve been working on a script to import large amounts of data (~800,000 list items) into a SharePoint site.  The first version of the script had obvious performance issues and I had estimated it to run for 29 hours.  Turning off workflows which start automatically when a new list item is created [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Recently at work I’ve been working on a script to import large amounts of data (~800,000 list items) into a SharePoint site.  The first version of the script had obvious performance issues and I had estimated it to run for 29 hours.  <strong>Turning off workflows which start automatically when a new list item is created cut this time to around 3 hours.</strong></p>
<p><span id="more-381"></span></p>
<p align="justify">The following PowerShell code shows a basic function which can be used to toggle workflows on a list off and on.  It shouldn’t take much work to convert this to C# if needed.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$snapin</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Microsoft.SharePoint.PowerShell&quot;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Get<span style="color: #339933;">-</span>PSSnapin <span style="color: #339933;">-</span>Name <span style="color: #000088;">$snapin</span> <span style="color: #339933;">-</span>ErrorAction SilentlyContinue<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span>eq <span style="color: #000088;">$null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Add<span style="color: #339933;">-</span>PsSnapin <span style="color: #000088;">$snapin</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$spWeb</span> <span style="color: #339933;">=</span> Get<span style="color: #339933;">-</span>SPWeb http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//sharepoint-site-url</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> toggle<span style="color: #339933;">-</span>workflows<span style="color: #009900;">&#40;</span><span style="color: #000088;">$listName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$enabled</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$prevVersionPattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;.+`(Previous.+`)&quot;</span>
    <span style="color: #000088;">$spList</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$spWeb</span><span style="color: #339933;">.</span>GetList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Lists/<span style="color: #006699; font-weight: bold;">$listName</span>&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$spListWorkflows</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$spList</span><span style="color: #339933;">.</span>WorkflowAssociations
&nbsp;
    <span style="color: #000088;">$latestWorkflow</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$spListWorkflows</span> <span style="color: #339933;">|</span> where<span style="color: #339933;">-</span>object <span style="color: #009900;">&#123;</span>
          <span style="color: #339933;">-</span>not <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_</span><span style="color: #339933;">.</span>Name <span style="color: #339933;">-</span>match <span style="color: #000088;">$prevVersionPattern</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">|</span> select<span style="color: #339933;">-</span>object <span style="color: #339933;">-</span>first <span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latestWorkflow</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        write<span style="color: #339933;">-</span>host <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$listname</span> workflow: <span style="color: #006699; font-weight: bold;">$enabled</span>&quot;</span> <span style="color: #339933;">-</span>fore yellow
    	<span style="color: #000088;">$latestWorkflow</span><span style="color: #339933;">.</span>AutoStartCreate <span style="color: #339933;">=</span> <span style="color: #000088;">$enabled</span>
    	<span style="color: #000088;">$spListWorkflows</span><span style="color: #339933;">.</span>Update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$latestWorkflow</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p align="justify">This function retrieves the specified list finds the <strong>first</strong> associated workflow which doesn’t have the “(Previous.. )” text in its name and sets it to the specified enabled status.  If you have more than one active associated workflow on your list this code will need to be update to handle an array of workflows instead of just a single result.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/powershell/quick-tip-turn-off-workflows-when-importing-large-amounts-of-data-to-sharepoint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using WCF for database access in XNA: Sample Project</title>
		<link>http://jason-mitchell.com/wcf/using-wcf-for-database-access-in-xna-sample-project/</link>
		<comments>http://jason-mitchell.com/wcf/using-wcf-for-database-access-in-xna-sample-project/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 20:38:20 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[data access]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/programming/using-wcf-for-database-access-in-xna-sample-project/</guid>
		<description><![CDATA[I spent a bit of time this evening on the phone talking to a friend about life, the universe and work.  We got around to talking a bit about accessing a database from within an XNA application so I thought I would make a quick sample and post it on here. This project uses a [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I spent a bit of time this evening on the phone talking to a friend about life, the universe and work.  We got around to talking a bit about accessing a database from within an XNA application so I thought I would make a quick sample and post it on here.</p>
<p><span id="more-378"></span></p>
<p align="justify">This project uses a simple WCF service to get some text from the database on the server and to store details of clicks within the game in the database.  On the server-side it uses LINQ to SQL to interact with the database for retrieving and adding records.</p>
<p align="justify">Before running this sample you should probably do a full rebuild of the solution and then update the service reference in the WindowsGame1 project.  Get it from the link below:</p>
<p align="justify"><strong><em>Download: <a title="http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip" href="http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip">http://www.jason-mitchell.com/Uploads/XNAWCFSample-20_09_2011.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/wcf/using-wcf-for-database-access-in-xna-sample-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET: &#8220;The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.&#8221;</title>
		<link>http://jason-mitchell.com/asp-net-programming/asp-net-the-incoming-tabular-data-stream-tds-remote-procedure-call-rpc-protocol-stream-is-incorrect/</link>
		<comments>http://jason-mitchell.com/asp-net-programming/asp-net-the-incoming-tabular-data-stream-tds-remote-procedure-call-rpc-protocol-stream-is-incorrect/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 19:43:57 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[exceptions]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/programming/asp-net-the-incoming-tabular-data-stream-tds-remote-procedure-call-rpc-protocol-stream-is-incorrect/</guid>
		<description><![CDATA[While working on my current ASP.NET project at work I encountered the following error when trying to call the SubmitChanges method on my LINQ to SQL data context: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 3 (&#8220;@p0&#8243;): The supplied value is not a valid instance of data [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">While working on my current ASP.NET project at work I encountered the following error when trying to call the SubmitChanges method on my LINQ to SQL data context:</p>
<blockquote>
<p align="justify"><em>The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 3 (&#8220;@p0&#8243;): The supplied value is not a valid instance of data type float. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.</em></p>
</blockquote>
<p><span id="more-374"></span></p>
<p align="justify">This error message was accompanied with the following stack trace showing the exception being thrown from deep within the bowels of LINQ to SQL:</p>
<blockquote><p>System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2062078<br />
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5050204<br />
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234<br />
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275<br />
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33<br />
System.Data.SqlClient.SqlDataReader.get_MetaData() +86<br />
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311<br />
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987<br />
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162<br />
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32<br />
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141<br />
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12<br />
System.Data.Common.DbCommand.ExecuteReader() +12<br />
System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1266<br />
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113<br />
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344<br />
System.Data.Linq.StandardChangeDirector.DynamicInsert(TrackedObject item) +151<br />
System.Data.Linq.StandardChangeDirector.Insert(TrackedObject item) +235<br />
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +336<br />
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +378<br />
System.Data.Linq.DataContext.SubmitChanges() +23</p></blockquote>
<p align="justify">The exception message is very specific about what the problem actually was: <em>“The supplied value is not a valid instance of data type float”.  </em>After a bit of searching the internet I found that the issue could be caused by trying to insert a double with an actual value of “NaN” or negative/positive infinity.  After a bit of investigation I was able to confirm that a value of “NaN” was being passed in therefore causing this exception.</p>
<p align="justify">After a bit more searching I found out that that <strong><em>a “NaN” value is generated as the result of dividing 0 by 0</em></strong>.  In the project I’ve been working on this calculation is entirely possible as it performs calculations using external data.  To fix the issue I determined what calculation was generating the “NaN” values and used the <strong>double.IsNaN</strong> method to check for any invalid values and replaced them with 0.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/asp-net-programming/asp-net-the-incoming-tabular-data-stream-tds-remote-procedure-call-rpc-protocol-stream-is-incorrect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA Texture2D Creation: byte[] vs. Color[]</title>
		<link>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/</link>
		<comments>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 19:22:21 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[XNA]]></category>
		<category><![CDATA[2D Graphics]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/programming/xna-texture2d-creation-byte-vs-color/</guid>
		<description><![CDATA[This is just a quick article to share this interesting article by Jonny Hughes about the difference in performance between using a byte array and a Color array to generate data for a new Texture2D object in XNA.  The results surprised me a bit; I didn’t expect the use of the Color object to use [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">This is just a quick article to share <a href="http://jonny-hughes.co.uk/2011/08/25/creating-a-texture2dbyte-vs-color/" target="_blank">this interesting article</a> by Jonny Hughes about the difference in performance between using a byte array and a Color array to generate data for a new Texture2D object in XNA.  The results surprised me a bit; I didn’t expect the use of the Color object to use so much more processor time.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-texture2d-creation-byte-vs-color/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XNA Twitter Powered Tic Tac Toe</title>
		<link>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/</link>
		<comments>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 20:57:04 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/2011/08/24/xna-twitter-powered-tic-tac-toe/</guid>
		<description><![CDATA[This was a project that was not mostly pointless and impractical but is as bizarre as this article’s title makes it sound.  One day about two years ago I was bored, very bored.  I was at a total loss for small projects to work on in the evenings after work.  Somehow I came up with [...]]]></description>
			<content:encoded><![CDATA[<p>This was a project that was not mostly pointless and impractical but is as bizarre as this article’s title makes it sound.  One day about two years ago I was bored, very bored.  I was at a total loss for small projects to work on in the evenings after work.  Somehow I came up with the idea to make a multiplayer XNA Tic Tac Toe game which used Twitter mentions for sending messages between players (wtf….I know).  Basically the game constructs simple text “commands” and posts them to a Twitter account and the other player’s game would poll Twitter looking for messages from the other player.</p>
<p>I decided to post this project because I’ve been at a loss for what to write about recently and I thought this project was so strange that I thought I should share it.  The project was made in XNA 3.0 or 3.1 and may not work anymore depending on what has changed with Twitter’s API; I really don’t know and I haven’t run the game.</p>
<p><strong><em>Download: <a title="http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip" href="http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip">http://www.jason-mitchell.com/Uploads/TwitterTicTacToe_24-08-2011.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/xna/xna-twitter-powered-tic-tac-toe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic XNA and Kinect SDK Sample</title>
		<link>http://jason-mitchell.com/kinect/basic-xna-and-kinect-sdk-sample/</link>
		<comments>http://jason-mitchell.com/kinect/basic-xna-and-kinect-sdk-sample/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 15:10:55 +0000</pubDate>
		<dc:creator>jmitch18</dc:creator>
				<category><![CDATA[Kinect]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[sample]]></category>

		<guid isPermaLink="false">http://jason-mitchell.com/2011/06/28/basic-xna-and-kinect-sdk-sample/</guid>
		<description><![CDATA[This past week I have spent a little bit of time playing with the Kinect SDK and trying to get to grips with how it all works.  My biggest interest is to use the Kinect SDK along with Microsoft’s XNA Framework for games development so I made a sample project using these technologies.  I’ve posted [...]]]></description>
			<content:encoded><![CDATA[<p>This past week I have spent a little bit of time playing with the Kinect SDK and trying to get to grips with how it all works.  My biggest interest is to use the Kinect SDK along with Microsoft’s XNA Framework for games development so I made a sample project using these technologies.  I’ve posted a link to this project at the end of the article.</p>
<p><span id="more-357"></span></p>
<p>The idea of this project was pretty simple.  I wanted to get the RGB camera stream of me goofing around and use it as the background in an XNA application.  I also wanted to try out some skeleton tracking functionality to test when my hands are intersecting with hot spots (represented as semi-transparent squares) on the screen.  As it turns out, this project was pretty simple and quick to make and overall I’m really quite impressed with how the SDK is to use.</p>
<p>This project makes use of a couple of <a href="http://jason-mitchell.com/2011/06/27/kinect-sdk-extension-methods/" target="_blank">simple extension methods I created</a> and is fairly heavily based on the samples which get installed with the Kinect SDK.</p>
<p><strong><em>Download: <a title="http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip" href="http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip">http://jason-mitchell.com/Uploads/XNA_And_Kinect-28_06_11.zip</a></em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://jason-mitchell.com/kinect/basic-xna-and-kinect-sdk-sample/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

