tags:

views:

54

answers:

2

Hi

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?

Thanks Michele

+3  A: 

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.

Developer Art
Thanks for your answer; the problem here is that Game class could be improved with many other features in the future and then controller need to be filled with more logic too;this design aspect is not trivial.
systempuntoout
A: 

I would definately keep as much as possible out of the controller. Otherwise your controller bloat will be rediculous. Please see video for reference.

Al Katawazi
Nice video :).So, what do you suggest?Another layer that access game.py?
systempuntoout
I often use a folder called infastructure, I think I picked that up from Connery, but you can stick it in the model too if you like. Its up to you how many tiers you want to put in.
Al Katawazi