having a simplified class like this that regulate a quiz game:
class Game():
def __init__(self,username):
...
self.username=username
self.question_list=db.getQuestions()
self.game_over=False
def get_question(self):
...
if self.question_list.is_not_empty():
return question
def check_answer(answer)
...
if answer.is_correct():
self.game_over=False
else:
self.game_over=True
and having a web controller that receive input parameters like username, questions and answers..is it correct to use Game class directly from controller?
I ask this question because looking at controller code, i feel guilty to have coded inside some logic too; for example controller instantiates Game just when username is received and then calls get_question and check_answer in order.
Do you think is more correct to have another “layer” that receives input parameters from controller and talk directly to Game class?
I think you’re good.
The thing is you can’t have a perfect architecture with complete isolation of layers and data formats only visible on one side of the boundary. It’s inevitable some layer will have to talk to the other.
It’s okay to call get_question and check_answer if you’re doing a very basic logic to decide in controller how to proceed next. If it’s a matter of a few checks that will lead to a few possible controller decisions, then do it.
But if you wanted, say, to collect data from the user, convert it to some specific format, validate it, then maybe call business methods that could yield dozens of possible outcomes, and all that directly in the controller, it would have been too much clearly.