|
As an update, this is what I have done to make it
work, but I'd still love to know if there is another way to do this and I'm
barking up the wrong tree ...
So when my application starts, each module will
look in my database store to find if there are any formats registered against
entity properties in their namespace. For each entity and property, a method is
called on my base entity (the one all IdeaBlade entities inherit from). This method saves the property name and
format required in a dictionary, and once per entity only adds a property
interceptor action for the whole entity.
Then finally when a property on the entity is set, the action is called
and it loops through the dictionary and adds verifiers for each of the
properties that need formats. Then the
property interceptor action is removed.
Here is example code: This would be called from Module.Initialize()
(hard-coded values for example purposes only) EntityBase
entity = (EntityBase)Type.GetType("Courier").GetConstructor(new Type[0]
{ }).Invoke(newobject[0] { }); entity.RegisterPropertyInterceptor("Code",
"^[0-9]{4}$"); entity.RegisterPropertyInterceptor("AccountNumber",
"^[A-Z]{3}[-][0-9]{2}$"); Then this is
inside my EntityBase class:
#region Property Formatting private Dictionary<string, PropertyFormatting>
propertyFormatInterceptorList = new Dictionary<string,
PropertyFormatting>(); internal class PropertyFormatting { private readonly string format; private readonly string formatMessage; internal PropertyFormatting(string
format, string formatMessage, bool verifierIsLoaded) { this.format = format; this.formatMessage = formatMessage; } internal string Format { get { return this.format; } } internal string
FormatMessage { get { return
this.formatMessage; } } } public void
RegisterFormatPropertyInterceptor(string
propertyName, string format, string formatMessage) { string entityPropertyInterceptorKey = String.Format("{0}",
this.EntityAspect.Entity.ToString()); if
(!propertyFormatInterceptorList.ContainsKey(propertyName)) { if (this.EntityAspect.EntityMetadata.EntityProperties.Where(ep
=> ep.Name == propertyName).FirstOrDefault() != null) { propertyFormatInterceptorList.Add(propertyName, new PropertyFormatting(format,
formatMessage, false)); List<PropertyInterceptorAction<PropertyInterceptorArgs<Entity, Object>>>
actions = PropertyInterceptorManager.CurrentInstance .GetActions<PropertyInterceptorArgs<Entity, Object>>(this.GetType(), null,
PropertyInterceptorMode.BeforeSet) .ToList(); if (actions.Where(a => a.Key ==
entityPropertyInterceptorKey).FirstOrDefault() == null) { PropertyInterceptorManager.CurrentInstance.AddAction( new PropertyInterceptorAction<PropertyInterceptorArgs<Entity, Object>>( this.GetType(), null, PropertyInterceptorMode.BeforeSet, new Action<PropertyInterceptorArgs<Entity, Object>>(PropertyFormatInterceptor), 0.0, entityPropertyInterceptorKey)); } } } }
public void
PropertyFormatInterceptor(PropertyInterceptorArgs<Entity, Object>
args) { if (args.Instance.EntityAspect.EntityManager != null &&
args.Instance.EntityAspect.EntityManager.VerifierEngine != null) { // This should only be called once foreach (var
propertyFormatter in this.propertyFormatInterceptorList) { PropertyFormatting propertyFormatting =
propertyFormatter.Value; Verifier formatVerifier = new RegexVerifier(this.GetType(), propertyFormatter.Key, true, new NamedRegexPattern(propertyFormatting.FormatMessage,
propertyFormatting.Format)); formatVerifier.VerifierOptions.ShouldExitOnBeforeSetError
= true; formatVerifier.VerifierOptions.ExecutionModes =
IdeaBlade.Validation.VerifierExecutionModes.InstanceAndOnBeforeSetTriggers; args.Instance.EntityAspect.EntityManager.VerifierEngine.AddVerifier(formatVerifier); } string entityPropertyInterceptorKey = String.Format("{0}",
args.Instance.EntityAspect.Entity.ToString()); List<PropertyInterceptorAction<PropertyInterceptorArgs<Entity, Object>>>
actions = PropertyInterceptorManager.CurrentInstance .GetActions<PropertyInterceptorArgs<Entity, Object>>(this.GetType(), null,
PropertyInterceptorMode.BeforeSet) .ToList(); foreach (var action in actions.Where(a => a.Key ==
entityPropertyInterceptorKey)) { PropertyInterceptorManager.CurrentInstance.RemoveAction(action); } } }
#endregion
I have one final
problem now, but it's to do with the UI (WPF) not IdeaBlade. The problem is that a UI textbox is bound to
the entity property. Now I have the UI
text box go red and the tooltip show the validation error message when the user
types in the wrong thing, BUT the UI will still display what the user has typed
in even though the actual entity property is something else. So if anyone has any answers for this that
would also be a help :) thanks
|