/// <summary> /// Event: Get detailed information about a friend. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event Args</param> override protected void lstFriends_MouseDoubleClick(object sender, MouseButtonEventArgs e) { #region The code required for diagnosis - measures the time var spectator = new Stopwatch(); spectator.Start(); #endregion if (lstFriends.SelectedItem == null) return; var frm = new InfoWindow { StartedRedis = _services.RedisIsStarted, // Do not show on the taskbar. ShowInTaskbar = false }; Connected(_listOfFriends[lstFriends.SelectedIndex], frm); #region The code required for diagnosis - measures the time spectator.Stop(); var ts = spectator.Elapsed; frm.TimeParam = $"Lead time: {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds/10:00}"; #endregion frm.Show(); }
/// <summary> /// Event: Get detailed information about a friend. /// </summary> /// <param name="sender">Sender</param> /// <param name="e">Event Args</param> private void lstFriends_MouseDoubleClick(object sender, MouseButtonEventArgs e) { #region The code required for diagnosis - measures the time var spectator = new Stopwatch(); spectator.Start(); #endregion if (lstFriends.SelectedItem == null) return; var userId = _listOfFriends[lstFriends.SelectedIndex]; var frm = new InfoWindow {ShowInTaskbar = false /* Do not show on the taskbar. */}; if (userId <= 100000) { using (var context = new Storage1Context()) { ConnectToServer1(context, userId, frm); } } else if (userId > 100000 && userId < 200000) { using (var context = new Storage2Context()) { ConnectToServer2(context, userId, frm); } } else if (userId > 200000 && userId < 300000) { using (var context = new Storage3Context()) { ConnectToServer3(context, userId, frm); } } else if (userId > 300000 && userId < 400000) { using (var context = new Storage4Context()) { ConnectToServer4(context, userId, frm); } } else if (userId > 400000 && userId < 500000) { using (var context = new Storage5Context()) { ConnectToServer5(context, userId, frm); } } #region The code required for diagnosis - measures the time spectator.Stop(); var ts = spectator.Elapsed; frm.TimeParam = $"Lead time: {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds/10:00}"; #endregion frm.Show(); }
/// <summary> /// Determine the database with which it is necessary to make the connection. /// The method for the modal form. /// </summary> /// <param name="userId">User ID.</param> /// <param name="frm">An instance of a modal form.</param> private void Connected(int userId, InfoWindow frm) { try { var localDb = new DataBase(); if (userId <= 100000) { using (var context = new Storage1Context()) { localDb.ConnectToServer1(context, userId, _services, _logging, _friend, lstFriends, frm); } } else if (userId > 100000 && userId <= 200000) { using (var context = new Storage2Context()) { localDb.ConnectToServer2(context, userId, _services, _logging, _friend, lstFriends, frm); } } else if (userId > 200000 && userId <= 300000) { using (var context = new Storage3Context()) { localDb.ConnectToServer3(context, userId, _services, _logging, _friend, lstFriends, frm); } } else if (userId > 300000 && userId <= 400000) { using (var context = new Storage4Context()) { localDb.ConnectToServer4(context, userId, _services, _logging, _friend, lstFriends, frm); } } else if (userId > 400000 && userId <= 500000) { using (var context = new Storage5Context()) { localDb.ConnectToServer5(context, userId, _services, _logging, _friend, lstFriends, frm); } } } catch (ArgumentOutOfRangeException ex) { _logging.ProcessingException(ex); MessageBox.Show(ex.Message, "Connection error", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// The connection method to Storage5. /// Called from an event handler lstFriends_MouseDoubleClick. /// </summary> /// <param name="context">The context of the interaction with the database.</param> /// <param name="userId">User ID.</param> /// <param name="frm">Instance a modal window.</param> private void ConnectToServer5(Storage5Context context, int userId, InfoWindow frm) { RedisOrSQLStorage5(context, userId, frm); var queryFriendsStorage = context.Friends.Where(x => x.User_Id == userId); var friends = queryFriendsStorage.ToList(); foreach (var f in friends) { if (f.Friend_Id < 100000) { using (var storage1Context = new Storage1Context()) { frm.FriendsParam = FindFriendsStorage1(storage1Context, f, frm); } } else if (f.Friend_Id > 100000 && f.Friend_Id < 200000) { using (var storage2Context = new Storage2Context()) { frm.FriendsParam = FindFriendsStorage2(storage2Context, f, frm); } } else if (f.Friend_Id > 200000 && f.Friend_Id < 300000) { using (var storage3Context = new Storage3Context()) { frm.FriendsParam = FindFriendsStorage3(storage3Context, f, frm); } } else if (f.Friend_Id > 300000 && f.Friend_Id < 400000) { using (var storage4Context = new Storage4Context()) { frm.FriendsParam = FindFriendsStorage4(storage4Context, f, frm); } } else if (f.Friend_Id > 400000 && f.Friend_Id < 500000) { frm.FriendsParam = FindFriendsStorage5(context, f, frm); } } }
/// <summary> /// Methods of forming a string name = Last Name + First Name, which will be added to the list. /// Called from the methods GetUserInfoFromStorage. /// </summary> /// <param name="context">The context of the interaction with the database.</param> /// <param name="f">The object to be added.</param> /// <param name="frm">Instance a modal window.</param> private static string FindFriendsStorage5(Storage5Context context, Friends f, InfoWindow frm) { var queryFriend = context.User.Where(x => x.Id == f.Friend_Id); var elementToAdd = queryFriend.ToList(); frm.ListOfFriendsParam = elementToAdd[0].Id; return (elementToAdd[0].Last_Name + ' ' + elementToAdd[0].First_Name); }
/// <summary> /// Check Method cached user in Redis or not. /// If the user is not cached in Redis then its cache. /// Called from the method ConnectToServer (modal). /// </summary> /// <param name="context">The context of the interaction with the database.</param> /// <param name="userId">User ID.</param> /// <param name="frm">Instance a modal window.</param> private void RedisOrSQLStorage5(Storage5Context context, int userId, InfoWindow frm) { IDatabase db = null; if (_redisIsStarted) { using (var redisClient = ConnectionMultiplexer.Connect("localhost")) { db = redisClient.GetDatabase(); string value = db.StringGet($"user:{userId}"); if (string.IsNullOrEmpty(value)) { var queryUserStorage = context.User.Where(x => x.Id == userId); GetDataFromSQL(queryUserStorage, db, true, frm); } else { GetDataFromRedis(value, frm); } } } else { var queryUserStorage = context.User.Where(x => x.Id == userId); GetDataFromSQL(queryUserStorage, db, false, frm); } }
/// <summary> /// Method of getting data from Redis. /// Called from the method RedisOrSQLStorage. /// </summary> /// <param name="value">JSON-string.</param> /// <param name="frm">Instance a modal window.</param> private static void GetDataFromRedis(string value, InfoWindow frm) { value = value.Substring(value.IndexOf("[", StringComparison.Ordinal) + 1); value = value.Substring(0, value.LastIndexOf("]", StringComparison.Ordinal)); var u = JsonConvert.DeserializeObject<User>(value); frm.FirstNameParam = u.First_Name; frm.LastNameParam = u.Last_Name; frm.AgeParam = u.Age.ToString(); frm.BioParam = u.Bio; frm.CountryParam = u.Country; frm.CityParam = u.City; }
/// <summary> /// Method of getting data from SQL. /// Called from the method RedisOrSQLStorage. /// </summary> /// <param name="queryUserStorage">Query our request.</param> /// <param name="db">Connecting with Redis.</param> /// <param name="redisIsStarted">The flag to check launched Redis.</param> /// <param name="frm">Instance a modal window.</param> private static void GetDataFromSQL(IEnumerable<User> queryUserStorage, IDatabase db, bool redisIsStarted, InfoWindow frm) { var user = queryUserStorage.ToList(); frm.FirstNameParam = user[0].First_Name; frm.LastNameParam = user[0].Last_Name; frm.AgeParam = user[0].Age.ToString(); frm.BioParam = user[0].Bio; frm.CountryParam = user[0].Country; frm.CityParam = user[0].City; if (redisIsStarted) RedisSetData(db, user); }