I have the following classes:
public abstract class BaseGridViewModel
{
protected BaseGridViewModel()
{
Timer = new List<long>();
}
public List<long> Timer { get; set; }
}
public class CityReportViewModel : BaseGridViewModel
{
public IEnumerable<City.Grid> Grid { get; set; }
}
In my action code I am doing timing like this:
var vm = new CityReportViewModel();
var sw = Stopwatch.StartNew();
try {
//
vm.Timer.Add(sw.ElapsedMilliseconds);
//
vm.Timer.Add(sw.ElapsedMilliseconds);
//
// Lots more vm.Timer.Add lines ...
} catch (Exception e) {
log(e);
} finally {
sw.Stop();
}
Because I do a lot of timing I have the vm.Timer.Add code repeated many times in my controllers.
Is there some way I could simplify the timer coding by making a change to the ViewModels. The kind of thing I am thinking of is to set up the timer as a field in the ViewModels and then have some way of telling the ViewModel that I want to record a new timing event.
I’m not really sure about your requirement, but it sounds like you need an observer.
Your ‘BaseGridViewModel’ may register himself to an obeserver. The observer raises may an event to update all timers in all attached BaseGridViewModels.
Edit
This is what I’m talking about:
May this is what you need. The above code is only an example..