Angular, Do we need to make service singleton or by default it works as a singleton? - service

I am working with angular 8 version to create a e-commerce application.
Want to share same data between components like product and wishlist.
So for that, Is service works like singleton by default or I have to make changes to make it singleton.

Service is not work as singleton by default.
There are 2 ways by which we can make it singleton
To create a singleton service is to set providedIn to root on the service's #Injectable() decorator
#Injectable({
providedIn: 'root',
})
2. Or we can add in providers in NgModule of root module (AppModule)
#NgModule({
providers: [UserService],
})

Related

Using getX in Flutter with a singleton service

Using getX in Flutter, suppose I need to use the same service for different controllers.
For example, the same DB service for both UsersController and ProductsController.
What would be a best practice to do that?
Creating a singleton DB service?
Using getIt with the DB service?
Some other getX trick?
GetxService?
It would be more accurate to create and use a singleton object with the following method.
Get.put<LoginService>(LoginService(), permanent: true);
Get.find<LoginService>();
GetX Documentation about Get.put
the class that you want to get to save, like a controller or anything
// note: "S" means that it can be a class of any type

Why do we need a interface to define every service in aem?

I have been working with for a while but somehow never thought about this. Every aem project that I have worked on, has one similarity in their code structure. There is an interface for every service written.
My question is why do we need a interface for every service?
Can #reference or #inject not use the services without an interface?
Using interfaces is a good practice to decouple the user of a service from the implementation. In many cases you even want to have an API bundle so the user of the service does not need a maven dependency to the implementing bundle.
On the other hand you are not required to use interfaces. Especially when I wire components inside a bundle interfaces are often an unnecessary layer. In this case simply export the service directly with the class.
See here for an example:
#Component(service = DistributionMetricsService.class)
public class DistributionMetricsService {
...
}
and here for the client code:
#Reference
private DistributionMetricsService distributionMetricsService;
So the main difference is that you have to specify the service property if you want to export a component with its implementation class.

How to implement Shopware plugin providing both frontend and backend functionality?

I want to implement a Shopware plugin, that provides some changes to the frontend as well as a backend interface. Is that possible or will I need to implement two separate plugins? In case of separate plugins, how could I make sure that both plugins are installed and updated together?
it is only important to put them into their directory.
Controllers/backend
Controllers/frontend
there are also two different classes to extend.
the backend controller extends Shopware_Controllers_Backend_ExtJs
the frontend controller extends Enlight_Controller_Action
Found it my own: You can add both frontend and backend controllers, so you're not restricted to one of these areas...

zend framework 2 - use global method

i need one function which i use in views, hydrator, some controllers and so on...
Where i can put it?
Where it would be the best add that can be maintained PHP OOP and zend 2 architecture?
Thanks
It sounds like you should be looking at the Zend\ServiceManager. You can register factories (functions) and services in the service manager, and technically access them from anywhere in your application. Your class would need to implement the ServiceLocatorAwareInterface in order to access the service manager, OR you would pass/inject the service into your class/model/hydrator/etc.

Using the ServiceLocator in ZF2 with Doctrine 2 from/in a Custom Class?

i have a little problem using doctrine 2 in Zend Framework 2 i have custome class that i use to manipulate doctrine generated model (basically to inject data and populate), to make that work i need the entity manager which is available through the service manager as indicated in Jason Grimes tutorial here http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/.
In his tutorial it works (i tested it) as the ServiceLocator is called from a controller class, but for the application i am writing i have to use custom (non controller) classes to interact with the entities. How do i achieve this? Using the servicelocator from a custom class that is not a controller? Thank you all in advance
You need to do two steps
Implement Zend\ServiceManager\ServiceLocatorAwareInterface in your custom class. This allows to the Framework to inject the Service Locator for you.
Convert your custom class to a service and retrieve it using Service Manager. This component will check if the class implement ServiceLocatorAwareInterface and the inject the ServiceLocator before returning to you the instance