可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm currently implementing a Monotouch application that will eventually be ported to Monodroid. The application is just a client to an OData Web service. Nothing too fancy or performance critical.
The challenge is to reuse as much code as possible. I'm aware that the UI APIs for Monotouch and Monodroid are quite different, but I'm hoping to to reuse the data data abstraction and business layers.
Since my UI layer follows the MVP pattern, I also hope to reuse UI controllers by coding an abstract representation of each view. However, I can only guess if this will work since I am not yet allowed to the Monodroid beta.
Now my questions:
What do you think about this approach? Is this a good idea, or will it just lead to a mediocre application because of differences in the UI concept between IPhone and Android?
Can you offer any hints on how to structure the application to maximize code re-use?
Thanks,
Adrian
回答1:
What do you think about this approach? Is this a good idea, or will it just lead to a mediocre application because of differences in the UI concept between IPhone and Android?
I would say the latter, but you can definitely re-use a large portion of your business and domain objects. The same Mono Sqlite is used in Monodroid, so the data persistence part of your app (if it uses that) is re-usable.
I wouldn't bother creating a middle layer UI - the two are completely different. For example on Android apps you have the bottom menu, which can contain 6 buttons on screen. On the iPhone you are more than likely not to have 6 buttons in a tab bar or toolbar. To make a common pattern for that won't help you much.
Another example is ListViews (UITableViews). They're completely different. As you'd expect the Monodroid implementation is faithful to its ugly Java sister. On Android you don't have to use the huge tangle of indirection that Apple force upon you, but just a simple ArrayAdapter as the data source - subclassed for more complicated layouts.
Another important thing to note is Android doesn't have one screen size. You create images for 3 different screen densities. Font sizes aren't absolute.
Android provides you with layout mechanisms similar to XAML and the web, on the iPhone you're not so lucky (or more fortunate, depending how you view it) as everything is generally absolutely positioned (they can do this as it's always 320x480).
Can you offer any hints on how to structure the application to maximize code re-use?
I think you've got most of covered with separate layers for the data and sticking to controllers. Without seeing your app it's hard to say how easy it will be re-use the controllers (whether you use UITableViews or custom uis), but Android is so much faster to develop with I think it should be a quick task.
(I'm on the Monodroid preview and also have a MT apps out)
回答2:
It sounds like you have a pretty solid concept there. In fact there is an open source project called MonoCross (http://code.google.com/p/monocross/) uses the MVC pattern to do something similar.
Miguel de Icaza has forked what looks like a pretty cool MVVM project that might assist you as well. https://github.com/migueldeicaza/MonoTouch.MVVM
回答3:
I have successfully implemented the MVC pattern for my Presentation layer, the Domain Model, Services Layer, Repository, and Common are all platform agnostic. Where I do need to have platform specific code such as the NetworkConnectionManager (my name) I use #if #endif to wrap the objects or where I need to do Unit Testing I use a Console app for all my unit tests, its the exact same project as the Android, iPhone and Windows Phone projects except I leave out the UI layer which is all the User Interface platform specific crud. I also tag my Console with the CONSOLE define, and my Android projects with ANDROID define so that I can do the #if #endif
I have to say it works great, if I can put my entire MVC layer under unit test in the console and have it working under Android than I am pretty much gauranteed it will work under iPhone and Windows Phone because Console doesn't even have an interface. It's the perfect way to test the Genericness of my Presentation layer. Albeit this approach I am taking may be overkill, I plan to support this application for a long time, and I also plan to port it to Android Tablets, iPad and Windows 8 framework so IMO its necessary to take the extra time to get this right.
I attempted the MVP pattern but it wasn't quite flexible enough under this circumstance to work. I tried various frameworks as well but I have ended up custom developing the entire framework as it gives me the most flexability. It is not trivial by any means, and if you do not have a very thorough grasp on abstraction, generics, and object oriented design than I suggest a simpler approach or you will make your life hell trying to get it to work.
As previously stated, Android has a lot of ins-and-outs, for instance the biggest problem I have run into with Android is multi-threading or async operations and Activity rotation, which will completely destory your activity and recreate it thus obliterating your View along with it. I chose the path to manage all the rotation configuration on my own which means I must clean up all drawables, and resources used by the activity manually.
回答4:
I tried doing something similar - in my case because I wanted to use VS and all my Windows development tools for as much code as possible.
However, I reverted to just having the model layer being "generic" - and doing the UI layer (controllers and views) on the Mac (i.e. in MonoDevelop). The effort involved was overkill for the relatively small application I was working on - and only I was the only one working on it.
Also, if you are new to the iPhone and/or Android, trying to do something relatively sophisticated will make it more difficult to find samples or get answers to questions. I found I was making life harder for myself (certainly in the early days of my iPhone work).
Of course, without knowing the ins and outs of your project(s) and business model, it's difficult to give any concrete advice, but those were my experiences - for what they're worth.
回答5:
May be a bit late, but this cross-platform mobile development video is quite helpful. I'm also writing an MT app which will be ported to Android and WP. For data storage I'm seriously considering Vici CoolStorage which is supposed to make data model completely re-usable. Additionally, I'm moving any platform-agnostic code to Utilities and Common projects inside the solution. I also hope to be able to re-use web service communication code in MD and WP. The rest is iOS specific at the moment.
It will be very interesting to know how your project is going. Is code re-usability really paying off?