Home arrow ASP.NET arrow Page 6 - 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 - Putting it all together
(Page 6 of 8 )

Now that the SqlObjectManager class has been defined, we can write code to manage objects in the database. The following test code sets up the object manager, creates a Student table, and manages student records within the table:

static void Main(string[] args)
{
try
{
// used to manage our students
SqlObjectManager om = new SqlObjectManager();
// create a connection to the database
SqlConnection conn =
new SqlConnection("server=localhost;uid=sa;" +
"pwd=;database=mydb");
// open up the connection
conn.Open();
// set the connection property of the object manager
om.Connection = conn;
// create a new student
Student s = new Student();
// create a table definition for the student

Console.WriteLine("Creating student table");
om.CreateTableForObject(s);

// add 5 students to the database
ExcerciseAddingStudents(om, 5);
// retrieve the 5 studens and write
// to the console
ExcerciseRetrievingStudents(om, 5);
// update the address field of the
// student object and have the
// object manager update the
// database
ExcerciseUpdatingStudents(om, 5);
// retrieve the 5 studens and write
// to the console
ExcerciseRetrievingStudents(om, 5);
// clean up the database by deleting
// the 5 students we created
ExcerciseDeletingStudents(om, 5);
}
catch(Exception ex)
{
Console.WriteLine("------------------------------------");
Console.WriteLine("An Exception was caught: ");
Console.WriteLine("------------------------------------");
Console.WriteLine(ex.Message);

}
}


The code block above creates an instance of the SqlObjectManager, sets the Connection property, creates the student table, adds five student records, retrieves the records, updates them, and finally deletes them. Here are the methods that the above code block invokes:

static void WriteStudentHeader()
{

Console.WriteLine("ID\tName\t\t\tAddress\t\t" +
"Number of Courses");
Console.WriteLine(new string('-', 80));
}
static void WriteStudentDetails(Student s)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
s.ID, s.FirstName.Trim() + " " +
s.LastName.Trim(),
s.Address.Trim(),
s.NumberOfCourses);
}
static void ExcerciseDeletingStudents(SqlObjectManager om, int iNumStudents)
{
if(iNumStudents > 0)
{
Console.WriteLine("Deleting {0} student records",
iNumStudents);
// iterate through each of the students and
// removes them
for(int i = 0; i < iNumStudents; i++)
{
// construct the student
Student storedStudent = new Student();
storedStudent.ID = i;
// delete the student
om.DeleteObject(storedStudent);
}
}
else
{
Console.WriteLine("The number of students requested " +
"was invalid.");
}
}
static void ExcerciseUpdatingStudents(SqlObjectManager om, int iNumStudents)
{

if(iNumStudents > 0)
{
Console.WriteLine("Updating {0} student records",
iNumStudents);

// iterate through each of the students
for(int i = 0; i < iNumStudents; i++)
{

Student storedStudent = new Student();
storedStudent.ID = i;
// retrieve the student
om.RetrieveObject(storedStudent);
// update the student
storedStudent.Address = "Updated " + i;
// update the repository
om.UpdateRepository(storedStudent);
}
}
else
{
Console.WriteLine("The number of students requested " +
"was invalid.");
}
}
static void ExcerciseRetrievingStudents(SqlObjectManager om, int iNumStudents)
{
if(iNumStudents > 0)
{
Console.WriteLine("");
Console.WriteLine("Listing {0} student records",
iNumStudents);
WriteStudentHeader();
// iterate through the list of students
for(int i = 0; i < iNumStudents; i++)
{
Student storedStudent = new Student();
storedStudent.ID = i;
// retrieve the object
om.RetrieveObject(storedStudent);
// write out the details
WriteStudentDetails(storedStudent);

}
}
else
{
Console.WriteLine("The number of students requested " +
"was invalid.");
}

}
static void ExcerciseAddingStudents(SqlObjectManager om, int iNumStudents)
{
if(iNumStudents > 0)
{
Console.WriteLine("Creating {0} student records",
iNumStudents);
// lets create 50 studen records
for(int i = 0; i < 5; i++)
{
// create a new student
Student newStudent = new Student();
// set the student properties
newStudent.ID = i;
newStudent.Address = i + " Smith Drive";
newStudent.FirstName = "FirstName " + i;
newStudent.LastName = "LastName " + i;
newStudent.NumberOfCourses = i;
// add the student to the repository
om.AddToRepository(newStudent);
}
}
else
{
Console.WriteLine("The number of students requested " +
"was invalid.");
}
}


When executed, the test code above produces the following output:

Figure 3
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