After a late night of hacking I have finally got an end to end transformation of E# validation rules into CSLA code that compiles. I am using the NVelocity code generator I created to do this, here is the example entity I have defined:
using NBusiness.Frameworks.Csla.Templates; using Csla.Validation.CommonRules;
family Test { entity A as EditableRoot { field auto id int aid; field nullable string data; field nullable double value;
validate data StringMaxLength { MaxLength : 10 }; } } |
After running this through the compiler here is the code that it generated:
using System; using Csla; using Csla.Validation; using System.Collections.Generic;
namespace Test { [Serializable] public partial class A : BusinessBase<A> { #region Properties private static PropertyInfo<int> aidProperty = RegisterProperty<int>( typeof(A), new PropertyInfo<int>(“aid”)); /// <summary> /// aid property /// </summary> public int aid { get { return GetProperty<int>(aidProperty); } } private static PropertyInfo<string> dataProperty = RegisterProperty<string>( typeof(A), new PropertyInfo<string>(“data”)); /// <summary> /// data property /// </summary> public string data { get { return GetProperty<string>(dataProperty); } set { SetProperty<string>(dataProperty, value); } } private static PropertyInfo<System.Nullable<double>> valueProperty = RegisterProperty<System.Nullable<double>>( typeof(A), new PropertyInfo<System.Nullable<double>>(“value”)); /// <summary> /// value property /// </summary> public System.Nullable<double> value { get { return GetProperty<System.Nullable<double>>(valueProperty); } set { SetProperty<System.Nullable<double>>(valueProperty, value); } } #endregion
#region Relationships #endregion
#region Validation protected override void AddBusinessRules() { Dictionary<string, object> dataArgs = new Dictionary<string, object>(); dataArgs.Add(“MaxLength”, 10); ValidationRules.AddRule( Csla.Validation.CommonRules.StringMaxLength, new DecoratedRuleArgs(dataProperty, dataArgs)); } #endregion } } |
(You can see the power of a DSL simply by looking at how many more lines of code it takes to represent the same thing in a lower level language!)
It has taken me a loooong time to get to this point and I think it’s all downhill from here. Trying to find out how to discover validation / access / authorization rules from arbitrary business object frameworks turns out to be a terribly difficult thing to do. There is lots of room for improvement but I think I have the basics down for now. This should allow me to generate code for CSLA and NBusiness frameworks at least and perhaps a couple of others without too much work.
Next up is largely a process of cleaning up code (I have been hacking on things a lot recently), fixing up unit tests and fleshing out CSLA templates for each major stereotype (I love that word in a software context!). What a relief!
You must log in to post a comment.