Contents
The Comarch.POS.ExtensionPoints module allows multiple extensions to the same viewmodel method. In this way, you can create a number of independent extensions that modify the same native function.
Architecture of extension points
For an extended viewmodel, an additional inheriting viewmodel has been created, along with its dedicated business service. For example:
public class DocumentViewModelExtensionPoint : DocumentViewModel public class DocumentViewModelExtensionPointService : PrintingViewModelNavigationExtensionPointService<IDocumentViewModel>, IDocumentViewModelExtensionPointService IDocumentViewModelExtensionPointInternalService
Each viewmodel method to be extended has two events to make attachments to in the service: Before and After. However, it does not refer to activators, i.e. methods that return a Boolean flag. In their case, there is only one event.
When extending a given viewmodel method, you gain access both to the parameters of a given call and to the instances of a given viewmodel. This aims to make the full context of the action available.
[Dependency] public IPaymentViewModelExtensionPointService PaymentViewModelExtensionPointService { get; set; } public override void Initialize() { PaymentViewModelExtensionPointService.BeforeAddToPaymentFormEvent += _paymentViewModelExtensionPointService_BeforeAddToPaymentFormEvent; } private void _paymentViewModelExtensionPointService_BeforeAddToPaymentFormEvent(object sender, EntityViewModelCancelEventArgs<IPaymentViewModel, PaymentFormRow> e) { if (e.Cancel) // handling of input status return; // code }
Calling the ViewModel native method
There may be a situation where, as part of an extension, you want to make the calling of a native operation dependent on the operator’s decision. POS messages are non-blocking messages, so handling may be continued only in the callback handler from the message window.
private void _documentViewModelExtensionPointService_BeforeProductAddEvent(object sender, EntityViewModelCancelEventArgs<IDocumentViewModel, LotRow> e) { e.Cancel = true; MonitService.ShowQuestion(“Would you like to add the item?", (s, m) => { if (m.MonitResult == MonitResult.Yes) { var serv = DocumentViewModelExtensionPointService. GetViewModelActions(e.ViewModel); serv.ProductAdd(e.EntityRow); } }); }
The GetViewModelActions method returns an instance that allows calling native methods of the base viewModel disregarding extensibility.
Available extension points
By default, each viewmodel extension allows the modification of navigating methods:
- OnActivated() – BeforeOnActivatedEvent, AfterOnActivatedEvent
- OnInitialization() – Before OnInitializationEvent, AfterOnInitializationEvent
- OnDeactivated() – BeforeOnDeactivatedEvent, AfterOnDeactivatedEvent
Additionally, viewmodels that handle the printout process have extensions to the methods:
- Print() – BeforePrintEvent, AfterPrintEvent
- CanPrint() – CanPrintEvent
Implementation example
The following example illustrates the implementation of a ready-made extension that will show two messages when the user attempts to save a trade document – one before and the other after it is saved.
using Microsoft.Practices.Unity; using Comarch.POS.ExtensionPoints.Presentation.Sales.ViewModels; using Comarch.POS.Presentation.Core.Services; using Comarch.POS.Presentation.Core; public class Module : ModuleBase { private readonly IUnityContainer _container; [Dependency] public IMonitService MonitService { get; set; } [Dependency] public IDocumentViewModelExtensionPointService DocumentViewModelExtensionPointService { get; set; } public Module(IUnityContainer container) : base(container) { _container = container; } public override void Initialize() { DocumentViewModelExtensionPointService.BeforeSaveEvent += _documentViewModelExtensionPointService_BeforeSaveEvent; DocumentViewModelExtensionPointService.AfterSaveEvent += _documentViewModelExtensionPointService_AfterSaveEvent; } private void _documentViewModelExtensionPointService_AfterSaveEvent(object sender, GenerateViewModelEventArgs<IDocumentViewModel> e) { MonitService.ShowInformation("Extension: after save"); } private void _documentViewModelExtensionPointService_BeforeSaveEvent(object sender, GenerateEntityViewModelCancelEventArgs<IDocumentViewModel> e) { if (e.Cancel) return; MonitService.ShowInformation("Extension: before save"); } }