private void Apply(AssessmentReminderUpdatedEvent assessmentReminderUpdatedEvent) { var propertyName = assessmentReminderUpdatedEvent.Property; var value = assessmentReminderUpdatedEvent.Value; if (_propertyCache == null) { _propertyCache = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy).ToDictionary(pi => pi.Name); } var property = _propertyCache.ContainsKey(propertyName) ? _propertyCache[propertyName] : null; if (property == null) { throw new InvalidOperationException(string.Format("Invalid property name {0}", propertyName)); } if (value != null && !property.PropertyType.IsInstanceOfType(value)) { var convertToType = GetConvertToType(property.PropertyType); if (value is string) { value = TypeDescriptor.GetConverter(convertToType).ConvertFromInvariantString(value.ToString()); } else { value = Convert.ChangeType(value, convertToType); } } property.SetValue(this, value); }
/// <summary> /// Handles the specified message. /// </summary> /// <param name="message">The message.</param> public void Handle(AssessmentReminderUpdatedEvent message) { if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.PatientKey)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [PatientKey] = @PatientKey, [PatientFirstName] = (SELECT FirstName FROM [PatientModule].[Patient] WHERE PatientKey = @PatientKey), [PatientLastName] = (SELECT LastName FROM [PatientModule].[Patient] WHERE PatientKey = @PatientKey) WHERE [RecurrenceKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, PatientKey = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.CreatedByStaffKey)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [CreatedByStaffKey] = @CreatedByStaffKey WHERE [RecurrenceKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, CreatedByStaffKey = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, string>(a => a.Title)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [Title] = @Title WHERE [RecurrenceKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Title = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, DateTime>(a => a.Start)) { CreateAssessmentReminders(message.Key); } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, string>(a => a.Description)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [Description] = @Description WHERE [RecurrenceKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Description = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.AssessmentDefinitionKey)) { using (var connection = _connectionFactory.CreateConnection()) { var assessmentDefinition = _assessmentDefinitionRepository.GetByKey(Guid.Parse(message.Value.ToString())); if (assessmentDefinition != null) { connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [AssessmentDefinitionKey] = @AssessmentDefinitionKey, [AssessmentName] = @AssessmentName, [AssessmentCode] = @AssessmentCode WHERE [RecurrenceKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, AssessmentDefinitionKey = message.Value.ToString(), AssessmentName = assessmentDefinition.CodedConcept.Name, AssessmentCode = assessmentDefinition.CodedConcept.Code }); } } } // todo: need to check whether the records should be updated based on the recurrencekey if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, DateTime?> (a => a.AlertSentDate)) { using (var connection = _connectionFactory.CreateConnection()) { var alertSentDate = (DateTime?)(message.Value); connection.Execute( @"UPDATE [MessageModule].[AssessmentReminder] SET [AlertSentDate] = @AlertSentDate WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, AlertSentDate = alertSentDate.HasValue ? alertSentDate.Value.ToString() : "NULL" }); } } }
public void Handle(AssessmentReminderUpdatedEvent message) { if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.PatientKey)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [PatientKey] = @PatientKey, [PatientFirstName] = (SELECT FirstName FROM [PatientModule].[Patient] WHERE PatientKey = @PatientKey), [PatientLastName] = (SELECT LastName FROM [PatientModule].[Patient] WHERE PatientKey = @PatientKey) WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, PatientKey = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.CreatedByStaffKey)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [CreatedByStaffKey] = @CreatedByStaffKey WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, CreatedByStaffKey = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, string>(a => a.Title)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [Title] = @Title WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Title = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, DateTime>(a => a.Start)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [Start] = @Start WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Start = ((DateTime)(message.Value)).ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, string>(a => a.Description)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [Description] = @Description WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Description = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, AssessmentReminderStatus>(a => a.Status)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [Status] = @Status WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, Status = ((AssessmentReminderStatus)(message.Value)).ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, Guid>(a => a.AssessmentDefinitionKey)) { using (var connection = _connectionFactory.CreateConnection()) { connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [AssessmentDefinitionKey] = @AssessmentDefinitionKey, [AssessmentName] = (SELECT AssessmentName FROM [AssessmentModule].[AssessmentDefinition] WHERE [AssessmentDefinitionKey] = @AssessmentDefinitionKey), [AssessmentCode] = (SELECT AssessmentCode FROM [AssessmentModule].[AssessmentDefinition] WHERE [AssessmentDefinitionKey] = @AssessmentDefinitionKey) WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, AssessmentDefinitionKey = message.Value.ToString() }); } } if (message.Property == PropertyUtil.ExtractPropertyName <AssessmentReminder, DateTime?>(a => a.AlertSentDate)) { using (var connection = _connectionFactory.CreateConnection()) { var alertSentDate = (DateTime?)(message.Value); connection.Execute(@"UPDATE [MessageModule].[AssessmentReminder] SET [AlertSentDate] = @AlertSentDate WHERE [AssessmentReminderKey] = @AssessmentReminderKey", new { AssessmentReminderKey = message.Key, AlertSentDate = alertSentDate.HasValue ? alertSentDate.Value.ToString() : "NULL" }); } } }