This is my mvp impl. where I wrong?
IMainView = interface
//guid
property caption : string read write;
end;
IPresenter = interface
// guid
procedure bindView(const view: IMainView);
procedure setFormCaption(const msg : string);
end;
// mainView.pas
TMainView = class (TInjectable, IMainView)
private presenter : IPresenter;
private procedure setFormCaption();
public procedure FormCreate(Sender: TObject);
end;
Implementation
public procedure FormCreate(Sender: TObject)
begin
presenter.bindView(self);
end;
procedure TMainView.setFormCaption()
begin
presenter.setFormCaption('Test caption');
end;
initialization
GetDIRegistry.RegisterFactorySingleton(IMainView, TMainView);
// presenter.pas
TPresenter = class (TInjectable, IPresenter)
private mainView : IMainView;
public procedure setFormCaption(const msg : string);
public procedure bindView(const view: IMainView);
end;
Implementation
procedure TPresenter.bindView
begin
mainView := view;
end;
procedure TPresenter.setFormCaption
begin
mainView.caption := msg;
end;
initialization
GetDIRegistry.RegisterFactorySingleton(IPresenter, TPresenter);
The issue is occure when I close mainView. There is a EAccessViolation
exception due to when presenter is destroy the main view destroy too.
Maybe its a worg reason. Sorry for my bad english.
First, I apologize for you long wait. I have been working a lot :(
I have finally analysed your code. I noticed you have some circular
dependency among your classes, that is:
Object A has a reference to object B, while object B has a reference
to object A. Delphi uses a reference counting mechanism to handle the
life-cycle of interfaces, and reference-counting does not work with
circular dependency. This is not an Emballo issue, this is a
characteristic of reference-counting.
I commented the line 45 of your mainViewPresenterImpl and it solved
all of the erros when closing the application, but of course it will
prevent your application from working.
Try to redesign your classes to avoid circular reference (the best
solution), or use weak-reference
Ive found this problem too. The solution was like so:
I introduced new object. thus object A(main view) has a reference to
object B(presenter), object B (presenter) has reference to object
C(main view interfaced object), object C has reference to object
A(main view).
In a mainViewModule .pas:
unit mainViewImpl;
type
TForm1 = class(TForm)
preseneter: IPreseneter;
...
end;
TMainView = class(TInterfacedObject, IMainView)
private
{ Private declarations }
fView: TForm1;
protected
IStatusBar: IGUIStatusBar;
IWorkspace: IGUIWorkspace;
public
{ Public declarations }
constructor create();
destructor destroy(); override;
function getObject(): TForm;
function getStatusBar(): IGUIStatusBar;
function getWorkspace(): IGUIWorkspace;
end;
When the main form create there is a injectdependences, and when
TMainView create there is a injectdependences too.
I dont like this solution, but it work.
I want to create mvp template for delphi. When I'll create new
application i'll able to generate new mvp app from this template.