Ian Vink
Ian Vink

Reputation: 68740

MonoTouch+MonoDroid: Common business objects with Images

I am building a project that has both Android and iOS clients with MonoTouch and MonoDroid. A common business layer project contains common domain entities.

One of them has a need to store an Image, Name and Age. The image is not downloaded.

Is it possible to create a common object that holds the actual data for a PNG or JPG but that is somehow workable in both iOS and Android?

///Simplified customer
class Customer {
   public Image???;
   public string Name;
   public Int Age;
}

Upvotes: 1

Views: 380

Answers (2)

Stuart
Stuart

Reputation: 66882

I do this type of sharing by using interfaces or abstract base classes with the concrete implementations injected at runtime.

For example, I might put in place some interfaces like:

 public interface IImage
 {
      int Height { get; }
      int Width { get; }
 }

 public interface IImageTools
 {
      IImage Load(string assetPath);
      void SaveToJPEG(IImage image, string savePath, int quality);
      IImage DuplicateAndResize(IImage original, int newHeight, int newWidth);
 } 

My business model would then have some way to get a reference to the IImageTools - e.g. it might be injected in the constructor or it might be available from some Container (e.g. an IoC singleton)

This then allows me to inject separate native implementations at runtime for Droid, Touch, WP7, WinRT or NUnit.

In the UI projects, when you actually need to show the image on the UI, then I typically would cast the IImage to a native implementation e.g. TouchImage - which would then contain a method to get hold of the underlying UIImage


e.g. Not quite your image requirement, but here's what I do for images for Camera Capture operations: https://github.com/slodge/MvvmCross/tree/vnext/Cirrious/Plugins/PictureChooser

Upvotes: 1

moljac
moljac

Reputation: 56

Options:

  1. represent Image as byte[] or MemoryStream/Stream
  2. stay with native platform specific code requires partial classes and linking common class in Customer.cs - linked through projects I call this approach split-n-link or link-n-split

    public partial class Customer { public string Name; public Int Age; }

delta class for monotouch in monotouch project

public partial class Customer {
    public UIImage;
}

delta class for mono for android in m4a project

public partial class Customer {
    public ImageView;
}

So, each project will contain Customer.cs One original/source and other link to it, which one depends on preferences or common-feature-denominator if dealing with Windows Phone (currently it has smallest set of features - better it is more restrictive). Besides main/original/source Customer.cs file, there are 2 (3 if WP) deltas with platform specific stuff. Remember partial classes are "additive" meaning You can add Attributes in different cs file and they apply, so in this delta class one can add platform specific Attributes and enable stuff like binding for iOS ad/or Android if needed. From Holisticware experience (Visual Studio centric 80%+, 20% or less on Mac) source project is Mono for Android, but it could be reverse...

With link-n-spilt one ends up basically with POCO + delta and this POCO can be used for targeting desktop platform (WF, WPF) where everything is easier: debugging, unit testing, etc.

And there is advantage that each project is compiled/build with configuration for that platform - so ig guys from Xamarin come up with some other bright idea for builds like size optimizations this should not affect projects - recompile and go!

HTH mel

Upvotes: 4

Related Questions