Search code examples
c#unity-containerprisminsight.database

Parameter injection with Prism 7 and Unity


I'm using Insight.Database by Jon Wagner as my micro-ORM, meaning there are no concrete implementations for my (database) interfaces.

I'm trying to inject one of those interfaces into my Prism 7 MainWindowViewModel, simply to see if the database exists (at this stage - obviously, I'll do a lot more later).

However, either the new Unity has screwed things up or Prism doesn't like the way I'm doing it. In the old days with just Unity, it was not exactly a delight but reasonably okay, using the following code -

var conn = ConfigurationManager.ConnectionStrings["default"];
container.RegisterType<IConnection>(new InjectionFactory(con 
                         => conn.AsParallel<IConnection>());

But this is different. I'll admit I'm new to Prism but nothing on IContainerRegistry has anything like this functionality.

by using

var container = containerRegistry.GetContainer();

I can get the base container but this has changed too, and it follows the standards of its predecessors by eschewing decent documentation with examples so my best approximation is this, which compiles at least.

container.RegisterInstance(typeof(IConnection),
     "DbConnection", new InjectionFactory(con =>    
        connection.AsParallel<IConnection>()), null);

This compiles, but it absolutely doesn't inject. I hit run-time errors when the AutoWireUp tries to instantiate MainWindowViewModel.

Can anyone at all point me to an answer or a useful resource?


Solution

  • Typically, continued faffing about has solved this.

    First, make sure that App.xaml.cs includes this

    using Unity;
    

    which gives us back access to RegisterType<T>().

    From there, this is all that's needed.

    container.RegisterType<IConnection>(new InjectionFactory(s => 
    connection.AsParallel<IConnection>()));
    

    Sorry to bother you, chaps. It's fine now.

    I've posted the answer because I doubt I'll be the last to run into this but we live in hope.