9

For example if I have a User that has debt. I want to change his debt. Should I do it in UserRepository or in service for example BuyingService by getting an object, editing it and saving it ?

2
  • We need more info about your setup. What regulates the debt? Is it a field in the user class? At first glance I'd say: retrieve the user from the repository and call some method from the user class that changes the debt. Commented Nov 9, 2013 at 18:25
  • yes, debt is a field in user class. Commented Nov 9, 2013 at 18:26

1 Answer 1

9

You should leave the responsibility of mutating an object to that same object and use the repository to retrieve this object.

Example situation:

class User { private int debt; // debt in cents private string name; // getters public void makePayment(int cents){ debt -= cents; } } class UserRepository { public User GetUserByName(string name){ // Get appropriate user from database } } 

Usage (Jack pays 10€):

userRepository.GetUserByName("Jack").makePayment(1000); 

Keep in mind that this is just an example approach. There is not one set way in programming to achieve something, you could very well do this entirely different.

Sign up to request clarification or add additional context in comments.

2 Comments

Business logic should really be in a service. Not in a model.
@Steve: I have dedicated a question on programmers.SE to this statement. Please feel free to join in and clarify your statement, I can imagine a comment box will not suffice and is rather annoying to use in the first place. programmers.stackexchange.com/questions/218011/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.