خرید بک لینک

Vote count: 0

(There is a TL;DR: at the bottom)

I have a PDF produced via MVC patte. I am working with an existing code, which was a bit of a mess, but now I am seeing a patte emerge.

Currently, I have a Controller class, and inside I have many many separate functions, roughly one function per page. Each function does something like this:

function showPage()
{ 
    //get some data from repository
    $data1 = $this->repository->getData1();
    $data2 = $this->repository->getData2();

    //pass that data to the PDF API class, aka "the view"
    //and the class takes care of creating PDF pages
    //with the appropriate data
    $this->pdfApi->showView($data, $data2);
}

The above achieves a clean separation between Repository (which only retus data), the PDF API service (which receives the data and doesn't need to care or maintain data retrieval constructs. And Controller which pretty much just asks for Data, and passes it to PDF API. And all was well until I came across this problem:

Problem

Most every page has a "footer" with a message, and a "Proposal Number" that needs to be displayed on the page. Sometimes it also has other pieces of data. Since PDF API class has no data in itself, someone has to pass that data to PDF API. I have been passing the above to pieces of information every time as part of function parameters but it became inconvenient -- there are too many parameters to pass and they are cluttering up the code.

Try at Solution

To reduce the clutter in parameter passing, In my Controller I have created pulled data (via Repository) for variables such as $footerText and $proposalNumber and then used them I populate PDF API's own class properties. Side-effect of this is that now my PDF API has the relevant bits of data embedded directly in the API (which I consider to be undesirable, since data layer now imposes into API class)

So far I have resisted the temptation to just pass the entire Repository object to PDF API because that will do very much the same - mix data layer and API Layer, plus, API layer will have unrestricted access to Data, which can also be undesirable.

Actual Problem

When I want clean layer separation, my code is cluttered with multiple function parameter passing.

When I pass the entire Repository to my API class, I mix data and API layers, and API layer gets too much freedom to use Repository class.

Can I somehow achieve layer separation without the clutter or "mixing layers" issues identified above?

If you like to see code, here is some code below of my various unsuccessful tries :)

TL;DR: Which way of handling data/Repository is the recommended Way?

//in Controller - Exhibit 1

//maximum layer separation but lots of aoying data passing
$data1 = $this->repository->getData1();
....
$data24 = $this->repository->getData24();
$this->pdfApi->showView($data1, $data2, $data3, ... );

//in Controller - Exhibit 2 (mixing in data with API)

//in constructor
$data1 = $this->repository->getData1();
....
$data24 = $this->repository->getData24();
$this->pdfApi->setData1($data1);
$this->pdfApi->setData24($data24);

//inside function (API already has data as part of its own vars):
$this->pdfApi->showView();

//in Controller - Exhibit 3

//in constructor
$repo = new Repository();
$this->pdfApi->setRepository($repo);

//inside function (API has full Repository access):
$this->pdfApi->showView();

asked 44 secs ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 7 ارديبهشت 1395 ساعت: 5:56

صفحه بندی