473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Create new object properties based on datatable structure

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

Aug 9 '07 #1
5 4451
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


Aug 9 '07 #2
Yes it is possible (I have done it), but depending on your level of
experience you may wish to consider simply using a DataTable, and
dynamically adding columns. Personally I'm not a big fan of
DataTables.

To create dynamic properties, you need to know about the
component-model. But first: realise that (purely) dynamic properties
will *not* be avialable at compile time, but can be used for
data-binding. This means that it doesn't necessarily help devs much.
If you really want to generate programmable classes, then perhaps look
at other tools, such as sql-metal. I believe that you can use this to
generate simple classes from an existing database, but this will need
to be re-executed to adopt new changes. Equally, tools like
n-hibernate may have scope.

If you want mw to give an overview of dynamic properties via the
component-model, please say - but it is quite a large topic, and I
just want you to know that I'm not sure that it (by itself) will do
everything you want. However, the starting point is to understand
TypeDescriptionProvider (2.0) or ICustomTypeDescrptor (1.1 and 2.0)

Marc
Aug 10 '07 #3
Thanks Marc. I'll take your offer. Could you please share yoir knowledge
about how did you make possible what I'm trying to do?
I'm going to start reading about the topics you recommend. You did exactly
understand the point of what I need to do.
I'm aware that dynamics properties won't be able on design time and devs
would have to use Reflection, but I think that if I acomplish that, I'm
practically automating simple catalogs programming, just like a serial
production of programs.

TIA
Sergio

"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:Or*************@TK2MSFTNGP05.phx.gbl...
Yes it is possible (I have done it), but depending on your level of
experience you may wish to consider simply using a DataTable, and
dynamically adding columns. Personally I'm not a big fan of DataTables.

To create dynamic properties, you need to know about the component-model.
But first: realise that (purely) dynamic properties will *not* be
avialable at compile time, but can be used for data-binding. This means
that it doesn't necessarily help devs much. If you really want to generate
programmable classes, then perhaps look at other tools, such as sql-metal.
I believe that you can use this to generate simple classes from an
existing database, but this will need to be re-executed to adopt new
changes. Equally, tools like n-hibernate may have scope.

If you want mw to give an overview of dynamic properties via the
component-model, please say - but it is quite a large topic, and I just
want you to know that I'm not sure that it (by itself) will do everything
you want. However, the starting point is to understand
TypeDescriptionProvider (2.0) or ICustomTypeDescrptor (1.1 and 2.0)

Marc

Aug 10 '07 #4
I'll pen something over the weekend (about to get train) - a minor
point, but technically this isn't reflection.

Mar
Aug 10 '07 #5
I got a deja-vu moment...

The following covers the basics of creating a property-bag
component-model implementation - but please read the rest of this post
before following:
http://tinyurl.com/3b6wxz

Note that this simplified example only covers a single Type; for a
more extensible framework you need more code. Note that known
properties also can be added at compile time, ideally also referencing
the property-bag so that everything is considered equal. However! You
would still need to add the properties at some point, so you'd also
need some code to discover the schema. Adding proper opportunity to
handle property changes (before they happen) adds more code.

If all you want is simple data storage, then in this instance I would
probably advise you to avoid complexity, and just use a DataTable.
This is a rarity for me - I don't normally suggest such.

Finally, if you have 3.0, you might also wish to note that this
includes an additional model for properties, used (as parallel
implementations IIRC) by WF and WPF. You might be able to trick these
into doing a lot of the above for you, but again - keep it simple ;-p

Marc
Aug 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Olivier Jullian | last post by:
Hi, I'm new to .NET and am trying to take advantage of the object structure while accessing relational databases. I started a small project for managing "projects". Here is a description of...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
1
by: jg | last post by:
I need to create a DataTable object, but I dont want to add columns programatically, the DataTable should have columns based on a Table in my SQL database. How can I create a DataTable with column...
12
by: Doug Bell | last post by:
Hi, I am having problems trying to create a (temporary) DataTable from a selection from a DataGrid (dgOrders). dtOrdDetails is declared as a Public DataTable Sub is: Dim stFilter as String...
5
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
5
by: jehugaleahsa | last post by:
Hello: I am trying to find what is the very best approach to business objects in Windows Forms. Windows Forms presents an awesome opportunity to use DataTables and I would like to continue doing...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.