views:

48

answers:

2

Hi all,

I have a custom attribute that accesses the database with nhiberate. The attribute actually inherits from Castle's AbstractValidationAttribute and is used to validate properties against regular expressions. Problem is, these regular expressions are cms managed and stored in the database, and when you change the values in the database, they are not reflected in the attribute. Basically, the attribute only initializes and there fore loads from the database once. Is there any way around this?

Thanks, Robin

A: 

In my book it feels a bit odd to have an attribute perform database operations. That said, the attribute object is instantiated each time you call GetCustomAttributes for instance, so if the attribute picks up the data as part of its construction, it should fetch it whenever attributes are inspected using that method.

Fredrik Mörk
It is not that uncommon to have validation have to go to the database. When you look at the Validation Application Block, it's contain a `CreateValidator` method that allows to create a validator that communicates with the database. This allows good separation of concerns, but functional, effect is the same. The attribute enables validation by reading the database.
Steven
@Steven: I don't find it strange to have validation depend on the DB, but I do find it a bit odd to access the DB from an attribute class.
Fredrik Mörk
Is there a better approach to this that will get round the problem?
robinbetts
You can't really go around this. What you can do is create a 'validator class' and let the attribute create a new instance of that class and let that class do thee validation. This is the approach VAB takes and conforms to the SRP.
Steven
A: 

Attributes generally don't do anything. They are markers. Other code should use them to perform some operation based on the data used in the attribute.

clocKwize