Model help - Switchables / Parent-Child & Inheritance

Model help - Switchables / Parent-Child & Inheritance

Old forum URL: forums.lhotka.net/forums/t/2589.aspx


juddaman posted on Monday, March 26, 2007

Heya

I'm having difficulties with a particular section of my model. The problem resolves around these classes:

Question
ResponseMechansim (abstract)
ChoiceResponseMechansim (inherits ResponseMechansim)
ScaleResponseMechansim (inherits ResponseMechansim)

Basically Question (currently, early stages) consists of the text of the question (string) and response means as ResponseMechansim. I was going to create the Question like this:

Question q  = Question.NewQuestion();
q.ResponseMechansim = ChoiceResponseMechanism.NewChoiceResponseMechanism();

However this would mean making ResponseMechanism and its derivatives switchable objects as they would need to be a root so they can be created then appended as a child an object of type Question.

I came up with the idea of allowing questions to create the new type of response using an enum, something along these lines:

public void SetResponseMechanismType(ResponseMechanismType type)
{
  switch (type)
  {
    case ResponseMechanismType.Choice:
      _ResponseMechanism - ChoiceResponseMechanism.NewChoiceResponseMechanism();
      break;
     case ResponseMechanismType.Scale:
   }
}

Though this seams a bit messy and unnecessary. I think I'm missing something somewhere. Any thoughts and suggestions welcomed.

Thanks guys

George


       

RockfordLhotka replied on Monday, March 26, 2007

Nothing stops you from making the create factory public on a child object. Really, being a child object only blocks the Save() from working - technically you can have public create and fetch methods on a child with no problem (though I think having a public fetch could have other bad side-effects on design).

There are good reasons why you might want to use the create-and-add model for child objects, rather than the created-by-collection model I show in the book, and there's no real issue in doing that - nor do you need to go to all the complexity of making your object a switchable object.

In your child class, just implement a public factory for creation. Then implement DataPortal_Create() to initialize/load defaults. Call MarkAsChild() in the ctor as normal. This allows the UI (or any other code) to create these objects at will.

Then you can just use the pre-existing Add() method on the collection to add this new item. BusinessListBase will automatically set the edit level and parent reference on the new object.

Then the insert operation will be automatically handled when you save the collection, because the object is just like any other new child at that point.

juddaman replied on Monday, March 26, 2007

Thanks Rocky!!! Very much appreciated. I kind of got it in my head that making the factory public was a big no no. :-) George

Copyright (c) Marimer LLC