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
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
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
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.
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");
...
}
Absolutely.
protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)
{
base.OnDeserialized(context);
this.addCustomRulesDelegate += AddCustomRules;
this.addInsertParamatersCompleteDelegate += AddCustomParameters;
this.addUpdateParamatersCompleteDelegate += AddCustomParameters;
...
}
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
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
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
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).
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
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
Copyright (c) Marimer LLC