Adobe Flex 2.0 + Cairngorm + Fluorine + ASP.NET Sample Application / Tutorial

After my last tutorial/post about using WebOrb, I began using Fluorine. Recently a request was made for a tutorial, so here goes…

Using Service Capture , an ABSOLUTELY fantastic tool and Fluorine with AMF3, still seems to be a problem. Also, I wanted to try and use the RemoteObject tag with my solution to make using .NET a little less coupled. Well, it turns out; Fluorine still seems to work great with AMF0, the remoting format from Flash 8. This led me to Renaun’s RemoteObjectAMF0 which easily plugs into a Cairngorm ServiceLocator. So there’s the pieces, now to put together the puzzle.

Renauns’s RemoteObject also works with AMF3. The reason I’ve been using AMF0 is that AMF3 + Fluorine doesn’t seem to display correctly in Service Capture. Possibly after I’m done developing my current project, I’ll switch this to AMF3 and see if any problems occur. You simply need to change the the line in RemotingConnection.as:

objectEncoding = ObjectEncoding.AMF0;
to:
objectEncoding = ObjectEncoding.AMF3;

First you will need the following for this tutorial: Visual Studio 2005, Adobe Flex Builder 2.0, Renaun’s RemoteObjectAMF0 (link), and Cairngorm (link), and Fluorine.

Open Visual Studio and Create a New Web Service: File -> New -> Website -> Empty Web Site.
Create a new C# Class: File -> New -> File -> Class -> Name the class EmployeeService.cs. You may be prompted about placing the cs file in the App_Code Folder, choose Yes.

A file EmployeeService.cs will be created in the App_Code Folder. Open EmployeeService.cs. Enter the following code.

[csharp]
using System;
using System.Web;

using net.shrefler.vo;

namespace net.shrefler.service
{
public class EmployeeService
{
public EmployeeService()
{
}

public EmployeeVO[] getEmployees()
{
EmployeeVO[] emps = new EmployeeVO[2];
EmployeeVO emp = new EmployeeVO();

emp.firstName = “Sam”;
emp.lastName = “Shrefler”;
emps[0] = emp;
emp = new EmployeeVO();
emp.firstName = “Your”;
emp.lastName = “Name”;
emps[1] = emp;

return emps;
}
}
}
[/csharp]

Create a new C# Class: File -> New -> File -> Class -> Name the class EmployeeVO.cs. You may be prompted about placing the cs file in the App_Code Folder, choose Yes.

A file EmployeeVO.cs will be created in the App_Code Folder. Open EmployeeVO.cs. Enter the following code.

[csharp]
using System;
using System.Web;

namespace net.shrefler.vo
{
public class EmployeeVO
{
public string firstName;
public string lastName;

public EmployeeVO()
{
}
}
}
[/csharp]

Next we’ll need to add Fluorine to our application. Add a “Bin” Folder to your Web Site. Right-Click on Bin and go to “Add Existing Item.” Browse to where you downloaded Fluorine and add “com.TheSilentGroup.Fluorine.dll”. Right click the root folder of your website and go “Add Reference”. Click the Browse tab, and add the “com.TheSilentGroup.Fluorine.dll” in your Bin folder.

Open web.config and add the following code.

[xml]




net.shrefler.vo.EmployeeVO
net.shrefler.vo.EmployeeVO






[/xml]

Next, create an empty Gateway.aspx file in the root of your website.

Finally, Rather than describing all the flex code: I decided to just use the Source View. So here is the flex code: (link)

I hope this helps everyone as much as I hope it will help me. As always, comments are appreciated. Good Luck!

Attention .NET + Flex Developers…Be Heard!

Following Graeme’s lead (I suggest reading his post), I also wanted to write a quick note to Adobe about the importance of .NET Developers…

A long time Coldfusion Developer, due to new employement, I find myself becoming an ASP.NET Developer. While not yet totally sold on the .NET Framework and often finding myself missing the convenience of Coldfusion, that is neither here nor there. The fact of the matter is, my company has too much investment (again, I’m not here to argue for or against this) in .NET to convince a move to Coldfusion. There is however, a void in the presentation of our application. Enter Flex.

From my previous work with Coldfusion and Flash, and having been led/heavily pushed toward Flex by Darron Schall, I’m now so grateful, I’ve been aware of Flex for quite some time. I waited and waited for Flex Builder 2 to be released. I jumped at the first opprotunity to incorporate it into my current .NET project.

I was met with reistance from co-workers due to them having NO knowledge of Flex.

No problem: I’ll just woo them with Examples of Flex being used with .NET.

Problem: I couldn’t find any.

Now, luckily, Flex examples, especially anything using the charting components, with a promise that I could utilize our .NET base was enough for me to get a chance, unfortunately, for some, I would guess they’ll have to pass on Flex. Also, I started to question, was I tricking myself…was it worth it…am I going to actually be able to do this based on what a couple of blogs say?

The resounding answer is YES! So far, utilizing Flex is wonderful and my company is over joyed seeing some of the things Flex can do. I love working with Flex.

So to answer Matt Chotin’s Question: The following is a little wish list, complete with a disclaimer of “Thank you Adobe for all you do. I love being a part of your developer community and I think the work you are currently doing is Fantastic!”

Adobe needs to help us .NET developers. We need to know we are cared for and loved. How about adding some .NET + Flex articles to the Adobe Developer Center. It would be an privaledge to help with something like that, the question is, are you interested Adobe, is there anything like this in the works?

Please educate me on Adobe’s stance on Fluorine (a tool that I highly recommend and that I’m using in all my projects). An article endorsing its use would make me feel great moving forward as a “Flex.NET” developer.

All of these things would combine to increasing the number of and the awareness of Flex.NET developers. As a current developer, it would motivate me to continue this amazing ride knowing that as the community grows, so will my demand as a Flex.NET developer rather than worrying about being left behind.

Again, Thank you Adobe for the Fantastic Work!

Using “Some “ActionScript expressions in curly braces - Data Binding

I seem to have come accross a little problem with the Logical Operator “&&” and Data Binding in MXML. It seems the MXML in Flex Builder doesn’t like the & character. Therefore, you get the following error when trying to bind an expression using the “&&” operator:

“The entity name must immediately follow the ‘&’ in the entity reference.”

An example of when this would be used would be if you are trying to bind the enabled property of a button:

[xml]

[/xml]

Therefore, you have two options as a solution

1:

You have to go back to De Morgan’s laws
(A && B) == ! (!A || !B)

Therefore, i can fix the first expression like so:

[xml]

[/xml]

Now you should be good to go.

Flex + Cairngorm + WebOrb 2.0 Standard (Free) + .NET Sample Application / Tutorial

After my last tutorial/post about using JSON, Mark Piller from TheMidnightCoders commented that my requested functionality (Easily pass back client classes back and forth) could be completed using his product, WebOrb.NET. Thanks Mark!

After further investigation, I was still a little confused as how to accomplish this using a non-beta, standard (free) version of WebOrb.NET. Well, it turns out; WebOrb v2.0 works great with AMF0, the remoting format from Flash 8. This led me to Renaun’s RemoteObjectAMF0 which easily plugs into a Cairngorm ServiceLocator. So there’s the pieces, now to put together the puzzle.

First you will need the following for this tutorial: Visual Studio 2005, Adobe Flex Builder 2.0, Renaun’s RemoteObjectAMF0 (link), and Cairngorm (link).

Install WebOrb 2.0. Following all the system defaults should work just fine.

Open Visual Studio and Create a New Web Service: File -> New -> Website -> Empty Web Site.
Create a new C# Class: File -> New -> File -> Class -> Name the class EmployeeService.cs. You may be prompted about placing the cs file in the App_Code Folder, choose Yes.

A file EmployeeService.cs will be created in the App_Code Folder. Open EmployeeService.cs. Enter the following code.

[csharp]
using System;
using System.Web;
using Weborb.Util;
using net.shrefler.vo;

namespace net.shrefler.service
{
public class EmployeeService
{
public EmployeeService()
{
}

public EmployeeVO[] getEmployees()
{
EmployeeVO[] emps = new EmployeeVO[2];
EmployeeVO emp = new EmployeeVO();

emp.firstName = “Sam”;
emp.lastName = “Shrefler”;
emps[0] = emp;
emp = new EmployeeVO();
emp.firstName = “Your”;
emp.lastName = “Name”;
emps[1] = emp;

return emps;
}
}
}
[/csharp]

Create a new C# Class: File -> New -> File -> Class -> Name the class EmployeeVO.cs. You may be prompted about placing the cs file in the App_Code Folder, choose Yes.

A file EmployeeVO.cs will be created in the App_Code Folder. Open EmployeeVO.cs. Enter the following code.

[csharp]
using System;
using System.Web;

namespace net.shrefler.vo
{
public class EmployeeVO
{
public string firstName;
public string lastName;

public EmployeeVO()
{
}
}
}
[/csharp]

Next we’ll need to add WebOrb to our application. In your webroot folder should be a weborb directory. Copy “weborb.dll” from its bin directory into a bin directory in your new website. Then copy “weborb.config” and “web.config” from the weborb root directory to the root of your local website.

Open weborb.config and add the following code inside the tag.

[xml]

net.shrefler.EmployeeVO
net.shrefler.EmployeeVO

[/xml]

Rather than describing all the flex code: I decided to just use the Source View. So here is the flex code: (link)

I hope this helps everyone as much as I hope it will help me. As always, comments are appreciated. Good Luck!

Flex + JSON + .Net Sample Application / Tutorial

Don’t have enough time to wait for Adobe to release .NET Remoting for Flex?  Not quite ready to dive into Flourine?  Not able to purchase or install webOrb.NET on your server?  Dreading working with XML and WebServices?  Me Too, Me Too, Me Too, Me Too.  Luckily, I’ve found something that seems to have made my life a little easier for the time being.  JSON - as originally explained by Mike Chambers here. JSON for Actionscript 3.0, a jewel written by Darron Schall for Adobe, in the corelib released by Adobe and JSON.net for .NET.

JSON - pronounced Jason - allows you to serialize and deserialize data. 

Broken down:  You can send a bunch of different data types from .NET <-> Flex including Arrays and Objects and Arrays of Objects.  Where I see room for improvement would be after getting a generic object, being able to automatically convert that to a ValueObject (TransferObject) on both the client and server side.  Until that happens, I’ll just take care of it manually - unless someone has a better recommendation. 

On With The Show:

First you will need the following to follow this tutorial:  Visual Studio 2005, Adobe Flex Builder 2.0, JSON.net

Open Visual Studio and Create a New Web Service: File -> New -> Website -> ASP.NET Web Service.

 A file Service.asmx will be created and in the folder “App_Code” Sective.cs will be created.  Open Service.cs.  Enter the following code.

[csharp]
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Newtonsoft.Json;

public class Employee
{
public int id = 0;
public string firstName = “”;
public string lastName = “”;
}

public class Service : System.Web.Services.WebService
{
public Service () {
}

[WebMethod]
public string GetEmployee() {
Employee e = new Employee();
e.id = 12345;
e.firstName = “Sam”;
e.lastName = “Shrefler”;

string output = JavaScriptConvert.SerializeObject(e);

return output;
}
}
[/csharp]

Next, you’ll need to copy “Newtonsoft.Json.dll” and “Newtonsoft.Json.XML” into your App_Code directory.  Next, right-click the Project and go to “Add Reference”.  Select Browse then Find “Newtonsoft.Json.dll” and hit OK.  A Bin directory should be created containing the “Newtonsoft.Json.dll” and “Newtonsoft.Json.XML” in your project.  Right click on Service.asmx and go to “View in Browser”.   A Service Description should come up.  Click “GetEmployee” then click “Invoke”.  If all is working properly, you should see the following output:

[xml]

{”id”:12345,”firstName”:”Sam”,”lastName”:”Shrefler”}
[/xml]

Great!  Your Webservice is ready to go.  Next we’ll move to the client-side: Flex!

Create a new Project. In the main mxml file, add the following code:

[as]



import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import com.adobe.serialization.json.JSON;

private function onJSONLoad(event:ResultEvent):void
{
//get the raw JSON data and cast to String
var rawData:String = String(event.result);

//De-serialize the object
var employee:Object = JSON.decode(rawData);

//Add the Object to an Array
var arr:Array = new Array();
arr.push(employee);

//create a new ArrayCollection passing the de-serialized Object Array
//ArrayCollections work better as DataProviders, as they can
//be watched for changes.
var dp:ArrayCollection = new ArrayCollection(arr);

//pass the ArrayCollection to the DataGrid as its dataProvider.
grid.dataProvider = dp;
}
]]>











[/as]

Compile and Build the application. You should see the data grid populated with one record straight from ASP.NET. Future enhancements I’d like to make to this application will be to clean the source, implement cairngorm in the flex code, and find a way to automatically transfer from generic objects to value objects in both Flex and .NET.

I hope this helps and any feedback is appreciated!
 

One for the Money

I’ve decided to start my blog up again since I’m lucky enough to be working with Flex, .NET, and SQL once again at my new job.  After much thought on how I’d run this blog, I’ve decided to throw in a little bit of everything.  In the day and age of great aggregators, I figure I can write about whatever I want and readers can choose what they’ll read…So a quick preview of the future of this blog:

 Work: Flex to the future…I’m convinced that Adobe is on point with Flex and the highly anticipated Apollo.  Who’s to say that Flex will always be the software of choice?  Not me, but I do say that Flex is an excellent choice for me right now in the present and that by learning and working with Flex now, I’ll be be in a great position for the future.  Also, my company has a lot of software currently still using Classic ASP and some .NET stuff.  I’m looking forward to getting into the full swing of things with .NET.  I’m sure Flex + .NET combined will lead to many posts on this blog, especially regarding flourine.  I do miss developing in Coldfusion and wish I could enjoy the seemless integration with Flex, but another day…

 Personal: An amazing wife, fabulous family, and great friends.  As a service to myself, I believe I’ll make mention of things going on in my life on this blog.  It will be great to go back someday and have access.  Oh, I did forget one thing, probably what I find most comfortable writing about in detail for the world to read, my house!  Stacy (the amazing wife) and I love the house and always seem to have a project going on.  The last year and a half, “The Basement” - I still need a better name, but basically its a theatre with a full bathroom attached and a remodelled laundry/entryway.  I’ll gladly post more details, pictures and updates.  In my opinion its worth checking in for, and definately worth visiting us to see.

 Cool Fact #1:  The bathroom has its own speaker and volume control so you don’t miss any of the action, game, or movie.