C# Dependency Injection MVVM – Should IoC Container Be Part of Portable Library

cdependency-injectioninversion-of-controlmvvmportability

I am seeing the benefits of Portable Class Libraries for centralizing Models and ViewModels to a single code-base. Where should dependency injection management (IoC container) take place, though? My Models have dependencies that I believe would benefit from being supplied through injection. I'm not sure how to go about "bootstrapping" to set up the container in the portable library, and I also saw some suggestions that a portable library shouldn't have a dependency on a DI framework. Those points have me thinking maybe the DI should be set up in the applications that use my portable library. This seems rather backwards though, requiring someone using the library to add the bootstrapping portion to their application, as well as it would be duplicated in every app that uses the library. Should the library or the application handle DI?

Some specifics on what my environment in question is like: c#, WPF application referencing PCL with Models, ViewModels and core logic. Considering using Autofac for my IoC container.

Best Answer

If you need IoC in your library then it may be helpful to provide hooks so a user can use the same containers, but don't require it as the developer may want to use something else.

So make it available since you will include it, but don't limit the programmer's options, if possible.