//----< write details of element with enumerable Data to string >------ public static string showElement <Key, Data, T>(this DBElement <Key, Data> elem) where Data : IEnumerable <T> // constraint clause { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { IEnumerable <object> d = elem.payload as IEnumerable <object>; if (d == null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } else { bool first = true; accum.Append(String.Format("\n payload:\n ")); foreach (var item in elem.payload) // won't compile without constraint clause { if (first) { accum.Append(String.Format("{0}", item)); first = false; } else { accum.Append(String.Format(", {0}", item)); } } } } return(accum.ToString()); }
//----< write details of element with simple Data to string >---------- public static string showElement <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } return(accum.ToString()); }
static void Main(string[] args) { "Testing DBExtensions Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; elem1.children = new List <int> { 1, 2, 3, 4 }; Write(elem1.showMetaData <int, string>()); DBEngine <int, DBElement <int, string> > dbs = new DBEngine <int, DBElement <int, string> >(); dbs.insert(1, elem1); dbs.show <int, DBElement <int, string>, string>(); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.children = new List <string> { "Key1", "Key2" }; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string>, string>()); DBEngine <string, DBElement <string, List <string> > > dbe = new DBEngine <string, DBElement <string, List <string> > >(); dbe.insert("key1", newelem1); dbe.show <string, DBElement <string, List <string> >, List <string>, string>(); Write("\n\n"); }