开发者

About view models and interfaces

I'm developing a WPF Prism ap开发者_StackOverflowplication, and everything is working fine. My view models all have interfaces, which are injected by MEF.

However, I don't really understand the benefit of interfaces for view models. After all, a view is tied to its view model, so I think there will never be other implementations.

Actually, I also have interfaces for my views. It seems that this is also overkill?

So my question is: can't I just remove all view and view model interfaces and inject the views and view models directly? Is there any reason to keep interfaces for views and view models?

Thx, L


It is an overkill. I understand that you may want to mock your ViewModels, but I think it's more important to be practical. Plus, why would you even need to mock your ViewModels? Any logic that needs to be mocked should be put into a service class IMHO.


Interfacing your VieWModels gives you the benefit of mocking them in a test, interfacing your Views looks like some overkill indeed. You won't interchange your views and UI testing can be done on mocks of your ViewModel so you won't really need to interface them I think.


The biggest reason I can think of for interfaces for ViewModels would be that you can write mocks that implement those interfaces for use while unit testing. Since one ViewModel may talk to another, it enables you to stub out the second ViewModels behaviour when testing the first.

The MVVM pattern makes it easier to unit test the classes as it separates data and control from the UI layer (which is harder to write unit tests for). Personally, I don't write interfaces for my views though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜