Parsley’s Constructor Based Injection [InjectConstructor]

Implementing Constructor based Injection:

Constructor based injection in Parsley can be achieved using [InjectConstructor] metadata tag. It provides us with cleanest injection style as per encapsulation because it allows you to create immutable classes.

When we say its a constructor based injection, doesn’t that mean the metadata tag has to be placed on the constructor ?

Here is what I am trying to say:

package com.xyz{
     public class abc{
         [InjectConstructor] 
         public function abc(){
             }
    }
}

But, unfortunately the Flash Player currently ignores metadata tags placed on constructors you have to place a [InjectConstructor] tag on the class declaration to tell Parsley to perform Constructor Injection.

package com.bookstore.actions { 
 [InjectConstructor] 
 class LoginAction { 
 private var service:LoginService; 
 private var manager:UserManager;   
 function LoginAction (service:LoginService, manager:UserManager = null) { 
 this.service = service; 
 this.manager = manager; 


}

Constructor injection selects the dependencies based on the parameter types. This means that it only works for dependencies where you know that the Context will always contain at most one object with a matching type. It is also good practice to use interfaces as parameter types so that you can switch implementations in the configuration without modifying the class.

Using interfaces:

package com.bookstore.actions { 
 [InjectConstructor] 
 class LoginAction { 
 private var service:ILoginService; 
 private var manager:IUserManager;   
 function LoginAction (service:ILoginService, manager:IUserManager = null) { 
 this.service = service; 
 this.manager = manager; 


}

In the above code, parsley framework container throws an error if it does not contain an object of type LoginService, but it will simply silently ignore the second parameter if the Context does not contain an object of type UserManager.

Configuring Constructor based injection:

As MXML compiler generates the object creation code and parsley only gets hold of the object after it was instantiated to perform additional configuration, one cannot directly use simple MXML tags for configuration.

<Objects xmlns:fx="http://ns.adobe.com/mxml/2009" 
 xmlns="http://www.spicefactory.org/parsley" 
 <fx:Script> 
 <![CDATA[ import com.bookstore.actions.*; ]]> 
 </fx:Script>
 <fx:Declarations> 
 <Object type="{LoginAction}"/>
 </fx:Declarations> 
</Objects>  

Here is how we pass in the constructor args:

<parsley:Objects xmlns:parsley="http://www.spicefactory.org/parsley">
    <fx:Declarations>
          <!-- Configuring Login Action -->
          <parsley:Object  id="loginAction" type="{LoginAction}">    
             <parsley:ConstructorArgs>           
                <parsley:ObjectRef  idRef="userManager"/>             
                <fx:String>login</fx:String>
             </parsley:ConstructorArgs>
           </parsley:Object>

           <!-- Configuring User Manager -->
           <parsley:Object  id="userManager" type="{UserManager}">    
             <parsley:ConstructorArgs>           
                <fx:String>administrator</fx:String>
             </parsley:ConstructorArgs>
           </parsley:Object>
     </fx:Declarations>
</parsley:Objects>

When using Parsley’s Object tag it’s the framework that is responsible for instantiating the object so that Constructor Injection can be performed.

There are no restrictions when you are using XML configuration.

Source: http://www.spicefactory.org/parsley/docs/2.4/manual/injection.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s