For Those Using CodeSmith for CSLA 2.x

For Those Using CodeSmith for CSLA 2.x

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


rhoeting posted on Friday, September 22, 2006

Is it safe to assume that the codegen strategy for 2.0 is to put plumbing in a base clase (ie.  CustomerBase) and hand-crafted, custom logic on the subClass (ie. Customer)?   Or are people using the "partial class" separation strategy?

The next question is could someone post a minimal example of what a custom class looks like?... and I'm talking the most minimal code necessary to make the class "whole", if that makes sense...

TIA,

Rob

m.harper replied on Friday, September 22, 2006

We are using "partial class" separation strategy.  One file is the "editable" file which contains the custom logic, the other file contains the Code Generated code and no changes can be made to this file by hand.

 

Michael

skagen00 replied on Monday, September 25, 2006

Partial classes here too in terms of code generation.

rhoeting replied on Tuesday, September 26, 2006

So if you folks use the partial class approach, how do you inject custom code at particular points in an object's lifecycle?  For example, in the inheritance approach, you can create empty methods in your base class, for example:

OnDataPortalCreateBegin()

OnDataPortal()

And simply override them in the custom class to add custom behavior... Who do do folks inject custom behavior in the partial class world?

Rob

skagen00 replied on Tuesday, September 26, 2006

Hey there, the answer is delegates.

private void UpdateChildren(SqlConnection cn)

{

   _names.Update(cn, this);

   _contactInfo.Update(cn, this);

   _relationships.Update(cn, this);

   _roles.Update(cn, this);

   onUpdateChildrenComplete(cn);

}

Above is some generated code. onUpdateChildrenComplete() will call any handlers attached to the delegate - so there is a place in code to hook up delegates (Initialize and Deserialize) to appropriate handlers in the non-generated partial.

If one has a base "Entity" class inserted right beneath BusinessBase, it's certainly possible to insert logic in that manner too. But the delegate is a nice clean approach for handling the partial class issue without requiring inheritance.

skagen00 replied on Tuesday, September 26, 2006

Here's one example about hooking up the delegates:

protected override void Initialize()

{

   base.Initialize();

   this.addCustomRulesDelegate += AddCustomRules;

   this.addInsertParamatersCompleteDelegate += AddCustomParameters;

   this.addUpdateParamatersCompleteDelegate += AddCustomParameters;

   ...

}

And then my handler to add "custom business rules" might look like this:

protected override void AddCustomRules()

{

   ValidationRules.AddRule<Individual>(IsValidSocialSecurityNumber, "SocialSecurityNumber");

   ...

}

ajj3085 replied on Tuesday, September 26, 2006

Don't forget you'll have to override OnDeserialized to hook up the events again.

skagen00 replied on Tuesday, September 26, 2006

Absolutely.

protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)

   {

   base.OnDeserialized(context);

   this.addCustomRulesDelegate += AddCustomRules;

   this.addInsertParamatersCompleteDelegate += AddCustomParameters;

   this.addUpdateParamatersCompleteDelegate += AddCustomParameters;

   ...

}

rhoeting replied on Tuesday, September 26, 2006

Thanks guys, that's pretty slick I must say.  I guess I should have figured that out!

Are you generally happy with CSLA/CodeGen combo? 

Rob

 

skagen00 replied on Tuesday, September 26, 2006

I use CodeSmith 2.0 C# templates (partial classes) with CSLA 2.1 right now and I'm very happy to have taken the code gen plunge.

There's another option, one that Xal has (that I think is most up to date for VB).

If you go with the CodeSmith 2.0 templates, I should mention that you can find CodeSmith version 2.6 out there, which is free and as of the last version of C# templates seems to work just fine.

I would recommend keeping a log of the exact changes you make to the templates. I didn't do this the first time and I found myself looking line by line with a code comparison program to get myself upgraded to the newest templates.

You can get 2.6 here: http://www.codesmithtools.com/freeware.aspx

Good luck

rhoeting replied on Tuesday, September 26, 2006

skagen00,

Good to hear it's working for you.  We used CS on a project with CSLA1.5 and it was lights out!  We did have trouble dealing with template changes.  So How do manage the whole "template change control" process?  As far as I know CodeSmith doesn't integrate into source safe.

Rob

skagen00 replied on Tuesday, September 26, 2006

To be honest I just keep a word document documenting the deviations I've made along with a copy of the templates I've made the deviations from (the ones that one can download now).

Brian Criswell replied on Tuesday, September 26, 2006

What aspect of SourceSafe are you having trouble working with CodeSmith?

JoeFallon1 replied on Tuesday, September 26, 2006

I use Inheritance, not partial classes.

So I would have a CustomerBase class with all the code generated stuff in it and then the Customer class would Inherit it. I Override any methods that need to be tweaked. I also coded the DataPortal_XYZ methods to simply call a bunch of smaller methods which are all overridable. That way most of the code stays in the gen level and I only have to Override small bits at a time to tweak functionality.

I have my own set of Codesmith templates that use my DAL code so I do not use the public versions.

I did check them out recently though and they are pretty good. At least as a starting point. It is always best if you don't hack them up on your own but get changes implemented (by ricky or whoever) and then just keep up. But my set diverged early on and I have been maintaining my own. I check them into VSS so source control is not an issue.

Joe

 

 

rhoeting replied on Thursday, September 28, 2006

Brian,

I would love to be able to "right-click" a template in CodeSmith Studio and do check-in and check-out of source safe.  As far as I know, it's not integrated in that way.

 

Rob

Brian Criswell replied on Thursday, September 28, 2006

No, it is not.  I get around this at home and at work by using Subversion.  So the templates are not read-only by default.  This also makes versioning generated code very easy.  SourceSafe does have a command line interface that you could try tying into, but I am not sure how far you could extend that part of CodeSmith.  Nor does CodeSmith seem to have a plug in architecture from what I can see.

Copyright (c) Marimer LLC