/// <summary> /// Gets a message from the repository by the given ID /// </summary> /// <param name="aID">The integer identifier for the message</param> /// <returns>A Message object or Null</returns> public Model.Message GetByID(int aID) { if (aID <= 0) { throw new ArgumentException("Cannot search for a Message Object with a non positive ID", "aID"); } Message result = null; using (DataModel.VoxAuditingEntities context = new DataModel.VoxAuditingEntities(connectionString)) { var selectResult = from m in context.Messages where m.MessageID == aID select m; // Check the results. Can only have one result if (selectResult.Count() == 1) { DataModel.Message selectedMessage = selectResult.First(); result = new Message(selectedMessage.MessageID, selectedMessage.MessageNumber) { ClientID = selectedMessage.MessageClientID, MessageDateTime = selectedMessage.MessageDateTime, BatchID = selectedMessage.BatchID, MessageApplicationID = selectedMessage.MessageApplicationID }; } } return(result); }
/// <summary> /// Adds the message into the SQL database /// </summary> /// <param name="entity">A new message to add</param> /// <returns>True if the message was added</returns> public bool Add(Model.Message entity) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity.ID != 0) { throw new InvalidMessageException("Cannot add a message which has already been added!"); } if (entity.ClientID == 0) { throw new InvalidMessageException("Cannot add a message with no valid client!"); } if (entity.BatchID == 0) { throw new InvalidMessageException("Cannot add a message with no valid batch!"); } bool added = false; // Check if the batch is valid first using (DataModel.VoxAuditingEntities context = new DataModel.VoxAuditingEntities(connectionString)) { DataModel.Message newMessage = new DataModel.Message() { MessageNumber = entity.Name, BatchID = entity.BatchID, MessageDateTime = entity.MessageDateTime, MessageApplicationID = entity.MessageApplicationID, MessageClientID = entity.ClientID }; context.Messages.AddObject(newMessage); added = context.SaveChanges() != 0; if (added) { AddMessageToDictionary(entity); entity.ID = newMessage.MessageID; } } return added; }
/// <summary> /// Adds the message into the SQL database /// </summary> /// <param name="entity">A new message to add</param> /// <returns>True if the message was added</returns> public bool Add(Model.Message entity) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity.ID != 0) { throw new InvalidMessageException("Cannot add a message which has already been added!"); } if (entity.ClientID == 0) { throw new InvalidMessageException("Cannot add a message with no valid client!"); } if (entity.BatchID == 0) { throw new InvalidMessageException("Cannot add a message with no valid batch!"); } bool added = false; // Check if the batch is valid first using (DataModel.VoxAuditingEntities context = new DataModel.VoxAuditingEntities(connectionString)) { DataModel.Message newMessage = new DataModel.Message() { MessageNumber = entity.Name, BatchID = entity.BatchID, MessageDateTime = entity.MessageDateTime, MessageApplicationID = entity.MessageApplicationID, MessageClientID = entity.ClientID }; context.Messages.AddObject(newMessage); added = context.SaveChanges() != 0; if (added) { AddMessageToDictionary(entity); entity.ID = newMessage.MessageID; } } return(added); }