Prism Regions with Catel IoC Container

Topics: Questions
Sep 19, 2012 at 9:14 AM

Hello,

to get Prism Region's to work without an external Container like Unity

i have to modify Catel ServiceLocatorAdapter class.

Maybe this is because Catel does not respect the PartCreationPolicy Attribute?

   protected override object DoGetInstance(Type serviceType, string key)
        {
            Catel.Argument.IsNotNull("serviceType", serviceType);

            // TODO: Evaluate if this behavior should be promoted to the service locator 
            if (serviceType.IsClass && !serviceType.IsAbstract)
            {
                var singleton = !(serviceType.FullName.StartsWith("Microsoft.Practices.Prism.Regions.Behaviors") ||
                    serviceType.GetInterfaces().Contains(typeof(Microsoft.Practices.Prism.Regions.IRegionBehavior)));
                // Required by ConfigureRegionAdapterMappings, RegionBehaviours
                Catel.IoC.ServiceLocator.Instance.RegisterTypeIfNotYetRegistered(serviceType, serviceType, singleton);
            }

            return Catel.IoC.ServiceLocator.Instance.ResolveType(serviceType);
        }

Best regards, Uli

Coordinator
Sep 19, 2012 at 8:20 PM

The ServiceLocator of Catel now supports both Singleton and Transient registration. However, it does not yet check the creation of external containers. We are looking into that.

Developer
Oct 4, 2012 at 4:27 PM

@uli_laegeler: We update the ServiceLocatorAdapter implementation moving the required registration of non-abstract classes to the ModuleInitializer class. Now the DelayedRegionCreationBehavior is registered as Transient. Please try out with the latest sources.

Coordinator
Oct 4, 2012 at 6:08 PM

Or try the latest nuget package, we just published a new one.

Developer
Oct 5, 2012 at 2:14 PM

All behaviors are now registered at ModuleInitializer.

We also added support to do something like this:

    var uiVisualizerService = GetService<IUIVisualizerService>();
    var windowVM = new WindowWithRegionViewModel();
    uiVisualizerService.Show(windowVM);
    uiVisualizerService.Activate(new PersonViewModel(), windowVM, "WindowRegion");

Please try out with the latest sources.

Oct 12, 2012 at 11:07 AM

Ok, i checked it with the Beta.

All works.

One Item:

In one Module i use RegionManager.RegisterViewWithRegion

To get this to work with the Beta i have to manually register my View in the Container
otherwise the View cannot be resolved.
Not sure why it worked before without manually registration of the View.
Maybe it has to to with the AutoPopulateViewBehaviour?

 

Best regards, Uli 

Developer
Oct 12, 2012 at 12:46 PM
Edited Oct 12, 2012 at 12:48 PM

The reason why It worked before is related with this removed code from the ServiceLocatorAdapter

if (serviceType.IsClass && !serviceType.IsAbstract)
{    
 Catel.IoC.ServiceLocator.Instance.RegisterTypeIfNotYetRegistered(serviceType, serviceType);
}

When the RegionViewRegistry call the method GetInstance with the view type as parameter the view type is not actually registered at the container.

So, we will re-introduce the code now with this behavior:
if (serviceType.IsClass && !serviceType.IsAbstract) 
{    
 Catel.IoC.ServiceLocator.Instance.RegisterTypeIfNotYetRegistered(serviceType, serviceType, RegistrationType.Transient);
}
Developer
Oct 12, 2012 at 1:14 PM

Fixed, try with the latest sources.

Developer
Oct 12, 2012 at 1:17 PM

Fixed, try with the latest sources.

Oct 22, 2012 at 1:58 PM

Hmm, now ConfigureRegionAdapterMappings() from the Bootstrapper returns null.

Developer
Oct 23, 2012 at 3:06 PM
Edited Oct 23, 2012 at 3:16 PM

For a testing issue of the Bootstrapper, the RegionAdapterMappings registration was removed. Now RegionAdapterMappings is registered as singleton in the  Module Initializer.

I will write some tests to check the right behavior of the ServiceLocatorAdapter for Prism.

Developer
Oct 29, 2012 at 8:59 PM

There are a lot of bug fixes on the last beta package. Everything is working now for NET and SL.

Please try again.

Oct 30, 2012 at 6:42 AM

Looks good.

 

When will there be a WinRT version?
Have you to wait until Prism is available for WinRT?

Coordinator
Oct 30, 2012 at 7:18 AM

The beta already contains a (beta) WinRT version of Catel.Core, Catel.MVVM and Catel.Extensions.Controls.

We also created project and item templates so you should be ready to go on WinRT as well.

Oct 30, 2012 at 7:23 AM

Yes, i already tried out these parts in WinRT.
I had some problems with Deserialization but i have to look into further.

My question pointed to Catel.Extensions.Prism

Coordinator
Oct 30, 2012 at 10:17 AM

I am not aware of prism for winrt yet. If there is a version, let us know and we can implement support.