Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for feature: Service layer #66

Open
archlicher opened this issue May 10, 2016 · 5 comments
Open

Request for feature: Service layer #66

archlicher opened this issue May 10, 2016 · 5 comments

Comments

@archlicher
Copy link

I have struggled with unit testing controllers. In my personal opinion, AspectMock is pretty tricky and takes much time to be configured. I need something quick, easy to extend, showing results asap, especially when I need to test data with logged user.
Yet, controllers, as they are supposed to, return (in most cases) html with response status, rather than simple data or anything testable - not that they are not important, but I find that this is also tested during development, so it may be left out in the unit testing.
Still I need to test the data I get from the models.
I have some experience with ASP.NET and Java EE, where it is considered good practice to separate the (so called) business logic from the controller and model in another layer - Service Layer. This way the work is done as follows:

  1. the controller receives request and returns responses. When data is needed, it requests it from the service
  2. the service basically implements all the business logic and manipulates/modifies data accordingly
  3. the model represents the table from the db

Personally I find it simpler to keep the validation of input fields in the controller, rather than move it to the service. The service should only modify, no need to validate inputs.

Please do check the sample code I have uploaded at my https://github.com/archlicher/fuelphp_req_4_feat. For brevity I have uploaded only the controller and the service, as they are the only piece of software that I have changed. (Notice! if you are to try it with your own project, don't forget to add the Service namespace and classes to your config and bootstrap files).

Finally I wish to point out a few pros:

  1. testing is much easier - especially with logged user - as I need to pass the user to the service action.
  2. the controller is much shorter, easier to follow, maintainable and expandable. The same goes for the service, but in additionally calling the service from another controller/service is much clear and easier.
  3. less coupling, higher cohesion

As it is quite simple and I must have surely missed out some pivotal points, way will not be the best, still I do believe there are reasons to put some thought in to it.

@emlynwest
Copy link
Contributor

We are planning for v2 to address these concerns. I am currently working on a rewrite of the current v2 foundation to make things like this possible. I also intend to provide test harnesses for phpunit as well as a codeception module to allow for easy testing, but this won't happen until we are happy with the state of the core request/response code.

@archlicher
Copy link
Author

Is there a planned release date, or it will be released when it is ready. Can I get involved especially in the Service layer development, if it is still TODO or under development?

@emlynwest
Copy link
Contributor

As of yet there is no planned release date. We will make official announcements when we are ready to release. As it stands the v2 code has a concept of this built in, as far as I understand what you are asking for, provided via fuelphp/dependency. My current task is getting routing up and working, and as part of that I am going to set it up so controllers can be configured to have services automatically injected when they are initialised.

@archlicher
Copy link
Author

Yes, that is exactly how I would expect it to be.

@emlynwest
Copy link
Contributor

Good, sounds like there's nothing extra to do here then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants