0

Which is better as a design perspective?

An object that gets initialized and then checks to see if the parameters passed to it are valid before destroying itself (so in __init__) or

Check to see if the parameters are valid and then the object gets initialised?

I think the answer is the latter. However that leaves me with this question. What class owns the primary functions for checking this input? Under an MVC design, would it be in the controller, another class, or as a @staticmethod in the class in question

I'll provide a simple example:

Let's say class B is valid when x > 10 under a MVC design.

    class Controller():
        ...
        age = view.age()
        HumanObj = Human(age)
        Humanobj.is_Of_Age()
        # Delete obj
        ...

or

    class Controller() 
        ...
        age  = view.age()
        if age > 10:
            HumanObj = Human(age)
        ...

or

     class Controller()
         ...
         age= view.age()
         if Human.is_Of_Age(age):
             HumanObj = Human(age)
         ...

thanks

  • 2
    ...do you know the difference between a class and an object? Based on your questions and notations, it appears you are confusing both. – dagnelies Oct 10 '16 at 13:31
  • Thanks, I changed the first part to reflect the differences. Isn't an obj just initialisation of a class inheriting (right word?) all those functions within? – nimbus Oct 10 '16 at 13:38
  • 1
    I don't really understand your question. Your code doesn't compile and doesn't seem to match what your question is asking (you appear to be asking about constructors, but the examples don't contain constructors). Can you edit your examples to actually match what your question is? – enderland Oct 10 '16 at 13:46
  • "Isn't an obj just initialisation of a class inheriting all those functions within?" => No. Go check some beginner tutorials, you will really need it ;) – dagnelies Oct 10 '16 at 13:46
0

I believe your question is:

In a Model-View-Controller framework, where does the business logic go?

If you are using a strict MVC framework, they go in the model. So, for example, the logic that "A person's age should never be below 10" goes in the model. This is because if you have multiple endpoints that affect somebody's age, then you'd have to put that check in multiple spots in your code.

That said, that isn't to say that all logic goes in the model. For example, verifying that you received an integer and not a double from a POST request would go in the controller, as that has no thing to do with business logic.

That said, it is quite common for many frameworks to actually have a 4th layer, a "Services" layer. In this case, the services actually contain the business logic, and the Model is only in charge of storing/retrieving data from the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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