473,383 Members | 1,846 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,383 software developers and data experts.

Code Generation

Hello,
I hear some hypes about creating code to generate C# or VB .NET code, that
is, code generation (sounds straight forward enough). I haven't really seen
how it's done in real world. I'm wondering in what kind of situation code
generation makes most sense. I'm think that sort of tricks can be done in
relation to unit testing. Has anyone done code generation and can you share
your story with the rest of the world?

TIA
Nov 15 '05 #1
9 2458
Hello

In my case, I used code generation to generate data access classes in c#.
I made a tool that reads the database schema (tables, fields, primary keys,
foreign keys, data types, etc) and generate one class per table with private
fields holding the data, and public properties to access these fields. And a
method for insert, update, delete from datable

The advantage of doing this rather than using a dataset / datatable is first
speed (in data sets all data are stored as objects, which means a lost of
type casting / boxing / unboxing occurs when reading and writing data in the
dataset.) and compile time type checking.

Best regards

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
Hello,
I hear some hypes about creating code to generate C# or VB .NET code, that
is, code generation (sounds straight forward enough). I haven't really seen how it's done in real world. I'm wondering in what kind of situation code
generation makes most sense. I'm think that sort of tricks can be done in
relation to unit testing. Has anyone done code generation and can you share your story with the rest of the world?

TIA

Nov 15 '05 #2
Tool used to build software for the business, using code generation

http://www.manager-erp.com/eng/products/its/platform/

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
Hello,
I hear some hypes about creating code to generate C# or VB .NET code, that
is, code generation (sounds straight forward enough). I haven't really seen how it's done in real world. I'm wondering in what kind of situation code
generation makes most sense. I'm think that sort of tricks can be done in
relation to unit testing. Has anyone done code generation and can you share your story with the rest of the world?

TIA

Nov 15 '05 #3
Excellent article from MSDN:
http://msdn.microsoft.com/msdnmag/is...n/default.aspx

It is commonly used to generate data access layer code, or simple user
interface code (add/update/delete/list for database table operations, for
example).

Hope this helps,
--- Nick

"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
Hello,
I hear some hypes about creating code to generate C# or VB .NET code, that
is, code generation (sounds straight forward enough). I haven't really seen how it's done in real world. I'm wondering in what kind of situation code
generation makes most sense. I'm think that sort of tricks can be done in
relation to unit testing. Has anyone done code generation and can you share your story with the rest of the world?

TIA

Nov 15 '05 #4
Ditto on data access. The code generation for strongly typed datasets that
comes with VS.Net doesn't meet our needs, so we generate code that provides
strongly typed data access for all of the tables in our database.
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:uQ**************@TK2MSFTNGP09.phx.gbl...
Hello,
I hear some hypes about creating code to generate C# or VB .NET code, that
is, code generation (sounds straight forward enough). I haven't really seen how it's done in real world. I'm wondering in what kind of situation code
generation makes most sense. I'm think that sort of tricks can be done in
relation to unit testing. Has anyone done code generation and can you share your story with the rest of the world?

TIA

Nov 15 '05 #5
That's interesting.
What about code generation in relation to declarative programming? Do
you know anything about it?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
I'm afraid not. If you could throw a "for instance" at me, it might help.
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...
That's interesting.
What about code generation in relation to declarative programming? Do
you know anything about it?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #7
Thanks for your reply.
OK, for instance, let's say in the design phase, you declare all the
classes and their members in a document. The document could be a
specially made Word document. From there, you'd generate code out of the
document. That way, especially in a regulated environment, the document
and code go together very well. Of course, we could generate document
out of the code, but that's against the concept of declarative
programming and everyone of us know that catching any logical errors in
design phase is much cheapter than catching them in later phase.

The reason I'm interested in this that Scott Hanselman (RD in Oregon)
mentioned about it in .NET Rocks radio show. And I'm basically tortured
by the amount of documentation I have to do after I code at work as
well...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #8
Now I understand. We chose to purchase on that one:

We are just now beginning to implement Rational XDE. I haven't used it
enough to give a good review, but it does a lot of what you are talking
about.

It's a UML modeling tool, and (unlike Visio) it supports round-tripping.
XDE actually runs inside of VS.net, so the diagram surface is just another
tab in the code editor.
I can, for example, sketch out a class in a class diagram, and instantly see
the code declarations. If I put text in the documentation window for the
UML diagram, it inserts that text as a <summary> xml comment in the code.
If I modify the code directly (example: I add a helper method that doesn't
really affect the architecture of the class), the diagram is instantly
updated to show the new method.

Looks pretty cool so far -- it better be; it was not cheap.

On a more general note, the problem always seems to be in maintenance. For
some types of classes (especially those with no artificial behavior), you
can just always change the document and regenerate new code wholesale.
Often though, I seem to run into situations where some behavior of code that
you want to generate needs to be tweaked or customized in some way. Unless
the document is rich enough to catch all of the important nuances, you won't
be able to overwrite the existing code with newly generated code without
fear of losing some manually applied changes to the method bodies. On the
other hand, if the document format is rich enough to capture all of the
important details, at what point does it cease to be a document, and become
the programming language? i.e.: when does the level of detail in the
document become so rich as to drown out the clarity that you were trying to
create in the first place?

My colleagues and I have been debating this a lot lately, as we try to
figure out exactly what role our UML models will have in the organization.
The two extremes seem to be
(super weak): the drawings are really just reference material, created after
the fact
(super strong): all code is generated from the models with no manually
entered code.

Now obviously, those are at the very edges of the spectrum, and the right
balance for us lies somewhere in the middle. We've been trying to figure
out exactly where that point is.

FWIW: in our data classes (which we generate from an inhouse generator), we
found that we do have to modify behavior in some cases. Our solution was to
generate 2 levels of code: an abstract class that is "generate only". It
will always be overwritten, and hand-coded sections are not allowed (all the
strong typed field definitions, ect go here) and layer 2, the
implementation, which is auto-generated once, and then developers are
allowed to override methods etc to modify behavior. There are some inherent
problems with just doing that, because in the future the data model might
change (incremental and major upgrades), so we had to write some custom
merge code for handling changes due to modification to the database
structure. It was kind of a bummer, but we've got it working pretty well
now. Of course, Whidbey, generic types, and partial classes would have
helped us out a lot with some of this stuff.
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
Thanks for your reply.
OK, for instance, let's say in the design phase, you declare all the
classes and their members in a document. The document could be a
specially made Word document. From there, you'd generate code out of the
document. That way, especially in a regulated environment, the document
and code go together very well. Of course, we could generate document
out of the code, but that's against the concept of declarative
programming and everyone of us know that catching any logical errors in
design phase is much cheapter than catching them in later phase.

The reason I'm interested in this that Scott Hanselman (RD in Oregon)
mentioned about it in .NET Rocks radio show. And I'm basically tortured
by the amount of documentation I have to do after I code at work as
well...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #9
You are absolutely right about "balance". If we have to explain everything
we code, then why does even document exist, right?

I've used XDE for some time now. So far, it's nothing but a diagram tool
that allows me to create diagram so that I can communicate with my
colleagues on object model. Maybe it's my ability to really look for good
ways to utilize it, but I haven't found it very useful. It IS a cool tool
though.

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
Now I understand. We chose to purchase on that one:

We are just now beginning to implement Rational XDE. I haven't used it
enough to give a good review, but it does a lot of what you are talking
about.

It's a UML modeling tool, and (unlike Visio) it supports round-tripping.
XDE actually runs inside of VS.net, so the diagram surface is just another
tab in the code editor.
I can, for example, sketch out a class in a class diagram, and instantly see the code declarations. If I put text in the documentation window for the
UML diagram, it inserts that text as a <summary> xml comment in the code.
If I modify the code directly (example: I add a helper method that doesn't
really affect the architecture of the class), the diagram is instantly
updated to show the new method.

Looks pretty cool so far -- it better be; it was not cheap.

On a more general note, the problem always seems to be in maintenance. For some types of classes (especially those with no artificial behavior), you
can just always change the document and regenerate new code wholesale.
Often though, I seem to run into situations where some behavior of code that you want to generate needs to be tweaked or customized in some way. Unless the document is rich enough to catch all of the important nuances, you won't be able to overwrite the existing code with newly generated code without
fear of losing some manually applied changes to the method bodies. On the
other hand, if the document format is rich enough to capture all of the
important details, at what point does it cease to be a document, and become the programming language? i.e.: when does the level of detail in the
document become so rich as to drown out the clarity that you were trying to create in the first place?

My colleagues and I have been debating this a lot lately, as we try to
figure out exactly what role our UML models will have in the organization.
The two extremes seem to be
(super weak): the drawings are really just reference material, created after the fact
(super strong): all code is generated from the models with no manually
entered code.

Now obviously, those are at the very edges of the spectrum, and the right
balance for us lies somewhere in the middle. We've been trying to figure
out exactly where that point is.

FWIW: in our data classes (which we generate from an inhouse generator), we found that we do have to modify behavior in some cases. Our solution was to generate 2 levels of code: an abstract class that is "generate only". It
will always be overwritten, and hand-coded sections are not allowed (all the strong typed field definitions, ect go here) and layer 2, the
implementation, which is auto-generated once, and then developers are
allowed to override methods etc to modify behavior. There are some inherent problems with just doing that, because in the future the data model might
change (incremental and major upgrades), so we had to write some custom
merge code for handling changes due to modification to the database
structure. It was kind of a bummer, but we've got it working pretty well
now. Of course, Whidbey, generic types, and partial classes would have
helped us out a lot with some of this stuff.
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
Thanks for your reply.
OK, for instance, let's say in the design phase, you declare all the
classes and their members in a document. The document could be a
specially made Word document. From there, you'd generate code out of the
document. That way, especially in a regulated environment, the document
and code go together very well. Of course, we could generate document
out of the code, but that's against the concept of declarative
programming and everyone of us know that catching any logical errors in
design phase is much cheapter than catching them in later phase.

The reason I'm interested in this that Scott Hanselman (RD in Oregon)
mentioned about it in .NET Rocks radio show. And I'm basically tortured
by the amount of documentation I have to do after I code at work as
well...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #10

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
8
by: Max M | last post by:
Yesterday there was an article on Slashdot: http://books.slashdot.org/article.pl?sid=03/09/04/1415210&mode=flat&tid=108&tid=126&tid=156 It is about automatic code generation. I got interrested...
0
by: Rasmus Fogh | last post by:
Someone raised the question of automatic code generation a few weeks back. And yes, we (CCPN) are using automatic Python code generation in a major way. Basically we are making data models in...
8
by: Wilk Teverbaugh | last post by:
I'm using get rows to build a table for a calendar. The specific view for this calendar is for an entire month. Each appointment slot is one half hour long. If I were to generate a page for the...
1
by: Gina | last post by:
I need to add the cell generation to a templated program. I am using graphics magician, but my problem is with the math. I cannot figure out my cell generations. I do know that I need two...
8
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and...
4
by: Julia | last post by:
Hi, Is there any why I could use attribute or something else to instruct the compiler to inject a specific code for a property? for example i have the following property and i would like that...
1
by: George Meng | last post by:
I want to evaluate some code generation tool for my company. Want to get some idea from you. The bottom line is I want to get a tool to generate both store procedure as well as business object...
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
1
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.