My understanding of the Command Pattern is that you simply have 1 virtual method 'execute()', and all dependencies that an implementation might have are put in the constructor or through setter injection in the implementation (like discussed here).
However, in the WPF implementation of the pattern, I noticed they are passing a generic argument to the execute() function (explained here).
This seems like a pollution of the interface to me, what would have been the motivation to add a generic parameter to the execute() function?
The canonical command pattern is usually illustrated with nice self-contained commands. In that any information needed by the command is stashed away within the Command object instance (typically via a parameterized constructor).
However in some cases, the parameters needed for Execute may not be available at command-creation time (are known only at runtime). e.g. Imagine a SignOutCommand( username ). The username is determined when the user clicks on the SignOut button after signing in first.
So username is passed in as a generic parameter to Command.Execute(); Each command is free to define its input and cast accordingly e.g. an arbitrary command can require 5 parameters as an object[].
It's for databinding. When you bind the command to every object in a list, for example, the current instance is sent to the execute method so that you don't have to keep track of the current instance yourself.
That said, I don't think that the WPF command notion is an implementation of the command pattern, they just share terminology.
The reason behind that parameter is the isolation between the creator of the command - who know what command needs to be executed, and the caller - who knows when a command need to be executed.
In certain commands some of the information that needed for the execution is not available to the creator. The caller the fills in the blank by passing a parameter to execute. An example: The creator creates a command that filters a list of records according to some criteria. The list is not available at the creation site, as there are many kinds of lists in the application.
The caller will specify which list needs to be filtered by passing at as a parameter.
We use a bit changed command pattern, so that in addition to Execute method we have two properties Request and Response and we parametrize them using polymorphism.
What's wrong with:
public class DeleteCommand : BaseCommand
{
private Dictionary<string, object> parameters;
public DeleteCommand(Dictionary<string, object> parameters)
{
this.parameters = parameters;
}
public void Execute()
{
var person = (Person)parameters["Person"];
var salary = System.Convert.ToDouble(parameters["Salary"]);
// etc.
}
}
Now if you have a controller that collects parameters you can pass those through to your commands.
Related
I try to uinderstand how method chaining (calling previous or default implementation) works (https://github.com/Microsoft/ConcordExtensibilitySamples/wiki/Component-discovery-and-configuration) but still have many doubts.
DkmExample seems to be dispatcher, but how this class is defined ? Can I define such class ?
Concord interfaces (Microsoft.VisualStudio.Debugger.ComponentInterfaces) do not pass dispatcher in their methods, so how it can be added in implementation ? Do any Concord interface can be chained this way ?
can anyone show how to implement method chaining in https://github.com/microsoft/ConcordExtensibilitySamples/tree/master/HelloWorld/Cs (show how to call default implementation), or provide other real exapmle ?
Thanks in advance
In the Method Chaining Example IDkmExample represents an interface that is part of the Concord API. This is the interface you, as the developer, are implementing. In the method call itself, DkmExample is not the dispatcher but rather a dispatcher object that the dispatcher knows how to handle. These are defined by the dispatcher and cannot be defined externally.
The method chaining example is there to show that if the implementation does not want to handle the call of the interface method, then it can call the method of the same name on the dispatcher object (first item in the API method signature), passing in all the parameters that are taken by the method signature minus the dispatcher object itself. This will allow the dispatcher to pass the call, based on filtering and priority, to the next implementation of the interface that it can find.
For a concrete example, we can look at the following block from the Microsoft.VisualStudio.Debugger.Engine.xml from the microsoft.visualstudio.debugger.engine nuget package:
<member name="M:Microsoft.VisualStudio.Debugger.ComponentInterfaces.IDkmStartDebuggingOperations.LaunchDebuggedProcess(Microsoft.VisualStudio.Debugger.Start.DkmProcessLaunchRequest)">
<summary>
Causes the debug monitor to create a new process under the debugger. The process
should be left suspended until ResumeDebuggedProcess is called. The debug monitor
must wait for ResumeDebuggedProcess before creating the DkmProcess object since
it needs the UniqueProcessId value from the AD7 Layer.
Note that this method may only be called in response to the Visual Studio
debugger package requesting a launch. Components that wish to launch another
process under the debugger should send a custom event to a visual studio package.
From a package, a launch can be requested through the
IVsDebugger.LaunchDebugTargets API.
</summary>
<param name="request">
[In] DkmProcessLaunchRequest is used to describe the process that debugger should
launch.
</param>
<returns>
[Out] DkmLaunchedProcessInfo is returned from APIs that launch a process.
</returns>
</member>
The interface we are overriding is IDkmStartDebuggingOperations and the method is LaunchDebuggedProcess which in the implementation will take a DkmProcessLaunchRequest, which is a dispatcher object. If the implementation does not want to handle the call, it can call the next implementation by taking the dispatcher object and calling the method of the same name on it, passing the necessary parameters.
For example:
internal class MyStartDebuggingOperations : IDkmStartDebuggingOperations
{
public DkmLaunchedProcessInfo LaunchDebuggedProcess(DkmProcessLaunchRequest request)
{
if (/* custom check that this class is to handle it */)
{
// Handle custom implementation here
}
else
{
// This calls the base implementation
return request.LaunchDebuggedProcess();
}
}
}
I've been trying to find some info on difference between instantiating form fields through static method and the new keyword. Can somebody tell me what are the practical implications, limitations, between new MyFormField and MyFormField::create() esp. with regards to SilverStripe
Using the create factory method would check for overloads (set via Object::useCustomClass()) and return an instance of the custom class in that case.
This method first for strong class overloads (singletons & DB
interaction), then custom class overloads. If an overload is found, an
instance of this is returned rather than the original class. To
overload a class, use Object::useCustomClass()
So using the create method rather than instantiating the Object yourself would provide a possibility to overload the used Class without altering the code.
see
http://api.silverstripe.org/3.1/class-Object.html#_useCustomClass
http://api.silverstripe.org/3.1/class-Object.html#_create
I've been working on a WPF application that involves moving many shapes. It is mostly MVVM and relies heavily on commands. I had not worried about Undo/Redo until just recently. I don't think it will be too difficult since most of my changes involve commands that inherit a base class CommandBase that implements ICommand.
So far I have added another Interface named IUndoCommand that uses ICommand. I have added an Undo method that will perform the operations needed when an undo is called.
I will be using a stack for both Undo and Redo, but I'm running in to an issue with the parameters for the Execute/Undo methods. Is there a proper way to store these parameters of type object? Is it advisable to add a field/method to IUndoCommand? If so should I set it in the Execute method or in a constructor (if I even can.)
If not should I pass it as it's own object in the Stack?
Secondly, (although this can probably be it's own question) is there a better data structure to track multiple commands? I currently have a loop that runs multiple commands to move multiple selected shapes and would like to allow one undo to undo them all. I guess I could convert this to a command on it's own and pass commands to it, but again I'm new to this and would rather do it right.
Thanks for reading and any help would be greatly appreciated.
Sources:
Code Project
VisualStudioMagazine
StackOverFlow
Since the interface doesn't need access to the data (it should just need an Undo()/Redo() method pair, and potentially a flag for whether it can undo), it doesn't need to know about the parameters at all.
One option might be to make your implementation of IUndoCommand generic. You could then use this to store the parameter in a type-safe manner.
Your CommandBase class could then be generic, ie:
class CommandBase<T> : ICommand, IUndoCommand
{
// You could then store the parameter directly...
public T Parameter { get; private set; }
}
I've asked a question here couple days ago, about how to avoid a function to be called from the browser. Some nice people pointed out that if put an underscore before de name of the function, it'll only accept calls from inside app. But, by doing that, I cannot call the function from an element (by using requestAction). Is there another way to do it? Thanks!
You can't call a private or protected method from outside it's class; (that's the whole point of private and protected methods!)
Your only option, unless I'm mistaken, is to make the method public, or call it from within another method, perhaps with an ambiguous method name if you are worried about a user accidentally calling it.
You can also check to see if the request was made via requestAction using:
if (!empty($this->params['requested'])) {
//requestAction was used, requested is set to 1
}
So you could use this in a public method, and do the appropriate action depending on whether it was requested or not.
Is it possible to pass a collection of objects to a RIA Data Service query? I have no issues sending an Entity, an Int or an array of primitive types, but as soon as i declare a method like this
public void GetLessonsConflicts(Lesson[] lessons)
{
}
i get a compilation error
" Operation named
'GetLessonsConflicts' does not conform
to the required signature. Parameter
types must be an entity type or one of
the predefined serializable
types"
I am just trying to do some validation on the server side before i save the data. I've tried List, IEnumerable etc.
Thanks
I think the problem is actually the lack of a return value. As I understand it, you can identify DomainOperations by convention or by attribute. You're not showing an attribute so RIA will be trying to match it by convention.
For example, by convention, an insert method must:
have Insert, Add or Create as the method name prefix, e.g. InsertEmployee
match the signature public void name(Entity e);
a query method must:
be public
return IEnumerable, IQueryable or T (where T is an entity).
a custom domain operation must
be public
return void
have an Entity as the first parameter.
EDIT: See Rami A's comment below. I believe this was true at the time but I'm not currently working with this technology so I'm not current enough on it to update this answer other than to note that it may be incorrect.
Or you can use Attributes such as [Insert],[Delete],[Update],[Query],[Custom]. From my docs, all the attributes do is remove the requirement for the name convention - it's not clear from them, to me, what the [Query] and [Custom] attributes achieve.
As well as DomainOperations, you can define ServiceOperations (using the [ServiceOperation] attribute) and InvokeOperations.
This article might help (although I think it's a bit out of date).