Home > front end >  How To Choose Which IoC Container WPF Prism Application Use?
How To Choose Which IoC Container WPF Prism Application Use?

Time:06-07

I Am Developing Application With Prism 7 and .Net 6 and MediatR , My app.xaml is

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var bootstrapper = new Bootstrapper();
        bootstrapper.Run();
    }
}

class Bootstrapper : PrismBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //I Want To Use Unity Here
    }
}

the problem is i want to use extension method that is designed for IUnityContainer but i cant as i have IContainerRegistry not IUnityContainer in RegisterTypes method in my Bootstrapper

  public static IUnityContainer RegisterMediator(this IUnityContainer container, ITypeLifetimeManager lifetimeManager)
    {
        return container.RegisterType<IMediator, Mediator>(lifetimeManager)
            .RegisterInstance<ServiceFactory>(type =>
            {
                var enumerableType = type
                    .GetInterfaces()
                    .Concat(new[] { type })
                    .FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>));

                return enumerableType != null
                    ? container.ResolveAll(enumerableType.GetGenericArguments()[0])
                    : container.IsRegistered(type)
                        ? container.Resolve(type)
                        : null;
            });
    }

prism support several IoC Container , DryIoc , shiny , but there is really no documentation how to change default IoC Container ,How To Choose Which IoC Container WPF Prism Application Use ? in this case i want to choose Unity so i can use my mediatR Extension ,I have search the whole website but with no success. thank you

CodePudding user response:

In the Prism.Unity namespace, there is an extension method for IContainerProvider.

public static IUnityContainer GetContainer(this IContainerProvider containerProvider)

You can use this inside your PrismBootstrapper.

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    IUnityContainer container = Container.GetContainer();
}
  • Related