Model View Presenter: how to use with a command [message #486816] |
Sat, 19 September 2009 09:41  |
Eclipse User |
|
|
|
Hello everyone
I am still working on a reference implementation for myself using the
model-view-controller pattern in a rcp. So I set up a couple of projects
rcp.mvp.view // the actual rcp
rcp.mvp.presenter
rcp.mvp.data // eclipselink and DAOs
rcp.mvp.data.mysql // fragment providing the mysql driver
rcp.mvp.model // just my model pojos
My question now gos for the view and the presenter. According to what I know
about MVP, when a view (that implements a interface the presenter also knows)
gets created, it creates a presenter object and passes itself to it (so the
presenter can call the view as well, using the interface).
So now my eclipse-views are having a reference a particular presenter.
Now comes the problem: when I use a command, I specify a command handler which
executes the command. This is usually a separate class that has now reference to
my view.
How can I call my presenter from the commandhandler, in a best-practise way,
since I dont want to have any businesslogic in my view-project?
The only idea I got, is making my view implementing IHandler2 and use it as
commandhandler for all commands that can occur for it. But this doesnt sound
right to me.
regards
Marc
|
|
|
|
|
Re: Model View Presenter: how to use with a command [message #487099 is a reply to message #486928] |
Mon, 21 September 2009 16:53  |
Eclipse User |
|
|
|
Daniel Krügler schrieb:
>
> You might consider to take advantage of the ISourceProvider:
>
> http://blog.eclipse-tips.com/2009/02/commands-part-5-authent ication-in-rcp.html
>
>
> You can access this via the ISourceProviderService inside the
> AbstractHandler.execute method.
>
> HTH && Greetings from Bremen,
>
> Daniel Krügler
>
Thanks for the idea. I implemented a SourceProvider as follows
public class PresenterSourceProvider extends AbstractSourceProvider {
public static final String PRESENTERS = "rcp.mvp.view.presenters";
private Map <String, Object>presenters;
public PresenterSourceProvider() {
presenters = new HashMap<String, Object>();
}
@SuppressWarnings("unchecked")
@Override
public Map getCurrentState() {
return presenters;
}
@Override
public String[] getProvidedSourceNames() {
return new String[]{PRESENTERS};
}
public void addPresenter(String id, Object o){
presenters.put(id, o);
}
public Object getPresenter(String id){
return presenters.get(id);
}
public void removePresenter(String id){
presenters.remove(id);
}
@Override
public void dispose() {
}
}
In the Handler
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
ISourceProviderService service = (ISourceProviderService)
window.getService(ISourceProviderService.class);
PresenterSourceProvider presenterSourceProvider = (PresenterSourceProvider)
service.getSourceProvider(PresenterSourceProvider.PRESENTERS );
CustomerPresenter p =
(CustomerPresenter)presenterSourceProvider.getPresenter(Cust omerExplorerView.ID);
p.createNewCustomer();
In the view I am overriding dispose, so the presenter can be removed from the
sourceprovider when the view gets destroyed.
This works, but its quite a lot of work to get a simple reference.
As far as I understand, the SourceProviders are more important when you want to
use Expressions with the defined variables.
A probably easier way would be to define a global (thread-safe) singleton with
the same Map. But I doubt that this would be best-practise since I am reading a
lot about "bad" singeltons.
It would be really nice to get some feedback from people that have successfully
adopted the MVP together with the command framework.
In my opinion it is important to have a easy-to-understand solution, so that in
a team, new developers can work right away with the designed structure. The
SourceProvider approach seems more difficult (create presenter in view, add to
the sourceprovider, dont forget to remove it on view-dispose, ...).
regards
Marc
|
|
|
Powered by
FUDForum. Page generated in 0.03129 seconds