Creating objects with out defining them — WHAT?

•September 20, 2008 • Leave a Comment

Lately my time has been devoted to one project, a very very cool project. That is about the depths I can go on in my blog but I can say that it involve a windows service that is basically the host for many assemblies and are accessed by webservices via .NET Remoting.

I wanted this service to be able to do 2 things. Since more and more assemblies were being added and certain customers purchased some features and other did not, I wanted it to be able to dynamically load them, getting the information from a config file and if the assembly failed during initialization, not continue the load.

My first approach:

At first I just added the singleton object in my service code, initialized and wham, all is good but this proved to be non-scalable. If I didn’t want the assembly to load (perhaps I was debugging, there was a know bug, customer didn’t purchase module, etc), it would still load and took up memory. Also when I coded newer assemblies, I would have to manually add an object reference and initialize them, maintenance nightmare.

I wanted each assembly to be autonomous, as well as the service. Neither should know ‘too much’ about each other. This way I could develop new assemblies, add them to the config file and it would load it when the service ran. Also, if I wanted to remove it, I just removed it from the config file.

My second approach:

So how did I accomplish this? With .NET Activator class and by calling its CreateInstance method, I can dynamically create an object base on a type with out defining it in the code. For example:

Class1.CClass1 objClass = new Class1.CClass1();

Is the same as

object objClass = Activator.CreateInstance(Type.GetType(“Class1.CClass1,Class1”));

These do the SAME thing yet one you have to define Class1, in the other piece of code, a string defines it. Magic I say.

Download here

Disable iTunes BackUp

•August 16, 2008 • Leave a Comment

Love my iPhone 3G but man the backing up process, mega slow.  Check out this link to see how to disable it.

Who loves GUIDs?

•August 14, 2008 • Leave a Comment

I love GUIDs. Why, well first I love saying ‘Guew-ehdd’ though I have heard it called something else but any time you need a unqiue ID, GUIDs come to the rescue.  So as I moved into Flex, I wanted the same thing; here it is..

import mx.utils.UIDUtil;

UIDUtil.createUID() – returns a guid looking string

Pandora!

•July 26, 2008 • Leave a Comment

Pandora is a GREAT Free application that streams music to your PC. What more can you ask for other than Pandora’s Box.

Pandora

AIR Version

A time for re’flex’tion

•July 25, 2008 • Leave a Comment

Any of you who use Java and C# know about reflection. Wikipedia has a definition here, but basically it allows the application to self interrogate itself and figure out things during runtime. 2 actionscript methods come to mind when I think of reflection.

getQualifiedClassName and getDefinitionByName both in the flash.utils package.

So I wanted to tell Flex to create a button without coding a ‘new mx.controls.Button’, so how is this done? Well with reflection.

Lets take a look at the Create method below. It takes an undefined data type, a string, two integers and two optional integers. The last four parameter are obvious so I will talk about the first one, objClass. This is where you tell Flex what kind of object to create, you pass in the class name like Button, List, etc and Flex will get the class name from the object, create a reference to it and then create an instance of that reference. Then it will return the object so you can use it, set data, add events, etc.

public function Create(objClass:*, strTitle:String, X:int, Y:int, iWidth:int = -1, iHeight:int = -1):*
{
var strClass:String = flash.utils.getQualifiedClassName(objClass);

var objViewRef:Class = null;
var objView:Object = null;

objViewRef = flash.utils.getDefinitionByName(strClass) as Class;
objView = new objViewRef();
if(objView.hasOwnProperty(“label”) == true)
objView.label = strTitle;
objView.x = X;
objView.y = Y;

if(iWidth != -1)
objView.width = iWidth;

if(iHeight != -1)
objView.height = iHeight;

addChild(objView as DisplayObject);

return objView;
}

To create objects:

var objButton1:Button = Create(Button, “Button 1″, 10, 10);
var objList:List = Create(List, “List 1″, 10, 100, 100);
var objComboBox:ComboBox = Create(ComboBox, “ComboBox 1″, 10, 300, 100);

Download the code here or click here to view it!.

O/R Mapping with XML

•June 15, 2008 • Leave a Comment

Like XML? Like Objects? Ever wish you would manipulate xml with objects? Read on, will you shortly. Microsoft has a utility – the XML Schema Definition Tool called XSD.exe. It takes a xml flile you make and creates a schema file (a file that describes xml). With that schema file, you create a C# or VB class out of it. Then a few lines of code allow you to easily read in a xml file and automatically map it to an object and vice versa!

Here is a link to the tool

It is a command line tool that works like this. Assuming my xml file is called Data.xml, 2 steps get you a class to use.

Step 1 :XSD Data.xml
Creates data.xsd

Step 2: XSD -c Data.xsd
Creates Data.cs

Step 1 creates the xsd file. Normally you do not have to edit this file. Step 2 then creates a C# class. The -c parameter creates a C# object, I believe -vb creates a Visual Basic object.

Using this class, you can marshal the xml back and forth to objects. Here is some of the code in the example you can download.

XmlDocument xmlData = new XmlDocument();
xmlData.Load(“Data.xml”);
Data objData = (Data)DeserializeObject(xmlData.InnerXml, typeof(Data));

objData.FirstName = “John”;
objData.LastNameName = “Smith”; <- HEY change data using object properties !!!!!

String strData = SerializeObject(typeof(Data), objData);

strData wil be a string of your changed xml.

You load download the source here.

So why do you want to use this. Well for one, the mapping of elements are automatically done for you. You do not have to enumerate through the xml, parse out element, attributes, etc and map them to a class you would have to create. XSD.exe does this for you. XSD.exe is automatically installed for you with Visual Studio. To access it, open a Visual Studio Command Prompt ( this just sets the path for you so the command window can find XSD.exe ). Take a look at the data.cs file it creates. The second and most important reason, you can change data via properties! Just like a good object should.

So from xml to objects back to xml – hmmmm. Life is good.

Now the caveats – This tool takes the lowest common denominator approach. It cant’ all ways figure out all xml so it will default some object in the class as object not as the real class. So you may have to redesign your xml data to use this process but it is well worth it to have the tools do the work. Another huge point, if you decide to add something to your xml or remove it, just rerun the process to build a new class! All done – in a very short amount of time.

This is a very simple example there is a MUCH more you can do with it.

Enjoy….

Saving Flex UIComponents as JPG’s over a C# webservice

•June 14, 2008 • Leave a Comment

Ever needed to do screen shot in Flex and save it? What about saving a component like an image, swf or flv? I have created a sample that shows how to save an image to a C# webservice. It is very simple and builds off of other pieces of code but basically converts any Flex component that has an ID into BitmapData, encodes it as a JPG and streams it to the C# webservice where it saves it. Volia!

Download Source

Note:

  • Visual Studio 2005 was used in creating the webservice.
  • Make sure you edit the location of the webservice in the Flex code.

Resting with Flex – No not REST but …rest

•June 13, 2008 • 1 Comment

I recently came across an interesting feature in Actionscript/Flex. In C++, you can use the … parameter to pass in a variable amount of…. variables! Using …rest will allow you to do the same in Actionscript.

public function CoolFunction(…rest):void
{

for(var iParam:uint = 0; iParam < rest.length; iParam++)
{

if(rest[iParam] is String)

trace(“Passed in a String”)

if(rest[iParam] is Image)

trace(“Passed in a Image”)

}

}

public function OnInit():void
{

CoolFunction(“tst”);
CoolFunction(“tst”,”test”);
CoolFunction(imgImage2,”test”);

}

Nice stuff….

Practicing safe Flex

•April 21, 2008 • Leave a Comment

Welcome to my blog, I finally dove in. I will be blogging about Adobe Flex very soon.

 
Follow

Get every new post delivered to your Inbox.