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 ?
1 Answer
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.
2 Comments
Steve
Business logic should really be in a service. Not in a model.
Jeroen Vannevel
@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/…
userclass? At first glance I'd say: retrieve the user from the repository and call some method from the user class that changes the debt.