System.Collections.Generic Dictionary String, String
CleanCode.Data AttributeDictionary
Namespace: CleanCode.Data
Assembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
[SerializableAttribute] public class AttributeDictionary : Dictionary<string, string>
attr1=val1;attr2=val2;...attrN=valn
FillGrid(dataGridView, flatList); // fill DataGridView flatList = GridToString(dataGridView); // extract from DataGridView
string flatList = "aaa=1;bbb=2"; AttributeDictionary dict = new AttributeDictionary(flatList); dict["aaa"] = "5"; dict["bbb"] = "xyz"; dict["c"] = "new item here"; flatList = dict.ToString(); // result will be "aaa=5;bbb=xyz;c=new item here"
The utility of the above technique may not be immediately obvious, though I have found it useful in a variety of situations. The scenario that will be relevant to almost every developer is that of a ConnectionString. A typical ConnectionString might be stored in your Settings file as Data Source=SQL1X5;Initial Catalog=MyDbName;Persist Security Info=True, which is precisely an attribute string that AttributeDictionary is designed to handle. What you should probably not store in a ConnectionString are the User ID and Password attributes, though they need to be added. So here is the previous example rewritten in a ConnectionString context:
AttributeDictionary dict = new AttributeDictionary(myConnString); dict["User ID"] = "username"; dict["Password"] = "open_sesame"; connection = new SqlConnection(dict.ToString());
The above discussion handles a single-valued attribute list. AttributeDictionary also handles multi-valued attribute lists when working with a DataGridView. Consider that the single-valued attribute list could be viewed as a table with just one row. The attribute names are the column names, and the attribute values are the values in the single row. (Recall that the above talked about a DataGridView with 2 columns. Here we have just transposed that table (columns become rows) and subsumed the attribute names into the column headers.) A multi-valued attribute list, then, allows you to have multiple rows in this table. A multi-valued attribute list is stored into a DataGridView with exactly the same method call as a single-valued list shown above:
FillGrid(dataGridView, flatList);
attr1[0]=val;attr2[0]=val;...attrN[0]=val; attr1[1]=val;attr2[1]=val;...attrN[1]=val; . . . attr1[m]=val;attr2[m]=val;...attrN[m]=val
flatList = GridToString(dataGridView);
Since CleanCode 0.9.07.