0

I am trying to replace the built in IOC container in Catel with SimpleInjector.

From their prism example (i am using catel+prism) the bootstrapper claims to be configuring Unity in the following fragment:

/// <summary>
/// Configures the <see cref="IUnityContainer"/>. May be overwritten in a derived class to add specific
/// type mappings required by the application.
/// </summary>
protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    Container.CanResolveNonAbstractTypesWithoutRegistration = true;

    Container.RegisterType<IDepartmentRepository, DepartmentRepository>();
    Container.RegisterType<IEmployeeRepository, EmployeeRepository>();
}

However I don't see how this configures the IUnityContainer and according to my debugger, it does not in fact configure unity (the Container being used is the built in ServiceLocator of Catel). Can someone more familiar with Catel help me figure out how to do this? I would be happy to write it up for the documentation.

Bitfiddler
  • 3,942
  • 7
  • 36
  • 51

2 Answers2

0

The documentation (xml docs) is probably outdated because it doesn't use IUnityContainer. In fact it uses the IServiceLocator in Catel.

The best way to replace the IServiceLocator with your own implementation is to inject it in the constructor (BootstrapperBase). I will also add the same constructor overrides to the other bootstrappers (with the TShell and TShell, TModuleCatalog)

Another option is to replace the IoCConfiguration.DefaultServiceLocator. The bootstrapper will fall back on that one if no service locator is specified.

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32
  • Yes, the docs indicate that Unity is no longer used internally. The above is from the Example solution, specifically the WPF+Prism sample. The comments indicate it's supposed to show you how to replace the built in servicelocator with another IOC container like Unity. However, to my understanding, the sample solution does not do this and the documentation as to how to do it is too sparse to be very useful. – Bitfiddler Oct 24 '13 at 15:39
  • Did you read this as well? https://catelproject.atlassian.net/wiki/pages/viewpage.action?pageId=622682#IoC(ServiceLocatorandTypeFactory)-Replacingthedefaultcomponents – Geert van Horrik Oct 24 '13 at 15:46
  • To implement your own IServiceLocator it would be helpful if there was working sample implementation of the IServiceLocator for an existing third party IOC container like Unity. I have been digging through the source, but the only implementation I have found thus far is for the native ServiceLocator. This implementation very complex, composed of nested private classes and all sorts of other logic. It is difficult to know what parts of the code would be handled by a third party IOC container and which logic would have to be hand coded. – Bitfiddler Oct 24 '13 at 15:49
  • Put simply, it appears it is not easy to use your own IOC with Catel. – Bitfiddler Oct 24 '13 at 15:50
  • Yes I did read that link. The part that is missing is how to implement: "MyCustomServiceLocator()" – Bitfiddler Oct 24 '13 at 15:51
  • We will work on an example, but don't expect this soon (no high prio). See https://catelproject.atlassian.net/browse/CTL-210 for details. – Geert van Horrik Oct 24 '13 at 15:51
  • Also, what does this mean? // Core elements CoreModule.RegisterServices(serviceLocator); MVVMModule.RegisterServices(serviceLocator); // Extensions ExtensionsPrismModule.RegisterServices(serviceLocator); ExtensionsMementoModule.RegisterServices(serviceLocator); – Bitfiddler Oct 24 '13 at 15:51
  • When you put that code into a bootstrapper, VS can't find MVVMModule as a Type so I'm guessing this is user code but from the context I have no idea how to interpret MVVMModule? – Bitfiddler Oct 24 '13 at 15:53
  • That's because they are introduced in Catel 3.8. The documentation visible is always the latest. If you want to stick with 3.7, then you can find that version specific documentation at https://catelproject.atlassian.net/wiki/display/CTL37/Catel+documentation+Home – Geert van Horrik Oct 24 '13 at 16:17
  • I have to use framework 4 for the time being (issues with WCF and framework 4.5). As far as Nuget can tell, 3.7 is the most recent available for framework 4. – Bitfiddler Oct 28 '13 at 17:27
  • 3.8 is currently in development and the nightly builds (nuget calls this prereleases) contains these features. – Geert van Horrik Oct 29 '13 at 02:51
0

Here is my solution for Catel 3.8 and Autofac. Should work for different 3rd party container as well.

https://stackoverflow.com/a/20458474

Community
  • 1
  • 1
Mario Eis
  • 2,724
  • 31
  • 32