How to use code templates for entity properties

A

Andrus

I'm using VCS Express 2005.
I have a lot of property definitions in different classes like

public class MyEntity {
public string somestringfield {
get { return (string)Row["somestringfield"]; }
set { Row["somestringfield"] = value; }
}

public string someotherstringfield {
get { return (string)Row["someotherstringfield"]; }
set { Row["someotherstringfield"] = value; }
}
}

When template changes I must manually change it in hundreds of places.

I tried to use template

#define StringFieldTemplate( propname ) \
public string propname { \
get { return (string)Row[propname]; } }
set { Row[propname] = value; } \
};

public class MyEntity {
StringFieldTemplate("somestringfield2")
StringFieldTemplate("someotherstringfield2")
}

But got error.
How to implement this ?

Andrus.
 
M

Marc Gravell

C# does not expand macro expansion, for various reasons that have been
discussed many times.

Can I ask... what sort of changes are you talking about? And what sort
of architecture? For instance, if there is a common base-class it
might be possible to use a method from a base-class (i.e.
GetValue(...) and SetValue(...)) - although since this appears to be
Data-Table based I'm not 100% sure how well this would work.

If the code is *that* predictable, it might be possible to script the
changes? I have done this before using reflection (or
System.ComponentModel) to list the properties by Type. This might also
be something that something like "resharper" could help with (I'm not
a regular user of resharper, so I don't know if it can do this
particular type of tranform en-masse to an existing codebase).

Finally; are these properties to support the model ar the view? i.e.
are they for binding purposes or for coding purposes (or both). If
they are mainly for binding, then you might be able to put the main
code (i.e. the bit that might change) into a bespoke
PropertyDescriptor using a TypeDescriptionProvider.

Marc
 
A

Andrus

Can I ask... what sort of changes are you talking about?

They classical entity objects described everywhere.

I created in entity base class property Row

GetEntity() method fills Row property from database.

poperty getter and setter methods convert to/from Row
And what sort
of architecture? For instance, if there is a common base-class it
might be possible to use a method from a base-class (i.e.
GetValue(...) and SetValue(...)) - although since this appears to be
Data-Table based I'm not 100% sure how well this would work.

I have the following entity base class

public abstract class Entity {

public DataRow Row;

}

If the code is *that* predictable, it might be possible to script the
changes? I have done this before using reflection (or
System.ComponentModel) to list the properties by Type. This might also
be something that something like "resharper" could help with (I'm not
a regular user of resharper, so I don't know if it can do this
particular type of tranform en-masse to an existing codebase).

Finally; are these properties to support the model ar the view? i.e.
are they for binding purposes or for coding purposes (or both). If
they are mainly for binding, then you might be able to put the main
code (i.e. the bit that might change) into a bespoke
PropertyDescriptor using a TypeDescriptionProvider.

They are for use strong typing in code.
For binding to winforms textboxes etc. I use DataTable
entity object Row property refers to this datatable row.

I read your codeproject article and tried your code from previous thread but
havent found
a way to use dynamic properties.

So I started to manually create getter/setter method for every entity for
every property.

Andrus.
 
M

Marc Gravell

They are for use strong typing in code.
For binding to winforms textboxes etc. I use DataTable
entity object Row property refers to this datatable row.

So I started to manually create getter/setter method for every entity for
every property.

If you want to use them directly from code with strong typing, then
you need compile-time properties. I understand you are using a DataRow
as the storage, but by wrapping it you prevent yourself from using
DataView that would provide dynamic properties at runtime for you;
likewise, typed datasets would additionally provide compile-time
property support.

You might have already answered, but is there any specific reason you
can't just expose the DataRow (or a sub-class) directly? It just seems
a peculiar hybrid of an OO and DataTable solution.

So: where do you want to go? Scripting the properties seems like it
might be quite possible (assuming that the schema is available
somewhere) - but likewise, so might a typed DataSet (which would be
less effort). Dynamic properties remains an option, but doens't
provide the compile-type support you want, unless you back them with
compile-time properties, which puts you right back at square one. I'd
be looking at either scripting from schema, or typed DataSet from
schema.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top