Services

POS application uses the mechanism of Unity containers. It allows you to easily and quickly access all previously registered services in any ViewModel and View class. It also resolves all dependencies, provided that they have also been previously registered. An example here may be each view sourced from a container with the simultaneous injection of a dependent viewmodel in its constructor. It is possible, since each viewmodel is registered in the container beforehand (see the chapter How to register navigation and layout management views in the How to create views article). In turn, that viewmodel is injected with other required services that are essential for it (provided that they bave been previously registered).

How to get services from a container

There are three methods of getting an instance of a selected service:

1. Defining a public property with the added Dependency attribute from the Practices.Unity namespace.

[Dependency]
public IProductsService ProductsService { get; set; }

2. Injecting the instance in the viewmodel constructor through a simple parameter declaration.

public OrdersViewModel(IProductService productService) { … }

3. Calling the Resolve method for the IUnityContainer object available in the ViewModelBase class under the Container property.

var productsService = Container.Resolve<IProductsService>();

A service instance is created every time you get a service from a container. However, it does not refer to services registered as singletons, whose instances are created only once upon the first attempt to get them.

How to register own services in a container

You can register a service at any moment, wherever you have access to the IUnityContainer object. If you want to make sure that a service is available from the very beginning, you need to register it in the Module class. This is applicable in the case of all viewmodels. You may register a service based on its interface. If a service class has no interface, it does not need to be registered, unless you want it to be available as a singleton.

To register a service in the Module class, use the method Register<TInterface,TClass>(), where TInterface is the interface’s name and TClass is the name of a class that implements this interface. Additional parameters include:

  • singleton (bool) – it specifies whether an instance you get from a container should always be created anew or the same instance should always be returned. By default, this parameter is set as false.

The following example shows how to register a service of the CustomService class implementing the ICustomService interface as a singleton within the Module class:

Register<ICustomService, CustomService>(true);

If you want the registered service to be immediately instantiated, but it contains dependencies that will be automatically injected, you must make sure that this will be possible. If a service depends on another service from a different module, you should make sure that it has already been registered. To guarantee that, instantiation (retrieving from a container) should be performed within the AfterAllModulesLoaded method (located in ModuleBase).

Services available in POS

Services may be divided into basic services that concern the core of POS application and business services that concern the logic of the application’s business-related activities.

Basic services include:

  • IMonitService (Comarch.POS.Presentation.Core.Services)

It allows displaying messages (info messages and questions) – see the Messages chapter in the Notifications and messages article for more details.

  • INotificationService (Comarch.POS.Presentation.Core.Services)

It allows displaying notifications – see the Notifications chapter in the Notifications and messages article for more details.

  • IViewManager (Comarch.POS.Presentation.Core.Services)

It allows navigation between views (by opening and closing views) – see How to navigate between views for more details.

  • ILoggingService (Comarch.POS.Library.Logging)

It allows saving information to a log file.

  • ISecurityService (Comarch.POS.BusinessLogic.Interfaces.Security)

It is responsible for the authentication and authorization of POS users – see Validation of permissions for more details.

  • IAutorizationService (Comarch.POS.Presentation.Base.Services)

It allows the validation of permissions of logged-in POS users – see Validation of permissions for more details.

Business services include:

  • IConfigurationService (Comarch.POS.Library.Settings)

It allows gaining access to the application configuration.

  • IFiscalizationService (Comarch.POS.Presentation.Fiscalization.Services)

It allows printing documents on a receipt printer.

  • ISynchronizationService (Comarch.POS.Synchronization.Interfaces)

Synchronization service.

  • IPrintoutManager (Comarch.POS.Presentation.Core.Services)

Printout service.

  • And all other services within the Comarch.POS.BusinessLogic.Interfaces namespace

Czy ten artykuł był pomocny?