<?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>The Grubbsian &#187; Flex</title>
	<atom:link href="http://www.thegrubbsian.com/category/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thegrubbsian.com</link>
	<description>Writings on .NET, Ruby, JavaScript, HTML/CSS, Design, and all things Web.</description>
	<lastBuildDate>Sun, 01 Aug 2010 20:57:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Integrating Adobe Flex and .NET with ASP.NET MVC</title>
		<link>http://www.thegrubbsian.com/2009/01/02/integrating-adobe-flex-and-net-with-aspnet-mvc/</link>
		<comments>http://www.thegrubbsian.com/2009/01/02/integrating-adobe-flex-and-net-with-aspnet-mvc/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 23:30:14 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=104</guid>
		<description><![CDATA[There are a lot of tools out there to help you utilize .NET as a back-end for Flex applications.&#160; All of these solutions come with a heavy customization and implementation cost.&#160; I&#8217;ve used WebORB with quite a bit of success in the past but it always seemed a little heavy to me.&#160; The truth is [...]]]></description>
			<content:encoded><![CDATA[<p>
	There are a lot of tools out there to help you utilize .NET as a back-end for Flex applications.&nbsp; All of these solutions come with a heavy customization and implementation cost.&nbsp; I&#8217;ve used <a href="http://www.themidnightcoders.com/products/weborb-for-net/overview.html">WebORB</a> with quite a bit of success in the past but it always seemed a little heavy to me.&nbsp; The truth is that WebORB and its compatriots are the right choice for large applications or apps with highly complex server side needs.&nbsp; But for the lightweight web application that doesn&#8217;t require tons of server side business logic there is another choice.
</p>
<p>
	With the advent of <a href="http://www.asp.net/mvc">ASP.NET MVC</a> we have a very simple way to construct web accessible .NET endpoints – controller actions.&nbsp; In the past when you needed to get data out of .NET using HTTP you might spin up a web service or a WCF service.&nbsp; These are still great options, but if the data you need to transfer is small and/or jagged you might find yourself writing a lot of extra code.&nbsp; You’ll also pay the XML tax and nobody likes paying taxes.
</p>
<p>
	So if not XML, what then?&nbsp; Well, <a href="http://www.json.org/">JSON</a> of course.&nbsp; JavaScript Object Notation is a perfect media for transmitting moderate amounts of data over the wire.&nbsp; And there are excellent serialization options on both the .NET and Flex sides of the game.&nbsp; So here’s a little example of this pattern in action.
</p>
<p>
	<strong>Create an ASP.NET MVC Controller Action &#8211; Return JSON.</strong>
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ContentResult GetInventory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
var repository <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InventoryRepostiory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var items <span style="color: #008000;">=</span> repository.<span style="color: #0000FF;">GetItems</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #008000;">&lt;</span>InventoryItem<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">return</span> Content<span style="color: #000000;">&#40;</span>items.<span style="color: #0000FF;">ToJSON</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	I’m not going to go into the data access pattern here in this article, but I’m using a repository to return a set of inventory items.&nbsp; You might also be wondering where this ToJSON() method comes from.&nbsp; It’s an extension method…and here it is (c/o <a href="http://weblogs.asp.net/Scottgu/">ScottGu</a>):
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ToJSON<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">object</span> thing, <span style="color: #FF0000;">int</span><span style="color: #008000;">?</span> recursionDepth<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    var serializer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JavaScriptSerializer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>recursionDepth.<span style="color: #0000FF;">HasValue</span><span style="color: #000000;">&#41;</span>
        serializer.<span style="color: #0000FF;">RecursionLimit</span> <span style="color: #008000;">=</span> recursionDepth.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">return</span> serializer.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span>thing<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	All it does is take an object, do a little reflection magic on it via the System.Web.Script.Serialization.JavaScriptSerializer class and spits out a JSON string.
</p>
<p>
	<strong>Calling Your ASP.NET MVC Controller Action from Flex</strong>
</p>
<p>
	The first thing you’ll need on the Flex side before you can consume your MVC action is a way to de-serialize the JSON string.&nbsp; Fortunately, the Adobe guys have provided a handy library of utilities called <a href="http://code.google.com/p/as3corelib/">AS3CoreLib</a> which contains just such a service.&nbsp; Once you have the SWC just pop it in the Libs folder of your Flex app.
</p>
<p>
	So now let’s create a wrapper class in ActionScript to consume our ASP.NET MVC action.&nbsp; I like to mimic the repository pattern on the Flex side as well so that I can keep up with a somewhat consistent API.&nbsp; There’s nothing that technically holds you to this rule, but it’s helpful to keep things logically organized.&nbsp; So, here’s the InventoryRepository in AS3:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> Data <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> com.adobe.serialization.json.<span style="color: #000000; font-weight: bold;">*</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span>.<span style="color: #000000; font-weight: bold;">*</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #000000; font-weight: bold;">*</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.collections.ArrayCollection;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> InventoryRepository <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> completeCallback<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>;
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> GetInventory<span style="color: #000000;">&#40;</span>callback<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
            completeCallback = callback;
            <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/Home/GetInventory&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #004993;">loader</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">COMPLETE</span>, getInventoryComplete<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> getInventoryComplete<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> obj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = JSON.<span style="color: #004993;">decode</span><span style="color: #000000;">&#40;</span>event.<span style="color: #004993;">target</span>.<span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span>;
            completeCallback<span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	You’ll notice that I’ve imported the com.adobe.serialization.json classes from AS3CoreLib.&nbsp; That provides us with the JSON.decode() method.
</p>
<p>
	Now, because remote calls in Flex are asynchronous, we need to do a little fancy footwork to deal with it.&nbsp; So, when you call the GetInventory() method we also have to supply a callback to fire off when the data returns from the MVC action.&nbsp; This is one of my favorite aspects of ActionScript…it’s dynamic…dynamic in the JavaScript sense of the word.&nbsp; So we can supply a callback that doesn’t need to be defined in our repository class itself.&nbsp;
</p>
<p>
	So let’s look at some code that consumes our repository method.&nbsp; This is just a code-behind file for an MXML appl ication:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> InventoryApplication <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.core.Application;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.DataGrid;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.collections.<span style="color: #000000; font-weight: bold;">*</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> Data.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> HomeApplication extends Application <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> inventoryGrid<span style="color: #000000; font-weight: bold;">:</span>DataGrid = <span style="color: #0033ff; font-weight: bold;">new</span> DataGrid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> InventoryApplication<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            LoadInventory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> LoadInventory <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> repository<span style="color: #000000; font-weight: bold;">:</span>InventoryRepository = <span style="color: #0033ff; font-weight: bold;">new</span> InventoryRepository <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            repository.GetInventory<span style="color: #000000;">&#40;</span><span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">data</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
                inventoryGrid.dataProvider = <span style="color: #004993;">data</span>;
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	So the only really interesting thing here is how we’re defining the callback as an anonymous function passed into the GetInventory() method.&nbsp; When the data is returned we just bind it to our data grid and we’re done.
</p>
<p>
	<strong>Sending Data To an ASP.NET MVC Action From Flex</strong>
</p>
<p>
	So, now we can pull data out of our .NET back end, but how do send data into an MVC action from Flex?&nbsp; The solution couldn’t be simpler.&nbsp; So here’s a new method for the AS3 repository:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CreateKind<span style="color: #000000;">&#40;</span>itemName<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>,
    itemType<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>,
    itemSize<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>,
    callback<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> variables<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLVariables</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLVariables</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    variables.<span style="color: #004993;">name</span> = itemName;
    variables.<span style="color: #004993;">type</span> = itemType;
    variables.<span style="color: #004993;">size</span> = itemSize;
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> request<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLRequest</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/Home/CreateKind&quot;</span><span style="color: #000000;">&#41;</span>;
    request.<span style="color: #004993;">method</span> = <span style="color: #004993;">URLRequestMethod</span>.<span style="color: #004993;">POST</span>;
    request.<span style="color: #004993;">data</span> = variables;
&nbsp;
    <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">URLLoader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLLoader</span><span style="color: #000000;">&#40;</span>request<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	Here you can see we’re passing in the properties of our new item and adding them to the variables object directly (another nice thing about ActionScript being dynamic).&nbsp; So now these new properties will become variables in the form collection sent to our controller action.&nbsp; Here’s our controller action:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ContentResult CreateItem<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> name, <span style="color: #FF0000;">string</span> type, <span style="color: #FF0000;">string</span> size<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    var item <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InventoryItem
    <span style="color: #000000;">&#123;</span>
        Name <span style="color: #008000;">=</span> name,
        Type <span style="color: #008000;">=</span> type,
        Size <span style="color: #008000;">=</span> size
    <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    var repo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InventoryRepository<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    repo.<span style="color: #0000FF;">SaveItem</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> Content<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;true&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
	You’ll notice that the parameters it accepts are “name”, “type”, and “size”.&nbsp; These are the same properties added to the Flex URLVariables object in our Flex repository class.&nbsp; Through the magic of ASP.NET MVC these items are simply available to the controller action as parameters, simple as that.
</p>
<p>
	So, hopefully that demonstrates how easy it is to use ASP.NET MVC to integrate a .NET back-end with Flex.&nbsp; I think the advantages of this method are substantial, but do your research and take a look at the other options as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/01/02/integrating-adobe-flex-and-net-with-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
