Currently, I'm implementing MVVM pattern for a WPF application but not 100% no code behind, sometimes I put some business logic code into GUI and so what are potential problems for this? Thanks
1 Answer
The whole point of MVVM is to abstract as much logic as possible away from the View, the surface that the user sees. If you're using CodeBehind to implement business logic, you're probably not practicing MVVM.
This has several benefits:
- It makes the code more testable. UI is difficult to test, especially UI that has CodeBehind.
- If the business logic is somewhere else besides the UI, you can swap out the UI for another UI (such as a mobile device or web page), and the business logic will still work.
Of course, if this is a very small application and you never intend to talk to a database on a server (a rarity these days), you may not need all this ceremony; CodeBehind might be perfectly adequate.
-
1CodeBehind is ok until it contains buisness logic. View's internal logic can be in the CodeBehind without breaking MVVM pattern– FabioOct 23, 2015 at 12:50