Help understanding Cairngorm Event in Flex - apache-flex

I was going through this article by David Tucker (http://www.davidtucker.net/2007/10/29/cairngorm-part-3/) in which he talks about Cairngorm Events.
There are two things that I want to ask in this-
What is the significance of calling the constructor of the parent class with the event arg? super(LOGIN) in the example
Why do you need to overrride the clone method?
Understanding these things will give me a better insight into the way things are done with Cairngorm.
Thanks

Both questions are not specific to Cairngorm events. This is standard mechanism for AS3 events.
Cairngorm events inherit from Event and Event class requires a "type" string. That's why you need to specify it in the constuctor.
When creating a custom event class, you need to override the clone method. This method is automatically called by the Flash Player when you want to re-dispatch an instance of a custom event.

Related

Behavior vs Event Handler

What's the purpose of having behaviors in Xamarin.Forms? Isn't it possible to add new functionality to UI elements through event handlers? I am confused about when to use an event handler and when to use behaviors.
This is a very simple answer and is only intended to start your research. Undoubtedly a better answer will come along. I am still working through this and will keep an eye on it because I too would like an answer that is more thorough than mine.
With proper separation of your View and ViewModel, Events aren't really the way to go. Behaviors allow you to maintain a proper MVVM approach (for example, when calling a Command from an Entry's Completed event) by using an EventToCommandBehavior system.
Additionally, Behaviors allow you to create prepackaged, well, behaviors that can be applied to multiple controls' events without having to reuse code.

Why do Presenters attach to View events instead of View calling Presenter Methods in most ASP.NET MVP implementations?

I noticed that in the Webforms MVP implementation and most other examples, the Presenter usually attaches handlers to View events. Why can't the Views just call methods in the presenter directly? Just wondering, since the whole task of attaching a handler to an event, defining EventArgs for special parameters, checking if the event is null on the view side seems a lot more tedious than just calling a method.
Sure they can, and I find that to be the best middle ground. What you are describing is I believe called Observing Presenter style. This allows you to completely decouple View from the Presenter, making the view less susceptible to changes within presenter. But it also introduces complexity in testing, and that is the reason to use MVP to begin with. I would not bother with this style at all. On very large project we use Encapsulated Presenter style, where View has a reference to Presenter, injected via IoC container, and view just calls methods on the Presenter. Easy to understand, easy to debug, easy to test.

MVVM light toolkit messenger problems

I'm using your messenger class to communicate between views/controls and viewmodels.
Currently I have the same usercontrol multiple times on the same view.
The problem is that when one viewmodel sends a message back to the usercontrol then all of the
usercontrols (of that same type) in my view get updated.
How can this be prevented?
FYI, I played around with the token functionality that you provide, but I couldn't make it work for my particular case.
Have you tried altering your usercontrol so that upon construction, you can specify whether or not it should Register for that particular message? I would do that as a starting point.
Another thing to watch out with using Messenger.Default -- it isn't threadsafe, so if you end up using it in lots of places other than usercontrols (like in worker threads to notify the main thread of events happening), then you had better wrap it in another class that performs the requisite locking.
You could set the target of the message if your message inherits from MessageBase class,
or use a Guid as a Messenger token.

Intercept Page object creation to hook up events

I'm looking for a way to intercept the ASP.NET processing pipeline in such a way to be able to register event handlers to all events on the Page class. The reason is, I need to maintain a session-bound instance of a component that needs to be notified of all important Page events, starting from OnPreInit.
There's an arbitrary number of Page descendants in the application, which are not under my control. Hence I cannot use an approach like using a single custom descendant, that would notify the session-bound component, as a base class for all pages in the web application.
I don't think creating a custom IHttpHandler or IHttpModule implementation would solve the problem. Also note I cannot create a custom HttpApplication descendant.
It isn't going to be an elegant process to do what you are looking at, especially if you need to handle multiple page events, but in theory it is fully possible from within the Global.asax to setup handlers that you need for each and every page.
The trick here is to add your code to the global.asax in the PreRequestHandlerExecute method, from here you can get access to the HttpApplication object, get access to the page from there, and then register your events. This process is necessary as a new page instance is created for every page that is processed.
Now, other options as you know are far more elegant, but this should get to where you need to be. One helpful tutorial I found although around Themeing shows you the whole process here.
EDIT:
After seeing your comment, yes, you can simply do what I'm stating above, in a custom HttpModule. The article I linked even shows you that process :)
Without knowing more about what you're trying to accomplish it really sounds like you do indeed want to create a http module or handler. You might want to take a look at this question

MVP - Should views be able to call presenter methods directly or should they always raise events?

I've been using the MVP pattern for a while with ASP.NET. I've stuck to the defined pattern of raising presenter events from the view.
It has struck me that I could expose methods in the presenter that the view could call directly.
Technically, using direct methods calls would require less code. The other benefit is that I tend to share presenters across multiple views that offer similar functionality. This means that sometimes some of the event handlers are forced to be registered in a view, only to comply with the shared presenter's interface, but then are not used in that particular view at all.
A example of this would be a diary view, that in one view allows you to cancel an appointment, and in another it doesn't. The rest of the presenter events for loading the data, and saving an appointment are used in both. I don't want to write two separate presenters that almost offer the same functionality.
I'd like to hear what others think who are actively using MVP. Are there any reasons you can think of why direct method calls from the view to the presenter are bad in MVP?
I use direct method calls, and don't see any worhtwhile reason to be defining events on the presenter. What you are using with events is called I believe "Observing Presenter" style. It does offer complete decoupling of View from Presenter, but with added complexity.
We can write an interface which contains all methods that we want to call from view. Presenter will implement this interface. We can then pass instance of this interface to view, and view can call methods on this interface. This will reduce coupling between two.
If your view calls the presenter directly, then it would be tightly coupled like in mvc. I don't understand why some frameworks take this approach as opposed to raising events on the view.

Resources