|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
PrefaceThis article demonstrates a first version of a DataTable Generator. This generator uses information found in custom attributes on any class, and transforms this class, or several instances of it, to a ProblemIn any modern application, you deal with a lot of custom classes, like Often, you need a quick way to capture the current values of the class at once, view the current data contained in them, or simply transform all values to an XML file. Luckily, there is class that is able to do all of this already: the The only question is how a custom class can transformed to a Xteq.Data.DataTableGeneratorThe first solution that might come to mind is to define a custom interface, and that each class that should be transmogrified to a However, for the project I needed this generator, this would have been too bloated, and also I simply didn't wanted to add another piece of code for each class. What I wanted to have was something like this: public class CustomClass1
{
//Include this private member
[DataTableInclude]
private string _name = string.Empty;
}
That way, I only need to apply the custom attribute Creating this custom attribute was the easy part. Just create a new class, derive it from As we want to be able to transform properties, member variables, and functions, we add the following on our custom class [AttributeUsage(AttributeTargets.Property |
AttributeTargets.Field | AttributeTargets.Method)]
public sealed class DataTableIncludeAttribute : System.Attribute
....
Reflect and create data columnsWith this To create a To retrieve this information, we will use Reflection. Reflection is the method in the .NET Framework to retrieve information about an object (actually, only the type of the object) and use it. For our purpose, we need to know on which parts of a given class (properties, members, or methods) our custom attribute has been applied. Since DTG will use the retrieved information to define the columns, the function is called public void DefineColumns(Type type)
To make the life for all lazy programmers easier, we also define a second method that can be used on an existing instance: public void DefineColumns(object obj)
{
//get the underlying type of the object
Type type = obj.GetType();
DefineColumns(type);
}
Within Each of them return an array of Finally, DefineColumn()
Next, it needs to know the data type that this member encapsulates. Depending on which member it is working on, this information can be found at:
This type (e.g., It then adds this newly created column as well as the current SortingOne thing that was requested 10 minutes after other people started using DTG was "How do I control the order of the columns?". My first reaction was to say that this sorting should be done in the front end. But as DTG should make lives easier, it should also be able to control the order in which the columns appear. This sorting is done through a temporary list ( This sorting takes the [DataTableInclude(SortID=1)]
private string _name = string.Empty;
As Adding dataOnce all columns are added, we should add rows with data from our custom objects. This is achieved by using the method: public void Add(object o)
Rather simple, isn't it? And indeed, the function itself is very simple since it only needs to do the following:
For each column retrieved, the function does the following:
And that's it. For your convenience, there is also a second ArrayList list = new ArrayList();
//Add some objects
for (int i = 0; i < 30; i++)
{
CustomClass1 cc = new CustomClass1();
cc.Age = i;
cc.Name = "John Doe " + i;
list.Add(cc);
}
generator.Add(list); //adds 30 rows
ExampleAfter all this, what is the result actually? Well, here's some code to demonstrate how easy DTG can be used. This is a custom class we want to display in a data grid: using System;
using Xteq.Data;
namespace TestAppDataTableGen
{
class CustomClassSimple
{
[DataTableInclude]
private long _myField=13;
[DataTableInclude(SortID=10)]
public string Prop
{
get
{
return (_myField + 10).ToString();
}
}
[DataTableInclude("Ticks")]
protected long GetCurrentTickCount()
{
return DateTime.Now.Ticks;
}
}
}
Using the To display this class, we only need to write: //create our custom classs
CustomClassSimple ccs = new CustomClassSimple();
//create the generator
DataTableGenerator generator = new DataTableGenerator();
//use the current instance to define the columns
generator.DefineColumns(ccs);
//add the current object to the data table
generator.Add(ccs);
//use the grid to display the generated data table
grid1.DataSource = generator.DataTable;
The data grid will then display:
Private members noteAlthough DTG is able to retrieve private members (see For example, if you create Search methodWhen creating a new instance of DTG, you can also pass in one or more values from the DataTableGenerator generator =
new DataTableGenerator(DataTableGeneratorSearch.Fields);
You can also OR several options together like this: DataTableGenerator generator =
new DataTableGenerator(DataTableGeneratorSearch.Fields|
DataTableGeneratorSearch.Properties);
DataTable tricksSince DTG provides full access to its underlying ConclusionAs said at the beginning of this article, this is only the first version of the But using this code as the basis, it should be easy to implement these or any other features. Enjoy! DisclaimerTHE SOFTWARE AND ALL ACCOMPANYING FILES, DATA AND MATERIALS, ARE DISTRIBUTED AND PROVIDED "AS IS" AND WITH NO WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED. THE USER ACKNOWLEDGES THAT GOOD DATA PROCESSING PROCEDURE DICTATES THAT ANY PROGRAM, INCLUDING THE SOFTWARE, MUST BE THOROUGHLY TESTED WITH NON-CRITICAL DATA BEFORE THE USER RELIES ON IT, AND THE USER HEREBY ASSUME THE ENTIRE RISK OF USING THE PROGRAM. IN ADDITION, IN NO EVENT XTEQ SYSTEMS, OR ITS PRINCIPALS, SHAREHOLDERS, OFFICERS, EMPLOYEES, AFFILIATES, CONTRACTORS, SUBSIDIARIES, OR PARENT ORGANIZATIONS, WILL BE LIABLE FOR ANY INDIRECT, INCIDENTAL, CONSEQUENTIAL OR PUNITIVE DAMAGES WHATSOEVER RELATING TO THE USE OF THE SOFTWARE OR TO THE RELATIONSHIP OF THE USER WITH XTEQ SYSTEMS. THIS INCLUDES, BUT IS NOT LIMITED TO, MERCHANTABILITY AND FITNESS FOR A PARTICULAR FUNCTION.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||