Where to put logic dealing with more than one models -


In a MVC framework like Rail, business logic is required in the overall consensus model. However, when it comes to logic as "all the problems solving user", I'm not sure which model class I should argue because it needs to first look at all the solutions submitted by all users, And the problem is to gather the ID from each solution, then the problem IDs all need problems.

It looks more beautiful to put in the user model, so we can make some calls like user.getAllProblemsSolved (). However, user frequency needs us all the User ID In those places where a user instance is not easily available, we have to make one to call our getAllProblemsSolved method.

What's more, because logic mainly deals with problem and solution models, it stinks feature jealousy to put in the user model. To avoid the feature jealousy , it seems equally well to put into problem or solution. Intuitively, I will make it like the problem in the Problem.getAllProblemsSolvedBy (userId) , but I do not have good justification for it.

So where should I put that argument?

I can see that the relationship between problem and is an M2M relation, so if you want to abide by the rules and put business logic in the model class, you can put getAllProblemsSolvedby in the model code, which is M2M Represents the relationship, but using that logic, the getAllProblemsSolved By problem ID will return, and then you problem model class Problems must achieve.

I will return examples of this problem class


update April 23 2014, 07:00 UTC

Note that in your case, depending on the M2M class, solution , exis those two models users and < The strength of the code> problem , so that you can query both models of this model, and so if there was an M2M model in my code, then I would give it two inquiries of both ends of the M2M relationship Gym for Responsibility.

For example, in your case, getAllProblemsSolved By can be implemented in the Solution model class and this problem Can return a list of frequencies and this will be user .

Comments

Post a Comment

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -