public void Save(SavedCommand command, bool isNew) { using (var connection = new SqlConnection(DatabaseConnectionString)) { if (isNew) { InsertCommand(command, connection); } else { UpdateCommand(command, connection); } } }
private void InsertCommand(SavedCommand c, SqlConnection connection) { var serializedCommand = c.Serialize(); const string query = @" INSERT INTO logs.Command ( AggregateIdentifier, ExpectedVersion, CommandIdentifier, CommandClass, CommandType, CommandData, IdentityTenant, IdentityUser, SendStatus, SendError, SendScheduled, SendStarted, SendCompleted, SendCancelled ) VALUES ( @AggregateIdentifier, @ExpectedVersion, @CommandIdentifier, @CommandClass, @CommandType, @CommandData, @IdentityTenant, @IdentityUser, @SendStatus, @SendError, @SendScheduled, @SendStarted, @SendCompleted, @SendCancelled )"; using (var command = new SqlCommand(query, connection)) { var parameters = command.Parameters; parameters.AddWithValue("AggregateIdentifier", serializedCommand.AggregateIdentifier); parameters.AddWithValue("ExpectedVersion", serializedCommand.ExpectedVersion ?? (object)DBNull.Value); parameters.AddWithValue("CommandIdentifier", serializedCommand.CommandIdentifier); parameters.AddWithValue("CommandClass", serializedCommand.CommandClass); parameters.AddWithValue("CommandType", serializedCommand.CommandType); parameters.AddWithValue("CommandData", serializedCommand.CommandData); parameters.AddWithValue("IdentityTenant", serializedCommand.IdentityTenant); parameters.AddWithValue("IdentityUser", serializedCommand.IdentityUser); parameters.AddWithValue("SendStatus", (object)serializedCommand.SendStatus ?? DBNull.Value); parameters.AddWithValue("SendError", (object)serializedCommand.SendError ?? DBNull.Value); parameters.AddWithValue("SendScheduled", (object)serializedCommand.SendScheduled ?? DBNull.Value); parameters.AddWithValue("SendStarted", (object)serializedCommand.SendStarted ?? DBNull.Value); parameters.AddWithValue("SendCompleted", (object)serializedCommand.SendCompleted ?? DBNull.Value); parameters.AddWithValue("SendCancelled", (object)serializedCommand.SendCancelled ?? DBNull.Value); try { command.Connection.Open(); command.ExecuteNonQuery(); } catch (SqlException ex) { throw new SqlInsertException($"The command ({serializedCommand.CommandType}) could not be saved.", ex); } } }
/// <summary> /// Returns a serialized command. /// </summary> public static SerializedCommand Serialize(this SavedCommand commandSchedule) { var serializer = new Serializer(); var data = serializer.Serialize(commandSchedule.Command, new[] { "AggregateIdentifier", "AggregateVersion", "IdentityTenant", "IdentityUser", "CommandIdentifier", "SendScheduled", "SendStarted", "SendCompleted", "SendCancelled" }); var serialized = new SerializedCommand { AggregateIdentifier = commandSchedule.Command.AggregateIdentifier, ExpectedVersion = commandSchedule.Command.ExpectedVersion, CommandClass = serializer.GetClassName(commandSchedule.Command.GetType()), CommandType = commandSchedule.Command.GetType().Name, CommandData = data, CommandIdentifier = commandSchedule.Command.CommandIdentifier, IdentityTenant = commandSchedule.Command.IdentityTenant, IdentityUser = commandSchedule.Command.IdentityUser, SendStatus = commandSchedule.SendStatus, SendError = commandSchedule.SendError, SendScheduled = commandSchedule.SendScheduled, SendStarted = commandSchedule.SendStarted, SendCompleted = commandSchedule.SendCompleted, SendCancelled = commandSchedule.SendCancelled, }; if (serialized.CommandClass.Length > 200) { throw new OverflowException($"The assembly-qualified name for this command ({serialized.CommandClass}) exceeds the maximum character limit (200)."); } if (serialized.CommandType.Length > 100) { throw new OverflowException($"The type name for this command ({serialized.CommandType}) exceeds the maximum character limit (100)."); } if ((serialized.SendStatus?.Length ?? 0) > 20) { throw new OverflowException($"The send status ({serialized.SendStatus}) exceeds the maximum character limit (20)."); } return(serialized); }
/// <summary> /// Returns a deserialized command. /// </summary> public static SavedCommand Deserialize(this SerializedCommand serializedCommand) { var serializer = new Serializer(); var data = serializer.Deserialize <ICommand>(serializedCommand.CommandData, Type.GetType(serializedCommand.CommandClass)); data.AggregateIdentifier = serializedCommand.AggregateIdentifier; data.ExpectedVersion = serializedCommand.ExpectedVersion; data.CommandIdentifier = serializedCommand.CommandIdentifier; data.IdentityTenant = serializedCommand.IdentityTenant; data.IdentityUser = serializedCommand.IdentityUser; var savedCommand = new SavedCommand(data) { SendStatus = serializedCommand.SendStatus, SendError = serializedCommand.SendError, SendScheduled = serializedCommand.SendScheduled, SendStarted = serializedCommand.SendStarted, SendCompleted = serializedCommand.SendCompleted, SendCancelled = serializedCommand.SendCancelled, }; return(savedCommand); }
private void UpdateCommand(SavedCommand c, SqlConnection connection) { const string query = @" UPDATE logs.Command SET SendScheduled = @SendScheduled, SendStarted = @SendStarted, SendCompleted = @SendCompleted, SendCancelled = @SendCancelled, SendStatus = @SendStatus, SendError = @SendError WHERE CommandIdentifier = @CommandIdentifier "; using (var command = new SqlCommand(query, connection)) { var parameters = command.Parameters; parameters.AddWithValue("CommandIdentifier", c.Command.CommandIdentifier); parameters.AddWithValue("SendScheduled", (object)c.SendScheduled ?? DBNull.Value); parameters.AddWithValue("SendStarted", (object)c.SendStarted ?? DBNull.Value); parameters.AddWithValue("SendCompleted", (object)c.SendCompleted ?? DBNull.Value); parameters.AddWithValue("SendCancelled", (object)c.SendCancelled ?? DBNull.Value); parameters.AddWithValue("SendStatus", (object)c.SendStatus ?? DBNull.Value); parameters.AddWithValue("SendError", (object)c.SendError ?? DBNull.Value); try { command.Connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { // TODO: Where to get the command name // {/*c.CommandType*/} throw new Exception($"The command () could not be saved.", ex); } } }
public SavedCommandSelectedEventArgs(SavedCommand cmd) { Command = cmd; }
private void OnSavedCommandSelected(SavedCommand cmd) { RaiseEvent(new RoutedEventArgs(SavedCommandSelectedEvent)); }