When you work with ORM which implement UnitOfWork pattern (NHibernate's Session, Entity Framework's ObjectContext, etc.), there are two types of data services methods: those which save/commit changes and those which just modify model properties.

In some time it becomes difficult to support this mess: when you call a method you are not sure, whether it will save changes or not (if it doesn't you need to do it in some of outer methods).

How can I solve this problem? The only idea I have is a special naming. For example, AddCustomer for saving method and FillForAddCustomer for non-saving method. Any other ideas?


Services which just change model properties don't sound like data services to me. They sound like model functions, and could (probably should) be in one model class or another. Maybe those methods weren't put in a model class because it wasn't clear where they belonged. I avoid circular references between model classes, so I let owning models depend on owned models, but never the other way around. Then I put methods that affect multiple models in the lowest-level class that owns all the affected models. Alternatively, if you are wanting to propagate changes from owned models to their owners, you could let the owners observe their children.

Considering AddCustomer: what are you adding the customer to? If the answer is 'the database', then I would just write

customer = new Customer(); 
customer.setProperties(...); /* Does whatever FillForAddCustomer does */
customer.save(); /* or whatever your ORM requires */

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.