Where To Put This Validation Code?

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 21-Nov-2006 16:21:33   

All,

This is not specific to LLBLGen, although we are using it on this project (v.2, Adapter, C#).

This comes up every once in a while and I always struggle with the best way to handle it.

Our architecture looks like this:

UI => BL (mostly fascade) => DAO

All layers have knowledge of the domain objects, which we call the DAL (this is LLBL-generated code).

I have static methods in the DAO (and a fascade for it in the BL) for saving that wrap the adapter's save functionality with whatever overloads I need (depending on the project refetch, recursive, etc.).

The project has to do with entering personal expenses. One of the expenses is per diem. Only one per diem record can be entered for a given person on a given day. Because per diem is a type of expense (expense_type_id), it's not possible to enforce this with a UC at the database level (other expense_types can be entered more than once on a given day).

So essentially, before I save I need to query the database to ensure that a per diem record does not already exist for that person for that day.

If I use entity validation, my DAL layer would have to have knowledge of the DAO, which I don't want (correct me if this is wrong). I could also create a new save interface for Expenses (only), but that seems messy to me (not to mention dangerous--what if the UI programmer calls the wrong save?).

My best idea right now is to have a switch statement inside the Save method in the BL, based on EntityType. If EntityType is Expense, then a validator gets created. The main limitation I see is that when I do recrusive saves, only the main entity will get validated.

Any thoughts? My preference is to enforce this validation in the BL, but I'd be open to other solutions, so long as they are clean.

Thanks,

Phil

(EDIT: I marked this thread as "done" accidentally. flushed )

PilotBob
User
Posts: 105
Joined: 29-Jul-2005
# Posted on: 21-Nov-2006 16:31:45   

Can you not put the code into the OnValidateEntityBeforeSave() method of the entity? If a perdiem already exists for that day then throw an excection.

Are you saying you can't do this because the project your LLBLGen Enitites are in doesn't reference your DAO project? If so you can still query the entities from your DAL without using the DAO layer, right?

Another option is a trigger in the db to enforce this business rule. Or, you could add a PerDiem table to enforce the unique constraint.

BOb