Repository pattern

I am coming from Laravel, where i used to use Repository pattern, the folder structure is something like that
App -> Repositories -> UserRepository
and then inject UserRepository in UserController

What is the best place to write the Business Logic in Adonis?

The repo pattern still applies if you wish to use it. It’s not Laravel specific.

If you know principle, you can implement it independently from your Laravel knowledge.

Just in case you use Nuxt.js as a different stack for your frontend, that patteren is implemented and explained on this blog:

I managed to do it myself. That was easy.

1 Like

Soon i will share my structure on git publicly.

1 Like

Waiting for your structure

I have developed it. It’s a ready made CRUD Structure with Repository pattern. It’s A boiler plate to quickly start developing projects. I am in documentation phase. I hope i can get developer’s contributions just to make it more stable.

1 Like

Thank, please share GitHub link in stable version.

1 Like

This is just for APIs.
I have plans for Admin, Views, Postman file with just one command etc. I will update the repository soon.

1 Like

Thanks this is helpful me and our Adonis and Laravel Developers.

1 Like

Yeah I still used repository patter in adonisjs

1 Like

It sounds like Laravel has changed a lot since I last used it.

Nice implementation. Although, by the looks of the code, you seem to tying the Repository functions to the request/response object. Repositories, last I checked when reading Domain Driven Design by Eric Evans, don’t really need to know about the request and response objects directly, to keep the two layers decoupled.

Wouldn’t implementing the repository logic as regular classes and then extended to fit a specific model work a bit better?

Just curious to hear your thoughts :slight_smile:


Is any examples? Or GitHub link if you already implement. It is beneficial for us.

The Repositories actually act as a bridge between controller and model, where you can write your business logic. Let’s say you need to Register a user for which you will write a code in Repository class, and link it with the controller to get access to it. Now the point is we cannot write business logic in controller neither in Model. The Model class will be responsible for getting and setting the data in database and while repository class manages the business logic. That’s pretty clean and manageable.
This extremely useful incase you are developing Mobile APIs as well as Admin and website using the same functionality / business logic, with this you dont need to write the core logic again and again, we just need different controllers for API, Admin, Web.

You might have noticed that i didn’t use abstract classes for implementation rather i used IoC (Inversion of control) for model dependency in repository class, you cannot use any other model in that repository.

Please give your thoughts on this structure how can we improve it.
Thanks for taking interest.