views:

29

answers:

2

I have a DateNavigatorViewModel + DateNavigatorView on my ButtonBar.

Below are 2 Views which get exchanged: DailyView and WeeklyView. Each View has a DailyViewModel and WeeklyViewModel.

In my DateNavigatorViewModel I have messenger.Send(SelectedDate);

In my DailyViewModel and WeeklyViewModel each register in the constructor:

messenger.Register<DateTime>(this, LoadDailyData);
messenger.Register<DateTime>(this, LoadWeeklyData);

guess what happens when I select a date...

I am using MVVM Light toolkit.

How can I solve that problem of getting 2 times data from database?

A: 

In your data access layer you could use caching stored in some static dictionary, load all the data you need from the database for both view and filter at the data layer for the individual viewsmodels.

Or an alternative, have the DateChanged messege be received by the Data objects, load the data and then have a second message raised and received by your two views.

benPearce
the first one won`t work for my scenario as the performance will be too slow switching forth/back the calendar... Can you explain the second suggestion a bit more please, I do not understand what you mean. Thank you!
Lisa
I can't see how performance will be impacted, apart from a small increase in memory usage for the caching. The second idea is to have your data layer respond the the request for data, then load the data and raise a second message, received by your ViewModels, with the data being passed as a message parameter. Therefore you only load the data once, but pass it off to be received by multiple ViewModels.
benPearce
Either I was not clear or you misunderstand my scenario. About the performance penalty: When I click the moveNextDate button atm I get right now 10 rows multiplied 2 RichTextBoxcolumns. That takes time a second. Any more delay in the loading and I need a progress bar...
Lisa
I want to keep the DAL out of this scenario because its application logic so I want to solve it among the ViewMOdels. Will post later sth.
Lisa
A: 

MainViewModel is instantiated then:

clicking on the Daily-Button instantiates the:

public DailyViewModel(IMessenger messenger)
        {
            _messenger = messenger;
            _messenger.Register<DateNavigatorInfoObject>(this, LoadDailyData);

        }

        private void LoadDailyData(DateNavigatorInfoObject infoObject)
        {
            if (infoObject.DateNavigatorMode != DateTest.DateMode.Day)
                return;

            // get daily database stuff
        }

After the DateNavigatorViewModel got instantiated see BELOW

clicking on the Weekly-Button instantiates the:

public WeeklyViewModel(IMessenger messenger)
{
    _messenger = messenger;
    _messenger.Register<DateNavigatorInfoObject>(this, LoadWeeklyData);                
}

private void LoadWeeklyData(DateNavigatorInfoObject infoObject)
{
    if (infoObject.DateNavigatorMode != DateTest.DateMode.Week)
       return;

    // get weekly database stuff
}

After the DateNavigatorViewModel got instantiated see BELOW

 public DateNavigatorViewModel(IMainRepository mainRepo, IMessenger messenger)
        {
            _mainRepo = mainRepo;
            _messenger = messenger; 

            SelectedDate = DateTime.Now;
             // Wether daily/weekly data is fetched the start date for the data is  NOW // when the  ViewModels are instantiated the first time using a ViewModelLocator...
        }

Now the property that got fired setting the DateTime.Now in the Ctor

private DateTime _selectedDate;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set
            {
                if (_selectedDate.Date == value.Date)
                    return;

                _selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");


                var infoObject = new DateNavigatorInfoObject();
                switch (DateNavigatorMode)
                {    
                    case DateTest.DateMode.Day:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        break;

                    case DateTest.DateMode.Week:
                        infoObject.DateNavigatorMode = DateNavigatorMode;
                        infoObject.SelectedStartDate = value;
                        infoObject.SelectedEndDate = value.AddDays(6);
                        break;
                }

                _messenger.Send(infoObject);                
            }

public class DateTest
    {
        public enum DateMode
        {
            Day,
            Week,
            Month,
        }
    }

The infoObject is send both to the Daily and WeeklyViewModel but depending on the DateNavigatorMode the database fetching is rejected from the ViewModel.

For me thats a solution because it firstly works and second no DAL is involved just the ViewModels.

Someone might mark it as solution if you like it. Critics are welcome too maybe I can still improve something?

Lisa
ok forget about that solution I will post tomorrow a better one :) less code less hackstyle :P
Lisa
back... as I have changed my requirements daily and weekly view has now all different buttons, not same datepicker etc... except one usercontrol with 5 buttons. So no need anymore to search for an appropriate algo. But still thanks for help to all above all Ben.
Lisa