Cross Platform Dependency Injection

JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
edited September 2012 in Cross Platform with Xamarin

I'm not sure I can build an application these days without using dependency injection. It is just so much better to use interfaces for cross platform apps.

Any suggestions for the best dependency injection/IoC container for cross-platform Xamarin products?

Ninject compiles for MonoTouch/Mono for Android: http://www.ninject.org/
So does TinyIoC: https://github.com/grumpydev/TinyIoC

Is there a "simpler" or "better" one?

Best Answer

  • BryanCostanichBryanCostanich USMember, Xamarin Team Xamurai
    Accepted Answer

    I think that TinyIoC is the simplest one out there.

    You can also use the Provider pattern as an alternative to dependency injection.

Answers

  • BryanCostanichBryanCostanich USMember, Xamarin Team Xamurai
    Accepted Answer

    I think that TinyIoC is the simplest one out there.

    You can also use the Provider pattern as an alternative to dependency injection.

  • WMeintsWMeints NLMember, Insider

    I don't know of a better one, but tinyIoC seems to work perfectly fine. And smaller is better on mobile devices, so the smaller you can get without losing to much functionality, the better it is.

  • StuartLodgeStuartLodge USBeta ✭✭✭

    I've used TinyIoC and OpenNetCf IoC. For my latest code I'm using a simple singleton I setup myself - but it is very simple...

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
    edited October 2012

    Sounds like TinyIoC is a winner.

    My concern with TinyIoC is the use of reflection, and it seems a bit slow with lots of dependencies when using constructor injection.

    It makes me want to create a simple "XNA-style" ServiceContainer, basically a static class with a Dictionary<Type, object> to store dependencies in for an entire app. I would probably make some helper classes like a "messenger" (an abstraction of NSNotification or equivalent on each platform) to go along with it.

    If I make one, I'll put it on Github.

  • BryanCostanichBryanCostanich USMember, Xamarin Team Xamurai

    That would be awesome.

    I've been told by @Clancey that there are some issues with TinyIOC's reflection stuff on the device, so i think this would be a huge win.

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
    edited October 2012

    Here is what I cranked out really quick: https://github.com/jonathanpeppers/XPlatUtils

    Right now it's MonoTouch projects only, but you should be able use the *.cs files in any PCL or MonoTouch/Mono for Android project. I'll clean up and add tests down the road.

    We're actually using this exact ServiceContainer class in a few apps right now. Simple and works great.

  • StuartLodgeStuartLodge USBeta ✭✭✭

    Interesting. I might switch over to that - thanks!

    I'm also interested in:

     // TODO: measure performance impact of weak references and implement
    

    As I think it's probably needed - the way most people seem to subscribe-and-forget on the MessengerHub

    Thanks again

    Stuart

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭

    I'll probably take a stab at using weak references pretty soon.

    Are there other spots that would be helpful in this library? One I was think of a helper class to throw stuff on the UI thread in a cross-platform manner.

  • StuartLodgeStuartLodge USBeta ✭✭✭
    edited October 2012

    If it helps, Blewzman made this WeakEventManager - https://github.com/Blewzman/MvvmCross/tree/b4030a7fae01eba8bdd278d9ba59f60e463e260a/Library/WeakEventManager - which I haven't yet integrated into Mvx Master (will do soon...)

    I think I'd try to keep this library small - smallness is why TinyIoC is useful (IMHO).

    If people need other components (like a UI thread helper) then those are easy to add from other libraries - especially once you have IoC :) - that's the way I've headed with my Plugins anyway.

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
    edited October 2012

    Hmm, don't like the use of reflection in that class.

    I wonder if I just held a weak reference to an Action<T> if that is enough for the GC to be able to collect everything as needed. I will probably test this with HeapShot.

    BTW - I've cleaned up a few things, and added tests. So weak refs are next.

  • StuartLodgeStuartLodge USBeta ✭✭✭

    I think the Action would work for the simpler Messenger case - that WeakEventManager sample was for weak INotifyPropertyChanged event binding - so the event reflection was needed there.

    On a messenger, I guess you also need to make it clear that your subscription is a weak subscription - it's not what people normally expect :)

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
    edited October 2012

    Got WeakReferences in there, wasn't too bad.

    Tested with HeapShot, everything seems to collect just fine--so definitely a good change.

    So only thing left is for me to write some docs/examples now. The PCL also doesn't build on Windows until you drop the target for Win Phone 7, but doing that breaks the build in MonoDevelop. So I could replace Lazy<T> with my own, or figure out an MSBuild variable to swap out:

    `- Profile1

    • Profile36`
  • StuartLodgeStuartLodge USBeta ✭✭✭
    edited October 2012

    JonP - I don't suppose you could post what your MonoDevelop/MonoDroid setup is, could you?

    I've got a clean install of everything on a new Mac, and PCLs just aren't building for me - including your code (more on this thread) - just wondering which (if any) bits I have to hack/change to get it building.

    And if you do figure out an easy-to-use MSBuild trick for that profile swapping, I'd love to know - although hopefully MonoDevelop will support other profiles too soon.

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭

    Yeah, I don't know. I just made a new Mono for Android project and referenced XPlatUtils and it was able to build just fine.

    I have Mono for Android 4.2.8 on a Mac. If I look at my SDK manager for Android, I have all the SDK platforms installed from 2.2 up to 4.1.

    Have you tried making a brand new project?

  • StuartLodgeStuartLodge USBeta ✭✭✭

    Thanks

    I'm on MonoTouch only on this Mac right now. Think I've exhausted my Droid activations - I'll need to go back and remove it from the old machines.

    I've tried brand new projects and they don't seem to work.

  • StuartLodgeStuartLodge USBeta ✭✭✭
    edited October 2012

    This is what I see when I try to build the XPlat project

    fail

    I'm really keen on PCLs... but it feels like I'm fighting the Mono tools still at this point :(

    If I don't get any feedback, then I'll try some of the old hacks again - see if editing the targets XML can persuade PCLs back into life.

  • StuartLodgeStuartLodge USBeta ✭✭✭

    Yeah .... it looks like the hack on http://stackoverflow.com/questions/12041290/monodevelop-is-it-possible-to-switch-pcls-compiler/12062589#12062589 is still needed :/

    But maybe it depends on which Mono platform is installed?

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭

    Oh, mine might be working because I have a prerelease version of Mono installed.

    I bet it will be fixed in the next couple months. Xamarin is trying get PCL support better.

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭
    edited October 2012

    I got the MSBuild thing working:

    <TargetFrameworkProfile Condition="'$(OS)' != 'Windows_NT'">Profile1</TargetFrameworkProfile> <TargetFrameworkProfile Condition="'$(OS)' == 'Windows_NT'">Profile36</TargetFrameworkProfile>

    Added a decent readme as well.

    Only thing I'm not quite happy with is the fact you can unregister lambdas on the messenger, you have to do this:

    messenger.Unregister<MyMessage>(MyMethod);

    This is why TinyIoC returned a token you have to store in a variable and use to unregister. I'll have to think on it, maybe there is a better way.

  • StuartLodgeStuartLodge USBeta ✭✭✭

    I bet it will be fixed in the next couple months.

    I bet that about 6 months ago :/

    But I think now that VS2012 and mono3 are GA, then it will move forward much quicker :)

    And the arrival of mono3 and is great too - can't wait to try to get portable async/await integrated :) :) :)


    Love your condition thing on the MSBuild project files - will give it a go!

  • JonathanPeppersJonathanPeppers USInsider, Beta, University ✭✭

    I just put Apache 2.0 in the readme.

  • LeelaPrasannaKumarLeelaPrasannaKumar USUniversity

    Hi Jonathan,

    Can you check on query on XPlat and IOC in this thread .
    http://forums.xamarin.com/discussion/8906/tinyioc-and-xplat-ioc-containers#latest

    You feedback would help

Sign In or Register to comment.