Introduction
Robotlegs is an MVC framework for building flash / flex applications. This framework is based on the swiftsuspenders library, which implements injection mechanisms using the [Inject] meta tag. Google has a lot of material on the use of this framework. Therefore, I will not dwell directly on inject. However, in all the articles that I came across, it was the injection mechanism that was described, but too little attention was given to declaring the classes being injected. I would like to devote this article to the way of declaring injected classes.
Model ad
The most commonly used model for injection. In the Robotlegs framework, they are inherited from the Actor class. The declaration of the injected classes occurs in the startup () method of the main context (inheritor of the Context class). The injector property is responsible for the injection; its methods implement various injection methods.
There are several ways to register (declare) classes for later injection. Consider them.
mapClass
mapClass is the easiest way. The first parameter is the requested class (or interface), the second parameter is the class, an instance of which will be created upon request. The third argument is an optional identifier.
In this case, with each request of the desired class, a new instance will be created. The class constructor must not take arguments or have default values.
example:
injector. mapClass ( ITestClassActor, TestClassActor, "testClassActor" ) ;
Now in all classes that use injections, an instance of the class TestClassActor will be used instead of ITestClassActor. Moreover, I note that every time it will be a
new instance.
mapSingletonOf
mapSingletonOf - works the same way as mapClass, only when an instance is requested, it is created only once. And each subsequent query returns exactly this instance.
injector. mapSingletonOf ( ITestSingletonOfActor, TestSingletonOfActor ) ;
mapSingleton
mapSingleton - just like mapSingletonOf, creates only one instance of a class, but in this case only the injected class is specified.
injector. mapSingleton ( TestSingletonActor ) ;
I note that the above methods themselves create instances of the required classes, so their constructors must not contain arguments. If you need to associate a class with an already existing object, you need to use the mapValue method.
mapValue
mapValue is used to associate a class with a specific instance.
injector. mapValue ( TestValueActor, new TestValueActor ( ) ) ;
However, there is a small feature. As mentioned earlier, when using mapClass, mapSingletonOf, mapSingleton, an instance of the class is created automatically. Moreover, during the creation, if the object itself uses injections, then all injections are initialized with the necessary instances. When using mapValue, we create the necessary instance ourselves, and the izhekty do not work. In order for injects to work, we need to use the injectInto method, passing our instance as a parameter.
To make it clearer, consider a small example:
- package test. model
- {
- import org. robotlegs . mvcs . Actor ;
- / **
- * class we depend on
- * @author Vadim (-FeLiX-) Seluyanov
- * /
- public class TestValueActor extends Actor
- {
- public function TestValueActor ( )
- {
- super ( ) ;
- }
- }
- }
- package test. model
- {
- import org. robotlegs . mvcs . Actor ;
- / **
- * class dependent on TestValueActor
- * @author Vadim (-FeLiX-) Seluyanov
- * /
- public class TestInjectIntoActor extends Actor
- {
- [ Inject ]
- public var testValueActor: TestValueActor;
- public function TestInjectIntoActor ( )
- {
- super ( ) ;
- }
- }
- }
in context initialization:
- injector. mapValue ( TestValueActor, new TestValueActor ( ) ) ; // associate the dependent class with a specific instance (in any other way)
- var injectIntoActor: TestInjectIntoActor = new TestInjectIntoActor ( ) ; // create an instance of a dependent class
- injector. mapValue ( TestInjectIntoActor, injectIntoActor ) ; // bind
- trace ( ( injector. getInstance ( TestInjectIntoActor ) as TestInjectIntoActor ) . testValueActor ) ; // before the injection property is null
- injector. injectInto ( injectIntoActor ) ; // injectable
- trace ( ( injector. getInstance ( TestInjectIntoActor ) as TestInjectIntoActor ) . testValueActor ) ; // after the inject property is set
We looked at the basic methods for declaring injected instances.
PS: I have not considered another declaration method: use the mapRule method. Now I understand how it works. There are no materials on this method either in our segment of the Internet or in the western one. Therefore I pick source codes.