• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NTier architecture and CodeIgniter

#1
Hello everyone!

Firstly, let me thank all of you volunteers who are in this forum, this is such an amazing and underrated framework!

I've got an architecture question which has been bugging me for a small while here.
Recently, I have been working on a huge (work) project which uses the N-Tier architecture, and I am now confused on its DTO and DAO aspect within CI.

For those unfamiliar with the ntier architecture, here is a quick summary of both those terms. 
A DTO, which stands for Data Transfer Object, is what should be returned by the models. 
 It does not contain accès to the database, as this logic is in the DAO.
A DAO, which stands for Data Access Object, which is whatever method you use to access your data.

In my case, I'm trying to port my user login to such an architecture, with typed data.
In CodeIgniter, I would call my users model and use a method such as getUserFromId($id), where the model would act as DAO and return a DTO. 

As I'm working towards having a lot of abstraction, here is how I currently planned on structuring my folders:


 - Models
 |-- IUserDAO
 |-- MariaDB
    |-- UserDaoImpl
 |-- DTO
    |-- IUser
    |-- Impl
       |-- UserImpl

As you can see, this doesn't seem very clean.
It feels wrong to have the DTO folder inside the Models folders, yet I can't seem to find a better place.

In a usual project, I would have both a DTO and DAO folder but there does not seem to be such a predefined folder in CI. 
Perhaps I should create it, but I preferred asking here first Smile

Update: While writing this post, I've had a quick look in newmythmedia's blog and it seem Entities would solve my problems.
Leaving this question up if I'm mistaken 

On an unrelated note, here are few other questions not worthy of a post themselves I'd like to ask:
 - Are there any medium-big open-sourced projects using CI?
    I'd like to navigate into the architecture of decently-sized projects for inspiration.

 - CI4: While I only had a quick glance over the proposed changes, would you recommend waiting for CI4 until making an architecture revamp (which would take few months) or waiting until it is out before attempting this change?
Reply

#2
It's too early to recommend waiting on CI4 for a production project. And the Entity blog posts that you're talking about are actually geared toward CI4, though many of the same ideas will work in CI3 just fine. And, yes, I think the DAO/DTO is very similar to the Entity/Repository pattern I was describing.

Here's what I've done in the past: get Composer running on that install if you haven't already. Then either create an App namespace that points to the application folder so you can namespace your files there, or - create a new "top-level" folder named after your company, or whatever. Then you can create any class in there that's not a Controller basically, and load them using Composer's autoloading.

If you go that second route, then you probably won't be using many of the standard ways to load models, libraries, etc, but it provides lots of freedom in how you organize your application. If you put everything in that new, namespaced folder, then it actually future-proofs the application a little too, since it's much simpler to handle any client/ requests to switch to a new framework down the road, or to move to CI4, etc. All of your business logic is contained in ways that require the framework itself as little as possible.
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4
Reply

#3
The namespace solution would make it hard to use CI-related features in my models, which would be more hassle than it's worth in my opinion.

Considering I'm working on a bigish personal project, I believe working with CI4 standards using a mocked framweork could be better than keeping on using CI3, or using CI3 while keeping track of the divergence between CI3 and CI4 then port to 4 once it is ready to be tested.

Thanks for you answer!
Reply

#4
(04-19-2017, 08:37 AM)antony Wrote: The namespace solution would make it hard to use CI-related features in my models, which would be more hassle than it's worth in my opinion.

Not at all! I've done this on several past projects. I would create a new class App\BaseModel that extends \CI_Model. I would typically add common CRUD methods to that, but the point is that since it extends from CI_Model, you get all of the CI-related functionality you're used to.

Here's an example. That repo uses namespaced classes for all of it's additional functionality.

EDIT: I just realized that model doesn't actually extend CI_Model. But if you look at CI_Model, there's really only method in it that provides a hook back into the CI superobject, which got incorporated into that BaseModel. Same thing, though, just with one less layer of inheritance.

(04-19-2017, 08:37 AM)antony Wrote: Considering I'm working on a bigish personal project, I believe working with CI4 standards using a mocked framework could be better than keeping on using CI3, or using CI3 while keeping track of the divergence between CI3 and CI4 then port to 4 once it is ready to be tested.

Fair enough. If I were in your shoes I might do the same thing. But I can't officially recommend it, due to potential security issues, etc. And I do prefer a number of the features in CI4, but I'm probably the most biased person to ask Smile
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.