I`m doing an application in air(flex4), as this is my first app, I need some advices to do the integration part.
All integration between my system and the back-end have to be done using MSMQ, alright开发者_StackOverflow中文版, I have heard some libs that I can import inside my flex project to put and read messages in the MSMQ queue, but this is not my doublet, I would like to know, how it`s a good approach to do this communication, what I mean is, should I have an actionscript class, called for example MSMQService, and there, I should have the functions to put and read in the queue? or should I create a new custom tag in Mate, and have separate eventMap just to deal with the integration?! I know that there are lots approach, but if someone already done that, I would like to know how you did, like some good patterns.
Thanks for all help!
First, I've never used MSMQ and my answer will be generally for Mate and server communication(due to my point of view and 3 months experience).
Second, I don't think that your communication classes need another eventMap
.
Overview of the structure of my current project:
- view classes, responsible only for showing user interface, we tried to not include any application logic in them.
- controller classes, one big all-seeing
eventMap
and bunch of events. TheeventMap
include logic for 'what view.event triggers what model.function'. There must be NO calculations or other application logic in the 'eventMap'. modelMap
responsible for binding the one direction link between model and view, describing what must change in the view classes (UI) when something is changed in the model.modelMap
MUST NOT allow the view classes to directly manipulate model, it is against principles of MVC- model classes - the classes responsible for application logic. Also the communication classes are there. Something like `ServerCommunicationManager, class sending GET/POST/tec requests and requesting for response
MATE logic:
1.) Some View class is manipulated and this view sent some event. Example: user with name and password presses the login button (RegisterScreen.mxml)
dispatchEvent(new UserRequest(UserRequest.AUTHENTICATION, name, password));
2.) The eventMap
instance receives the event in EventHandler
and invokes some method in the ServerCommunicationManager
class. Example:
<EventHandlers type="{UserRequest.AUTHENTICATION}">
<MethodInvoker generator="ServerCommunicationManager"
method="signUser"
arguments="{[event.name, event.password]}" />
</EventHandlers>
3.) The model class method is invoked. Example: my example is stubbed!
public function signUser(user:String, password:String):void
{
var passwordEncoded:String = encodePassword(password);
var jsonMessage:Object = new Object();
jsonMessage.type = "checkUser";
jsonMessage.name = name;
jsonMessage.password = passwordEncoded;
sendGetRequest(serverIP, json, receiveResponseHandler);
}
public function receiveResponseHandler(response:Object)
{
var userDetails:UserDetails = decodeJsonToUser(response);
if(userDetails is NoUser)
{
FlexGlobals.topLevelApplication.dispatchEvent(new ServerResponseEvent(ServerResponseEvent.NO_USER));
}
else if(userDetails is NormalUser)
{
FlexGlobals.topLevelApplication.dispatchEvent(new ServerResponseEvent(ServerResponseEvent.NORMAL_USER, userDetails));
}
else if(userDetails is Administrator){...} else ...
}
4.) Back in the EventMap
<EventHandlers type="{ServerResponseEvent.NORMAL_USER}">
<PropertySetter generator="UserModel"
targetId="signedUser"
source="{event.userDetails}"/>
</EventHandlers>
<EventHandlers type="{ServerResponseEvent.NoUser}">
<PropertySetter generator="UserModel"
targetId="signedUser"
source="null"/>
<PropertySetter generator="ViewModel"
targetId="state"
source="loginDenied"/>
</EventHandlers>
And in the modelMap
:
<Injectors target="{RegisterScreen}">
<PropertyInjector targetKey="state"
source="{ViewModel}"
sourceKey="state" />
<PropertyInjector targetKey="userName"
source="{UserModel}"
sourceKey="signedUser"/>
</Injectors>
Overview: In this approach you can successfully decopulate view classes from communication classes. It works stable so far in our project.
Edit: Since I'm relatively new to Mate, if someone see errors in my approach he MUST make a comment about it. It really important for me if some of this logic is partially or entirely wrong.
精彩评论