Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: 13 Productivity Tips for Building a .NET Distributed Application : Page 6

The Common Ground Framework for .NET provides a set of starter classes for building database applications in a distributed environment.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Tip 11: Server-side and Client-side Business Objects
A development team often must decide where to implement business rules. For performance, or simply when no valid reason exists to go back to the server, some business objects may reside in the client piece. Automatic calculations during data entry represent one example.

The namespaces in Table 2 include business objects for both the client and server pieces. During the technical design phase of a project, the development team must decide where to implement business rules. Client-side business objects are a perfectly valid approach, so long as they remain separate from UI classes.

One example is a calculation of an extended amount during data entry. The user may change quantities and prices several times during data entry: a round-trip to the server to recalculate and display the total would be slow and inefficient. Therefore, developers should construct a client-side business object and call it at the appropriate times.

// Developer may chose to pass the entire datarow, // or simply the quantity and unit price values private decimal CalculateTotal(DataRow DrDetail) { decimal nTotal = 0; nTotal = Convert.ToDecimal( DrDetail["Qty"]) * Convert.ToDecimal( DrDetail["UnitPrice"]); return nTotal; }

Tip 12: Baker's Dozen Potpourri - Miscellaneous Uses of ADO.NET
Optimal use of database stored procedures should normally minimize the need for heavy processing in other layers of the application. However, there are instances where a developer needs to munge through several sets of data to produce a result. Here are a few mini-tips that demonstrate the more powerful capabilities in ADO.NET.

If you've used ADO.NET you know that it does not have the full capabilities of a SQL language. However, a combination of the more advanced capabilities of ADO.NET, along with a little "elbow grease," can usually get the job done.

For example, suppose a user is looking at a set of orders, and wants to filter on orders where the Account name contains the word "National." So long as a relation exists between the orders table and the account table, a developer can reference parent columns in a RowFilter as follows.

MyDataSet.Relations.Add("RelName", DtAccounts.Columns["AcctID"], DtOrders.Columns["AcctID"],false); System.Text.StringBuilder sbRowFilter = new System.Text.StringBuilder(); string cAcctName = "National"; sbRowFilter.AppendFormat("Parent(RelName) like '%{0}%'",cAcctName); DtOrders.DefaultView.RowFilter = sbRowFilter;

While it's a simple property, some people (myself included!) get confused by the syntax for creating a primary key.

// Untyped dataset DtMaterials.PrimaryKey = new DataColumn[] { DtMaterials.Columns["PrimaryKey"]}; // Typed dataset DtMaterials.PrimaryKey = new DataColumn[] { DtMaterials.PrimaryKey};

A developer may need to create and maintain multiple views of a DataTable—perhaps a different Sort and RowFilter for each view.

// Multiple DataViews — the developer can create // two different dataviews on the same table, and // then bind them to two different datagrids DataView dv1 = new DataView(DtMaterials); dv1.Sort = " amount Desc "; dv1.RowFilter = " AccountType = 1 "; myDataGrid1.DataSource = dv1; DataView dv2 = new DataView(DtMaterials); dv2.Sort = " acctname Desc "; dv2.RowFilter = " amount > 5000 "; myDataGrid2.DataSource = dv2;

Tip 13: Implementing Basic Audit Trail Capability into an Application
How many times have you heard someone ask, "Who changed this data, and when?" Audit trail functionality is a vital component in many database management systems. This tip introduces a generic form to show audit trail data to the user.

In my T-SQL article that appeared in the February/March issue of CoDe Magazine, I presented some update trigger code to write changes to an audit trail log. The CG Framework extends this by providing a base form class (FrmAuditTrail) for viewing audit trail changes. Figure 5 shows a basic example of audit trail history for a particular record.

Figure 5: Example of audit trail history screen.
The base data maintenance form back in Figure 4 contains an option to view the audit trail form for the current record, by performing the following.

cgsFrmAuditTrail oAuditTrailForm = new cgsFrmAuditTrail(); oAuditTrailForm.cTableName = this.GetPrimaryTable(); oAuditTrailForm.nPrimaryKey = this.GetCurrentPrimaryKey(); oAuditTrailForm.ShowAuditTrail();

The download project contains a script to generate the entire SQL database. The database includes the necessary tables for the audit trail classes. This includes an audit trail table (AuditTrail), a user table (UserName), a data dictionary to store English-like names for each column (MasonryDataDictionary), and update trigger code to insert rows into AuditTrail.

Closing Thoughts
You can pull the entire project, as well as the script for generating the database, from www.commongroundsolutions.net. The zip file contains a Word document with release notes on all functionality in the CG Framework.

I hope this article provides some tips for developers who are learning .NET. There are many things in the framework that are not covered in this article, such as incorporating Crystal Reports, as well as some of the other capabilities in the base data access class. The release notes (Common Ground Framework.DOC) lists all the capabilities in the framework.

There are also many opportunities to improve the framework, such as encrypting the XML tables, retaining the selected connection at sign-on, and even adding some intelligence to the sign-on process to automatically determine which connection to use, based on the user's current IP address.

If you have suggestions or ideas you'd like to share, I'd love to hear from you. Feel welcome to contact me. You can find the entire source code online at www.commongroundsolutions.net.

This is an ongoing project, so there may be a few enhancements from time to time. Check the release notes Word document for details.

Kevin S. Goff is the founder and principal consultant of Common Ground Solutions, a consulting group that provides custom web and desktop software solutions in .NET, Visual FoxPro, SQL Server, and Crystal Reports. Kevin has been building software applications for 17 years. He has received several awards from the U.S. Department of Agriculture for systems automation. He has also received special citations from Fortune 500 companies for solutions that yielded six-figure returns on investment. He has worked in such industries as insurance, accounting, public health, real estate, publishing, advertising, manufacturing, finance, consumer packaged goods, and trade promotion. In addition, Kevin provides many forms of custom training.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date