Generating basic business rules based on data model attributes

Generating basic business rules based on data model attributes

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


Kevin Fairclough posted on Thursday, February 10, 2011

Hi,

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. 

Regards

Kevin

JonnyBee replied on Thursday, February 10, 2011

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

Kevin Fairclough replied on Thursday, February 10, 2011

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.

 

JonnyBee replied on Thursday, February 10, 2011

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.

Kevin Fairclough replied on Thursday, February 10, 2011

Thanks for your replies Big Smile

Hmm.

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 ???

 

RockfordLhotka replied on Thursday, February 10, 2011

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).

Kevin Fairclough replied on Friday, February 11, 2011

That's what I want to achieve Smile.  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.

 

JonnyBee replied on Friday, February 11, 2011

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());
      if (!rules.Initialized)
        lock (rules)
          if (!rules.Initialized)
          {
            rules.Initialized = true;

            try
            {
              AddBusinessRules();
            }
            catch (Exception)
            {
              BusinessRuleManager.CleanupRulesForType(this.GetType());
              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.

 

 

Kevin Fairclough replied on Friday, February 11, 2011

Ok, thanks Jonny.  This code must have changed between 4.0.1 and 4.1.0.

JonnyBee replied on Friday, February 11, 2011

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