Introduction
This web page tells you how to add a model to the presenter.
It assumes you have already created your basic project and also your top level
model.
Steps to add a model
A class level reference to the model needs to be stored in
each presenter. The reason, for clarity, the model is not passed around to each
underlying method from the main control function. Instead the control function
asks the presenter to provide a copy of its model, and it is expected the
presenter then keeps a reference to the copy.
1)
You need to set up a model folder in the Windows application (if you
have not already done this). Your Form_Load event will now look like this) –
Replace the "D:\Work\TestData" with a suitable folder on
your harddisk.
|
private
void Form1_Load(object
sender, EventArgs e)
{
NWModel.ModelDirectory = @"D:\Work\TestData";
PresenterMyAppName presenter
= new PresenterMyAppName();
this.Controls.Add(presenter);
presenter.NWResizeControls();
}
|
2)
Create a model level variable to store the model object for every
presenter.
|
public
class PresenterMyAppName
: NWBasePresenter
{
private ModelMyAppName
_model = null;
}
|
3)
Create a function to initialise the model for every presenter.
|
public
override INWModel
NWCreateModel()
{
_model = new ModelMyAppName();
return _model;
}
|
4)
Now create a function for every presenter to uniquely
identify the model type. Note that the hard coded strings "AppName&PresenterName"
will later on be replaced by application constants. You must include the
& character. Any additional arguments to represent
the context should also be supplied here.
|
public
override string
NWQueryString()
{
return "AppName&PresenterName";
}
e.g.
public
override string
NWQueryString()
{
return RouterSSLPortal.APPLICATION_NAME
+ "&" + PresenterEditLink.PRESENTER_NAME + "&" + _linkID;
}
|
5)
You can now access the model in any presenter function you define.
Remember to use the NWIsDirty flag if you change the model. For example,
|
private
void cmdOk_NWEventClick(object sender, NWButton.NWClickEventArgs e)
{
_model.Name = txtName.NWText;
_model.NWIsDirty = true;
this.NWGoBackPresenter(e.Url);
}
|
6) You
can give your presenter constructor any scalar parameters you like (so long as
they can be encoded as a string). This can be used to tell the presenter which
part of the model to access. For example,
|
public
class PresenterMyAppName
: NWBasePresenter
{
private ModelMyAppName
_model = null;
private string
_userID = String.Empty;
public PresenterEditUser(string
userID)
{
_userID = userID;
InitializeComponent();
}
private void
cmdOk_NWEventClick(object sender, NWButton.NWClickEventArgs
e)
{
ModelUser user = null;
if (_userID == "NEW")
{
user = new ModelUser();
_model.Users.Add(user);
}
else
{
user = ModelUser.Creator.GetObject(_userID);
}
}
}
|