Continuous Learning and Sharing of Team Foundation Server and Application Lifecycle Management RSS 2.0
# Friday, June 08, 2007

 

Objects that are tightly coupled to the table that they reflect can be referred to as DataObjects.  BusinessObjects, however are objects that represent a logic unit contain the attributes and methods of the business object.  Using CSLA.NET, CodeSmith Tools for code generation, and partial classes in .NET 2.0 we are able to take the benefits of code generation and a straightforward DAL with the flexibility and options needed for designing Business Objects.   Below are some of the features of my templates that allow me to generate my Business Objects based on table schema but still allow flexibility

Partial Classes

Partial Classes is a key feature to my entire code generation process. The golden rule for generating code is to “Never edit generated code”.  With partial classes this is easy.  All of my custom functionality can be put in to my “NoGen” partial class file.  All of the other features actually contol how and what is generated but I included partial classes because it really is fundamental to code generation and allows unlimited custom functionality added to a pure DataObject.

Foreign Key Fields

This started out a way to display both the key and value data of a code in a particular row.  For example if I had an Employee table with a RoleID column, I could add a RoleName “Foreign Key Field” to the template properties.  This would generate the property just like it was a column in the table.  It is really handy for binding drop down lists to your business object and wanting to display the text value somewhere else without having to go look it up again.  This feature started out as always generating the property as a string and has grown into a delimited array where I specify the column and the datatype.  Keep in mind this will generate code for the fetch, update, and insert logic.

Require Gen  Column

Sometimes I don’t want the property to be generated but I still want the fetch, insert, and update data portal functions generated.  In this case, I specify in my template properties, to skip particular property generation. Then I can create the custom property in the no-gen partial class.  One example for this is if you have a lookup id field in your table and want the property to be an enumeration instead of an integer.

“GenOnly” Tables

“GenOnly” tables are tables that I create to base my object when the data isn’t in the format I want the data displayed.  For example I had a table with 6 columns each with a date.  If I bound this to a grid, each data for a particular row would display across in column s.  However the users wanted to see each date in its own row.  I couldn’t change the format the data is stored, so I created a “GenOnly” table with the schema in the format I wanted.

MyDates Table

·         MyDateID

·         Year1Date

·         Year2Date

·         Year3Date

·         Year4Date

·         Year5Date

·         Year6Date

GenOnly_MyDates

·         MyDateID

·         YearNumber

·         YearDate

Now I can use my templates to generate my readonly collection and object classes based on the “GenOnly” table.  I then create the SELECT stored procedure to pivot the data into the format of my objects.  Lastly I can load the objects and bind it to my grid so the data displays down.  In this example the data is readonly so I didn’t need to worry about deletes and updates.  However, if I needed to that I could create the UPDATE and DELETE procs to use a CASE statement or some dynamic SQL.

I like to think you can get the best of both worlds in using a code generator to generate your objects based on table schema but still have some flexibility because something will be different. 

Code Smart Not Hard

Mike

Friday, June 08, 2007 10:54:10 PM (Central Daylight Time, UTC-05:00)  #    Comments [0] -
Code Generation | CSLA.NET

Visual Studio ALM MVP
Microsoft Visual Studio ALM MVP
Archive
<December 2014>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Mike Douglas
Sign In
Statistics
Total Posts: 102
This Year: 8
This Month: 0
This Week: 0
Comments: 90
All Content © 2014, Mike Douglas
DasBlog theme 'Business' created by Christoph De Baene (delarou)