This project is read-only.

Controller Actions question

Jun 27, 2013 at 5:55 PM
Markus,
I am still getting a handle on some of the calls since the syntax in some of the examples is rather terse with many things done in a single statement. Not complaining or asking for a change, just saying at times, for folks new to the framework, it can be a little more difficult to figure things out.

Anyway, I want to add a delete option to my controller. This option should be available either from the list or edit view. In either case, it should simply pop up a message to confirm I want it deleted and then, if so, delete it. The only UI is the messagebox.

I am sure it's easy, but I am at a slight loss as to how to do this. It may just be that I don't have the syntax correct.

In addition to getting just the syntax, is there a reference article on creating these items? For example, can I set up an Edit option that can accept a decimal value or only a string? Stuff like that.

Thanks!
Jun 28, 2013 at 12:54 PM
I hear you. With things like generics and lambda expressions, one can certainly create a lot of syntax that is quite confusing when moving into these environments. We have tried to tune that back a whole lot from what many other architectures do since we always see that people have problems with this, but there is still some of it left in it, no doubt.

One thing you can do in general is to move that code to a method. So for instance, rather than doing this:
Actions.Add(new ViewAction("Magazines", execute: (a, o) => Controller.Message("Delete the record?", "Delete", MessageBoxButtons.YesNo)));
You can also move the code from the definition of the action into a separate method. For instance, if you have this method:
private void ShowMessage(IViewAction a, object o)
{
    Controller.Message("Delete the record?", "Delete", MessageBoxButtons.YesNo);
}
And then the definition of the action simply points to that method:
Actions.Add(new ViewAction("Magazines", execute: ShowMessage));
Now as far as the message box specifically goes: Calling a message box is quite similar to the typical message boxes in .NET. Except instead of doing MessageBox.Show(), you can also do Controller.Message(). Standard message boxes are still available of course, but we also introduced our own mechanism for a number of reasons. We wanted message boxes to be stylable for instance. We also wanted to introduce better testing even with message boxes involved. And we wanted to extend message boxes by allowing people to add their own controls and such (message boxes often need things like "don't show this again" checkboxes or textboxes that allow entering a value and such). Finally, we wanted message boxes to not block execution. So rather than message boxes directly returning a result, they raise an event when competed (this also has other advantages such as working well in all kinds of different scenarios... at the admitted expense o sacrificing some discoverability).

Here's an example of a messagebox asking if a record shell be deleted and reacting accordingly:
private void ShowMessage(IViewAction a, object o)
{
    Controller.Message("Delete the record?", "Delete", MessageBoxButtons.YesNo,
        onComplete: r =>
                    {
                        if (r.ModelMessageBox.Result == MessageBoxResults.Yes)
                            CallMyDeleteMethod(); // Do whatever is needed to delete
                    });
}
OnComplete of course is another one of those delegates that you could break out into a separate method if you find that more natural.

BTW: More info about message boxes here: https://codeframework.codeplex.com/wikipage?title=MessageBoxes%20in%20WPF%20MVVM%2fMVC


Markus
Jun 28, 2013 at 6:49 PM
Markus,

Thanks, that should help.

I have done something similar in my other projects but I also have an option where if a special key (or key sequence) is pressed, then it essentially executes Debugger.Break();

This helps especially when I get a message when I don't expect it and want to know why I did.

If there isn't something like this in your messagebox, my knee jerk approach would be to create a subclass and add it there. But I am thinking that there is probably a better way to add this to your messagebox, but not sure how. I will check out the link once I have a few moments and see if it is covered there.

Thanks,

Fletcher
Jul 1, 2013 at 7:18 PM
You could take a look at what the controller does for message boxes. Basically, it creates a view model and some text and a view to show it all. You could do the same and add another button/action that only gets added in debug mode (for instance) and thus have a similar behavior.


Markus