Custom metadata tags in Silverlight as in Robotlegs

Identifying a debug or release SWF file at runtime example screenshot

Today, I chosen to take the time to have a deeper look into Robotlegs micro-architecture, an ActionScript project initiated by Shaun Smith and Joel Hooks. From what I tested and as Robotlegs supports MVC, is inspired by PureMVC and that it is really well thought by its author, I'm convinced that it is a good project to look into during my spare time.

As you may know Robotlegs uses Dependency Injection. And it also uses Dependency Injection using metadata tags. In Robotlegs this is done with SwiftSuspenders a lightweight Dependency Injection library for ActionScript developed and maintained by Till Schneidereit. This means that when you want Robotlegs to automatically inject a dependency at runtime you just have to write something like :

 
public class MyClass
{
    [Inject]
    public var userProxy:UserProxy;
}
 

This is not explained in the Robotlegs nor SwitSuspenders documentation, but the [Inject] metadata tag is not a native metadata tag. It's a custom metadata tag "declared" by the SwiftSuspenders library. And this is something allowed only because Flash Builder compiler allows to compile custom MetaData tag description that you can retrieve using describeType on the Class that contains those metadata tags.

In Flex Builder this will look like this :

 
package
{
    import flash.display.Sprite;
    import flash.utils.describeType;
 
    public class TestCustomMetadata
        extends Sprite
    {
        public function TestCustomMetadata()
        {
            var xml:XML = describeType( MyClass );
            trace(xml);
        }
    }
}
 
class MyClass
{
    [Inject]
    public function foo():void{}
}
 

Which will output :

[...]
<method name="foo" declaredBy="::MyClass" returnType="void">
    <metadata name="Inject"/>
</method>
[...]
 

Note that as well custom metadata tags are automatically compiled in the debug version of your SWF. They aren't in the release version. As documented in the README from SwitSuspenders library, if you want it to be compiled in your SWF release version, you have to add -keep-as3-metadata+=Inject argument to the compiler command line.

Okay, so we have it in Flex. Now I think you understood that my goal this afternoon was simply to verify if Silverlight also supports custom metadata tags to check if Robotlegs could be one day ported to Silverlight with the exactly same syntax as it uses in Flex.

The MSDN explains that C# (and VB) supports custom metadata attributes. I've tested it and hopefully it works in Silverlight as well.

Here is my test class :

 
namespace CustomMetaData
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            injectMembers( typeof(MyClass) );
        }
 
        private void injectMembers( Type Type )
        {
            MemberInfo[] members = Type.GetMembers();
 
            foreach( MemberInfo memberInfo in members )
            {
                object[] customAtts = memberInfo.GetCustomAttributes(false);
                foreach( object att in customAtts )
                   if( att is Inject )
                       log( memberInfo.MemberType + " " + memberInfo.Name + " is injectable with " + ((Inject)att).FirstParam + " , " + ((Inject)att).SecondParam );
            }
        }
 
        private void log( string message)
        {
              stdout.Text += message + "\n" ;
        }
    }
 
    [AttributeUsage(AttributeTargets.All)]
    public class Inject : System.Attribute
    {
        public string FirstParam;
        public string SecondParam;
        public Inject( string firstParam, string secondParam )
        {
            FirstParam = firstParam;
            SecondParam = secondParam;
        }
    }
 
    public class MyClass
    {
        [Inject("FooFirstParam","FooSecondParam")]
        public void Foo(){}
 
        [Inject("BarFirstParam","BarSecondParam")]
        public String Bar{ get; set; }
 
        [Inject("FooBarFirstParam","FooBarSecondParam")]
        public String FooBar;
    }
}
 

Which outputs :

 
Method Foo is injectable with FooFirstParam , FooSecondParam
Property Bar is injectable with BarFirstParam , BarSecondParam
Field FooBar is injectable with FooBarFirstParam , FooBarSecondParam
 

With this test, I'm sure now that it will work in Silverlight too. If you want to test it by yourself you can download the VS 2010 Silverlight 4 project I used by myself.

If there's some real interest in it, I'm seriously thinking to work on a port of Robotlegs and SwiftSuspenders to C# for Silverlight. Both licenses of the libraries authorize it. I think this could be a good challenge for me and the RIA community.

4 thoughts on “Custom metadata tags in Silverlight as in Robotlegs

  1. Pingback: Twitter Trackbacks for Tekool.net - Développeur Flash - Custom metadata tags in Silverlight [tekool.net] on Topsy.com

  2. For those interested in, I seriously had started to work on the Robotlegs to Silverlight port.

    In its first releases, it will probably not use SwiftSuspenders but better a C# injection framework as NInject.

    For the moment I successfully have converted the Robotlegs framework itself using replacement classes for Events, EventDispatcher and am looking into what to use to replace DisplayObject, Sprite and all the Stage events dependencies (which can cause me serious problems, at least fatal ones).

    I will post some news here as soon as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>