We have a service layer for one of our primary applications that works very much like how you're describing.
We've been slugging through it for about six months in development, and here's what I learned:
1) You don't want to serialize your objects, because full-blown objects are massive, and often have recursive relationships. Clients have Invoices, and an Invoice as a Client, which has Invoices, etc, etc. You need to build Data Transfer Objects (DTO's) that are built for wire transfer. Based on the needs of the consumer, you will often have 1-10 DTOs for a given base class. Some business classes don't need a DTO, like Phone, for example. It only has a few properties (usually), so it's no big deal. But for your complicated stuff, you need to slim them down.
2) Normally the life cycle works like this: (a) Open the session, (b) build a DTO from a persisted business object, (c), serialize the DTO, (d) close the session. When it's time for you to handle an update, you (e) open the session, (f) apply the changes to the persisted object (based on the identifier) from the DTO (g) flush the session.
There's a lot of problems with disconnected serialized data, and if you go the DTO route you can save yourself a lot of headaches. Say you get an Employee sent back to you. Is the sender responsible for sending you a *complete* Employee object? What if the Phone[] collection is null? Does that mean the sender didn't send them, or there really is a null collection?
Sending back DTOs is easy, but taking them in for updates can be more dicey, because for every property you expose on your DTO, you need to handle those on an update. Personally I'd rather have service methods like EmployeePhoneInsert(EmployeeDTO e, Phone p) and EmployeePhoneDelete(EmployeeDTO e, Phone p), and so on, rather than handle that in EmployeeUpdate(EmployeeDTO dto).
_________________ http://rebelheart.squarespace.com
|