CSLA 3.6.2 Code Generation

CSLA 3.6.2 Code Generation

Old forum URL: forums.lhotka.net/forums/t/6940.aspx


Tom_W posted on Wednesday, May 13, 2009

Hi All

Does anyone know if there are any VB.NET code generation templates for CSLA 3.6.2?  I know CodeSmith are looking to add them, but I can't find any info about when they are likely to appear.

Specifically I'm looking for a template that uses the new RegisterProperty functionality to avoid the string literals.

The code gen is obviously only going to take us so far, but it seems pointless to write hundreds of sprocs and look up classes if we can avoid it! 

I know this question gets asked a lot, but what code gen tools do people prefer?  CodeSmith seems like the most widely used?  I had a look at CodeBreeze and CodeComplete, but neither looked that widely adopted from the amounts of posts on their forums, any thoughts/experiences?

I'm figuring that if we are going to need to adjust or write the templates ourselves (which seems likely) then going for the most widely adopted system with the most well known syntax is probably sensible?

On a related note, I'm in the process of reworking the standard CodeSmith (V5.1) Stored Procedure cst files to generate CSLA style sprocs (i.e. including the LastChanged column but not updating it, returning the NewLastChanged value, throwing an error if the record has changed etc).  If anyone wants these then let me know.

RedShiftZ replied on Wednesday, May 13, 2009

We use CodeSmith, but only just started. Price was the selling point for us.

Tom_W:

Does anyone know if there are any VB.NET code generation templates for CSLA 3.6.2?  I know CodeSmith are looking to add them, but I can't find any info about when they are likely to appear.


Can't help you there, But I'm currently rewriting the stock 3.6.2 templates for C# to use ADO.NET instead of L2S. (Along with some company specific requirements)

Tom_W:

On a related note, I'm in the process of reworking the standard CodeSmith (V5.1) Stored Procedure cst files to generate CSLA style sprocs (i.e. including the LastChanged column but not updating it, returning the NewLastChanged value, throwing an error if the record has changed etc).  If anyone wants these then let me know.


I would love to get a copy when you are done.

Jeff Young
KC, MO, US

whelzer replied on Wednesday, May 13, 2009

Tom_W:

On a related note, I'm in the process of reworking the standard CodeSmith (V5.1) Stored Procedure cst files to generate CSLA style sprocs (i.e. including the LastChanged column but not updating it, returning the NewLastChanged value, throwing an error if the record has changed etc).  If anyone wants these then let me know.


I would love to get a copy when you are done.

And me...Big Smile [:D]

mbblum replied on Wednesday, May 13, 2009

Over at CodeSmith, Blake has created beta templates for CSLA 3.6.2. See his blog entry (http://community.codesmithtools.com/blogs/blake/archive/2009/04/24/codesmith-csla-templates-part-1.aspx) Note they need the new v5.1 CodeSmith release. Also note they are not in the expected sample's Frameworks\CSLA directory, but in Frameworks\CodeSmith.CSLA. (Knowing they were supposed to be present, it still took me over 30 minutes to find them because of the name and not being in the expected directory!).

They are BETA! They had several problems with our database, and they highlighted some issues with the database. So I'm also working to resolve those database issues.

For example, they mis-read a FK and created the wrong Get method multiple times. I'm working with court cases, so the Person table with PersonID key holds judges, prosecutors, etc. The table has JudgeID, ProsecutorID, etc. with FK to PersonID. The templates created a GetByPersonID for each FK and tried to look up by PersonID in the table rather than JudgeID, ProsecutorID, etc.

Also, the Exclude RegEx does not seem to be working, at least a RegEx that works in PLINQO templates isn't removing the extra characters from SQL names in this template.

The templates are a starting point, but they need work yet. Unfortunately, other priorities have delayed me from working fully on them to get my objects upgraded from CSLA 2.0.

I would welcome collaborating to get these templates functioning, as I need them also.

wyz

lukky replied on Wednesday, May 13, 2009

Tom,

I've had a tough time deciding on a generator for my current project, but finally went with CodeSmith for a number of reason, ease of use being the top one. I tried (and even bought) other tools before deciding.

As for VB templates, I will simply say that my current project is a VB.NET one, but we've decided to do all our BusinessLayer in C# because of the availability of templates. So unless you're "religious" about VB, there's nothing keeping you from using a mix of C# and VB.

One other thing to consider is what technology do you want to use for the Data Access. Do you want to use embedded SQL in your code ? Do you want to use SPROCs ? Linq to SQL ? Entity Framework ?

I'm using a customized version of the CSLAContrib templates (by Rick Supit), which use Linq to SQL, and are available for CSLA 3.6. I've had to change a bunch of things to get them to my taste, but now they work fine.

One of the things about the available templates is the lack of customizing "hooks". For example, when  I wanted to customize the generated Linq to SQL query, I had to modify the template to add partial  methods so that all my customized code would go in a "static" code file.

So I guess whatever tool and templates you decide on, you will have a certain amount of customizing to do.

regards

cds replied on Wednesday, May 13, 2009

For my current project, I built my own code generator harness and am using the T4 code generator built into Visual Studio. I think this approach has a lot of merit. (See http://msmvps.com/blogs/kathleen/ and http://www.appventure.com/Downloads/T4Engine.aspx) At present, as I'm reverse engineering and rewriting an old system I have a SQL Server database that contains the meta-data for my new system which I'm using as the source of my code generation (well actually, I run a query on my metadata DB to generate some XML, then my T4 generator harness reads the XML which represents my business object definitions and uses these to generate my CSLA business objects code.)

I'm using a combination of raw ADO.NET and LLBLGenPro for my data access. So far it's working out pretty well.

I'm even using code generation to generate some of my XAML for some maintenance screens (I"m using CSLALight for my project).

When I get some free time I'm hoping to formalise this a bit more - at the moment it's just a console app that I run to re-do my code generation.

Craig

JoeFallon1 replied on Wednesday, May 13, 2009

I use Codesmith. I heard Kathleen on .NetRocks and she discussed T4. Sounds a lot like an early version of Codesmith to me! I guess the best thing about it is that it is "free" - if you don't count your time to learn it as costing anything. Anyway - sounds like you already chose Codesmith. Excellent program. I was one of the very early testers of it. (My name used to be in the About box somewhere - I doubt it is still there though.)

Joe

Tom_W replied on Friday, May 15, 2009

As ever, many thanks, some really good info. 

I've now had a thorough look at Blake's beta 3.6.x templates, the standard CodeSmith CSLA 3.5 templates and also Ricky's templates from CSLA.Contrib, they all seem grand, but as is always the case they all do slightly different things.. .and I want bits from all of them.  Time to roll my own I think.

What I'm doing now is trying to build a comparison chart for the three options above in order to try and get my head round the detailed features of each of the template sets.  If I get to the end of this I will post it, although considering the time constraints on the current project I think it's more likely we will pick one, use it to generate the base code and then just manually adjust the code as necessary.  Just using it for the first lump of code should save us a stack of time.


Specific answers to questions raised above:

@RedShiftZ and whelzer
I have finished these now and they seem to produce stored procedures that match up with Rocky's from ProjectTracker.  I haven't tested them yet though (will probably do that next week).  If you'd like them before this let me know, otherwise I will post them when I have used them for real.


@mbblum
Thank you so much, that saved me a lot of time!  It was confusing the hell out of me why there were two sets of CSLA templates in 5.1! 

I have exactly the same issue as you've had with the FK relationships, I think it's a template issue not a DB design issue, but I haven't gone into it in detail yet.  The problem I'm having is that every table's PK field is called 'ID', so when the template generates the FK methods it just creates say five methods all called 'GetID'. 

Incidentally, in the comments for these templates Blake gives a direct link for dowloading the latest templates: http://code.google.com/p/codesmith/downloads/list which might be useful if he releases a new version.


@Luc
Sticking to C# for the BLL is a good call, template availablility for VB seems pretty scarce. 

With regards to DAL technology; I'm pretty easy as long as I don't have to code it!  My only real objection to paramaterised sprocs is the mindnumbing coding of all the params, but if the code-gen does this then that's a moot point.  I like Linq-to-SQL but I don't like having to modify the dbml each time we adjust the DB Schema (which happens a lot..).  EF is probably the best solution for future proofing, but I think with learning CSLA and learning code gen we probably don't need to add any more unknowns into the mix!

@Craig and Joe
T4 is really interesting, but it's one step too far for us at the moment.  From a tech point of view I'm fascinated, but from a commercial point of view I just need something to help us get code out of the door faster!  I'd be fascinated to see it when you have formalised it though Craig.

JoeFallon1 replied on Friday, May 15, 2009

"use it to generate the base code and then just manually adjust the code as necessary. Just using it for the first lump of code should save us a stack of time."

You will be making a huge mistake if you manually adjust the code as necessary.

I strongly recommend usng inheritance (or partial classes) so that your Gen code is in one file and your hand written Developer code is in a derived class. In your code gen file just make some methods and properties Overridable. My DP_Fetch is just a start point - I call out to overridable methods like:

FetchData(criteria, cn)
PostFetchData(criteria, cn)
FetchChildren(criteria, cn)

That way if I need to override something about Fetch I can just override one of these methods and leave the Base behavior alone.

e.g. Suppose I want to "borrow a field" from another table. I can override FetchData, call MyBase.FetchData and then run another query to get the one extra piece of information. The field and property are in the developer class as is the override to FetchData. But otherwise it is just empty.

You can also code gen validation rules and add extra rules in the hand written class: MyBase.AddBusinessRules.

Partial classes do not allow overrides so there is a different technique involving hooking events. I don't use that so if you want to use partial classes get some more advice.

Joe

Tom_W replied on Friday, May 15, 2009

Thanks Joe, that's sound advice and a good compromise.

My gut says go for inheritance, but most of the templates I've looked at seem to use the partial class approach, and the code behind structure is a neat way of managing it all.  I need to spend some time getting my head round how partials work and then make a decision on partial classes vs. inheritance. 

I also need to get some standard base class functionality of our own into the mix, which I'd like to do without modifying the CSLA framework classes, so I need to tweak the code gen templates to create classes that inherit from our own custom classes (which then inherit from CSLA).  I know Ricky's templates allow for this concept so this are probably the place to start.


JoeFallon1 replied on Friday, May 15, 2009

Phew!

I use inheritance. I like Overriding methods. It is clearer to me what is happening.

I also have a base class for each BO stereotype.
All my BOs inherit from my Base classes.

Some have quite a bit of code- others are almost empty. But they are great placeholders for adding stuff to all your BOs of that stereotype.

Joe

Tom_W replied on Monday, June 01, 2009

Tom_W:
Hi All

On a related note, I'm in the process of reworking the standard CodeSmith (V5.1) Stored Procedure cst files to generate CSLA style sprocs (i.e. including the LastChanged column but not updating it, returning the NewLastChanged value, throwing an error if the record has changed etc).  If anyone wants these then let me know.



Apologies, development of these rather stalled.  However, I have attached the last version I did and from a visual inspection they seem to produce sprocs that line up with Rocky's.  Anyone is welcome to try them out and modify them as they see fit etc..  No warranties, no support provided etc!

Tom_W replied on Monday, June 01, 2009

Also, on the subject of Codesmith's new 3.6.x templates; these seem some way from being completed at present, but Blake has done some great work from what I've seen so far.  They look like they have the potential to be a really good resource.

There is a thread discussing them and for feedback if anyone's interested:
http://community.codesmithtools.com/forums/p/9322/36217.aspx

Copyright (c) Marimer LLC