Sunday, October 14, 2012

Changes to IoC in vNext

Just wanted to post a quick warning about a change to IoC in vNext.

In all previous code, the simple IoC system worked using generic interfaces and generic extension methods.

So, for example, to import a service like IMyService, then the user would write code like:
    public class TwitterViewModel          : MvxViewModel          , IMvxServiceConsumer<ITwitterSearchProvider>
      {
          private ITwitterSearchProvider TwitterSearchProvider
          {
              get { return this.GetService<ITwitterSearchProvider>(); }
          }
      }  
This has some duplication of the generic target - and it can make class declarations quite long winded... To make the code 'cleaner', in vNext, I've removed the generic part from the interface - so now code like this will work:

    public class TwitterViewModel          : MvxViewModel
          , IMvxServiceConsumer
      {
          private ITwitterSearchProvider TwitterSearchProvider
          {
              get { return this.GetService<ITwitterSearchProvider>(); }
          }
      }  

2 comments:

  1. Presumably this means that you now only need to use one ",IMvxServiceConsumer" to consume as many services as you like within the class? Which is great :)

    ReplyDelete
  2. Yes - and because MvxViewModel inherits from IMvxServiceConsumer, then it means that all ViewModels can automatically use the GetService extension methods :)

    This does kind of mean that we lose some declarative meaning from our classes... but it does make the IoC quicker and easier to use... so I think it's a WIN :)

    ReplyDelete