Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted
+50

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 */
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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