If the goal is to write less code and nothing much else, you might be better
off by generating proper classes from tables / views / stored procedures
beforehand.
If you want to generate dynamically, you can do that:roughly like this
- get metadata from database for target table / view / sp
- generate code for a class in text file or stream exposing required or all
properties. You can set attributes here as needed
- compile the code using reflection or available compiler into assembly, for
example as .dll
- register dll and use it or load assembly and instantiate class
- provide class instance to consumer
Take a look at reflection namespace for details.
However, if you provide object like this, programmers will need reflection
too to detect which properties to use and what are types / names etc. It's
same stuff as "database structure knowledge" because they need to find out
what are the properties instead of table fields. I don't believe you'll save
much coding or investigations in this way. More likely whole contraption
will become less stable. If you think about it, it's same concept as using
raw data reader, where you can access collection of field or column names,
without knowing beforehand what are the fields / types. You can subclass
datareader or dataset / datatable exposing generic access property and
analyze for each call if custom attribute is applicable, but this doesn't
help in any way other people trying to use the class. Not really useful in
my opinion.
I would recommend to use some class generator and pre-create classes
beforehand. If db structure is changed frequently, you can put all
pre-generated classes in separate assembly to make upgrade process simpler.
Usually structure of db is pretty stable.
"Sergio Montero" <ge************@hotmail.comwrote in message
news:uR**************@TK2MSFTNGP06.phx.gbl...
>I have a MustInherits Base class that implements a custom IDataLayer
interfase. IDataLayer expose CRUD methods. Base class constructor requires
two parameters:
ConnectionString
TableName
Another assembly, sharing the root namespace, contains a set of Custom
attributes used to validate properties values, just like the Validation
Application Block.
Base class is compiled into an Assembly.
I'd like to be able to give Base class the ability to add itself a set of
properties that match a datatable structure once the constructor is
executed and also have the posibility to add a customattribute to any of
these properties.
The set of properties could be as diferent as the Tablename target
changes.
I'd like programmers that consume Base class assembly could write less
code and need less database structure knowledge.
I've been Googling articles about IExtender, PropertyDescriptor and other
topics without have a clear idea to where to point all the effort.
Is, the scenario described, posible to achieve?
Could anybody point me to the rigth direction?
I'll appreciate any help.
T.I.A.
Sergio Montero
MX