c# - Adding handlers for some field values and other extension points -



c# - Adding handlers for some field values and other extension points -

i building application datamodel fixed, people (or me) can extend adding classes inherit base of operations class gets instantiated info in db , serialized in services.

i have 3 problem areas (case 1 2 , 3 in sample code below). case #1 maybe solve interface, doesn't help me case 2 or 3.

i think code sample speak improve attempts explain; idead on how approach each new field type doesn't need manually added bunch of places in code?

public class managerclass { public managerclass() { public managerclass() { } //case #1 public void process(allfields allfields) { foreach (field field in allfields.fields) { //currently need add together extention types seperate cases here manually //...this type of logic appears in several places in code if (field.gettype().name == "extendedfield") { //have extended field in way particular } else { //have base of operations field "normal" way } } } //case #2 //here case adding each case in hand //fieldtype string here because storing type of field in db public void create(string value, string fieldtype) { //currently need add together extention types seperate cases here manually if (fieldtype == "extendedfield") { //create extendedfield } else { //create field } } } } [datacontract] //case #3 [knowntype(typeof(extendedfield))] //currently need add together extention types here manually public class allfields { private list<field> fields; public allfields(){} [datamember] public list<field> fields { { homecoming fields; } set { fields = value; } } } [datacontract] public class field { private string fieldvalue; public field(){} [datamember] public string fieldvalue { { homecoming fieldvalue; } set { fieldvalue = value; } } } [datacontract] public class extendedfield : field { private string someotherattribute; public extendedfield(){} [datamember] public string someotherattribute { { homecoming someotherattribute; } set { someotherattribute = value; } } }

sounds you're trying build miniature extensibility framework. consider extension logic handled fieldhandler:

public class fieldhandler { public virtual field createfield(string value, string fieldtype){...} } // case 2 field field = null; foreach (fieldhandler handler in m_handlers) { if (handler.supportsfieldtype(fieldtype)) { field = handler.createfield (value, fieldtype); continue; } } if (field == null) { // create standard field. field = ...; }

c#

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -