tisdag, augusti 24, 2004
Custom attributes
Today I got through the section about custom attributes. At first I didn't really understand why you would want to use them, it just seemed like a cumbersome way to declare variables but afterwards I understand more about them, although I'm not completely sure about a practical use for them at the moment. As usual.
The author mentioned that you could use custom attributes in a database connection scenario, declaring the table and columns in attributes, in order to avoid coding up custom logic for each table and column you want to access. Admittely I am a bit tired now but I'm not sure how this would work. Perhaps like this?
[AttributeUsage(AttributeTargets.Property, AllowMultiple = False)]
public class TableNameAttribute : Attribute
{
private string tableName;
public TableNameAttribute(string tableName)
{
this.tableName = tableName;
}
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = True)]
public class ColumnAttribute : Attribute
{
private string columnName;
public ColumnAttribute(string columnName)
{
this.columnName = columnName;
}
}
[TableName("Customers")]
[Column("Name")]
[Column("Address")]
[Column("Phone")]
public bool Update()
{
// What the heck do I do here to use the attributes?
// Do I need to use GetCustomAttributes()
// and if so, how do I point it to
// this class?
}
All in all it seems like a clumsy way of doing things. Why not just do like this:
public bool Update(string tableName, array columns)
{
// Use the parameters passed to the class to update DB.
}
Or am I missing something?
