So I have a database (sql) management program I have been working on, dealing with items my company sells. For the main part of the program the item information is updated as the user goes through using some custom controls. That all works fine. But now I am on to the higher level section of the program that is adding/deleting/copying items. This cannot be "update on the fly", like editing item information, as it would be too easy to screw things up very badly. So I basically want to set up a file-> save type of deal for them here. How I see this working is creating all of the sql commands in a list and then running them all when saving.
The functions available here would be adding a new item, deleting an item, copying from another item, and renaming.
So it would be a list like:
insert...
update...
update...
delete...
update...
etc.
It would run through and execute all of the commands. The problem I am having is I already have a class that has methods to handle all of these functions. I can't think of a way to call all of those methods in a queue, and rewriting all the sql statements in another class seems stupid to me. Is there a way that I can remember a list of something like:
item.Rename()
item.ChangeSize()
item.Delete()
I feel like I'm overthinking this... or maybe underthinking... I don't know.
What you are looking for is a Command Pattern
.
The basic idea is that you have an ICommand
Interface which has a method Execute()
.
Then you implement multiple ConcreteCommand
, e.g. a RenameCommand
with a parameterized constructor or a property NewName
.
Dont't forget to handle exceptions accordingly and maybe implement an Undo-Method as well, if these could occur. I don't know enough about your application to make assumptions here.
public interface ICommand
{
void Execute();
}
public class RenameCommand : ICommand
{
private string newName;
public RenameCommand(string newName)
{
this.newName = newName;
}
void ICommand.Execute()
{
item.rename(newName);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With