• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to put plain object classes?

(01-05-2015, 01:25 PM)mwhitney Wrote: You'll probably never find a satisfactory generalized answer to the question of which classes are libraries and which are models. I generally think of libraries as a location for things which tend to interact with multiple parts of the MVC pattern, or don't easily fit into the pattern. I also frequently use it for code which was not originally written for an MVC framework in the first place, or even libraries originally written for other frameworks, even if they are MVC frameworks.

Models I tend to use primarily for data access, whether the data happens to be in a database or some other form. I tend to put my business rules into models, but I rarely have reason to store the business rules separate from the model(s) which interface the data to which those rules apply. In many cases I also include methods in my models which allow those rules to be retrieved or executed by the controller (more often than not because it simplifies working with certain libraries in CI, like form_validation).

I really like this thoughtful post.

I'm mostly thinking of my controllers as a receptionist at a hotel or restaurant. They help you find the right room or table and are knowledgeable about who knows what, but lack specialized skills to clean your room or cook your meal. They locate the responsible code and bring it to bear on the request at hand. These should be highly aware of security considerations.

I'm thinking models are our code. I.e., our code as distinct from libraries supplied by others. Things that pertain to our system and nothing else. Obviously this would encapsulate our data. It would also encapsulate our rules about who can do what when and who cannot. Or what data we create when certain actions are taken, etc. I expect most of our custom-written code to exist here.

Libraries seem to me to include third-party stuff like classes to send Email or maybe third party encryption tools or OAuth stuff or Paypal/Authorize.net/Amazon Payments/Google Checkout classes. Anything general purpose libraries (i.e., that handle email, SMS, FTP clients, CDN connections, cloud computing access, etc.) should go in the libraries folder. And generally speaking, I expect any interaction with these classes will probably be encapsulated by some of my model code. More precisely, I probably won't have much, if any, access to these libraries in my controllers. Whether or not I send a send someone an email or upload a file to a CDN seems much more a my system decision than a simple request-routing decision and is therefore in the Model domain.

At any rate, those are my thoughts now. Specific anecdotal issues that shatter this ideal are welcome.

Messages In This Thread
Where to put plain object classes? - by geekita - 12-02-2014, 05:51 AM
RE: Where to put plain object classes? - by sneakyimp - 01-05-2015, 02:25 PM
RE: Where to put plain object classes? - by Jamie - 01-06-2015, 05:18 AM
RE: Where to put plain object classes? - by Jamie - 01-06-2015, 03:08 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.