views:

148

answers:

1

Hello,

I´m trying to share a Data Service (Entity Manager) wrapped in a Repository from a ViewModel (called 'AVM') in Module A to a ViewModel (called 'BVM') in Module B, and I can't get this working. We use PRISM/Unity 2.0

This is my scenario:

A user may open multiple Customer screens (composite view as mini shell) each with another customer (unit of work). We realize this using child containers. Each child container resolves it's own repository with its own Entity manager (the repository is a singleton within the child container). This is done in module A. The main shell has a main region manager, and each Customer screen with its childcontainer creates a scoped region. In each customer screen there is a View 'AV' (connected to ViewModel 'AVM') with a SubRegion (tab control) registered as 'SubRegion'. We create this with a 'Screen Factory'

In Module B we have a Customer Orders in View 'BV' and ViewModel 'BVM'. In the constructor of Module B we get the main container by injection. In the initialize method we resolve the (main) region manager and register View 'BV' with it. In the constructor of View 'BV' a ViewModel 'BVM' is injected/created.

Now this works, but the ViewModel 'BVM' cannot get the child container. It only get the main container. Is this doable, or do I have to do this another way?

Thanks, Niels

A: 

There are a couple of possibilities:

My favorite option is do this entirely inside of a model binder specialized for Ioc - which is what you'll have to do anyway. I like the model that Autofac uses and the code spells it out quite succinctly.

I have no idea around Unity - I haven't touched it since v1.1. If you are rolling your own, one method is to take the created child container and register it in TLS. You can then override the Ioc model binder functionality to go after the TLS stored child container. Pretty hackish, but I'm not sure of another way. I would go with option #1 above first and foremost. Look to option #2 as only a way to investigate options wrt to unity.

S. Hebert
Thank you for your reply. For the time being I'm creating a factory which all binds it together. I'll look into Autofac.