Exemple #1
0
 /// <summary>
 /// Возвращает контекст из пула
 /// </summary>
 /// <param name="username"></param>
 /// <returns></returns>
 public static CustomDbContext GetContext(string username)
 {
     lock (obj)
     {
         if (customDbContextPool.ContainsKey(username))
         {
             var a = customDbContextPool[username].Database.Connection;
             if (customDbContextPool[username].Database.Connection.State == System.Data.ConnectionState.Open) ///Broken не работает
             {
                 return(customDbContextPool[username]);
             }
             customDbContextPool.Remove(username);
         }
         try
         {
             string          cnString = ConfigurationManager.ConnectionStrings["EFDB"].ConnectionString;
             CustomDbContext dc       = new CustomDbContext(cnString);
             //dc.Database.Initialize(false);
             customDbContextPool.Add(username, dc);
             return(dc);
         }
         catch (Exception ex)
         {
             ServerSideExceptionHandler.ExceptionHandler.HandleExcepion(ex, "(CustomDbContext)EFDataSourceLayer.Gateway.GetContext(string username)");
             throw;
         }
     }
 }
 /// <summary>
 /// Возвращает сотрудника по его идентификатору
 /// </summary>
 /// <param name="id"></param>
 /// <param name="username"></param>
 /// <returns></returns>
 public static Employee Select(string selectableUsername, string connectionUsername)
 {
     try
     {
         CustomDbContext dc     = Gateway.GetContext(connectionUsername);
         Employee        result = dc.Employees.FirstOrDefault(row => string.Compare(row.Username, selectableUsername) == 0);
         return(TranslateToDepletedEmployeeObject(result));
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "(Employee)EFDataSourceLayer.EmployeeGateway.Select(string selectableUsername, string connectionUsername)");
         throw;
     }
 }
 /// <summary>
 /// Возвращает коллекцию Id отправленных сообщений
 /// </summary>
 /// <param name="senderUsername"></param>
 /// <param name="connectionUsername"></param>
 /// <param name="deleted"></param>
 /// <returns></returns>
 public static List <int> SelectIds(string senderUsername, string connectionUsername, bool deleted)
 {
     try
     {
         CustomDbContext dc = Gateway.GetContext(connectionUsername);
         return(dc.Messages.Where(row => string.Compare(senderUsername, row.SenderUsername) == 0 &&
                                  row.Deleted == deleted).Select(row => (int)row.Id).ToList());
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "(List<int>)EFDataSourceLayer.MessageGateway.SelectIds(string senderUsername, string connectionUsername, bool deleted)");
         throw;
     }
 }
 ///<summary>
 ///Возвращает сообщение по его идентификатору
 ///</summary>
 ///<param name="id"></param>
 ///<param name="connectionUsername"></param>
 ///<returns></returns>
 public static Message Select(int id, string connectionUsername)
 {
     try
     {
         CustomDbContext dc     = Gateway.GetContext(connectionUsername);
         Message         result = dc.Messages.FirstOrDefault(row => row.Id == id);
         return(TranslateToDepletedMessageObject(result));
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "(Message)EFDataSourceLayer.MessageGateway.Select(int id, string connectionUsername)");
         throw;
     }
 }
 public static void Update(int id, string connectionUsername)
 {
     try
     {
         CustomDbContext dc     = Gateway.GetContext(connectionUsername);
         Message         result = dc.Messages.FirstOrDefault(row => row.Id == id);
         result.LastUpdate = DateTime.Now;
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "()EFDataSourceLayer.MessageGateway.SelectIds(string senderUsername, string connectionUsername, bool deleted)");
         throw;
     }
 }
        public static List <string> SelectIds(string connectionUsername)
        {
            List <string> result = new List <string>();

            try
            {
                CustomDbContext dc = Gateway.GetContext(connectionUsername);
                return(dc.Employees.Select(row => row.Username).ToList());
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleExcepion(ex, "(Employee)EFDataSourceLayer.EmployeeGateway.SelectIds(string connectionUsername)");
                throw;
            }
        }
 public static void Delete(int id, string connectionUsername)
 {
     try
     {
         CustomDbContext dc     = Gateway.GetContext(connectionUsername);
         Message         result = dc.Messages.FirstOrDefault(row => row.Id == id);
         dc.Messages.Remove(result);
         dc.SaveChanges();
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "()EFDataSourceLayer.MessageGateway.Delete(int id, string connectionUsername)");
         throw;
     }
 }
 /// <summary>
 /// Производит вставку письма в таблицу
 /// </summary>
 public static int Insert(Message message, string connectionUsername)
 {
     try
     {
         Message         depletedMessage = TranslateToDepletedMessageObject(message);
         CustomDbContext dc = Gateway.GetContext(connectionUsername);
         dc.Messages.Add(depletedMessage);
         dc.SaveChanges();
         return((int)depletedMessage.Id);
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "(int)EFDataSourceLayer.MessageGateway.Insert(Message message, string username)");
         throw;
     }
 }
 /// <summary>
 /// Добавляет нового адресата к письму
 /// </summary>
 /// <param name="recipient"></param>
 public static void Insert(Recipient recipient, string connectionUsername)
 {
     try
     {
         Recipient       depletedRecipient = TranslateToDepletedRecipientObject(recipient);
         CustomDbContext dc = Gateway.GetContext(connectionUsername);
         dc.Recipients.Add(depletedRecipient);
         dc.SaveChanges();
         MessageGateway.Update((int)recipient.MessageId, connectionUsername);
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "()DataSourceLayer.RecipientGateway.Insert(Recipient recipient, string username)");
         throw;
     }
 }
 /// <summary>
 /// Обновляет флаг прочитанности у строки таблицы Recipient
 /// </summary>
 /// <param name="username"></param>
 /// <param name="messageId"></param>
 /// <param name="viewed"></param>
 public static void Update(string recipientUsername, int messageId, bool viewed, string connectionUsername)
 {
     try
     {
         CustomDbContext dc     = Gateway.GetContext(connectionUsername);
         Recipient       result = dc.Recipients.FirstOrDefault(row => string.Compare(row.RecipientUsername, recipientUsername) == 0 &&
                                                               row.MessageId == messageId);
         result.Viewed = viewed;
         dc.SaveChanges();
         MessageGateway.Update(messageId, connectionUsername);
     }
     catch (Exception ex)
     {
         ExceptionHandler.HandleExcepion(ex, "()EFDataSourceLayer.RecipientGateway.Update(string recipientUsername, int messageId, bool viewed, string connectionUsername)");
         throw;
     }
 }
        /// <summary>
        /// Возвращает все строки таблицы Recipient с данным идентификатором сообщения
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List <Recipient> Select(int messageId, string connectionUsername)
        {
            List <Recipient> rows = new List <Recipient>();

            try
            {
                CustomDbContext  dc     = Gateway.GetContext(connectionUsername);
                List <Recipient> result = dc.Recipients.Where(row => row.MessageId == messageId).ToList();
                foreach (Recipient item in result)
                {
                    rows.Add(TranslateToDepletedRecipientObject(item));
                }
                return(rows);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleExcepion(ex, "(List<Recipient>)DataSourceLayer.RecipientGateway.Select(int messageId, string connectionUsername)");
                throw;
            }
        }
        /// <summary>
        /// Возвращает отправленные сообщения
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List <Message> Select(string senderUsername, string connectionUsername, bool deleted)
        {
            List <Message> rows = new List <Message>();

            try
            {
                CustomDbContext dc     = Gateway.GetContext(connectionUsername);
                List <Message>  result = dc.Messages.Where(row => string.Compare(row.SenderUsername, senderUsername) == 0 &&
                                                           row.Deleted == deleted).ToList();
                foreach (Message item in result)
                {
                    rows.Add(TranslateToDepletedMessageObject(item));
                }
                return(rows);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleExcepion(ex, "(List<Message>)EFDataSourceLayer.MessageGateway.Select(string senderUsername ,string connectionUsername, bool deleted)");
                throw;
            }
        }
        /// <summary>
        /// Возвращает все строки таблицы Recipient с данным адресатом и флагом удаления
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List <Recipient> Select(string recipientUsername, string connectionUsername, bool deleted, bool viewed)
        {
            List <Recipient> rows = new List <Recipient>();

            try
            {
                CustomDbContext  dc     = Gateway.GetContext(connectionUsername);
                List <Recipient> result = dc.Recipients.Where(row => string.Compare(row.RecipientUsername, recipientUsername) == 0 &&
                                                              row.Deleted == deleted &&
                                                              row.Viewed == viewed).ToList();
                foreach (Recipient item in result)
                {
                    rows.Add(TranslateToDepletedRecipientObject(item));
                }
                return(rows);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleExcepion(ex, "(List<Recipient>)EFDataSourceLayer.RecipientGateway.Select(string recipientUsername, string connectionUsername, bool deleted, bool viewed)");
                throw;
            }
        }