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

[eluser]-sek[/eluser]
I want to separate all social networking functionality in my application from user authentication and user profile code. I think it would be a good idea to gather all the code handling friends, etc. into a social networking library, but right now, there is an existing friends Model, so am wondering what the best architecture for this is.

Should I move friends Model code into the new library or should I let the library wrap the friends Model? Should I be making it dependent on loading the friends model?
#2

[eluser]Unknown[/eluser]
This is a stylistic choice in my opinion, I don't think there's one "right" answer.

My feeling on it is that programming should be a low-effort endeavour, so choose the path of least resistance. If you already have a well-defined model then there's no reason you can't load the model from within the library code, and use that as part of your library.

On the other hand a library is really intended for reuse across applications, so if it's something that's too narrowly specific to your application, my feeling is to keep it to model code, and just expand your model.

Your question is kind of vague, so if you gave a little more information about specifically what types of functions you might include in either the model or the library, it might be easier to give a specific reason to go one way or the other.
#3

[eluser]SitesByJoe[/eluser]
I'm interested in hearing some thoughts on libraries that tap into databases. Should model functionality be put in the library? I don't use too many libraries but the couple I have used do make their queries etc within the library file.

Like mentioned above, I assumed that libraries should contain their own stuff pretty much completely.

I've made a couple libraries of my own - they do the same thing.

Experts? Your thoughts?
#4

[eluser]Udi[/eluser]
None of my libraries accessing my database, the one that was close to do that is my Template Class - but its not.
I think that if you get into a situation where library supposed to access my database, it will be through Model class. The idea is simple: if you have a table in the database, there might be a model that handling it - right? [if not, there should be] so just use this model directly from your library.

When I wrote my template class, I need to get the Title/keywords/Description data from my DB table called settings, I could write few lines of code to do that, but its BAD practice in my opinion, instead I used my MSettings class [which is a model].
#5

[eluser]-sek[/eluser]
I built a social networking site on CI using one of the authentication packages, FAL, which is now obsolete, leaving my application tied to code that is essentially orphaned. I had the idea that if I could separate all the social networking functionality into a library, it would be possible to replace the authentication library with any other authentication library. The social networking code just needs an interface, some way to get data about users, from the authentication and user profile management systems (user auth and profile could be separate, but in FAL, they are in the same library). I just need to disentangle the social functions from auth and user profiles, but the social code is entangled in them.

Yes, the library would deal with a database. All our information is in a database.

A good example is something as simple as generating a list of friends of a user. I need to display the user picture, their user name or real name, linked to their profile, which requires a parameter with the user id number. The social library would need to know how to get access to the user data (user name, user id) and the profile data (picture and real name). The most efficient way to retrieve this information is a join query, but that means the social library is tied to the user auth library through the query itself. I could specify in configuration data for the social, the columns and tables the auth/profile library uses, making it relatively simple to change if I switched libraries. There is also the problem of all this database code being in the library, when using a model makes more logical sense. The code would not be all in one location, but it would still be possible to package up the library and whatever models it depends on. I am kind of hesitant to move the model functionality from the model into the library, since it seems like I'm doing something inelegant and wrong even if it works. I suppose if I do not need to distribute the code, it is less important for it to be all in one file or directory.

I do have an existing model for handling friends.

The bottom line is, I'd like to get to a place where I can just plugin whatever auth library I want when one goes belly up, as they tend to do in the CI community.
#6

[eluser]Udi[/eluser]
So create a model.
#7

[eluser]Jelmer[/eluser]
Best practice, in my opinion, is to use both a library and a model (or multiple models).

All the database access goes into the model (get a single friend, get a list of friends, delete, update, create, etc).
But all the procedural stuff goes into the library.

Using a authentication library as an example: getting users, usergroups and permission goes through 3 different models that have corresponding database tables. Checking for login, checking permissions for users by usergroup, and other stuff like that goes into the main library. So all data requests go through models but using that data to get a result should happen in the library.
It's a bit more work to begin with but if you ever change how something works you'll notice how much better it is to have it setup like this. Also this leads to cleaner code and probably more DRY code, both of which are almost always a good thing.

As to the problem with Authentication libraries that might change: you could write a wrapper library of you own that has only generic functions for authentication and user management. By doing that you can use the wrapper library when doing stuff from other libaries and controllers. And if you change your auth library you only have to change the code in the wrapper library to work with the new auth library instead of the old.
#8

[eluser]-sek[/eluser]
Thanks everyone. I will keep the friends model. I was thinking about a wrapper, but it seemed so complicated. Some of that may just be redirects to the auth lib controller, so I'll give it a try.




Theme © iAndrew 2016 - Forum software by © MyBB