mv.NET Extensions Library – Part 1
If you use mv.NET you may be interested in a collection of extension methods that I’ve created to streamline my own development. By packaging common functions in a separate library, I’m not only making it easier to re-use them across all projects, but I’m giving myself a set of standards o follow, making maintenance easier, and I’m removing some of the mechanics out of application code, making that easier to maintain as well.
Right now this isn’t packaged for sale but I can quickly do so if there is any demand. For now I’m just sharing what I’m doing here. I am also interested in learning about the tools that you mv.NET developers have created for yourselves. What would you like to help your own development. What can I add to this library that would make it a must-have tool kit for you?
Here are the methods currently in the NebulaRnDmvNetExtensions library in the NebulaRnD.Utils.mvNet namespace:
For CoreObjects, mvItem:
List<string> mvItem.ToStringList()
Return a List of strings where each element is an MV attribute.
This is helpful after CallProg where the return value is an attribute-delimited list of data.
Usage: acct.CallProg("MY.PROG" , ref inval , ref outval , ref errval);
mvItem item = new mvItem(outval);
List
List<string> mvItem.ToStringList(int Attribute)
Return a List of strings where each element is a value from a specified MV attribute. This is helpful after CallProg where the return value is a dynamic array and one of the attributes is a multivalued-list of data.
Usage: acct.CallProg("GET.ORDER.HDR" , ref inval , ref outval , ref errval);
mvItem orderHeader = new mvItem(outval);
List
List<string> mvItem.ToStringList(int Attribute, int Value)
Return a List of strings where each element is a subvalue from a specified MV attribute and nested MV value.
DataTable mvItem.ToDataRow(DataTable)
Convert mvItem to a DataRow using schema in provided DataTable.
DataRowCollection mvItem.ToDataRows( int startAtb , int endAtb, int values )
Returns a collection of DataTable rows without the table itself. Useful when the MV item is structured with groups of attributes as follows:
ITEM#]DESCRIPTION]UNIT-PRICE]UOM
Usage: DataTable table = new DataTable("Inventory");
table.Rows = mvdata.ToDataRows(3,5,4);
// results in table with 3 rows (atbs 3,4,5) and 4 columns
DataTable mvItem.ToDataTable(DataTable)
Convert each attribute of mvItem to DataRow in new DataTable using schema in provided DataTable. VMs become new AMs, thus each VM is a column in the row.
Usage: acct.CallProg("MY.PROG" , ref inval , ref outval , ref errval);
mvItem item = new mvItem(outval);
// if the data from the subroutine represents a single data object
DataRow rowWithData = item.ToDataRow(tableWithSchema);
// if the data from the subroutine represents a collection of records
DataTable tableWithData = item.ToDataTable(tableWithSchema);
For CoreObjects, mvItemList:
DataTable mvItemList.ToDataTable(DataTable)
Convert mvItemList to DataTable using schema in provided DataTable.
Usage: mvSelect selection = new mvSelect();
// specify details for selection here...
grid.DataSource = acct
.FileOpen("DATA.FILE").Select(selection)
.ToTable(tableWithSchema);
For SolutionObjects:
ArrayList collection.ToArrayList(string key)
Convert any mv.NET collection class to an ArrayList where each element has a unique key provided by singular objects in the collection. The key is extracted from each instance in the collection using the provided field name string.
Usage: DropDownList1.DataSource = Organization
.SelectGeneric(@"with NUMBER ""1]""" , "BY NUMBER" , repo)
.ToArrayList(OrgProps.OrgID);
// in a single line of code the data is retrieved and assigned to a dropdown list
Hashtable collection.ToHashtable(string key,string data)
Convert any mv.NET collection class to a Hashtable where each element has a unique key and a single data element.
Usage: Organizations orgs = Organization
.SelectGeneric(@"with NUMBER ""1]""" , "BY NUMBER" , repo);
DataList1.DataSource =
orgs.ToHashtable(OrgProps.OrgID , OrgProps.Name);
// the hashtable serves as a datasource for this UI control
DataTable collection.ToDataTable(OrderedDictionary schema)
Convert any mv.NET collection class to a DataTable with columns defined using provided field names and data types in an ordered dictionary. For each object in the collection, a row is created in the table with the specified fields loaded into the columns.
Usage: Organizations orgs = Organization
.SelectGeneric(@"with NUMBER ""1]""" , "BY NUMBER" , repo);
DataTable table = orgs.ToDataTable(
new OrderedDictionary
{ OrgProps.OrgID, emptyOrg.OrgID.GetType() },
{ OrgProps.OnHold , emptyOrg.OnHold.GetType() },
{ OrgProps.MainContactName , emptyContact.FullName.GetType()}
});
// that table can now be used in a DataSet, with any controls, and with
// any ADO.NET code that requires a DataTable.
DataView collection.ToDataView(OrderedDictionary schema)
Convert any mv.NET collection class to a DataView with columns defined using provided field names and data types. For each object in the collection, a row is created in an internal DataTable with the specified fields loaded into the columns. The final DataTable is simply re-cast as DataView with “return new DataView(table)”.
Usage: Organizations orgs = Organization
.SelectGeneric(@"with NUMBER ""1]""" , "BY NUMBER" , repo);
Organization emptyOrg = new Organization(); // allows us to get data types
Contact emptyContact = new Contact(); // allows us to get data types
// in one line, the collection is converted to a DataView and assigned to the datasource
DataList3.DataSource = orgs.ToDataView(
new OrderedDictionary
{ OrgProps.OrgID, emptyOrg.OrgID.GetType() },
{ OrgProps.OnHold , emptyOrg.OnHold.GetType() },
{ OrgProps.MainContactName , emptyContact.FullName.GetType()}
});
I’ve written more utilities like that but they’re mixed in with other code. As I find them I’ll extract them out into this library.
I might tweak a couple of those functions. For example, the ToDataTable and ToDataView methods for Solution Objects collections currently requires a data type. Well, if we know what the field name is then we should be able to infer the type for that field. I wrote it as I did, for example because you might want to cast data as string when the source is a numeric type. But if you just want the data in its original format then there’s no sense in asking the developer to do the extra GetType() work there.
So – do you find yourself writing the same code all the time? Are you struggling with getting data from MV into user controls in an elegant manner? Have you ever thought, “there’s gotta be a better way with this stuff”? Now that you see the kinds of things being done, am I missing something obvious? Let me know, I might be able to put something together.
And if you are interested in purchasing this library for your own use, please let me know!
🙂