AttributeDictionary ClassCleanCode C# Libraries v1.2.03 API
Implements a specialized Dictionary to handle an attribute list.
Inheritance Hierarchy

OnlineSystem Object
  OnlineSystem.Collections.Generic Dictionary OnlineString, OnlineString 
    CleanCode.Data AttributeDictionary

Namespace: CleanCode.Data
Assembly: CleanCode (in CleanCode.dll) Version: (1.2.03)

public class AttributeDictionary : Dictionary<string, string>

The AttributeDictionary class allows you to manipulate an attribute list packed into a string in the format:
You may pack/unpack an attribute string in two ways. First, you may move into or out of a OnlineDataGridView, with no ancillary functionality, using class methods. The DataGridView should contain 2 columns, the first contains the attribute name, the second contains the attribute value; any additional columns will be ignored. (But see below for handling multi-valued attribute lists.)
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"
Second, you may instantiate an AttributeDictionary if you merely wish to modify the original string, using a sequence like this:

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);
. . .
flatList = GridToString(dataGridView);
The difference is in the structure of the "flatList". Here is the generalized format (with no line breaks): A multi-valued attribute list is extracted from a DataGridView with exactly the same method call as a single-valued list shown above: The difference here is how many columns the DataGridView has: if 2, it is a single-value attribute list, if more than 2, than multi-valued.

Since CleanCode 0.9.07.

See Also