Been a while

•February 15, 2009 • Leave a Comment

So its been a little while since I posted an article.  Mostly because throughout the end of 2008 I was exceedingly busy at a new job.  Now I have actually started to change careers, but I am doing contracting work in Flex, PHP, and C#.  So, with my exploits over the next several months I will post as much as possible about my findings about the frameworks.  A couple of things I wanted to get into were the different frameworks that people use for development in Flex, especially cairngorm.  I have heard mixed things about it and I believe it is time to learn all I can.

I have also seen an influx in PHP development needed.  This will spurn me onto doing a lot of research into new PHP development models, including the zend framework, which is much like cairngorm in the way that I have heard much good and much bad about it.

Soon we will see.  I will probably start with the PHP research as it pertains to the contracts it looks like I will have coming up.  Until next time…

Flex 3 Runtime Shared Libraries, Interfaces, and Dynamic Instantiation

•July 31, 2008 • 3 Comments

Recently I started playing with RSL’s (Runtime Shared Libraries).  Coming from a programming language where Dynamic Libraries are a common way of separating implementation from contract, and making it easier to create code that can be easily interchanged, I ran into problems with how Flex merges libraries into your code.  To me, the idea of using a separate library is to make it so that you can make changes to the library, without necessarily re-compling the entire application.

When you add a project in Flex Builder under Properties->Build Path->Library Path, by default it will merge your library project into your compiled swf.  While this can make your swf smaller, it can also create problems, such as when your using remoting.

Flex only compiles the classes that it finds links to in your application, even if those classes are in another library that you are referencing.  This causes a problem with remoting, if you don’t use a class type in your application, but that class type is returned as a Strong Type from a remoting service call, you will get a very unfriendly server error.  If you don’t use an RSL, you would need to create a dummy variable for each of those class types, which in a large application can get messy, and I think is messy.

But if you change the Link Type of the library you have added to Runtime Shared Library from Merged Into Code, and check the Automatically extract swf to deployment path checkbox, it will compile the entire library by itself and put it into your bin directory when you compile.  If its a local SWF, there is no need really to create a Digest for the SWF.

Now, you have a compiled SWF which your application references, with ALL of your classes compiled into it.  This can be leveraged in many ways.

One: It can help with Decoupling implementation from contract.  You can compile into your application (via a third library) a set of interfaces which both your application, and your RSL use.  Now, your application doesn’t care about what the actual object is, it only knows that it is using something which implements the X Interface.  In your RSL, you have a class which implements the X interface, and a factory of some kind which spits out your objects.

Two: Dynamic Instantiation.  One of the things that I have commonly done in C# applications is dynamic instantiation of objects via either a config file saying what the class of the object should be, or a database which returns the name of the class.  In Flex, this is practically impossible without using an RSL.  With using an RSL, your application references an interface (as in above), and the database returns the class name of an object that implements that interface.  Then you use flash.utils.getDefinitionByName() to get a reference to a the class TYPE, such as:

var temp:Class = flash.utils.getDefinitionByName(“MyClassName”) as Class;

Now the variable temp is a reference to the class type in which we want, and all we have to do is create an object of that type:

var IInterfaceTheClassImplements = new temp() as IInterfaceTheClassImplements;

Now you have an object which has been dynamically instantiated, from a library which you can swap out every time you create a new object.  This is useful in instance such as a builder which uses Templates.  Create an interface ITemplate, which all the templates implement, create an RSL with all of your Templates in it, and a database which returns the class names of each of the templates.  When you need to create a new template, all you need to do is recompile the RSL with the Templates, drop it in your bin folder, and update the database with the new class name.

Warning:

Remember, the reason Flex merges your code into your applications swf is to decrease the size of your swf overall, and decrease loading time.  If you create an RSL with tons of code and assets in it, you are going to significantly increase your applications loading time.

FluorineFx, WebOrb, LiveCycle DS

•July 31, 2008 • 1 Comment

Well I am back after a long break.  I have changed jobs and moved to Orlando, Fl.

So lately I have spent some time researching the pros and cons of using the different remoting platforms for Flex.  I absolutely love Flex, I think Adobe has an amazing product there, but it has one failing, no strong backend system.  Yes yes, many people will say LiveCycle Data Services is the best thing there is, but I tend to disagree for a couple reasons, and yes the could be biased.

LCDS supports Flex remoting to make calls to Java objects.  My main problem with this is that I am not a fan of Java.  No, I am not a Java programmer, so that could be why.  But my exploits in Java, IE invoking web services etc… have seemed fruitless.  It seems very difficult to do very simple things in Java now.

Okay, now that I have gone this far, yes I am partial to .Net and C#.  Although I am not a fan of Windows itself, I think that Microsoft has come up with one of the best platforms for development there is, as well as the single best IDE out there.

So, Pros and Cons

LCDS has one main thing that I have found going for it:

Data Management is second to none, the ablity to push messages over the wire and view that on all connected clients.

WebOrb for .Net:

So, WebOrb is an enterprise level system for .Net-Flex remoting.  It has a great console to test the services that you have deployed to it.  It seems to be up to par almost with LCDS.  Its main drawback that I have found is that it doesn’t seem to support the IExternalizable interface, which allows for custom serialization of objects being passed for Flex to .Net and back.

They used to have a pretty high license fee, but in the past two months changed all the licenses to be free and sell support.  This is a good option to play with especially when your first starting out, as well as they have a pretty nice CodeGen tool to create your actionscript classes from the compiled .Net assemblies.

Custom Serialization:

With deep linking and LINQ objects, etc… You run the risk of passing unneeded amounts of data over the wire because of the objects that are linked to that object.  With IExternalizable, you decide what objects to pass, and in what order they are passed, so you only use the objects needed.

FluorineFx:

This is an open source package, which can be helpful in itself.  It has a console, but nearly as advanced as the WebOrb one.  It allows for custom serialization using IExternalizable, and leaves a pretty small footprint.  Unfortunately I have never been able to get the basic CodeGen they have to work, and the Console breaks all the time.

As of now, I have stuck with FluorineFx because (after getting used to it) it has a pretty simple setup procedure which is decently documented in their help files.  When I get a chance soon, I will be creating a .Net code gen tool, to create your actionscript classes for you, either using IExternalizable or through class aliases.

Memory Profiling in Flex/AS3 and Performance Centrique Coding

•June 17, 2008 • Leave a Comment

I started off my programming life in C++.  To this day, I love the agility you have to manipulate hardware, memory, and the performance you gain from having direct access to the previous two.  I started using Java, and I felt although it made certain things simpler, the performance hit that using a VM caused was to significant.  Especially since I am a perfectionist.  This left a bad taste in my mouth for many years about anything that used a VM or any kind of intermediary, like .Net.  Well, I finally overcame all of that and fell in love with C#, it is my language of choice these days when writing Windows only code.  But, to get on to the real topic I wanted to discuss.

When I started working with Flex and AS3, I noticed that it was exceedingly easy to throw together an application, even easier if you have had some experience building some of these applications in the past with Flash.  But, I also noticed it was all to easy to be very sloppy, and this also left a bad taste in my mouth because I noticed how easy it was to create memory leaks, and bog down an application with multiple requests, etc..  Well, Adobe has come out with a Profiler as a part of Flex Builder 3 that, even though I haven’t used it a ton, is the first step in building Rich Internet Applications which are hardware/memory/performance centrique.

A blog post I came across the other day on  InsideRIA was all about this profiler, but also on some other issues of memory leaks.  The main thing that I was unaware of was how easy it is to generate massive memory leaks through unremoved eventlisteners.  The post is here . Some of the highlights that I was interested in though where:

1. When adding an eventlistener to an object, we are creating a reference to that object, so even after that object falls out of scope or has been removed from the display list, the GC will not pick it up because it will still show that there is a reference pointing to that object.

2. Using weak references for event listeners, (the 5th parameter when using addEventListener), somewhat overcomes this.  If you set this flag to true, when the GC checks an object for references, and the only references to that object are “weak” references, it will mark it for collection.  This potentially frees up massive amounts of memory.

3. Use removeEventListener whenever possible, to keep your code concise and performing well.

I will post some more about the profiler after I run some tests this week using it on some applications that my company has built previously.  This will prove to be most interesting, and most likely somewhat depressing.

Until then…

Adding an existing assembly to your Silverlight application

•June 16, 2008 • 2 Comments

In response to a comment on my previous article, I have decided to post a little bit about adding an assembly to your Silverlight app.  If you have used Visual Studio before with other assemblies, it is pretty much the same thing.

First, find where the assembly that you are trying to add is located.  I usually copy it to a Lib somewhere above my project folder so that I can include it in source control when needed.  The next thing you do is right click on the References folder in your Visual Studio Silverlight project (not the website project if you chose to include that in the Solution) and click Add Reference.  Once the Add Reference dialog box shows up, click on the Browse tab and browse up to the folder that you put your assembly in, select it and click OK. Now that assembly will be included with your code.

Then to use this namespace in your Xaml code, you need to add the xml namespace directive to the top of your file along with the default Microsoft xml namespaces:

One of mine looks like this:

xmlns=”http://schemas.microsoft.com/client/2007″

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:CraigN_Windows_Controls=”clr-namespace:CraigN.Windows.Controls;assembly=CraigN.Windows.Controls”

xmlns:Data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”

Both the xmlns:CraigN_Windows_Controls and xmlns:Data were added.  As you can see, we direct the namespace to a CLR compiled library and a particular namespace inside that assembly.  After this you should be able to reference any of the classes inside those namespaces by using the prefix that you chose next to the “xmlns: ” tag:

Data:DataGrid x:Name=”StoriesList” Grid.Row=”1″ Margin=”5″ AutoGenerateColumns=”True”

I will try to go through some of the rest of Scotts lessons here shortly, thanks!

Beginning Silverlight – Walking through Scott Guthrie’s lessons

•June 11, 2008 • 2 Comments

Scott Guthrie has posted some great introductory lessons for Silverlight on his blog.  The only downside of it right now is they are a little outdated, they arewritten against the Silverlight 2 Beta 1 SDK, and there have been some significant updates in the new release of Silverlight 2 Beta 2.  If you have not already, you can download Silverlight 2 Beta 2 here.  This is an extension for VS 2008 including tools and the libraries needed.  The beta 2 was released on June 6, 2008.

Microsoft has some MSDN Documentation on the Silverlight project which is extremely helpful, you can find that here.  Especially the changes between Beta 1 and Beta 2 – here.

I found his blog postings very helpful, better than many of the tutorials I have found.  I did run across one problem which Craig Nicholson came up with a workaround for.  The WatermarkedTextBox control did not make it into the final evolution of Silverlight 2 beta 2.  Scott’s blog post still calls for this control though, and I personally think its a very useful control.  Craigs blog post is here, and you can download his project which still includes the WaterMarkedTextBox control here.

One thing I found about Craigs Tutorial, if your new to Silverlight and Xaml, the Xml Namespace declaration at the top needs to be prefixed with clr-namespace:, like so:

xmlns:CraigN_Windows_Controls=”clr-namespace:CraigN.Windows.Controls;assembly=CraigN.Windows.Controls”

Where Craig just had:

xmlns:CraigN_Windows_Controls=”CraigN.Windows.Controls;assembly=CraigN.Windows.Controls”

Note: Although this is a work around, for some reason when I used this control I was unable to type in the TextBox, I have not looked into this as of yet, will update when I take a look at the source.  For now, I just removed it and used a standard TextBox control.

I noticed by default System.Net is not included as a library reference when you create a Silverlight project in VS 2008, but this is where the WebClient class lives, so you will need to add that reference to be able to use WebClient.

On a separate note, on thing I have gotten used to in Flex and AS3 development is not only tracing things, but using Alert boxes to show data when I want to know if something has returned the correct result.  So, with this digg service request, I went to create a MessageBox (thinking its WPF!), well we actually don’t have a native MessageBox in Silverlight.  There are integrations though into the page DOM which allows us to create JS popups though:

HtmlPage.Window.Alert(result);

Also, to use Linq to XML  we need to reference the System.Xml.Linq assembly.

Data:

The Silverlight Data controls are in the System.Windows.Controls.Data assembly, in the System.Windows.Controls namespace.  This also is not referenced by default, so it needs to be added to the project before adding the xmlns directive on the xaml page.

More to come…