Is a library appropriate? |
[eluser]madshadow[/eluser]
Hello all, I recently started working with CodeIgniter and I am obsessed. I have become fairly comfortable with the idea of a framework and the MVC style in general. One of the points that initially confuses me, however, is when a library would be more appropriate compared to just a model. My scenario: My system manages a large set of customers. Some of the various aspects of a customer are -Basic biographical and contact info -A set of preferences -A set of billing information (including calculating billing cycles, etc) -Billing history -Site visit history -Lead submission history -sales rep information -login history There are several other aspects as well. My initial thought was that, because a client will show up on many different pages, it would be best to create a Client library that will build all these various parts of a client object. As I was building the library, I realized that it was mostly just making calls to the Client model. I started thinking that maybe I don't even need a library and that everything should be done in the model itself. Can anyone provide any insight/suggestion as to when a library is worthwhile compared to a well-designed model? I would appreciate the help as I try to learn how to best utilize CodeIgniter. So far I am unbelievably impressed by the CodeIgniter community and I hope to become a productive member of it. Thank you, Brian
[eluser]Buso[/eluser]
Libraries and Models can do the same things, the only difference is that Libraries are supposed to be reusable, and models are supposed to be application-specific. But sometimes you have libraries that use models that are reusable too, so they can have an extra layer of abstraction. This is how I see it, but maybe someone has a different opinion..?
[eluser]Jelmer[/eluser]
There's different ways of implementing the MVC pattern using CI's implementation. My personal opinion is that it's pretty much up to you to choose which is better. I use models when it's about CRUD operations on database data (or any other kind of data that benifits from having an object represent it). If I use CI with Active record I generally implement my models in a way that I load a new instance of the model for each row retrieved from the database. When I use RapidDataMapper (an ORM) I generally implement models to work as gatekeepers in retrieving RDM's objects. Libraries (IMO) can be application specific, but should not do CRUD operations (unless it's wrapping them in a general way to be usefull for models). I view libraries as toolboxes that are either singleton (never more then 1 instance) or are instanced only to change how or where they operate, but not to represent data. Sometimes it's difficult to make a choice. I recently created a file access library that wraps native PHP functions like rename(), unlink(), etc. I could have implemented it as a model because it's about data access. But in the end I made it very general and I've implemented it in 2 models that work with configurations of the library (one for general files and one for images). But to get back to the OP's question: in your case a model seems to be indeed the best solution because it's about clients and I think you could best implement it in a way that each instance you load represents a single client. Or you could create both a library and a client model where the library handles some of the higher function operations and the model is purely about representing the client and doing CRUD operations.
[eluser]madshadow[/eluser]
Buso and Jelmer, I appreciate both of you giving me your insight and your comments are very helpful. I'm beginning to get a much better understanding of how to set up my application. I appreciate your help and I look forward to advancing my CodeIgniter knowledge. -Brian
[eluser]bretticus[/eluser]
Pretty much everything in your list is an entity object scenario. I like this definition of Entity objects..."Entity objects are classes that encapsulate the business model, including rules, data, relationships, and persistence behavior, for items that are used in your business application. For example, entity objects can represent the logical structure of the business, such as product lines, departments, sales, and regions, business documents, such as invoices, change orders, and service requests, physical items, such as warehouses, employees, and equipment." In the MVC pattern, that's really a model. Libraries should be more generic. For example, I see a library being called from a model much more often than the other way around. Because libraries are often "tool-box" class instances (like helper objects.) That's my two cents anyways. Cheers! |
Welcome Guest, Not a member yet? Register Sign In |