开发者

Using the MVP pattern with MonoDroid

Here's my attempt at using MVP (or Passive View more specifically?) with a MonoDroid app:

https://gist.github.com/857356

My main goal isn't reuse, but rather increased testability and (hopefully) improved maintainability. Reuse would be a nice side-effect, but I currently don't plan to do a MonoTouch version or anything.

I'm generally pretty happy with it (with one except开发者_高级运维ion), but I definitely need some critique/evaluation.

The one exception: the method "GetPortfolioIdForContextMenu" returns an 'int', which doesn't feel quite right, given the rest of the methods don't have to do that. It just feels wrong, but I can't quite put my finger on 'why', or what to even do about it.


Have you considered the MVVM pattern? I use it with WPF development, although I haven't used it with my Android projects yet. I love the pattern, it's very similar to MVP, except that the ViewModel does not know that the View exists. MVVM is typically only possible with languages that support databinding.

Some useful links:

  1. Wikipedia MVVM
  2. You'll need the Android Binding project the motivation behind it. [main benefit: testing]
  3. Tutorial on MVVM with Android.

Hope some of this helps.

-JP

Edit: Just realized that you're using MonoDroid. This may not be as useful. Maybe it'll be useful to other internet searchers though.


onCreateContextMenu contains the View for which context menu is to be created. You can set the portfolio id as tag for each view and then use it in the onCreateContextMenu. HTH !


I took a look. I don't see how you could restructure it to remove this small "wrongness", unless you remove the GetPortfolioIdForContextMenu method from the MainActivity class and work with the value directly (since you have access to _presenter in the main class and you use GetPortfolioIdForContextMenu in one place only). The presenter, in this case, is still keeping its role (retrieve, persist, format).

Sometimes, in a "real" project, you have to break the rules. In this case, I believe that even with this little bit of inconsistence, you kept the spirit of MVP intact.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜