Jakehallas
asked on
How can a class be loosely coupled to an application if it is reading from a database and interpreting the results?
As a PHP dev, most of my projects are database-driven. I'm a fond lover of OOP & while new to the object world, I attempt to follow best practices to improve on my abilities.
If I have an object that requires information from my database, how is that object loosely coupled because it is heavily reliant on the returning results of the database?
Code example 1:
Here, my class is expecting an array key of name. This just feels wrong to me, because now my class is tightly coupled to the result set...
I have been doing a lot of reading on application layers, such as the domain layer, service layer, transaction scripts, application layer etc.
Even equipped with this knowledge I cannot see a solution to my problem.
It would be great to hear your thoughts on whether I'm driving myself mad for no reason.
It would also be great to see some code examples or further reading materials, books etc links.
My burning question
If I have an object that requires information from my database, how is that object loosely coupled because it is heavily reliant on the returning results of the database?
Code example 1:
<?php
class Test {
protected $repository;
public function __construct(Repository $repository)
{
$this->repository = $repository;
}
public function process()
{
$results = $this->repository->where('id', 1)->get();
( ! empty($results) ) ?: ''; // do something bad.
foreach ($results as $result) {
// This is my problem, my class now knows it needs an array key of name to be available.
// Is this bad?
echo $result['name'];
}
}
}
Here, my class is expecting an array key of name. This just feels wrong to me, because now my class is tightly coupled to the result set...
Layers
I have been doing a lot of reading on application layers, such as the domain layer, service layer, transaction scripts, application layer etc.
Even equipped with this knowledge I cannot see a solution to my problem.
To Finalise
It would be great to hear your thoughts on whether I'm driving myself mad for no reason.
It would also be great to see some code examples or further reading materials, books etc links.
ASKER
Thanks for your comment, It's very good insight - I understand squeezing performance is a good bet & a logical solution.
Due to this being a learning curve for myself, can you elaborate on:
I'm struggling to understand what you mean by that sentence..
Due to this being a learning curve for myself, can you elaborate on:
a consistent api exposed and simply swap out the foundation data class for each platform if you need to be portable
I'm struggling to understand what you mean by that sentence..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's a common approach to try to be database agnostic and remove as many dependencies as possible; both to try to keep the code "pure" but also to try to make it "portable."
Both of those sound like good ideas, but the reality is your business data is probably more important than any particular application that accesses it. Also, all of the major database platforms (oracle, mysql, sql server, db2) are pretty good at their job. So it behooves you to take advantage of the features within your particular platform. Write SQL, don't use a code generator or framework to do it for you. When you write the SQL, write it for one specific platform and squeeze maximum performance out of that.
From a design aesthetic and class perspective, you can have a consistent api exposed and simply swap out the foundation data class for each platform if you need to be portable. Yes, you'll have to do multi-maintenance by updating each platforms specific code but your DBA and your clients will appreciate it, and you will as well in the long run.