Creating default constructor for Model object

Feb 5, 2013 at 4:44 PM
Hi,

In my application, there's a fixed set of shapes and models, and there's a one-to-one relation between them. For example, shape "ProcessorShape" is always attached to model "ProcessorModel", other variants being impossible. For this purpose I have the following code:
private void MyAppStartUpMethod()
{
    // ...
    this.PrepareModel<ProcessorModel>(this.project1, "Processor", "ProcessorModel", false);
    // ...
}

private void PrepareModel<T>(Project prj, string templateName, string modelObjectTypeName, bool isConnector) where T : MyBaseModelClass
{
    Template template = prj.Repository.GetTemplate(templateName);
    T modelObj = (T)prj.ModelObjectTypes[modelObjectTypeName].CreateInstance();
    // Assign model object to template shape
    template.Shape.ModelObject = modelObj;
    // Add model object and update template
    prj.Repository.Insert(modelObj);
    // Map terminals (or our shape looses connectability - see http://nshape.codeplex.com/discussions/399871):
    int n = 0;
    foreach (ControlPointId id in template.Shape.GetControlPointIds(isConnector ? ControlPointCapabilities.Glue : ControlPointCapabilities.Connect))
    {
        template.MapTerminal(modelObj.Terminals[n++], id);
    }
    prj.Repository.Update(template);
}
Class ProcessorModel inherits from IModelObject interface:
// Constructor:
public ProcessorModel(ModelObjectType type)
{
    _modelObjectType = type;
}

// Implementation of IModelObject.ModelObjectType property:
public ModelObjectType Type
{
    get { return _modelObjectType; }
}
Now I need to make this class serializable via usual System.Xml.Serialization.XmlSerializer. For these purposes I'm planning to add a default constructor which looks like this:
// Default constructor (for serialization):
public ProcessorModel()
{
    _modelObjectType = DiagramContext.NShapeProject.ModelObjectTypes["ProcessorModel"];
}
(here DiagramContext is a public static class, and NShapeProject - is its public static property holding reference to the Project component of the main form; don't know other way to make Project available to models)

Is this OK?
I guess that yes - because of the one-to-one relation between shapes and models.
But I want to be sure.
Coordinator
Feb 7, 2013 at 7:12 AM
Comanche wrote:
Is this OK?
I guess that yes - because of the one-to-one relation between shapes and models.
Yes, everything's fine.
Feb 7, 2013 at 7:26 AM
Thank You.
So I'll hardcode ModelObjectType name in the model's constructor.