Welcome Guest, Not a member yet? Register   Sign In
What is difference Factories vs services
#1

Hello!!
I can't understand the difference between Factories vs Services.
I think it look similar.
They can return same class instance and the functions look similar.
I'm trying to google, and search forums, but I can't.
please tell me what is difference.
thank you
Reply
#2

(This post was last modified: 03-31-2022, 07:22 AM by ignitedcms.)

Services:
http://www.imperativedesign.net/insights...%20service.

Factories:
https://phptherightway.com/pages/Design-Patterns.html

Ref: https://designpatternsphp.readthedocs.io...EADME.html

Best pattern I use:
KISS
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

Services:
https://codeigniter4.github.io/userguide...vices.html

Factories:
https://codeigniter4.github.io/userguide...ories.html

> I think it look similar.

Yes. They both can  do:
- create an instance
- share the instance that was created

The big difference is that the Services have code to create an instance,
but the Factories does not have it.

So with the Service, you can create objects that are complex to create.
E.g.: https://github.com/codeigniter4/CodeIgni...s.php#L582
Reply
#4

The user guide in develop branch was updated.

Services
https://codeigniter4.github.io/CodeIgnit...vices.html
Factories
https://codeigniter4.github.io/CodeIgnit...ories.html#

If there are something difficult to understand, let us know.
Reply
#5

(04-06-2022, 04:27 PM)kenjis Wrote: The user guide in develop branch was updated.

Services
https://codeigniter4.github.io/CodeIgnit...vices.html
Factories
https://codeigniter4.github.io/CodeIgnit...ories.html#

If there are something difficult to understand, let us know.

Thanks to you, I understood it well.  Smile
Reply
#6

(This post was last modified: 03-08-2024, 02:12 AM by b126.)

(03-31-2022, 05:40 PM)kenjis Wrote: Services:
https://codeigniter4.github.io/userguide...vices.html

Factories:
https://codeigniter4.github.io/userguide...ories.html

> I think it look similar.

Yes. They both can  do:
- create an instance
- share the instance that was created

The big difference is that the Services have code to create an instance,
but the Factories does not have it.

So with the Service, you can create objects that are complex to create.
E.g.: https://github.com/codeigniter4/CodeIgni...s.php#L582

Hi Kenjis. 
Is it really useful to keep these two concepts when they seem so close?
I mean, would it be possible to just keep the Services (which are great and offer lot of flexibility), imagining that the Services could wrap the Factories if they are called and they can't find the method to create the instance like that is usually the case.

I find that Factories do not bring a big advantage but create a bit of confusion (it's true that we win a little time due to not having to implement the instance creation through Config\Services)

The underlying concept would therefore be something like :

$x = service('myLib')
--> if no instanciation method is found in Config\Services for myLib then return $x = Factories::libraries ('mylib')

What do you think?
Reply
#7

Services and Factories do essentially the same thing, i.e., create objects.

However, the process and API of creating objects is different.
Also, Factories offer some options that can be freely changed at run-time. Personally, I don't see much need for them.

If some of the features of Factories could be eliminated, it would be possible to integrate the two.
For example, if you specify full qualified class names.

PHP Code:
service('request')    // call request() as before
service('Config\App'// generate and return a Config\App instance 
Reply
#8

However, I do not know there is much benefit in integrating the two.

Normally, users would use the following three functions, and there seems to be no need to change them.
service(), config(), model()

Also, Services and Factories should be used as little as possible. The cost of retrieving objects is high.
I don't think it would take too much time in a normal application to worry about it, though.

Quote:Note
It is recommended to only create services within controllers. Other files, like models and libraries should have the dependencies either passed into the constructor or through a setter method.
https://codeigniter4.github.io/CodeIgnit...e-services
Reply
#9

(03-08-2024, 02:08 AM)b126 Wrote: The underlying concept would therefore be something like :

$x = service('myLib')
--> if no instanciation method is found in Config\Services for myLib then return $x = Factories::libraries ('mylib')

If you want to get only libraries, you can do it now if you *override* the service() function.
Something like this:

PHP Code:
return Services::$name(...$params) ?? Factories::libraries($name); 
Reply
#10

(03-08-2024, 05:32 PM)kenjis Wrote:
(03-08-2024, 02:08 AM)b126 Wrote: The underlying concept would therefore be something like :

$x = service('myLib')
--> if no instanciation method is found in Config\Services for myLib then return $x = Factories::libraries ('mylib')

If you want to get only libraries, you can do it now if you *override* the service() function.
Something like this:

PHP Code:
return Services::$name(...$params) ?? Factories::libraries($name); 
Thanks for your explanations. I will have a look.

What I appreciate also a lot with the Services, is this kind of calls : 
PHP Code:
Services::loggedInUser(); 

It's extra sharp and compliant with PhpStorm's autocomplete. This is not possible Factories as far as I know.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB