Пример #1
0
        /// <summary>
        /// Saves a single <see cref="IInvoice"/>
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/> to save</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IInvoice invoice, bool raiseEvents = true)
        {
            if (!((Invoice)invoice).HasIdentity && invoice.InvoiceNumber <= 0)
            {
                // We have to generate a new 'unique' invoice number off the configurable value
                ((Invoice)invoice).InvoiceNumber = _storeSettingService.GetNextInvoiceNumber();
            }

            var includesStatusChange = ((Invoice)invoice).IsPropertyDirty("InvoiceStatusKey") &&
                                       ((Invoice)invoice).HasIdentity == false;

            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IInvoice>(invoice), this))
                {
                    ((Invoice)invoice).WasCancelled = true;
                    return;
                }

                if (includesStatusChange)
                {
                    StatusChanging.RaiseEvent(new StatusChangeEventArgs <IInvoice>(invoice), this);
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateInvoiceRepository(uow))
                {
                    repository.AddOrUpdate(invoice);
                    uow.Commit();
                }
            }

            if (!raiseEvents)
            {
                return;
            }

            Saved.RaiseEvent(new SaveEventArgs <IInvoice>(invoice), this);
            if (includesStatusChange)
            {
                StatusChanged.RaiseEvent(new StatusChangeEventArgs <IInvoice>(invoice), this);
            }
        }