Introduction
This page tells you how to build a simple model using the
NavWin MVP Framework. The framework comes with a simple way to serialise your
data and also to read it and save it to a text file. The MVP does not use .NET
serialisation because we do not want to process the complete model data on
every server call back. The MVP model is ‘process-on-demand.’
Top level class
A model for an application has a single top level class
which inherits the NWModel base class. The model base class provides four pre-defined
functions for reading and writing the data. Unless you are using your own data
source, then you do not need to do anything with these functions. They are
invoked automatically when needed. In addition, the model inherits
NWModelObject which means the model is itself an object. Implement the abstract
methods, but leave them empty for now
|
public
class ModelMyApp
: NWModel
{
public override
void SetStructure()
{
}
public override
void GetStructure()
{
}
}
|
Model objects
The model is made up of a collection of objects which
inherit NWModelObject. Note, in the very simple case, only one model object is
needed and that is the model itself (as this inherits NWModelObject). If you
need more than one object class in your model then declare it like this
|
public
class ModelMyExtraModelClass
: NWModelObject
{
public override
void SetStructure()
{
}
public override
void GetStructure()
{
}
}
|
The model object creator
Every model object needs to declare a generic type called
NWModelObjectCreator<>. This provides some powerful functions that are
generic in function but specific to each type. You will see the functions allow
you to write very concise code when creating objects.
|
public
class ModelMyObject
: NWModelObject
{
internal static
NWModelObjectCreator<ModelMyObject> Creator = new NWModelObjectCreator<ModelMyObject>();
}
|
Getting and setting scalar properties
Scalar properties are easy to deal with. Simply declare a
public field (or use can use get/set properties) and these can be loaded and
saved directly using code like the following. The example below will read and
write a single string field called Name. Note that we directly access a
protected variable _objStruct which maintains an internal
representation of the object.
|
public
class ModelMyObject
: NWModelObject
{
public string
Name;
public override
void SetStructure()
{
this.Name = _objStruct.GetString("Name");
}
public override
void GetStructure()
{
_objStruct.SetString("Name", this.Name);
}
}
|
Getting and setting a list of scalar values
The framework also supports the idea that you want to store
list of scalar values (which all have the same type).
As usual, the list is only processed if it is accessed. The
following code demonstrates how to use a list of scalar values.
|
public
class ModelMyObject
: NWModelObject
{
public List<string>
_names = null;
public override
void SetStructure()
{
// no action needed – load on demand
}
public override
void GetStructure()
{
_objStruct.SetList("Names",_names);
}
public List<string> Names
{
get
{
return _objStruct.PropertyGetList("Names", ref
_names);
}
}
}
|
Getting and setting a reference to an NW object
So long as you are dealing with an object that is derived
from NWModelObject then you can make a reference to the object and this
reference is automatically stored. Note that the object unique ID
is stored. The specific object being referenced has its own database entry so
the whole framework supports multiple references to the same object and, more
generally, recursion.
|
public
class ModelMyApp
: NWModel
{
private ModelMyObject
_myObject = null;
public override
void SetStructure()
{
// no action needed – load on demand
}
public override
void GetStructure()
{
_objStruct.SetObject("MyObject",
_myObject);
}
public ModelMyObject
MyObject
{
get
{
return ModelMyObject.Creator.PropertyGetObject("MyObject ", _objStruct, ref _myObject);
}
set
{
ModelMyObject.Creator.PropertySetObject(ref _myObject, value);
}
}
|
Getting and setting lists of NW objects
So long as you are dealing with objects that are derived
from NWModelObject then you can set up a list of these objects which will be
stored in the database. In the database, you only store the list of unique
object ID’s. The structure shown below means it is necessary to only process
the list of objects in the event that this list is actually needed. If the list
is never needed then the list and the underlying objects will never be
processed.
|
public
class ModelMyApp
: NWModel
{
private List<ModelMyObject> _myList = null;
public override
void SetStructure()
{
// no action needed – load on demand
}
public override
void GetStructure()
{
ModelMyObject.Creator.SetListObject(_objStruct,
"List", _myList);
}
public List< ModelMyObject> MyList
{
get
{
return ModelMyObject.Creator.PropertyGetObjectList("List", _objStruct, ref _myList);
}
}
|
Model Is Dirty
Although the model provides functions to read and write data
and serialize the data (get and set) your code is not expected to call these
functions directly. You can override them (e.g. in order to write to your own
custom database and not a text file) however you should not call them. In order
to tell the framework to save the current model, you should set the following
flag at the end of any changes you want to save
This simply tells the framework that the date has changed
and needs to be saved. The framework will either save the data to the server or
send it to the client to be saved as local data.