/// <summary> /// Loads all the data from the database and into memory. /// </summary> private void Load() { using (var db = new DataContext()) { var query = db.Favorites.Include(favorite => favorite.Url); this._favorites = query.ToDictionary(entry => entry.Id, entry => entry); this._favoritesViewModels = new BindingList <FavoritesViewModel>( query.AsEnumerable().Select(fav => FavoritesViewModel.FromFavoritesLocation(fav, this._faviconCache)).ToList()); } }
/// <summary> /// The mock get or create. /// </summary> /// <param name="url"> /// The url. /// </param> /// <returns> /// The <see cref="FavoritesLocation"/>. /// </returns> public FavoritesLocation GetOrCreate(Url url) { var inDb = this._idLookup.SingleOrDefault(pair => pair.Value.Url == url); if (inDb.Value != null) { return(inDb.Value); } var loc = new FavoritesLocation(url); this._viewModel.Add(FavoritesViewModel.FromFavoritesLocation(loc, new MockFaviconCache())); this._idLookup.Add(this.id, loc); this.OnFavoritesAddOrUpdate?.Invoke(this, new FavoritesUpdateEventArgs(loc)); loc.Id = this.id++; return(loc); }
/// <summary> /// Updates the item in memory with the same id with new data. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="args"> /// The args. /// </param> private void UpdateFavorites(object sender, FavoritesUpdateEventArgs args) { // update favorites this._favorites[args.Location.Id] = args.Location; // update viewmodel var toUpdate = this._favoritesViewModels.SingleOrDefault(model => model.Url == args.Location.Url.ToString()); if (toUpdate == null) { this._favoritesViewModels.Add(FavoritesViewModel.FromFavoritesLocation(args.Location, this._faviconCache)); } else { toUpdate.Name = args.Location.Name; toUpdate.Url = args.Location.Url.ToString(); } }