views:

63

answers:

2

I'm using the Composite Application Library with Silverlight, and I need to add three "zones" to my region. These "zones" all have essentially the same view and presentation model. (I'm getting these words from the StockTraderRI application. Correct me if I'm wrong.) The only difference I have is where I get the data from, so I want to have a different service for each "zone".

Currently, I am able to initialize my view in the "RightsRegion" by doing this:

public void Initialize()
{
     RegisterViewsAndServices();

     this.regionManager.Regions["MainRegion"].Add(new DefaultViewUI());
     this.regionManager.RegisterViewWithRegion("RightsRegion", () => container.Resolve<ISecurityTreePresentationModel>().View);
}

private void RegisterViewsAndServices()
{
     container.RegisterType<ITreeViewService, EntityTypesService>(new ContainerControlledLifetimeManager());
     container.RegisterType<ISecurityTreeView, SecurityTreeView>();
     container.RegisterType<ISecurityTreePresentationModel, SecurityTreePresentationModel>();
}

I thought I would be able to register another copy of this view in the "RightsRegion" with my LocationsService, but that seems to overwrite my EntityTypesService.

How can I register three identical views and very similar presentation models into my "RightsRegion" so that they each use a different service?

A: 

Try creating a child container for each one.

var container = myContainer.CreateChildContainer();
container.RegisterType<ITreeViewService, EntityTypesService>(new ContainerControlledLifetimeManager());
container.RegisterType<ISecurityTreeView, SecurityTreeView>();
container.RegisterType<ISecurityTreePresentationModel, SecurityTreePresentationModel>();

Don't forget to dispose the child containers though if you are using container controlled lifetimes (you can use externally controlled lifetime manager too).

I'd also like to add that you should avoid dealing with the regions within an IModule. Your IModule can get messy pretty quick. I'd suggest making a controller (see StockTrader RI) to handle stuff like this.

-Jer

Jeremiah Morrill
Thank you for your answer and advice. This helps me get going now and know where I should be headed.
Rachel Martin
+1  A: 

Alternatively, if you want to use only one container, you could use named registration:

container.RegisterType<ITheInterface, TheInstance1>("Instance 1");
container.RegisterType<ITheInterface, TheInstance2>("Instance 2");
Konamiman