Hi all,
I think some of you know I worked closely with the p&p team on Prism 1, remained an advisor for Prism 2, and am now working closely with the team again as a contractor for Prism 4 (aligning versions like everyone else). I know many of you have competing frameworks and don’t want to get into a debate of what is wrong with Prism and how your frameworks are better. I’ve studied many of those other frameworks and see the value in the way they do things.
One thing we are discussing is whether to use commands at all or to just use behaviors everywhere. Even though they finally added commands in SL 4, the fact is that they still only cover a small subset of the communications you need between a view and a view model, so you usually have to rely on behaviors for the everything other than button/hyperlink/menu clicks. So it raises the question:
If you can also easily handle those cases with behaviors, why bother with commands? They require you to understand and mix two concepts and coding patterns, whereas behaviors can pretty much address any scenario, and consistency is important as well.
So we are leaning towards just using behaviors everywhere that we used to use delegate commands.
Thoughts on this? Vigorous defense of commands over behaviors in the places where they are an option?
Thanks
Brian
-----------------------------------------
Brian Noyes
Chief Architect, IDesign Inc
Microsoft Regional Director / MVP
http://www.idesign.net
+1 703-447-3712
-----------------------------------------
Brian,
Like others, I’ve been feeling this wind blowing for a while now.
I think about what we give up:
It’s hard to beat the CanExecute feature of the CommandManager. When used properly, it simplifies WPF programming. I’ve asked the WPF Team a more than a few times to either unseal CommandManager and allow overriding of a method that would allow developers to throttle RequerySuggested, or provide the ability for a command to specify throttling for RequerySuggested. For example, “only call CanExecute on focus changed.”
The current Cider and Blend Designer tooling both support wiring up commands. Cider does not have support for adding behaviors using drag and drop.
Enter, Silverlight. I agree with the blowing wind but, I still want automatic CanExecute and UIElement disabling when the behavior can’t execute. I’ve thought about writing a service that would work in WPF & SL that does the notification. Commands could register with the service, maybe the behaviors could too. I hope that what Prism 4 delivers works in WPF & Silverlight.
If you proceed with this, please have P&P send a Partner Request to the Cider Team to ensure that they add support for the Asset Library and drag and drop behaviors out of the box. I’ve been thinking about writing an add-in that does this in VS 2010. I just need a few days of free time to do it.
Currently the Blend behaviors don’t pass payloads too well, custom behaviors can do anything. Comamnds have the awesome CommandParameter for passing context to the command. Behaviors need to expose a way to pass a payload.
I agree that Behaviors offer developers the opportunity to write less glue code just to execute a method. I think about my current viewmodels only publically exposing a few properties and a constructor. Now, they would also be exposing methods. Just thinking out loud.
Chew the meat, spit out the bones,
Karl
No, the answer in Silverlight isn't to abandon commands, but rather to
FINALLY implement completely. The next version of SL needs to bring
commands up to the level that exists in WPF.
Then we could use some nice Behaviors that help us to bridge some
command/event gaps. ;)
--
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.
War is peace. Freedom is slavery. Bugs are features.
Hey Brian,
The main reason why I still advise my users to use the Command property on a ButtonBase rather than using EventToCommand (or InvokeCommand or another behavior) everywhere: The XAML syntax for behaviors is annoying to write by hand. Granted, I don’t spend a lot of time adding behaviors manually (that’s what Blend is for), but I know that not all of them do. The Command syntax is more straightforward.
A compromise would be to offer an alternative syntax with an attached property. I hear that Blend 4 is better with attached properties than it used to be, but I didn’t have time to investigate. We would need to find a good syntax to define the event name and the binding to the command / commandparameter.
If syntax is not an issue, I can imagine that some users might be put off by having to add an external object just to execute a command. There might be a perception that the Command property is faster to execute than EventToCommand for example.
Nikhil Kothari has an interesting alternative in his Silverlight FX framework where he can bind an event directly to a method on the VM. This avoids having to declare commands. Eventually I think that this is what we should have in the .NET framework. Having to resort to the third party ICommand implementation (either RelayCommand/DelegateCommand/etc… or custom built command classes) is annoying and should go away. I mentioned this to Anders Helsjberg when we were both in Belgium this spring, and he was apparently aware of the issue. I hope we see that coming soon (I know that Nikhil is pushing that within MSFT).
Cheers,
Laurent
A note regarding CanExecute: My EventToCommand does optionally disable the attached object if the command is disabled (and of course if the element can be disabled…). This action is optional, because it just does not make sense to always disable a control for every command. For example, disabling a control for a mouseover command often doesn’t make sense. But for the MouseLeftButtonDown of a Grid, for example, you can disable the Grid (and all its children) when the command’s CanExecute returns false.
As for the CommandManager, I am a bit unsure that it is really such a loss in Silverlight. Sure I have to raise CanExecuteChanged manually (the RelayCommand has a RaiseCanExecuteChanged method that does this) but on the other hand I have total control on when this should occur. The CommandManager, on the other hand, is a bit too magical for my taste.
Anyway, to come back to Brian’s point, by using behaviors instead of the Command property, one is not giving up the disabling of the control, if the behavior is programmed properly and takes care of this.
Cheers,
Laurent
Thanks for all the inputs. I personally have the same resistance to giving up commands for the same reasons everyone stated.
I agree that if you replaced a command with a behavior, that behavior would have to also support enable/disable and parameters. The one the team currently wrote and is using in the MVVM Quickstart supports the enable part as another method you can point to (optionally, like commands) on your view model. It currently does not support parameters, but not sure why it couldn’t.
I also have some resistance to making methods on my view model part of the exposed API instead of just properties.
Another point of resistance is that if you go with non-Blend behaviors, you limit Blendability, if you go with Blend behaviors, you have to take a dependency on the Blend SDK.
But the counter point that keeps coming back up is that it is not like you can often get away with no behaviors, so you will have to deal with all the negative aspects of behaviors already.
The real question we are trying to reconcile is “if you are already using behaviors for other things in your UI, what does using commands for those things you can buy you over using a behavior for those as well?” This does assume we are talking about a behavior that lets you point to an execute method, a can execute method, and carry bindable parameters.
The main argument I am hearing based on the above is easier syntax for commands. The counter to that would be better consistency, a single concept to master, and less glue (no delegate/relay command definition/hookup required).
Thanks
Brian
--
--
Not true, Blend SDK, not Blend. It is just an SDK and you can xcopy deploy the DLLs. So yes, you have a dependency, but no more so than if you depend on Prism, or MVVM Light, or Framework X.
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Josh Smith
Sent: Tuesday, June 15, 2010 11:00 AM
To: wpf-di...@googlegroups.com
On Tue, Jun 15, 2010 at 2:02 PM, Brian Noyes
<brian...@softinsight.com> wrote:
> Not true, Blend SDK, not Blend. It is just an SDK and you can xcopy deploy
> the DLLs. So yes, you have a dependency, but no more so than if you depend
> on Prism, or MVVM Light, or Framework X.
>
>
>
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
> On Behalf Of Josh Smith
> Sent: Tuesday, June 15, 2010 11:00 AM
>
>
>
--
I was mainly arguing to stick with commands with the team. Thanks for
giving me some additional arguments to do so.
Brian
-----Original Message-----
From: wpf-di...@googlegroups.com
[mailto:wpf-di...@googlegroups.com] On Behalf Of Bill Kempf
Sent: Tuesday, June 15, 2010 11:00 AM
To: wpf-di...@googlegroups.com
> RelayCommand/DelegateCommand/etc. or custom built command classes) is
I will say, though, that I've been moving away from
Delegate/RelayCommand in my own code, and relying almost exclusively
on CommandBinding. I do this through a service provided to the
ViewModel that will add the bindings to the View. This allows me to
use "built-in commands" in exactly the same way I use my own commands.
On Tue, Jun 15, 2010 at 2:06 PM, Brian Noyes
Sent from my phone, please forgive typos and brevity
On Jun 15, 2010, at 11:31 AM, Daniel Vaughan <dbva...@gmail.com>
wrote:
Actually the dependency on Sys.Win.Interactivity.dll is also a good reason not to use a behavior. This is why EventToCommand is in the Extras DLL of MVVM Light, I didn’t want to force the user to add this extra DLL to their application.
OK, I’ll bite… J
Why is it harder to reference that assembly than referencing your extras one? I’m sure some of my customers might be more comfortable referencing something produced and supported by Microsoft rather than one from that really smart guy in Switzerland. J
--
Sent from my mobile device
:)
Laurent
-----Original Message-----
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
On Behalf Of Glenn Block
Sent: Wednesday, June 16, 2010 2:32 AM
To: wpf-di...@googlegroups.com
Hey Brian,
No no, I meant that if they choose not to use the EventToCommand behavior, but they only want to use the RelayCommand, Messenger and ViewModelBase, they can add a reference to GalaSoft.MvvmLight.dll only. If they want to use EventToCommand, they need additional reference to GalaSoft.MvvmLight.Extras.dll and Sys.Win.Interactivity.dll.
I didn’t want to force them to add a reference to Sys.Win.Interactivity.dll if they don’t explicitly use that feature. Mostly it is because of the bulk added to the download (Sys.Win.Interactivity is added to the XAP file). So I didn’t mean it is harder, just that some are hesitating to use behaviors because of the extra bulk.
Does that make more sense?
Now who is that really smart guy in Switzerland, I’d love to meet him…
Hey Sacha,
Just wondering, why exactly?
Let’s say we could have the same CanExecute functionality as with commands now (I am not sure how, but for the argument, let’s just imagine that). What would then be the difference between calling a method directly or calling it through a command?
The way I see it:
· Access from the view could be governed by an attribute setting [ViewBindingAllowed] or something
· It would make testability better, because invoking commands from unit tests is more cumbersome than calling a method directly
I agree with you that the CallMethodAction is not a good way to solve that. IMO introducing reflection here is not a good idea. That said, if it was built in the .NET framework, I would probably prefer it.
Curious to hear what your thoughts are about this.
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Sacha Barber
Sent: Wednesday, June 16, 2010 10:14 AM
To: wpf-di...@googlegroups.com
Subject: Re: [WPF Disciples] Commands vs Behaviors in MVVM
Yeah I would hate to see Commands go away, I see nothing wrong with them. In fact some of the newer blend behaviours give me the creeps, CallMethodAction springs to mind. I do not like that at all. I like Commands they are known quantites and for me are the clear way that the UI talks to the VM. I know where to look. If we allow Views to call methods directly, I for one would not like that. When I hunt through my code so see how the view is doing something, I am generally looking for commands.
Just to be clear: I am not interested in promoting another ME or behavior.
What Nikhil and I had been discussing is more along the line of having the
event calling a method in the DataContext directly, without additional
layer. Granted, the syntax would need to allow at least the same capability
than the Command/CommandParameter pair does now. Also, it would need to be
part of the core.
Anyway, thanks for explaining.
Cheers,
Laurent
-----Original Message-----
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
I’ve been bugging various teams about renaming so called Blend-behaviors to something else without the Blend part. That name leads people to think that “oh it’s something for designers”.
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of John Gossman
Sent: Wednesday, June 16, 2010 11:39 AM
To: wpf-di...@googlegroups.com
Subject: Re: [WPF Disciples] Commands vs Behaviors in MVVM
This is a deep subject and unfortunately a topic that is taking far too long to resolve (I've been looking at Commands and Behaviors since 2003, but coordinating VS, Blend, SL, WPF and BCL is extremely cumbersome even when consensus has been achieved). In a theoretical computer science sense, Behaviors are the most general (Commands, Templates and even Styles can be seen as special cases of an attached behavior). This does not make them the simplest thing though.
As far as ME syntax agree today it may be a bit more mysterious but if
it was to become the standard people would use it., IMO.
I doubt we would deprecate commands, but we would probably demphasize
them if we had such a feature. That is unless there was a big push
back.
On the Blend behaviors, we are working on a tighter syntax which Pete
Blois has been pushing for. Something that would be equivalent to ME
from a look and feel perspective.
On 6/16/10, Laurent Bugnion <lau...@galasoft.ch> wrote:
> Hey Sacha,
>
>
>
> Just wondering, why exactly?
>
>
>
> Let's say we could have the same CanExecute functionality as with commands
> now (I am not sure how, but for the argument, let's just imagine that). What
> would then be the difference between calling a method directly or calling it
> through a command?
>
>
>
> The way I see it:
>
> . Access from the view could be governed by an attribute setting
> [ViewBindingAllowed] or something
>
> . It would make testability better, because invoking commands from
>> http://www.idesign.net <http://www.idesign.net/>
1. Unless you find a way for the behavior to be reusable in multiple
places, it's inferior to commands. The only way I can see to do this
is to write a custom behavior every time, which isn't an improvement.
2. Unless the behavior fully encapsulates both execution and query,
it's inferior to commands. The only way I can see to do this makes
point (1) even more important, because if it's not a custom behavior
we've then got a point for human error.
3. Behaviors are not only UI concepts, but WPF/SL concepts. This
limits their reusability in comparison to commands, which are truly
generic abstractions usable anywhere.
4. Without commands you lose the ability to define common operations,
such as cut/copy/paste.
On Wed, Jun 16, 2010 at 5:17 AM, Laurent Bugnion <lau...@galasoft.ch> wrote:
> Hey Sacha,
>
>
>
> Just wondering, why exactly?
>
>
>
> Let’s say we could have the same CanExecute functionality as with commands
> now (I am not sure how, but for the argument, let’s just imagine that). What
> would then be the difference between calling a method directly or calling it
> through a command?
>
>
>
> The way I see it:
>
> · Access from the view could be governed by an attribute setting
> [ViewBindingAllowed] or something
>
> · It would make testability better, because invoking commands from
> unit tests is more cumbersome than calling a method directly
>
>
>
> I agree with you that the CallMethodAction is not a good way to solve that.
> IMO introducing reflection here is not a good idea. That said, if it was
> built in the .NET framework, I would probably prefer it.
>
>
>
> Curious to hear what your thoughts are about this.
>
>
>
> Cheers,
>
> Laurent
>
>
>
> From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com]
> On Behalf Of Sacha Barber
> Sent: Wednesday, June 16, 2010 10:14 AM
>
>
>
--
5. Behaviors must be supported by all the tooling (not just Blend). Until then, they will always be considered "part of Blend" and likely avoided by many developers afraid of Blend.
6. The Sys.Win.Interactivity.dll and basic, standardized behaviors must become part of the framework. As mentioned before, keeping them in the Blend SDK just reinforces the message that they are part of Blend.
7. They must be usable in all scenarios - today, AFAIK, it's not possible to associate a behavior inside a Style - so I can't place a behavior directly on a ListBoxItem for example. I haven't tried this recently (i.e. with Blend4 and WPF4), but I hadn't heard anything about it changing.
I'm not opposed to Behaviors replacing commands via either invoking methods directly, or even just providing the shim to execute defined commands from the UI, but I will say that there are places where the RoutedCommand infrastructure is nice - Cut/Copy/Paste is a good one - I just don't see how today's behaviors could replace that without completely.
mark
Why would you see having to author custom behaviors?
"It would be ideal if it was clear what methods should be called from
XAML and why, but I would rather do this from metadata than from
enforcing this by requiring folks to wrap Commands around the methods
they want to expose."
See, when we're just talking about calling methods, I agree with you.
But I don't see commands in that light. A command is much more than
simply a mechanism to call a method. Sometimes what you want to do is
just call a method. For instance, performing complex logic in response
to a selection change fits this bill. Other times, it's not simply
calling a method. Performing cut/copy/paste, opening/saving a file,
etc. are all richer concepts than just calling a method. These are
specific actions taken by a user in different contexts using different
methods of invocation, all of which need to behave the same no matter
the circumstances surrounding the invocation. Treating these as simple
method calls is NOT the way I would ever treat them. If WPF/SL moved
away from commands, I'd just have to roll my own... and quite frankly,
at that point I'd question the framework designers decisions and
whether or not WPF/SL were the platform for me.
--
Apart from that, you're pointing out a limitation of specific ICommand
implementations, and not with the larger concept. I'll point out
Cut/Copy/Paste which is reusable in the larger sense. The same command
is used, with different code being executed based on context.
On 6/16/10, Bill Kempf <wek...@gmail.com> wrote:
--
Category 1 - What you want to do is invoke a method and have that
enabled / disabled. For these more targetted scenarios something like
CallMethodAction 'could' work.
Category 2 - You want to use Contextually aware reusable / standard
bits of app functiomality like Copy/Paste etc. For these call method
won't work as there is no 'method' on the VM to call.
Is that right? Are there any others?
Glenn
On 6/16/10, Bill Kempf <wek...@gmail.com> wrote:
--
Hey Brian,
No no, I meant that if they choose not to use the EventToCommand behavior, but they only want to use the RelayCommand, Messenger and ViewModelBase, they can add a reference to GalaSoft.MvvmLight.dll only. If they want to use EventToCommand, they need additional reference to GalaSoft.MvvmLight.Extras.dll and Sys.Win.Interactivity.dll.
I didn’t want to force them to add a reference to Sys.Win.Interactivity.dll if they don’t explicitly use that feature. Mostly it is because of the bulk added to the download (Sys.Win.Interactivity is added to the XAP file). So I didn’t mean it is harder, just that some are hesitating to use behaviors because of the extra bulk.
Does that make more sense?
Now who is that really smart guy in Switzerland, I’d love to meet him…
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Brian Noyes
Sent: Tuesday, June 15, 2010 10:45 PM
OK, I’ll bite… J
Why is it harder to reference that assembly than referencing your extras one? I’m sure some of my customers might be more comfortable referencing something produced and supported by Microsoft rather than one from that really smart guy in Switzerland. J
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion
Sent: Tuesday, June 15, 2010 12:31 PM
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] Commands vs Behaviors in MVVM
Actually the dependency on Sys.Win.Interactivity.dll is also a good reason not to use a behavior. This is why EventToCommand is in the Extras DLL of MVVM Light, I didn’t want to force the user to add this extra DLL to their application.
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Brian Noyes
Sent: Tuesday, June 15, 2010 7:54 PM
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] Commands vs Behaviors in MVVM
Thanks for all the inputs. I personally have the same resistance to giving up commands for the same reasons everyone stated.
I agree that if you replaced a command with a behavior, that behavior would have to also support enable/disable and parameters. The one the team currently wrote and is using in the MVVM Quickstart supports the enable part as another method you can point to (optionally, like commands) on your view model. It currently does not support parameters, but not sure why it couldn’t.
I also have some resistance to making methods on my view model part of the exposed API instead of just properties.
Another point of resistance is that if you go with non-Blend behaviors, you limit Blendability, if you go with Blend behaviors, you have to take a dependency on the Blend SDK.
But the counter point that keeps coming back up is that it is not like you can often get away with no behaviors, so you will have to deal with all the negative aspects of behaviors already.
The real question we are trying to reconcile is “if you are already using behaviors for other things in your UI, what does using commands for those things you can buy you over using a behavior for those as well?” This does assume we are talking about a behavior that lets you point to an execute method, a can execute method, and carry bindable parameters.
The main argument I am hearing based on the above is easier syntax for commands. The counter to that would be better consistency, a single concept to master, and less glue (no delegate/relay command definition/hookup required).
Thanks
Brian
From: wpf-disciples@googlegroups..com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion
Sent: Tuesday, June 15, 2010 10:08 AM
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] Commands vs Behaviors in MVVM
Hey Brian,
The main reason why I still advise my users to use the Command property on a ButtonBase rather than using EventToCommand (or InvokeCommand or another behavior) everywhere: The XAML syntax for behaviors is annoying to write by hand. Granted, I don’t spend a lot of time adding behaviors manually (that’s what Blend is for), but I know that not all of them do. The Command syntax is more straightforward.
A compromise would be to offer an alternative syntax with an attached property. I hear that Blend 4 is better with attached properties than it used to be, but I didn’t have time to investigate. We would need to find a good syntax to define the event name and the binding to the command / commandparameter.
If syntax is not an issue, I can imagine that some users might be put off by having to add an external object just to execute a command. There might be a perception that the Command property is faster to execute than EventToCommand for example.
Nikhil Kothari has an interesting alternative in his Silverlight FX framework where he can bind an event directly to a method on the VM. This avoids having to declare commands. Eventually I think that this is what we should have in the .NET framework. Having to resort to the third party ICommand implementation (either RelayCommand/DelegateCommand/etc… or custom built command classes) is annoying and should go away. I mentioned this to Anders Helsjberg when we were both in Belgium this spring, and he was apparently aware of the issue. I hope we see that coming soon (I know that Nikhil is pushing that within MSFT).
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Brian Noyes
Sent: Tuesday, June 15, 2010 6:19 PM
To: wpf-di...@googlegroups.com
Are you saying here that the long term vision is to move away from Commanding, or remove Commanding altogether? IMHO this second option would be a bad move and would alienate a lot of people who have invested a lot of time and money in their code only to effectively have it obsoleted for no real reason that I can see. As long as the tooling supports both, then let people use both - I, for one, prefer Commands for click behaviour because they are discrete and easy to code and test in isolation, whereas MEs have required that little bit more infrastructure for testing.Part of the reason here is that people do seem to like POCO, but MEs can put people off because they require a deeper understanding of the subsystem (not true, I know, but it is a perception).
On 16 June 2010 01:31, Glenn Block <glenn...@gmail.com> wrote:
On the platform side there have been lots of disciussions on moving
away from it long term. One of the reasons we were pushing for markup
extensions in SL was to support a custom ME for calling methods.
--
Sent from my mobile device
--
Peter O'Hanlon
Why?
The command is reusable. Even if currently the button is the only way
a user can invoke the operation, tomorrow the design might change and
we'll add a context menu, a keybinding, a mouse gesture and a double
click operation on a list. CallMethodAction isn't going to enable this
sort of reuse, when you take into consideration the execute/query
duality of a command.
There's plenty of need for a CallMethodAction, but I don't believe I'd
ever recommend using it instead of a command for any event associated
with the Command property of a control.
--
On 6/16/10, Bill Kempf <wek...@gmail.com> wrote:
I'm not dogmatic. If you use a CallMethodAction for a button click,
I'm not going to tell you that you're doing it wrong. However, any
guidance that suggested, or even just hinted, that CallMethodAction is
preferred in this scenario I'd have to strongly disagree with. From an
architectural point of view, this advice is wrong, IMHO.
This is similar to some of the Twitter discussions I have been having with Nikhil and Jeff Handley about the public EntityCollectionView which is being planned for the next version of WCF RIA Services. Nikhil doesn't like ICollectionView/IEditableCollectionView and feels that he wants the view to add or remove entities from the collection that he would call methods on the ViewModel to do that instead of flowing that back to the ViewModel through the IEditableCollectionView interface.
Personally, I don't like the idea of the View having to guess if the contents of the collection is editable or not through some other method and metadata, which is really only useful to tooling like Blend, isn't a valid substitute to me. I like my ViewModel to be discoverable at the code level, not just at the Blend level.
I have been thinking that the EntityCollectionView is something that should be discussed here at some point since it will eventually impact both Silverlight and WPF. The design I have been pushing on Jeff is something similar to a standard ICollectionView but instead of using Filter, SortDescriptions, and GroupDescriptions it would be LINQ based. In particular, it should support EntityQuery as well as standard queries so that the same LINQ query can be used for both loading data and filtering/sorting the EntityCollectionView. Adding. Removing. and Paging abilities would be provided by callbacks passed into the ECV through the constructor.
I think the design philosophy of the ECV is very similar to the design philosophy of the ICommand so I can see where Bill is coming from.
Bill Kempf <wek...@gmail.com> Sent by: wpf-di...@googlegroups.com 06/16/2010 10:05 AM AST Please respond to wpf-di...@googlegroups.com |
To |
wpf-di...@googlegroups.com |
cc |
||
bcc |
||
Subject |
Re: [WPF Disciples] Commands vs Behaviors in MVVM |
public MyViewModel(ICommandTarget commandTarget)
{
commandTarget.CommandBindings.Add(new
CommandBinding(MyCommands.Foo, FooExecuted));
}
Onyx supplies me with the ICommandTarget service associated with the
View, and I add bindings to it in the ViewModel's constructor.
It looks like Bills approach addresses some of those by using a proxy that
goes into the command binding that can still dispatch to a ViewModel and
get the handling out of the view, which addresses the first issue. Not
clear to me whether it handles the SubmitAll scenario.
I talk about the complexities of the focus stuff in my MSDN Magazine
article on Routed events and commands some, and more so in this follow on
post:
http://briannoyes.net/2008/11/09/TheTruthAboutRoutedCommandsRouting.aspx
Because of all these, we went with the DelegateCommand and
CompositeCommand approach and got the command handling out of the visual
tree and into our code's hands as quickly as possible. There are
definitely still scenarios where RoutedCommands still make sense,
Cut/Copy/Paste being the poster child. Things that are inherently coupled
to the view and the focus are fine to implement as custom RoutedCommands
for example. But otherwise I have shied away from them.
Basically, my solution is the same solution provided by WPF. It has
the same benefits and the same drawbacks. I will note, however, that
one people were complaining about how the first implementations of the
RibbonCommand wasn't "MVVM Friendly", using my approach I had no
issues with the design. ;)
Sounds like someone is missing work… ;)
RoutedCommands may be something different. I've never run into the
issues that I know exist with them, so it's hard to convince me to be
anti-RoutedCommand. However, given problems do exist, I'm not
campaigning to have them added to SL.
As for the rest... well, all I can say is that it makes me glad I'm a
WPF developer and not a Silverlight developer. Sorry, but I honestly
think you're making the wrong decisions.
As an experiment yesterday, I showed the other SL programmers where I work the CallMethodAction behavior. None of us use Blend very much so nobody had really looked at it before. Everyone had the same reaction: CallMethodAction doesn't "smell" right to them and they would much rather continue to use commands.
One question I was asked, if the P&P team is worried about best practices and what is the best way of doing things, why are they considering using an inferior way of doing things just because it is easier to explain to somebody. The point about multiple concepts and patterns also wasn't understood, the Command pattern feels like any other binding to us, it is the behavior that seems like a different way of doing things.
To |
wpf-di...@googlegroups.com |
cc |
|
bcc |
Subject |
[WPF Disciples] Commands vs Behaviors in MVVM |
John was just talking about RoutedCommands, not commands in general. Commands are in Silverlight because he fought for them in the first place.
I am not a fan of RoutedCommands myself. While they can be great to use for an experienced developers, I saw them make WPF beginners (for some developers with years of experience in other technologies) totally confused. Great that they are there, but while I mentioned them the last time I taught WPF to a group of beginners, I didn’t go deep into them. I spent more time showing normal commands and of course RelayCommand.
Cheers,
Laurent
I prefer RoutedCommand over Delegate/RelayCommand not because it's
Routed, but because of CommandBindings. That, and the fact that the
built-in set of commands are RoutedCommands.
That said, yes, "I is strange one wierd one", and I have no idea how I
got on the list ;).
OK, I’ll bite… J
Why is it harder to reference that assembly than referencing your extras one? I’m sure some of my customers might be more comfortable referencing something produced and supported by Microsoft rather than one from that really smart guy in Switzerland. J
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion
Sent: Tuesday, June 15, 2010 12:31 PM
Actually the dependency on Sys.Win.Interactivity.dll is also a good reason not to use a behavior. This is why EventToCommand is in the Extras DLL of MVVM Light, I didn’t want to force the user to add this extra DLL to their application.
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Brian Noyes
Sent: Tuesday, June 15, 2010 7:54 PM
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] Commands vs Behaviors in MVVM
Thanks for all the inputs. I personally have the same resistance to giving up commands for the same reasons everyone stated.
I agree that if you replaced a command with a behavior, that behavior would have to also support enable/disable and parameters. The one the team currently wrote and is using in the MVVM Quickstart supports the enable part as another method you can point to (optionally, like commands) on your view model. It currently does not support parameters, but not sure why it couldn’t.
I also have some resistance to making methods on my view model part of the exposed API instead of just properties.
Another point of resistance is that if you go with non-Blend behaviors, you limit Blendability, if you go with Blend behaviors, you have to take a dependency on the Blend SDK.
But the counter point that keeps coming back up is that it is not like you can often get away with no behaviors, so you will have to deal with all the negative aspects of behaviors already.
The real question we are trying to reconcile is “if you are already using behaviors for other things in your UI, what does using commands for those things you can buy you over using a behavior for those as well?” This does assume we are talking about a behavior that lets you point to an execute method, a can execute method, and carry bindable parameters.
The main argument I am hearing based on the above is easier syntax for commands. The counter to that would be better consistency, a single concept to master, and less glue (no delegate/relay command definition/hookup required).
Thanks
Brian
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Laurent Bugnion
Sent: Tuesday, June 15, 2010 10:08 AM
To: wpf-di...@googlegroups.com
Subject: RE: [WPF Disciples] Commands vs Behaviors in MVVM
Hey Brian,
The main reason why I still advise my users to use the Command property on a ButtonBase rather than using EventToCommand (or InvokeCommand or another behavior) everywhere: The XAML syntax for behaviors is annoying to write by hand. Granted, I don’t spend a lot of time adding behaviors manually (that’s what Blend is for), but I know that not all of them do. The Command syntax is more straightforward.
A compromise would be to offer an alternative syntax with an attached property. I hear that Blend 4 is better with attached properties than it used to be, but I didn’t have time to investigate. We would need to find a good syntax to define the event name and the binding to the command / commandparameter.
If syntax is not an issue, I can imagine that some users might be put off by having to add an external object just to execute a command. There might be a perception that the Command property is faster to execute than EventToCommand for example.
Nikhil Kothari has an interesting alternative in his Silverlight FX framework where he can bind an event directly to a method on the VM. This avoids having to declare commands. Eventually I think that this is what we should have in the .NET framework. Having to resort to the third party ICommand implementation (either RelayCommand/DelegateCommand/etc… or custom built command classes) is annoying and should go away. I mentioned this to Anders Helsjberg when we were both in Belgium this spring, and he was apparently aware of the issue. I hope we see that coming soon (I know that Nikhil is pushing that within MSFT).
Cheers,
Laurent
From: wpf-di...@googlegroups.com [mailto:wpf-di...@googlegroups.com] On Behalf Of Brian Noyes
Sent: Tuesday, June 15, 2010 6:19 PM
To: wpf-di...@googlegroups.com
Subject: [WPF Disciples] Commands vs Behaviors in MVVM
Hi all,
I think some of you know I worked closely with the p&p team on Prism 1, remained an advisor for Prism 2, and am now working closely with the team again as a contractor for Prism 4 (aligning versions like everyone else). I know many of you have competing frameworks and don’t want to get into a debate of what is wrong with Prism and how your frameworks are better. I’ve studied many of those other frameworks and see the value in the way they do things.
One thing we are discussing is whether to use commands at all or to just use behaviors everywhere. Even though they finally added commands in SL 4, the fact is that they still only cover a small subset of the communications you need between a view and a view model, so you usually have to rely on behaviors for the everything other than button/hyperlink/menu clicks. So it raises the question:
If you can also easily handle those cases with behaviors, why bother with commands? They require you to understand and mix two concepts and coding patterns, whereas behaviors can pretty much address any scenario, and consistency is important as well.
So we are leaning towards just using behaviors everywhere that we used to use delegate commands.
Thoughts on this? Vigorous defense of commands over behaviors in the places where they are an option?
Thanks
Brian
-----------------------------------------
Brian Noyes
Chief Architect, IDesign Inc
Microsoft Regional Director / MVP
http://www.idesign.net
+1 703-447-3712
-----------------------------------------