Start 2
This chapter provides a quick introduction to how to use parsley to build a simple application. Click here to see more details. Most of them apply to building flex or flash applications in the same way. This is applicable only to examples of flex development.
2.1 Hello World
If you want to study a simple example before reading the manual, you can find a hello World and right-click "View Source" to view the source code.
This section will not mention the instance application.
2.2 Add a framework swcs
To start development with parsley, you first need to add some swcs to your classpath. It depends on what features you want to use and whether you want to build a flex or flash application. For more information, see 1.5 dependencies.
Download parsley core and add the required swcs to your classpath from the release and related folders. You will find them in the release folder of the downloaded ZIP file.
Required swcs
You always need:
· Parsley-core-$ {version}. SWC
· Spicelib-commands-$ {version}. SWC
· Spicelib-reflect-$ {version}. SWC
· Spicelib-util-$ {version}. SWC
Flex additional SWC
For flex, you also need:
Parsley-Flex-$ {version}. SWC
Configure additional swcs in XML
You only need to add the following when using xml configuration:
· Parsley-XML-$ {version}. SWC
· Spicelib-XML-mapper-$ {version}. SWC
2.3 define object dependency
This may be the most frequently used feature of parsley .. Dependencies can be configured using metadata, mxml, XML, or programming.
Use the [inject] metadata tag
You can use this label on the property.
[Inject]
Public var loginservice: loginservice;
[Inject]
Public Function set loginservice (Service: loginservice): void {
Alternatively, you can use the method to declare multiple dependencies at a time.
[Inject]
Public Function Init (loginservice: loginservice, cartservice: cartservice = NULL): void {
In the preceding example, parsley will reflect the parameter type and process the optional parameters as optional dependencies. therefore, this example will throw an error if there is no loginservice, but if there is no cartservice in the container, it will be silently ignored.
Finally, you can use a similar Label [injectconstructor] to Construct Method injection above the class declaration (because the flex compiler ignores the metadata tag on the constructor ).
[Injectconstructor]
Public class loginaction {
Public Function loginaction (loginservice: loginservice, cartservice: cartservice = NULL): void {
Therefore, the second (optional) parameter is used as an optional dependency.
Best Practice
The dependencies of all examples shown in this section use classes. Because parsley performs reflection on the property and method parameter types. Benefiting from the loose coupling provided by an IOC container, you should prefer to declare the interface type at the injection point, in this way, you can switch the implementation in the configuration without touching the injection points of the classes that require these services.
Of course, this only applies when the object you add to the container has only one injection point that matches this type. If you have multiple implementations for the same interface, you need to switch the injection through ID. Only the attribute can use ID injection.
[Inject (ID = "mainloginservice")]
Public var loginservice: loginservice;
You decide to use attributes, methods, or constructor injection. Some people like to use constructor injection because it allows you to create immutable classes through better encapsulation. However, we only recommend that you use constructor injection in Flash Player 10.1 or later versions because the constructor has a reflection bug in earlier versions.
View more 4. Dependency Injection
2.4 send and receive messages
If you use interface types to define injection points, although dependency injection allows decoupling to a certain extent, sometimes you will prefer some part of your program or even loose coupling, make part of the application communicate through messages, and the sender and recipient do not need to know each other.
For example, you may change the underlying view event to call a presentation layer model for distributing application messages. Therefore, any operation/controller can be registered with all relevant message types.
For more information about this topic, refer to message 6 and build the MVC Architecture.
With parsley, you can use metadata labels to configure the sender and receiver.
The sender part may contain only one [messagedispatcher] tag on a public function attribute:
[Messagedispatcher]
Public var DISPATCHER: function;
Public class loginserviceimpl implements loginservice {
[...]
Private function handleloginresult (User: User): void {
DISPATCHER (New loginmessage (User ));
}
}
Here we use an injected dispatcher function to send a loginmessage instance. Messages with parsley do not need to inherit the event class (although they can do this if they need ).
On the receiver, you can use the [messagehandler] tag to tag specific message types.
[Messagehandler]
Public Function login (Event: loginevent): void {
For example, dependency injection selects a Message Receiver by type, which improves the robustness of the system, because you do not need to maintain the unique String constant of the event type in the system.
The acceptor has more tags, such as [messagebinding] tags, which will be explained in 6 messages.
2.5 Assembly object
In addition to tags, dependency injection and message passing also have many configuration options. But because these are very common, we will now show you how to assemble these objects for the IOC container. After you add metadata labels to a class, you must tell the container which objects should be managed.
Flex Application
In Flex, you may prefer mxml configuration. You can create a simple mxml class (using the <Object> Of the parsley namespace as the root tag) and add all the classes you want to manage by parsley.
<Objects
Xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns = "http://www.spicefactory.org/parsley"
Xmlns: services = "com. Bookstore. Services .*"
Xmlns: Actions = "com. Bookstore. Actions. *">
<FX: declarations>
<Services: loginserviceimpl timeout = "3000"/>
<Services: cartserviceimpl timeout = "3000"/>
<Actions: loginaction/>
<Actions: addtocartaction/>
<Actions: deletecartaction/>
</FX: declarations>
</Objects>
You can also use this configuration file to set other property values. They are processed together with the metadata tag of the class added to the container.
In this example, no ID attribute is specified. If you only use class injection, you do not need them. If you use ID injection, you must specify them here, just like partial injection in the last example.
<Services: adminloginserviceimpl id = "adminlogin" timeout = "3000"/>
<Services: userloginserviceimpl id = "userservice" timeout = "3000"/>
You can use these IDs at the injection point:
[Inject (ID = "adminlogin")]
Public var adminlogin: loginservice;
[Inject (ID = "userlogin")]
Public var userlogin: loginservice;
However, in general, you should avoid using ID injection and use it only when one class has multiple instances.
Flash applications
In this case, mxml is unavailable. Therefore, the preferred configuration mechanism may be to use XML files. If you do not want to compile the configuration into your application, you can also use an external XML file.
The above mxml example for configuring an example XML should look like this:
<Objects
Xmlns = "http://www.spicefactory.org/parsley"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.spicefactory.org/parsley
Http://www.spicefactory.org/parsley/schema/2.3/parsley-core.xsd"
>
<Object type = "com. Bookstore. Services. loginserviceimpl">
<Property name = "timeout" value = "3000"/>
</Object>
<Object type = "com. Bookstore. Services. cartserviceimpl">
<Property name = "timeout" value = "3000"/>
</Object>
<Object type = "com. Bookstore. Actions. loginaction"/>
<Object type = "com. Bookstore. Actions. addtocartaction"/>
<Object type = "com. Bookstore. Actions. deletecartaction"/>
</Objects>
Like the mxml example, we do not define an ID for the object.
You can add it if you need to inject it through ID.
These are the most common choices for assembling objects. You will find more examples in 3 configuration and initialization.
2.6 initialize the framework
Now, you have configured the class with metadata tags and the collection is in mxml or XML, and it is time to initialize it. Fortunately, in most cases, this is a line of code or a simple tag.
For the mxml configuration file we created, suppose we call it bookstoreconfig. mxml, the TAG should be like this:
<Parsley: contextbuilder Config = "{bookstoreconfig}"/>
This is everything.
For XML examples, we must initialize the context programmatically in a flash application:
Xmlcontextbuilder. Build ("config. xml ");
We include the most common use cases here. There are many options, such as splitting the configuration into multiple files, or even using different configuration mechanisms (such as using mxml and XML in the same application) and merging them into one context. This will be demonstrated in the 3.7 combination of multiple configuration mechanisms.
For modular applications, You Don't Want To package everything into a whole context and load everything in the application. To build modular applications and load and uninstall configurations as needed, see 10 build modular applications.
2.7 add more services.
Because this is the entry part, we only include the most common use cases. But I hope it is enough for you to solve simple applications. You may have noticed that the Manual is quite general. If you think you need more options than in this chapter, you should read the remaining chapters.
If you just want to get an idea from parsley's features, you may want to see the 1.1 feature list.
The most interesting options may not be included in this chapter:
3.6 configure DSL and 3.7 ActionScript: you do not have to limit to metadata, mxml, or xml configuration. There are two more options, which gives you extra flexibility.
6.7 message interception: A messagehandlers contained in this chapter even allows you to interfere with the Information scheduling process (such as pausing and resuming or canceling message processing ).
7. management commands: You can run commands in the container management mode when they are executed.
8.2 factory use: In addition to assembling the target object itself, you can also add the factory to the IOC container to create an object, which gives you extra flexibility on how to instantiate the object.
8.3 asynchronous object initialization: Configure options for asynchronous object initialization (for example, an object needs to load data before it can be operated ). In this case, the container will delay initializing other objects until these configurations have been prepared for Asynchronization.
9 Dynamic View Assembly: We have briefly mentioned this. Allows you to connect the flex component defined in mxml to objects in the declared IOC container.
10 build a modular application: Use a configuration context to associate the configuration service, operations, presentation layer model, and so on, and load and unload the application as needed.
12.1 resource binding: management objects of localized resources bound to attributes are automatically updated when the language environment changes.
13. Extension framework: If you want to add your own metadata, mxml or xml configuration tags, or use other available extension points. Used to simplify repetitive configuration tasks or create a framework on top of parsley.