Welcome Guest, Not a member yet? Register   Sign In
Library Vs Model
#1

[eluser]rossmurphy[/eluser]
Want to get some thoughts on the use of library vs. model in my situation.

I have a CI front-end that is developed around an API backend. So at the moment i have models for different functions of the API such as helpdesk, deposit, withdrawal.. things like that. All of the models are extended from MY_Model in which there is a method to build the query string and pass the action to the API, then return the XML to the model and on to controller for display.

I have often wondered if it is more appropriate to create Libraries based on functionality of API. Such as a helpdesk library with methods such as post a ticket, view ticket.

I guess the reason for using the models was because there was never going to be any database activity as the backend does everything through the API. So this kind of represents my data retrieval process.

Any thoughts would be greatly appreciated.
#2

[eluser]theprodigy[/eluser]
First, I would like to say that my way of thinking may differ from the next, or even differ from the majority, but this is how I look at it.

The controller is for the business logic, and the view to display things to the user. That is basically agreed upon by the majority.
Apart from that, I usually use the models for things OUTSIDE the web app. Things like database calls, pre-defined emails, API calls, etc.
Libraries I use as objects within the controller. Some of the more basic functionality, like users, vehicles, systems, etc, etc.

Depending on how complex the app gets, I sometimes have the controller communicate strictly with the libraries, and the libraries communicate with the models. I'll use the controller to set/get the attributes of the library object, then call a save function. The save function in the library calls the insert method of the model. In complex apps, it seems to be easier that way. In smaller, scaled down apps, it's easier to leave the libraries out of the picture.

This is just my two cents, but I hope it helps.
#3

[eluser]Colin Williams[/eluser]
Libraries exist to help either the Controller or the Model. For instance, the ActiveRecord library helps the Model make database calls. The URI class helps the Controller interpret requests. Etc.

Using libraries to manipulate users, vehicles, etc., makes no sense to me. That's data. That should be the model.
#4

[eluser]theprodigy[/eluser]
Sorry if there was any confusion. In this context, I was refering to custom libraries, not CI's core libraries.
#5

[eluser]Colin Williams[/eluser]
Quote:Depending on how complex the app gets, I sometimes have the controller communicate strictly with the libraries, and the libraries communicate with the models. I’ll use the controller to set/get the attributes of the library object, then call a save function. The save function in the library calls the insert method of the model.

I see no point in that. Just do all of that in the model.
#6

[eluser]theprodigy[/eluser]
You program your way, I'll program mine. I prefer a more OO structured approach.

Code:
$car = new car();
$car->name($this->input->post('name'));
$car->engine($this->input->post('engine'));
$car->wheelsize($this->input->post('wheelsize'));
$car->save();

$car2 = new car();
$car2->name($this->input->post('name2'));
$car2->engine($this->input->post('engine2'));
$car2->wheelsize($this->input->post('wheelsize2'));
$car2->save();

makes a little more sense to me then,

Code:
$this->load->model('cars');
$this->cars->save(
    $this->input->post('name'),
    $this->input->post('engine'),
    $this->input->post('wheelsize')
);
$this->cars->save(
    $this->input->post('name2'),
    $this->input->post('engine2'),
    $this->input->post('wheelsize2')
);
#7

[eluser]Colin Williams[/eluser]
That's fine. You can certainly write your models that way. But why call it a library when that is precisely what a model is?
#8

[eluser]theprodigy[/eluser]
I use libraries as objects, period. Whether I need to save them to the database or not. And as stated in my original post to RossMurphy, I use models to access databases, access api's, etc. If I want to use objects, and not save them to a database, or submit them elsewhere, or anything else, then it makes no sense to me to be in a model. To me, (as stated previously, models are for accessing OUTSIDE your web app. Libraries are for basic object types, that MAY get saved, may not.

I think in this instance, we will just have to agree to disagree. I don't plan to never use custom libraries. I like using libraries to help sort out the controller code when apps get a little more complex then normal.
#9

[eluser]attos[/eluser]
I have thought a lot about this issue.

One problem I find having the business logic in the controller is that you might expose methods to the outside world and having security issues. An attacker might guess a method name and exploit it.

My controllers are basically "traffic cops". Directing the flow of information from the requester to the models and getting the results to the views.

I usually use two level models. The ones close to the controllers perform the business logic. Then this communicate with the others that perform all database operations or implement ORM.

This architecture also allows me to use automated unit tests. For each controller I have there is another that performs the modules tests. I do not need to issue requests for testing and I can test the application from the command line.

As @theprodigy stated earlier: "...I would like to say that my way of thinking may differ from the next, or even differ from the majority..."
#10

[eluser]rossmurphy[/eluser]
Thanks for the inputs, good discussion. I think i like the way Collin views it. I too see the models handling anything data related such as API's, Databases. Then the libraries assist with this data retrieval or controller functionality, such as a cache library.




Theme © iAndrew 2016 - Forum software by © MyBB