0

I am working on a new project and am curious as to how I should go about doing something properly.

Lets say I have a table called "leads" in my database. This clearly means that a single "lead" would be an object, and I'd need a class in order to create, update, or pull information for said "lead" object.

Now here is where my question comes in. Lets say I have a page called leads.php with a table that I want to populate with multiple "leads" from my database. Do I create the method for listing all leads within the "lead" class itself, or do I create a seperate "data service" class, which would represent a service object used to retrieve multiple leads?

Any help would be greatly appreciated.

inkd
  • 103
  • 1
  • Are you using an [ORM](http://en.wikipedia.org/wiki/Object-relational_mapping) framework? –  Apr 07 '15 at 01:55
  • Are you generating your data pages on the server, or handing data to a page on the client and populating it with a frontend binding framework like Knockout? – Robert Harvey Apr 07 '15 at 02:04
  • 1
    In general, the `Lead` class would not be responsible for displaying itself. Something else would. That's the best answer I can give you without more information about your application architecture. – Robert Harvey Apr 07 '15 at 02:05

1 Answers1

1

If you add a method Lead.list() where it returns a list of leads it's violation of SRP, as the fetching a list of Leads is not the responsibility of Lead object.

So the best option is to use a data service or a repository. For example LeadRepository could have methods GetAll(), GetById(id)

Low Flying Pelican
  • 1,602
  • 9
  • 13