public void MarkEventMessagesForDelivery() { //Another spot for dependency injection //1. Select all messages that must be sent based on an event //Find all users who are trying to send a message based on an event happening var users = _userRepository.FindUsersWithEventBasedMessages(); foreach (AppUser user in users) { //Set the death messages for delivery var deathMessages = user.Message.Where(m => null != m.EventType && m.EventType.Description == "Death").ToList();; //If the user has some death messages and we haven't already determined that he/she is deceased if (null == user.Profile.DeceasedDate && 0 < deathMessages.Count()) { //Schedule all the death messages to be delivered foreach (Message msg in deathMessages) { msg.ScheduleDate = DateTime.Today.AddDays(Convert.ToDouble(msg.EventScheduleOffsetDays)); } } else { //Zero results found -- not verified that this is where it will go } //Set the incapacitation messages var incapMessages = user.Message.Where(m => null != m.EventType && m.EventType.Description == "Incapacitation").ToList();; if (null == user.Profile.IncapacitationDate && 0 < incapMessages.Count()) { //Schedule all the death messages to be delivered foreach (Message msg in incapMessages) { msg.ScheduleDate = DateTime.Today.AddDays(Convert.ToDouble(msg.EventScheduleOffsetDays)); } } else { //Zero results found -- not verified that this is where it will go } } _userRepository.Save(); }