开发者

Should a view model contain logic?

In a wpf application, Whats the responsibility of the viewmodel? can he manage everything or onl开发者_如何学编程y represent the view and send messages/event to the business layer and get information from it?


Short answer, Yes.

Longer answer ...

The main aims of the Model-View-ViewModel (MVVM) pattern are:

  1. Permit unit testing of your view logic. These are unit tests applied to the ViewModel layer which is executed without a View associated with it.
  2. Facilitate developer-designer workflow by minimising the amount of code-behind associated with your XAML files.

The MVVM pattern also provides separation of concerns between view logic and business logic in the same way that the MVC and their UI pattern do. However, the 2 points above are what really define the MVVM pattern.

Now, thinking about where you locate your business logic. If you place it in your ViewModel, are #1 & #2 above still valid? Yes. If you place it in a separate layer, are #1 and #2 still valid? Yes.

Therefore, in both cases you are still achieving the two main goals of MVVM. Which route you take really depends on the complexity of your application, and the number of developers working on it. As both these factors increase, you will benefit from having 3 layers ... or more!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜