CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jay Kimble -- The Dev Theologian

Philosophizing about the .Net religion

A new approach to Code Generation...

[I've been busy lately; sorry I haven't blogged much.  Working For the How To Select Guides has kept me pretty busy].

During the whole ORM debate on my blog here, both Frans and Paul Wilson pointed out a couple things with using tools like CodeSmith that I have been pondering (BTW, if I haven't mentioned it I love this CodeSmith! Intellisense Rocks!).  Paul mentioned that he truly believed that O/R technology was superior; he did accept that there may be ways that templating could achieve some of the same effect (he also admitted that he had never seen it happen because there is always maintenance).

I've been thinking about all this and I have been pondering ways to make my templated code more maintainable or better yet, get things to a point where I somewhat don't have to maintain code at all.

The idea I have come up with to create a CodeSmith template (I'm sure this can be done with other templating tools like MyGeneration) that let's me build CodeSmith Templates.  So in my current Data Access Layer templates I end up with the following classes (sorry this won't be full blown code):

Class sqlSomething
 ' Constants containing stored proc names, parameters, etc.
 
 ' Actual Calls
 Public Shared Function LoadSomethingData(param1 as int, param2 as string, etc....) as SomethingData
  ' Code would go here
 End Function

 Public Shared Sub ActionNameSomethingData(param1 as int, param2 as string, etc....)
 End Sub
 
 'Additional Stored Procs will follow
 
End Class

'-------------------------------------------------------

Class SomethingData ' Business Object
 ' Properties that hold values
 
 ' plus fromstring/ToString methods to read from/create a pipe delimitted array of data
End Class
'-------------------------------------------------------

Class SomethingCollection ' Collection of SometingData
 Inherits BaseCollection

 ' Code goes here
End Class
'-------------------------------------------------------
Class SomethingController ' Singleton that contains all
     ' the actions against the bus. object or the collection

 Public Shared Function GetInstance() As SomethingController
 End Function

 ' Actions
 Public Function Load(param1 as int, param2 as string, etc....) as SomethingData
  return sqlSomething.LoadSomethingData(...)
 End Function

 ' Additional Actions that call a stored proc and use a SomethingData object
End Class

My CodeSmith template builder template would simply accept a collection of some new type.  The new type would contain an action name, and what type of data that action would retrieve (does it return an object, value -- first field, true if there's a record, etc.-- or no value at all).  [I guess it might be possible to do this without a template generator]  The actual template generated would have properties/parameters for each action defined and would build code that knew whether that method produced an object, a value, or was simply a sub (void to you C#ers).

I could then archive this generated template with my code and if something changed like a field changed in the database I could simply regen from my saved template and move on...  I may still need to work with the frontend (but that always happens... I could at least minimize the effect).

What do you guys think?



Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home
Current Threat level
Terror Alert Level