Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding handlers for some field values and other extension points
    primarykey
    data
    text
    <p>I am building an application where the datamodel is fixed, but people (or just me) can extend it by adding classes that inherit from the base class that gets instantiated from the info in the db and serialized in services.</p> <p>I have three problem areas with this (case 1 2 and 3 in the sample code below). Case #1 I could maybe solve with an interface, but that doesn't help me with case 2 or 3. </p> <p>I think the code sample will speak better than my attempts to explain; any idead on how to approach this so that each new field type doesn't need to get manually added to a bunch of places in the code?</p> <pre><code> public class ManagerClass { public ManagerClass() { public ManagerClass() { } //Case #1 public void process(AllFields allFields) { foreach (Field field in allFields.Fields) { //Currently I need to add all extention types as seperate cases here manually //...this type of logic appears in several places in the code if (field.GetType().Name == "ExtendedField") { //Have the extended field do something in a way particular to it } else { //Have the base field do something the "normal" way } } } //Case #2 //Here is another case where currently I am adding each case in by hand //fieldType is a string here because I am storing what type of field it is in the DB public void create(string value, string fieldType) { //Currently I need to add all extention types as seperate cases here manually if (fieldType == "ExtendedField") { //Create a ExtendedField } else { //Create a Field } } } } [DataContract] //Case #3 [KnownType(typeof(ExtendedField))] //Currently I need to add all extention types here manually public class AllFields { private List&lt;Field&gt; fields; public AllFields(){} [DataMember] public List&lt;Field&gt; Fields { get { return fields; } set { fields = value; } } } [DataContract] public class Field { private string fieldValue; public Field(){} [DataMember] public string FieldValue { get { return fieldValue; } set { fieldValue = value; } } } [DataContract] public class ExtendedField : Field { private string someOtherAttribute; public ExtendedField(){} [DataMember] public string SomeOtherAttribute { get { return someOtherAttribute; } set { someOtherAttribute = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload