Wednesday, February 13, 2013

A second take on Constructor Injection IoC in MvvmCross - and a Badge of Awesomeness

Received this today - fab contribution from @Asudbury

By changing some of the OpenNetCf IoC files (by removing some of the changes I made!), Adrian's managed to get 'proper' IoC working - i.e. he's using code that has constructors like:

        ///

        /// Initializes a new instance of the class.
        ///

        /// The service.
        /// The translator.
        [MvxOpenNetCfInjectionAttribute]
        public LoginModel(
            IAuthenticationService service,
            ITranslator translator)
        {
            this.service = service;
            this.translator = translator;
        }

This isn't the only way to do this sort of thing - I know that someone else has also got TinyIoC working really cleanly - using some techniques built around http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html - hopefully will share that with you soon too.

The code changes from Adrian are in the attached gist....
But in the meantime... Adrian - thanks - a badge of awesomeness is your's :)





3 comments:

  1. Are you going to merge this into MvvmCross so it works like this by default?

    ReplyDelete
  2. The code will get merged back in, yes.

    Currently undecided on where ioc should go in v3. A bit of me likes the idea of switching constructor and/or property injection, but also I toyed with the idea of moving the current Get and Register extension methods to Object.

    Interested to hear opinions. 100% sure that there isn't one right answer.

    ReplyDelete
  3. I prefer construction injection over property injection.

    Maybe that's because in the main that's what I always use - its like asking a Java programmer 'do you prefer Java or C#?'

    To me if a class really needs something to get its job done it should be passed on the constructor - its much clearer - its saying to use me I want these things passing to me when you construct me.

    I once worked on someone else's code that used property injection and totally missed that it was done this way and wasted hours on a problem just because to me it was unclear the way in which the class should be constructed/used.

    maybe the last paragraph says more about me instead of coding styles :-(

    asudbury

    ReplyDelete