Ok, now we have base generic class that will do standard CRUD and GetQuery for us (see previous post). However real life impersonated as your current client is full of surprises and fancy requirements ;) What if we want to do custom CRUD or extend standard one? For example, consider this classic schema
What if we want to execute some custom business logic each time we add new Order and update OrderItem? Solution (as usual) is simple
yes, just override corresponding CRUD operation for root type (Order) and add method with correct signature (RIA uses name conventions by default) for child entity (OrderItem). You might have noticed that comments saying something about transactions…let me explain what’s going on here – by default all CRUD operations for DomainService executed outside transaction scope (we just attach, insert, delete entities to EF’s context).
However imagine that we need to call stored procedure from our CRUD method as extension for standard operation and this SP will modify something (calculate your salary for example) – we need to make sure that this calculation will be atomic as well as standard CRUD (which is maintained by EF when RIA call ObjectContext.SaveChanges()) That’s why we need this base class
ProcessEntityBeforeSave method can be defined in derived class to implement common entities processing – in our example it can be assigning LastUpdated field to current DateTime
You might wonder what is DataContext.Current? well… wait for next post about Using Repository pattern in RIA ;)