Browse DevX
Sign up for e-mail newsletters from DevX


Overcoming the Barriers Around Using Custom Classes in ASP.NET : Page 4

Custom classes and collections are often better choices than using a DataSet or DataTable, because they're truly object oriented, letting developers employ robust OOP programming techniques.




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

Creating Reusable Business Object Templates
I often encounter common functionalities in business objects. For purposes of reusability, I want to abstract these common functionalities into a business object template. For example, I often encounter data patterns (Active Record, data mappers, etc), iteration, and XML serialization in data-driven Web applications. The current solution for developers is to create code-generation templates and use code-generation tools such as CodeSmith to generate the custom class as needed. But with xType and xObject, you can easily abstract these data patterns into the base template classes with default implementations. In addition, you can augment these two interfaces to support other functionalities such as iteration, XML serialization, object factory, events, etc.

In the remainder of this article I will illustrate how to create the business object template by abstracting the Active Record and Dynamic Object patterns into template classes.

Active Record
Active Record is an extremely useful enterprise application pattern (References, [4]). Typically developers use it to mimic a table record with four basic data access operations: Create, Retrieve, Update, and Delete, commonly known as CRUD. Listing 4 depicts the template class called ActiveRecordBase for the Active Record pattern. One feature that makes this implementation distinct from others is that it can update only changed fields by building a dynamically updated SQL statement, showcasing state tracking in xType. You can use dynamic SQL or a stored procedure to accomplish CRUD. However, dynamic updating only works with dynamic SQL. I assume that the database table uses an identity column as its primary key. For simplicity, I only show two static supportive functions: BuildTableUpdateString and UpdateDataRecord. I use BuildTableUpdateString to build dynamic SQL update text and UpdateDataRecord to execute the updated SQL text. In ActiveRecordBase, you will find a sample implementation of ResolveRealName and GetValueByName. To create an Active Record, you need only extend ActiveRecordBase and supply all class properties; the Active Record is then ready for use.

Dynamic Business Object
When developing Web applications using custom classes, I often find that I need a way to quickly create and instantiate a custom class object, which I refer to as a dynamic object. A Hashtable or ArrayList can serve this purpose. You pass in the property name and type information through the dynamic object constructor. A dynamic object implemented with xType has two advantages: the data is always strongly typed internally and the number of properties may vary. You can also create a dynamic object according to some external configuration setting. You can find applications of the dynamic object in the places where performance is not a critical issue—for example, in Web Forms dealing with application configuration settings. The following code snippet illustrates two types of dynamic objects; one has a varying number of properties and the other does not:

public abstract class VaringDynamicObjectBase : xObject { Hashtable hashTable = new Hashtable(); public xType GetValueByName(string realName) { return (xType) hashTable[realName]; } public string ResolveRealName(string name) { return name; } } public abstract class QuickDynamicObjectBase : xObject { Hashtable hashTable = new Hashtable(); public xType GetValueByName(string realName) { return (xType)hashTable[realName]; } public string ResolveRealName(string name) { return name; } public QuickDynamicObjectBase(string[] propNames, SqlDbType[] propTypes) { for(int kk = 0; kk< propTypes.Length; kk++) { hashTable.Add(propNames[kk], xTypeHelp.CreatexTypeInstance( propTypes[kk])); } } }

Using the Techniques in Your Applications
You can easily adapt xType, xObject, and all the techniques in this article to your applications. All that you are required to do is to change a data type to a specific xType and have your business object inherit from xObject or the business object template. Then you can enjoy the features of SqlParameter building, populating a data business object or business object collection from SQL, active record, dynamic object, and generic two-way data binding. The implementation of xObject in the article uses reflection and is not optimal, so you can replace them with your own implementations to improve the performance. The only inconvenience is that you need to do a little more typing to access the value of a business object property.

With the two hardest problems resolved, you can write a truly object-oriented, data-access component. You can use these two functions to replace tens of lines of data-access-related code in real applications.
To better understand the advantages of xType and xObject, I'll show some code from a real application. Listing 6 shows some code excerpted from the Pet Shop project. Instead of modifying the code, I will point out how you can simplify the code with the use of xType and xObject.

Listing 5 depicts two functions, Insert and GetOrder, excerpted from Order.cs in the SQLServerDAL project of the Pet Shop. Look at the bulky code that deals with creating SQL parameters and populating data into the custom class. If you change the data type in the custom classes OrderInfo and AddressInfo to xType in the Model project, you can reduce the tens of lines of code into a couple lines of code. You can go further and inherit the custom classes from the active record. Note that Pet Shop emphasizes using stored procedures. It is not hard to create a stored-procedure based, active-record template. However, doing so requires you to restructure the code a little bit. I believe the code size will be further drastically reduced because of the built-in support of CRUD in the active record.

Next, look at the data-binding code in the user control AddressForm.ascx.cs in Listing 6. Actually you can replace the entire chunk of code with two data-binding function calls by using xControls instead of those plain server controls.

At this point you probably get the concept of xType and xObject. In this article I cannot discuss all the issues around using xType and xObject. But you should be able to see how you can benefit from them from the Pet Shop project In a word, xType and xObject are deliberately contrived to overcome the barriers resulted from SQL and .NET type mismatch and lost business object run-time identity. They connect the business object to the underlying data access, Web Form data binding, and application-level design patterns. You can use them to create reusable business object templates and avoid using or reduce the dependency on code generation. xType can make your application more compact, more readable, and easier to change and maintain.

Bill Xie is a .NET solution architect, hands-on developer, and software consultant based in southern California. He has extensive experience developing Web applications, Windows applications, and distributing applications in different types of industries including insurance, financial services, and IT management. He has had articles published in CoDe Magazine, on DevX, asp.netPRO, ASPToday, and The Code Project.
Thanks for your registration, follow us on our social networks to keep up-to-date