Custom attribute ?

  • Thread starter Thread starter Pete Kane
  • Start date Start date
P

Pete Kane

Hello All, I have a method in my App class called GetText, this function
accepts a string as input param, and returns a corresponding string
value from a lookup table, the table has two fields, KeyName and
KeyValue, my concern is , as and when I add more methods and classes
that will utilise this function I have to remember to add records to the
the underlying lookup table on the "live" server

e.g.

string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

in this case I have to add

KeyName KeyValue
"GetSiteName" "London"

to the lookup table, I got bitten by this recently ( i.e. forgot to add
the record ) and am trying to think of a way to avoid doing so in the
future - one way I've thought might be ok, is, decorate all the methods
that use the function with a custom attribute, e.g.

[GetTextAttribute("GetSiteName")]
string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

and in the app use Reflection to list all attributes of type
"GetTextAttribute" that are not present in the lookup table. Does this
seem a reasonable approach ?
 
But if you really want to use a custom attribute, I can't think of any
obvious, strong reason not to.

Perhaps I didn't get exactly what he was trying to do, but going the
attribute route sounded like a real Rube Goldberg solution to me.
 
But if you really want to use a custom attribute, I can't think of any
obvious, strong reason not to.

Perhaps I didn't get exactly what he was trying to do, but going the
attribute route sounded like a real Rube Goldberg solution to me.
 
Peter said:
[...]
[GetTextAttribute("GetSiteName")]
string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

and in the app use Reflection to list all attributes of type
"GetTextAttribute" that are not present in the lookup table. Does this
seem a reasonable approach ?

For some definitions of "reasonable", I suppose. I'm not really clear
on why simply testing the application isn't sufficient. Presumably if
the table doesn't have the necessary key and value, you'll get an error
at run-time. If you're worried about testing coverage, I would put more
effort into unit testing, which is going to be more generally useful
anyway.

But if you really want to use a custom attribute, I can't think of any
obvious, strong reason not to.

Pete

Thank you for introducing me to Rube Goldberg :-) - I do unit testing
but on a different network and sometimes forget to add any new values to
the live environment - thanks for your comments
 
Peter said:
[...]
[GetTextAttribute("GetSiteName")]
string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

and in the app use Reflection to list all attributes of type
"GetTextAttribute" that are not present in the lookup table. Does this
seem a reasonable approach ?

For some definitions of "reasonable", I suppose. I'm not really clear
on why simply testing the application isn't sufficient. Presumably if
the table doesn't have the necessary key and value, you'll get an error
at run-time. If you're worried about testing coverage, I would put more
effort into unit testing, which is going to be more generally useful
anyway.

But if you really want to use a custom attribute, I can't think of any
obvious, strong reason not to.

Pete

Thank you for introducing me to Rube Goldberg :-) - I do unit testing
but on a different network and sometimes forget to add any new values to
the live environment - thanks for your comments
 
Pete Kane said:
Peter said:
[...]
[GetTextAttribute("GetSiteName")]
string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

and in the app use Reflection to list all attributes of type
"GetTextAttribute" that are not present in the lookup table. Does this
seem a reasonable approach ?
-snip-
Thank you for introducing me to Rube Goldberg :-) - I do unit testing
but on a different network and sometimes forget to add any new values to
the live environment - thanks for your comments

Basically you're hard coding the values, and doing so in a rather
bizarre way. If hard coding is what you want to do, why not just do
so? Also, are you certain that you'll never want to delete existing
values?
 
Pete Kane said:
Peter said:
[...]
[GetTextAttribute("GetSiteName")]
string MethodOneInSomeClass()
{
return App.GetText("GetSiteName")
}

and in the app use Reflection to list all attributes of type
"GetTextAttribute" that are not present in the lookup table. Does this
seem a reasonable approach ?
-snip-
Thank you for introducing me to Rube Goldberg :-) - I do unit testing
but on a different network and sometimes forget to add any new values to
the live environment - thanks for your comments

Basically you're hard coding the values, and doing so in a rather
bizarre way. If hard coding is what you want to do, why not just do
so? Also, are you certain that you'll never want to delete existing
values?
 
Back
Top