473,748 Members | 8,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
ConnectionStrin g
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, PropertyDescrip tor 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 4470
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.comwro te in message
news:uR******** ******@TK2MSFTN GP06.phx.gbl...
>I have a MustInherits Base class that implements a custom IDataLayer
interfase. IDataLayer expose CRUD methods. Base class constructor requires
two parameters:
ConnectionStrin g
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, PropertyDescrip tor 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
TypeDescription Provider (2.0) or ICustomTypeDesc rptor (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**********@g mail.comescribi ó en el mensaje
news:Or******** *****@TK2MSFTNG P05.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
TypeDescription Provider (2.0) or ICustomTypeDesc rptor (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
1273
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 my data structure : A project would be made of tasks, and each task can contain sub-tasks, so I would have something like
16
25419
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
7
8867
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 want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
0
3645
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 datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
1
1503
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 structure based on a SQL database? Thanks
12
2286
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 Dim OrdersRows() as DataRow
5
6913
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
10
3992
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 in the list A i want to associate an undetermined number of items. The complication for me is the fact that the items to associate in list A are undetermined.
5
2007
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 so. I have worked with dynamically generated DataTables with customized DataRows. However, these classes are usually pretty limited. For instance, you are limited to a select few data types and you can't deal with nulls as well as desired (in...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.