1

I'm currently working on a WP 8.1 app(runtime, not silverlight) and I want to find a IoC container. I'm using Caliburn.Micro, which is great because it's using constructor injection in the view models, but I also have a background agent which can't accces the IoC container from Caliburn.Micro.

In my app, I can do something like this:

...
public MyViewModel(ISyncService syncService, IOtherService otherService)
{
    _syncService = syncService;//and so on for all services
}

Or something like this:

var syncService = IoC.Get<ISyncService>();

In my agent, I can't do the same thing, because Caliburn is of no use there, so I'm forced in doing something like this:

var syncService = new SyncService(new AppSettings(), new AuthenticationService(webClient),
            new PromotionalMessagesService(webClient)...));

I think you get the point. The ISyncService has 7 services required in the constructor, and each service has more in their ctor. The other downside besides ugly code, is that if I change something in the ctor of a service, I have to update it here everytime.

Do you know any good IoC container for WP 8.1 or maybe you have a better idea for this issue?

bogdanbujdea
  • 594
  • 8
  • 22

3 Answers3

1

I use SimpleIoc from the MvvmLight Toolkit.

A good How-To can be found here: how to use MVVMLight SimpleIoc?

Community
  • 1
  • 1
Florian Moser
  • 2,583
  • 1
  • 30
  • 40
1

If you look at "Supported .NET versions" table of this page, you'll see that the following DI libraries support WP8.1:

  • Autofac
  • Catel
  • DryOic
  • Endjin Composition
  • Grace
  • HaveBox
  • IfInjector
  • LightInject
  • Mugen
  • Munq
  • Ninject
  • Simple Injector
  • TinyIoc
  • Unity
Steven
  • 166,672
  • 24
  • 332
  • 435
0

I don't think this is a fault of an IoC container. If a Background Agent is invoked separately to the main app then you may have to configure the required dependencies for that process. In the case of autofac you could reuse modules to register the required dependencies then build the container. These are just rough hand-coded snippets:

var builder = new ContainerBuilder();
builder.RegisterModule<ServicesModule>();
backgroundTaskContainer = builder.Build();

Within the OnInvoke method you might then need to just use an instance scope (http://docs.autofac.org/en/latest/lifetime/instance-scope.html) to isolate the call invocation, or just dispose the container itself when the task is disposed.

protected override void OnInvoke(ScheduledTask task)
{
  using(var scope = backgroundTaskContainer.BeginLifetimeScope())
  {
   var sync = scope.Resolve<SyncService>();
  }
}
Brendan Kowitz
  • 1,795
  • 10
  • 14