CSLA 4: BusinessRules.CheckRules() - Is this not *always* called for DataPortal_Create?

CSLA 4: BusinessRules.CheckRules() - Is this not *always* called for DataPortal_Create?

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


Jaans posted on Sunday, December 19, 2010

I read somewhere that CSLA 4 always checks the business rules when creating an object via the data portal.
(Ps: Are there any other places where this is also implicitly done?)

Strangely, I'm not seeing this happen with an Editable Root object (haven't tested with others yet) with the following Factory and DP definitions:

Factory:

public static Staff New()
{
    return DataPortal.Create<Staff>( new Criteria(  ) );
}

DataPortal method:

[TransactionalTransactionalTypes.TransactionScope )]
[RunLocal]
private void DataPortal_Create(Criteria criteria)
{
using ( BypassPropertyChecks )
    {
        ...

}
}

I need to implement the DP method as follows in order to get the rules checked:

[TransactionalTransactionalTypes.TransactionScope )]
[RunLocal]
private void DataPortal_Create(Criteria criteria)
{
using ( BypassPropertyChecks )
    {
        ...

}
   BusinessRules.CheckRules();


}

Am I missing something?

 

Thanks,
Jaans

RockfordLhotka replied on Sunday, December 19, 2010

The only place all rules are implicitly run is in the default DataPortal_Create implementations.

Obviously rules for specific properties are implicitly run by SetProperty when you change the property. Otherwise you need to call CheckRules yourself.

RockfordLhotka replied on Sunday, December 19, 2010

btw, "default DataPortal_Create" means the implementation in the base class. So if your subclass doesn't call base.DataPortal_Create that code won't run.

Jaans replied on Sunday, December 19, 2010

Ah... that clears is up for me.

I wasn't sure where this CheckRules() was being invoked from, I had thought it was done in some "special" way to ensure it is called for any and all DataPortal_Create( ??? ) implementations, but as you explained it's from the base class such that it is only on base.DataPortal_Create().

So for the sake of verbosity and other readers, I would need to override the parameterless DataPortal_Create() method in my business object class and call base.DataPortal_Create().

For other DataPortal_Create( ??? ) methods that do have parameters, there would be no overriding, but would still contain either a call to base.DataPortal_Create() or your own call to BusinessRules.CheckRules().

We generate our classes from a DSL based designer, and so always generate a "Criteria" class to wrap parameters in - so for us it would just make sense to continue generating  a call to BusinessRules.CheckRules() to make our intentions clear as a call to the base.DataPortal_Create() doesn't make it clear that we are actually trying to invoke the CheckRules() method (albeit indirectly).

Thanks Rocky

Copyright (c) Marimer LLC