public static void Update <CounterType, TotalType>(ISession session, SortedDictionary <DateTime, TotalType> dict, GetTimestampCounter <CounterType, TotalType> getTimestampcounter, SetTimestampCounter <CounterType, TotalType> setTimestampcounter) where CounterType : IDbObject, new() { IEnumerator <CounterType> counterenumerator = session.CreateQuery(string.Format("FROM {0}", typeof(CounterType).Name)) .Enumerable <CounterType>().GetEnumerator(); while (counterenumerator.MoveNext()) { UpdateOrDelete <CounterType, TotalType>(session, dict, counterenumerator.Current, getTimestampcounter, setTimestampcounter); } SortedDictionary <DateTime, TotalType> .Enumerator remainingenumerator = dict.GetEnumerator(); while (remainingenumerator.MoveNext()) { CounterType counter = new CounterType(); TimestampCounter <TotalType> tc = new TimestampCounter <TotalType>( remainingenumerator.Current.Key, remainingenumerator.Current.Value); setTimestampcounter(counter, tc); session.Save(counter); } }
public static void UpdateOrDelete <CounterType, TotalType>(ISession session, SortedDictionary <DateTime, TotalType> dict, CounterType instance, GetTimestampCounter <CounterType, TotalType> getTimestampcounter, SetTimestampCounter <CounterType, TotalType> setTimestampcounter) { TotalType total = default(TotalType); TimestampCounter <TotalType> tc = getTimestampcounter(instance); if (dict.TryGetValue(tc.Timestamp, out total)) { if (!total.Equals(tc.Total)) { tc.Total = total; setTimestampcounter(instance, tc); session.Save(instance); } dict.Remove(tc.Timestamp); } else { session.Delete(instance); } }