one model for many controllers

Topics: Prism v4 - WPF 4
Nov 2, 2010 at 6:25 AM
Edited Nov 2, 2010 at 6:28 AM

Hi all! I'm beginner at Prism and I have one question. I studied the StockTrader RI and the question arose - where necesary and how right to use controllsers. E.g.:

    public class OrderDetailsPresentationModel : IOrderDetailsPresentationModel, INotifyPropertyChanged
    {
        private readonly IAccountPositionService accountPositionService;
        private readonly IOrdersService ordersService;

        public OrderDetailsPresentationModel(IOrderDetailsView view, IAccountPositionService accountPositionService, IOrdersService ordersService)
        {
            this.View = view; // no View.Model = this; and no action delegate
            this.accountPositionService = accountPositionService;
            this.ordersService = ordersService; 
        }

        public IOrderDetailsView View { get; set; }
        }

        public OrderDetailsPresentationModel(IOrderDetailsView view, IAccountPositionService accountPositionService, IOrdersService ordersService)
        {
            this.View = view;
            this.accountPositionService = accountPositionService;
            this.ordersService = ordersService;
        }
     }

and

    public class ArticlePresentationModel : IArticlePresentationModel, INotifyPropertyChanged
    {
        private IList<NewsArticle> articles;
        private NewsArticle selectedArticle;
        private readonly INewsFeedService newsFeedService;
        public event PropertyChangedEventHandler PropertyChanged;

        public ArticlePresentationModel(IArticleView view, INewsFeedService newsFeedService)
        {
            View = view;
            View.Model = this; // !
            this.newsFeedService = newsFeedService;
            View.ShowNewsReader += View_ShowNewsReader; // !
        }

        public IArticleView View { get; private set; }

        public INewsController Controller { get; set; }

        private void View_ShowNewsReader(object sender, EventArgs e)
        {
            this.Controller.ShowNewsReader();
        }
     }

My difficulty is that I need to create a single model (implemented INotifyPropertyChanged and using my WCF service) for my prism module, and a lot of controllers for each of the UserControls.

        <TabControl x:Name="tabControl1" Height="404" HorizontalAlignment="Left" VerticalAlignment="Top" Width="654">
            <TabItem Header="Login" x:Name="tabItem1">
                <uc:LoginView x:Name="LoginView1" />
            </TabItem>// and so on

Who have done similar? How template will be look like?

Nov 2, 2010 at 7:19 PM

Hi,

Regarding to your first question there is a specific documentation section about Patterns and the Application Controller Pattern is mentioned over there. So, for more information you could take a look at the following documentation section in the Prism4.chm:

  • Appendix B: Patterns in Prism Library, see the Application Controller Pattern section.

Based on my understanding, your scenario requires to create an only one instance of a ViewModel and it will be assigned to many Views/Controller. There is not guidance on this topic in Prism out-of-the-box. In general, the context of a view is assigned with its own ViewModel. But it is not something impossible to achieve, since you could use the ViewModel First pattern. In this separated presentation patterns the ViewModel will be responsible for creating the view/views and binding itself to the view/views. For more information on this topic, you could take a look at Rob Eisenberg's presentation at MIX: Build your own MVVM Framework.

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero