Home arrow ASP.NET arrow Page 4 - Demonstrating Attributes and Reflection in .NET
ASP.NET

Demonstrating Attributes and Reflection in .NET


In this article, Todd Clemetson demonstrates how the combination of attributes and reflection can result in some flexible and powerful software...

Author Info:
By: Wrox Team
Rating: 5 stars5 stars5 stars5 stars5 stars / 78
January 14, 2003
TABLE OF CONTENTS:
  1. · Demonstrating Attributes and Reflection in .NET
  2. · What are Attributes?
  3. · Generic Database Manager
  4. · Attributes Applied
  5. · Generating SQL using .NET Reflection
  6. · Putting it all together
  7. · Bug Tracking Attribute
  8. · Conclusion

print this article
SEARCH DEVARTICLES

Demonstrating Attributes and Reflection in .NET - Attributes Applied
(Page 4 of 8 )

Now that we have defined our custom attributes, let's look at a sample class that uses these attributes to specify how a class maps to a table within a database. The Student class below shows the above custom attributes in action.

[DBTable("Student", "dbo")]
class Student
{
private string _id; // id of the student
private string _firstName; // first name
private string _lastName; // last name
private string _address; // address
private int _numCourses; // number of courses

///
/// The ID of the student
///

///
// the student id is the primary key and identifies
// a unique row
[DBPrimaryKeyField("StudentID", DbType.String)]
[DBColumn("student_id", DbType.Guid, false)]
public string ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
///
/// the first name of the student
///

[DBColumn("first_name", DbType.String, false)]
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
///
/// the last name of the student
///

[DBColumn("last_name", DbType.String, false)]
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
///
/// the address of the student
///

[DBColumn("address", DbType.String, true)]
public string Address
{
get
{
return _address;
}
set
{
_address = value;
}
}
///
/// the number of courses the student is enrolled in
///

[DBColumn("num_courses", DbType.Int32, true)]
public int NumberOfCourses
{
get
{
return _numCourses;
}
set
{
_numCourses = value;
}
}
}


In the code above, we provide meta-data that indicates: 1) the table name that the Student class should persist rows to (Student in this example), 2) the attributes that indicate a unique row (there is only one key field called StudentID), and 3) the column names that the properties should map to (student_id, first_name, last_name, and address, respectively).

Let's look at some code that uses this metadata (i.e. the custom attributes) in a meaningful way. In the next sections we will write C# code using .NET reflection to generate SQL statements for a particular object.

The class generating these SQL statements will be called SqlGenerator. We will also design an SqlObjectManager class that exposes a simple interface for managing objects in a SQL server database. The class diagram for these two classes is shown below:

Figure 2
blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials