I currently have an NHibernate data model that I have decorated with Not Null and Length attributes that generally maps one-to-one with CSLA domain object for editing purposes. I would like to generate the basic per type rules for the domain object by reading these attributes. I'm using ObjectFactory. Has anyone done anything similar and has any pointers.
Why not use DataAnnotation attributes on your BusinessObjects properties?
Like, Required, StringLength, Range and more: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx
The reason I've done it from the data model is that, the data model is used to generate the database schema. It would be nice not to have to duplicate the attributes on the Csla domain objects too. I guess it must be possible as Csla must read those attributes to create the rules itself.
Well, you are taking a long shot here. You might get away with it using Repository pattern but I think you will have serious problem with ObjectFactory pattern.
In an ObjectFactory pattern:
1. The BO dows not know anything about the Data Acces Layer (other than perhaps the Interface og FullyQualified name of the the Factory.
2.If the BO should know about the DAL then you wold have to break encapsulation.
3. You would need to hook into the AddBusinessRules and use protected methods.
Plus: . In a Silverlight app you would most likely NOT have the Data Access (NHibernate) objects available at all.
Thanks for your replies
The only thing I can think of is to use a CommandObject to retrieve the rules for this type as a list of uri's from the ObjectFactory and then have a helper method/class in the BO that reads the uri list and creates them inside the BO. I would have to create a compatible uri list inside the ObjectFactory to send back to the BO.
??? Not even sure if that makes sense ???
This sort of thing is exactly why rules can be added programatically through AddBusinessRules and not just with attributes. I specifically allow programatic adding of rules with the intent that people could write code in AddBusinessRules to use a read-only object to get the rules for the object type.
This is substantially harder on SL due to the async server access requirement, but it is pretty trivial in .NET (especially in CSLA 4).
That's what I want to achieve . However were are also using async server calls (in most places.) How is it that much harder async? Surely I just hook up the complete and call AddBusinessRules from there?
I'm using WPF4 & CSLA 4.0.1.
InitializeBusinessRules is only called in the constructor/OnDeserialized/UndoChanges of the BO and should only run once per type:
private void InitializeBusinessRules()
var rules = BusinessRuleManager.GetRulesForType(this.GetType());
rules.Initialized = true;
throw; // and rethrow exception
So - if the async portal call in AddBusinessRules fails you need to cleanup/reset Initialized yourself.
You should probably also run async on client only and run sync when you are on the server.
Ok, thanks Jonny. This code must have changed between 4.0.1 and 4.1.0.
Yes, the reason was that if an error was thrown in AddBusinessRules, ex loading rules from a database, then you would end up with a partial rule set for that type. So we added code to rules in case of an exception so the rules would be reloaded on next access.
Copyright (c) Marimer LLC