/// <summary>
        /// Updates an existing subscription
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public Attempt <Subscription> Update(SubscriptionRequest request)
        {
            Updating.RaiseEvent(new SaveEventArgs <SubscriptionRequest>(request), this);

            var attempt = TryGetApiResult(() => BraintreeGateway.Subscription.Update(request.Id, request));

            if (!attempt.Success)
            {
                return(Attempt <Subscription> .Fail(attempt.Exception));
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                Updated.RaiseEvent(new SaveEventArgs <Subscription>(result.Target), this);

                var cacheKey = MakeSubscriptionCacheKey(request.Id);
                RuntimeCache.ClearCacheItem(cacheKey);

                return(Attempt <Subscription> .Succeed(result.Target));
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            LogHelper.Error <BraintreeSubscriptionApiService>("Failed to create a subscription", error);

            return(Attempt <Subscription> .Fail(error));
        }
Exemple #2
0
        /// <summary>
        /// The persist updated item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistUpdatedItem(IProductVariant entity)
        {
            if (!MandateProductVariantRules(entity))
            {
                return;
            }

            Mandate.ParameterCondition(!SkuExists(entity.Sku, entity.Key), "Entity cannot be updated.  The sku already exists.");

            ((Entity)entity).UpdatingEntity();
            ((ProductVariant)entity).VersionKey = Guid.NewGuid();

            var factory = new ProductVariantFactory(
                pa => ((ProductVariant)entity).ProductAttributes,
                ci => ((ProductVariant)entity).CatalogInventoryCollection,
                dc => ((ProductVariant)entity).DetachedContents);

            var dto = factory.BuildDto(entity);

            // update the variant
            Database.Update(dto);

            SaveCatalogInventory(entity);

            SaveDetachedContents(entity);

            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IProduct>(entity.ProductKey));
        }
        /// <summary>
        /// Quotes all available ship methods
        /// </summary>
        /// <param name="tryGetCached">
        /// If set true the strategy will try to get a quote from cache
        /// </param>
        /// <returns>
        /// A collection of <see cref="IShipmentRateQuote"/>
        /// </returns>
        public override IEnumerable <IShipmentRateQuote> GetShipmentRateQuotes(bool tryGetCached = true)
        {
            var quotes = new List <IShipmentRateQuote>();

            foreach (var gwShipMethod in ShippingGatewayMethods)
            {
                var rateQuote = tryGetCached ? TryGetCachedShipmentRateQuote(Shipment, gwShipMethod) : default(ShipmentRateQuote);

                if (rateQuote == null)
                {
                    // REFACTOR-v3 this would only need happen if an attempt was to get cached, then not, then cached again
                    // without any changes.
                    RuntimeCache.ClearCacheItem(GetShipmentRateQuoteCacheKey(Shipment, gwShipMethod));

                    //// http://issues.merchello.com/youtrack/issue/M-458
                    //// Clones the shipment so that with each iteration so that we can have the same shipment
                    //// with different ship methods
                    //// REFACTOR-v3 clone should be inherent on ICloneableEntity
                    var attempt = gwShipMethod.QuoteShipment(Shipment.Clone());
                    if (attempt.Success)
                    {
                        rateQuote = attempt.Result;

                        RuntimeCache.GetCacheItem(GetShipmentRateQuoteCacheKey(Shipment, gwShipMethod), () => rateQuote, TimeSpan.FromMinutes(5));
                    }
                }

                if (rateQuote != null)
                {
                    quotes.Add(rateQuote);
                }
            }

            return(quotes);
        }
Exemple #4
0
        /// <summary>
        /// Updates a payment method
        /// </summary>
        /// <param name="token">The payment method token</param>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt"/>.
        /// </returns>
        public Attempt <PaymentMethod> Update(string token, PaymentMethodRequest request)
        {
            Mandate.ParameterNotNull(request, "request");

            Updating.RaiseEvent(new SaveEventArgs <PaymentMethodRequest>(request), this);

            var attempt = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Update(token, request));

            if (!attempt.Success)
            {
                return(Attempt <PaymentMethod> .Fail(attempt.Exception));
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);

                RuntimeCache.ClearCacheItem(cacheKey);

                Updated.RaiseEvent(new SaveEventArgs <PaymentMethod>(result.Target), this);

                return(Attempt <PaymentMethod> .Succeed((PaymentMethod)RuntimeCache.GetCacheItem(cacheKey, () => result.Target)));
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            LogHelper.Error <BraintreePaymentMethodApiService>("Failed to update payment method", error);

            return(Attempt <PaymentMethod> .Fail(error));
        }
Exemple #5
0
        /// <summary>
        /// Quotes all available ship methods
        /// </summary>
        /// <param name="tryGetCached">
        /// If set true the strategy will try to get a quote from cache
        /// </param>
        /// <returns>
        /// A collection of <see cref="IShipmentRateQuote"/>
        /// </returns>
        public override IEnumerable <IShipmentRateQuote> GetShipmentRateQuotes(bool tryGetCached = true)
        {
            var quotes = new List <IShipmentRateQuote>();

            foreach (var gwShipMethod in ShippingGatewayMethods)
            {
                var rateQuote = tryGetCached ? TryGetCachedShipmentRateQuote(Shipment, gwShipMethod) : default(ShipmentRateQuote);

                if (rateQuote == null)
                {
                    RuntimeCache.ClearCacheItem(GetShipmentRateQuoteCacheKey(Shipment, gwShipMethod));

                    //// http://issues.merchello.com/youtrack/issue/M-458
                    //// Clones the shipment so that with each iteration so that we can have the same shipment
                    //// with different ship methods
                    var attempt = gwShipMethod.QuoteShipment(Shipment.Clone());
                    if (attempt.Success)
                    {
                        rateQuote = attempt.Result;

                        RuntimeCache.GetCacheItem(GetShipmentRateQuoteCacheKey(Shipment, gwShipMethod), () => rateQuote);
                    }
                }

                if (rateQuote != null)
                {
                    quotes.Add(rateQuote);
                }
            }

            return(quotes);
        }
Exemple #6
0
        protected override void PersistDeletedItem(ILanguage entity)
        {
            base.PersistDeletedItem(entity);

            //Clear the cache entries that exist by key/iso
            RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.IsoCode));
            RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.CultureName));
        }
 /// <summary>
 /// Removes products from cache.
 /// </summary>
 /// <param name="productKeys">
 /// The product keys of products that need to be removed from cache.
 /// </param>
 private void RemoveProductsFromRuntimeCache(IEnumerable <Guid> productKeys)
 {
     // clear the cache for other products affected
     foreach (var key in productKeys)
     {
         RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IProduct>(key));
     }
 }
        /// <summary>
        /// The persist deleted item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistDeletedItem(IEntityCollection entity)
        {
            base.PersistDeletedItem(entity);

            if (entity.ParentKey != null)
            {
                RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IEntityFilterGroup>(entity.ParentKey.Value));
            }
        }
Exemple #9
0
        /// <summary>
        /// The persist deleted item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistDeletedItem(IProductVariant entity)
        {
            var deletes = GetDeleteClauses();

            foreach (var delete in deletes)
            {
                Database.Execute(delete, new { entity.Key });
            }

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IProduct>(entity.ProductKey));
        }
        /// <summary>
        /// The persist deleted item.
        /// </summary>
        /// <param name="entity">
        /// The entity to be deleted
        /// </param>
        protected override void PersistDeletedItem(ICustomerAddress entity)
        {
            var deletes = GetDeleteClauses();

            foreach (var delete in deletes)
            {
                Database.Execute(delete, new { Key = entity.Key });
            }

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <ICustomer>(entity.CustomerKey));
        }
Exemple #11
0
        protected override void PersistDeletedItem(IDictionaryItem entity)
        {
            RecursiveDelete(entity.Key);

            Database.Delete <LanguageTextDto>("WHERE UniqueId = @Id", new { Id = entity.Key });
            Database.Delete <DictionaryDto>("WHERE id = @Id", new { Id = entity.Key });

            //Clear the cache entries that exist by uniqueid/item key
            RuntimeCache.ClearCacheItem(GetCacheIdKey <IDictionaryItem>(entity.ItemKey));
            RuntimeCache.ClearCacheItem(GetCacheIdKey <IDictionaryItem>(entity.Key));
        }
Exemple #12
0
        /// <summary>
        /// Deletes a persisted warehouse catalog
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistDeletedItem(IWarehouseCatalog entity)
        {
            var deletes = GetDeleteClauses();

            foreach (var delete in deletes)
            {
                Database.Execute(delete, new { entity.Key });
            }

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IWarehouse>(entity.WarehouseKey));
        }
        /// <summary>
        /// The persist deleted item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistDeletedItem(ICustomer entity)
        {
            var deletes = GetDeleteClauses();

            foreach (var delete in deletes)
            {
                Database.Execute(delete, new { Key = entity.Key });
            }

            // customer context cache
            RuntimeCache.ClearCacheItem(Cache.CacheKeys.CustomerCacheKey(entity.Key));
        }
Exemple #14
0
        /// <summary>
        /// Persists an updated warehouse catalog.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistUpdatedItem(IWarehouseCatalog entity)
        {
            ((Entity)entity).AddingEntity();

            var factory = new WarehouseCatalogFactory();
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);
            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IWarehouse>(entity.WarehouseKey));
        }
        /// <summary>
        /// The persist updated item.
        /// </summary>
        /// <param name="entity">
        /// The entity to be updated
        /// </param>
        protected override void PersistUpdatedItem(ICustomerAddress entity)
        {
            ((Entity)entity).UpdatingEntity();

            var factory = new CustomerAddressFactory();
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);

            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <ICustomer>(entity.CustomerKey));
        }
        /// <summary>
        /// The persist updated item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistUpdatedItem(IEntityCollection entity)
        {
            ((Entity)entity).UpdatingEntity();

            var factory = new EntityCollectionFactory();
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);

            entity.ResetDirtyProperties();
            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IEntityCollection>(entity.Key));
            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IEntityFilterGroup>(entity.Key));
        }
        /// <summary>
        /// The persist updated item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistUpdatedItem(ICustomer entity)
        {
            ((Entity)entity).UpdatingEntity();

            var factory = new CustomerFactory();
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);

            entity.ResetDirtyProperties();

            // customer context cache
            RuntimeCache.ClearCacheItem(Cache.CacheKeys.CustomerCacheKey(entity.Key));
        }
Exemple #18
0
        protected override void PersistUpdatedItem(ILanguage entity)
        {
            ((Entity)entity).UpdatingEntity();

            var factory = new LanguageFactory();
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);

            entity.ResetDirtyProperties();

            //Clear the cache entries that exist by key/iso
            RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.IsoCode));
            RuntimeCache.ClearCacheItem(GetCacheIdKey <ILanguage>(entity.CultureName));
        }
        /// <summary>
        /// Updates an existing item in the database.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistUpdatedItem(IOrder entity)
        {
            ((Entity)entity).UpdatingEntity();

            var factory = new OrderFactory(entity.Items);
            var dto     = factory.BuildDto(entity);

            Database.Update(dto);

            _orderLineItemRepository.SaveLineItem(entity.Items, entity.Key);

            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IInvoice>(entity.InvoiceKey));
        }
Exemple #20
0
        /// <inheritdoc />
        public virtual void Delete(TEntity entity)
        {
            var dto = this.Context.Find(entity.Id);

            if (dto != null)
            {
                Emit(Deleting, entity);
                this.Context.Remove(dto);
                Emit(Deleted, entity);

                DbContext.SaveChanges();
            }

            RuntimeCache.ClearCacheItem(GetCacheKey(entity.Id));
        }
        /// <summary>
        /// The persist new item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistNewItem(IProductVariant entity)
        {
            if (!MandateProductVariantRules(entity))
            {
                return;
            }

            Mandate.ParameterCondition(!SkuExists(entity.Sku), "The sku must be unique");

            ((Entity)entity).AddingEntity();

            ((ProductVariant)entity).VersionKey = Guid.NewGuid();

            var factory = new ProductVariantFactory(
                pa => ((ProductVariant)entity).ProductAttributes,
                ci => ((ProductVariant)entity).CatalogInventoryCollection,
                dc => ((ProductVariant)entity).DetachedContents);

            var dto = factory.BuildDto(entity);

            // insert the variant
            Database.Insert(dto);
            entity.Key = dto.Key; // to set HasIdentity

            Database.Insert(dto.ProductVariantIndexDto);
            ((ProductVariant)entity).ExamineId = dto.ProductVariantIndexDto.Id;

            // insert associations for every attribute
            foreach (var association in entity.Attributes.Select(att => new ProductVariant2ProductAttributeDto()
            {
                ProductVariantKey = entity.Key,
                OptionKey = att.OptionKey,
                ProductAttributeKey = att.Key,
                UpdateDate = DateTime.Now,
                CreateDate = DateTime.Now
            }))
            {
                Database.Insert(association);
            }

            SaveCatalogInventory(entity);

            SaveDetachedContents(entity);

            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IProduct>(entity.ProductKey));
        }
        /// <summary>
        /// Adds a credit card to an existing customer.
        /// </summary>
        /// <param name="customer">
        /// The customer.
        /// </param>
        /// <param name="paymentMethodNonce">
        /// The payment method nonce.
        /// </param>
        /// <param name="token">
        /// The token.
        /// </param>
        /// <param name="billingAddress">
        /// The billing address.
        /// </param>
        /// <param name="isDefault">
        /// A value indicating whether or not this payment method should become the default payment method.
        /// </param>
        /// <returns>
        /// The <see cref="Attempt{PaymentMethod}"/> indicating whether the payment method creation was successful.
        /// </returns>
        public Attempt <PaymentMethod> Create(ICustomer customer, string paymentMethodNonce, string token, IAddress billingAddress = null, bool isDefault = true)
        {
            //// Asserts the customer exists or creates one in BrainTree if it does not exist
            var btc = _braintreeCustomerApiService.GetBraintreeCustomer(customer);

            var request = RequestFactory.CreatePaymentMethodRequest(customer, paymentMethodNonce);

            if (!string.IsNullOrEmpty(token))
            {
                request.Token = token;
            }

            if (billingAddress != null)
            {
                request.BillingAddress = RequestFactory.CreatePaymentMethodAddressRequest(billingAddress);
            }


            Creating.RaiseEvent(new Core.Events.NewEventArgs <PaymentMethodRequest>(request), this);

            var attempt = TryGetApiResult(() => BraintreeGateway.PaymentMethod.Create(request));

            if (!attempt.Success)
            {
                return(Attempt <PaymentMethod> .Fail(attempt.Exception));
            }

            var result = attempt.Result;

            if (result.IsSuccess())
            {
                var cacheKey = MakePaymentMethodCacheKey(token);
                RuntimeCache.ClearCacheItem(cacheKey);

                var customerCacheKey = MakeCustomerCacheKey(customer);
                RuntimeCache.ClearCacheItem(customerCacheKey);

                Created.RaiseEvent(new Core.Events.NewEventArgs <PaymentMethod>(result.Target), this);

                return(Attempt <PaymentMethod> .Succeed((PaymentMethod)RuntimeCache.GetCacheItem(cacheKey, () => result.Target)));
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            LogHelper.Error <BraintreeCustomerApiService>("Failed to add a credit card to a customer", error);

            return(Attempt <PaymentMethod> .Fail(error));
        }
Exemple #23
0
        /// <summary>
        /// Deletes the Braintree <see cref="Customer"/> by it's customer id.
        /// </summary>
        /// <param name="customerId">
        /// The customer id.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Delete(string customerId)
        {
            try
            {
                BraintreeGateway.Customer.Delete(customerId);
                RuntimeCache.ClearCacheItem(MakeCustomerCacheKey(customerId));
            }
            catch (Exception ex)
            {
                LogHelper.Error <BraintreeCustomerApiService>("Braintree API customer delete request failed.", ex);
                return(false);
            }


            return(true);
        }
Exemple #24
0
        private void RecursiveDelete(Guid parentId)
        {
            var list = Database.Fetch <DictionaryDto>("WHERE parent = @ParentId", new { ParentId = parentId });

            foreach (var dto in list)
            {
                RecursiveDelete(dto.UniqueId);

                Database.Delete <LanguageTextDto>("WHERE UniqueId = @Id", new { Id = dto.UniqueId });
                Database.Delete <DictionaryDto>("WHERE id = @Id", new { Id = dto.UniqueId });

                //Clear the cache entries that exist by uniqueid/item key
                RuntimeCache.ClearCacheItem(GetCacheIdKey <IDictionaryItem>(dto.Key));
                RuntimeCache.ClearCacheItem(GetCacheIdKey <IDictionaryItem>(dto.UniqueId));
            }
        }
 /// <summary>
 /// Unit of work method that tells the repository to persist the updated entity
 /// </summary>
 /// <param name="entity">The entity to be updated</param>
 public virtual void PersistUpdatedItem(IEntity entity)
 {
     try
     {
         PersistUpdatedItem((TEntity)entity);
         RuntimeCache.GetCacheItem(GetCacheKey(entity.Key), () => entity);
     }
     catch (Exception ex)
     {
         LogHelper.Error(GetType(), "An error occurred trying to update an exiting entity", ex);
         ////if an exception is thrown we need to remove the entry from cache, this is ONLY a work around because of the way
         //// that we cache entities: http://issues.umbraco.org/issue/U4-4259
         RuntimeCache.ClearCacheItem(GetCacheKey(entity.Key));
         throw;
     }
 }
Exemple #26
0
        /// <inheritdoc />
        public virtual void Save(TEntity entity, bool noStateCheck = false)
        {
            var state = GetEntityState(entity);

            if (state == EntityState.Unchanged && noStateCheck)
            {
                state = EntityState.Modified;
            }

            switch (state)
            {
            case EntityState.Added:
                Emit(Adding, entity);
                this.Context.Add(entity);
                Emit(Added, entity);

                Emit(Saving, entity);
                DbContext.SaveChanges();
                Emit(Saved, entity);
                break;

            case EntityState.Detached:
                this.Context.Attach(entity);

                Emit(Saving, entity);
                DbContext.SaveChanges();
                Emit(Saved, entity);
                break;

            case EntityState.Modified:

                Emit(Saving, entity);
                DbContext.SaveChanges();
                Emit(Saved, entity);

                break;

            case EntityState.Deleted:
            case EntityState.Unchanged:
            default:
                return;
            }

            RuntimeCache.ClearCacheItem(GetCacheKey(entity.Id));
        }
        /// <summary>
        /// The persist new item.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistNewItem(ICustomer entity)
        {
            ((Customer)entity).AddingEntity();

            var factory = new CustomerFactory();
            var dto     = factory.BuildDto(entity);

            Database.Insert(dto);
            entity.Key = dto.Key;

            Database.Insert(dto.CustomerIndexDto);
            ((Customer)entity).ExamineId = dto.CustomerIndexDto.Id;

            entity.ResetDirtyProperties();

            // customer context cache
            RuntimeCache.ClearCacheItem(Cache.CacheKeys.CustomerCacheKey(entity.Key));
        }
Exemple #28
0
        private void SaveProductAttributes(IProduct product, IProductOption productOption)
        {
            if (!productOption.Choices.Any())
            {
                return;
            }

            var existing = GetProductAttributeCollection(productOption.Key);

            //ensure all ids are in the new list
            var resetSorts = false;

            foreach (var ex in existing)
            {
                if (productOption.Choices.Contains(ex.Sku))
                {
                    continue;
                }
                DeleteProductAttribute(ex);
                resetSorts = true;
            }
            if (resetSorts)
            {
                var count = 1;
                foreach (var o in productOption.Choices.OrderBy(x => x.SortOrder))
                {
                    o.SortOrder = count;
                    count       = count + 1;
                    productOption.Choices.Add(o);
                }
            }
            foreach (var att in productOption.Choices.OrderBy(x => x.SortOrder))
            {
                // ensure the id is set
                att.OptionKey = productOption.Key;
                SaveProductAttribute(att);
            }
            // this is required due to the special case relation between a product and product variants
            foreach (var variant in product.ProductVariants)
            {
                RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IProductVariant>(variant.Key));
            }
        }
Exemple #29
0
        /// <summary>
        /// Deletes a <see cref="PaymentMethod"/>.
        /// </summary>
        /// <param name="token">
        /// The token.
        /// </param>
        /// <returns>
        /// A value indicating true or false.
        /// </returns>
        public bool Delete(string token)
        {
            if (!Exists(token))
            {
                return(false);
            }

            try
            {
                BraintreeGateway.PaymentMethod.Delete(token);
                RuntimeCache.ClearCacheItem(MakePaymentMethodCacheKey(token));
            }
            catch (Exception ex)
            {
                LogHelper.Error <BraintreePaymentMethodApiService>("Braintree API payment method delete request failed.", ex);
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Saves a new item to the databse.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        protected override void PersistNewItem(IOrder entity)
        {
            ((Entity)entity).AddingEntity();

            var factory = new OrderFactory(entity.Items);
            var dto     = factory.BuildDto(entity);

            Database.Insert(dto);
            entity.Key = dto.Key;

            Database.Insert(dto.OrderIndexDto);
            ((Order)entity).ExamineId = dto.OrderIndexDto.Id;


            _orderLineItemRepository.SaveLineItem(entity.Items, entity.Key);

            entity.ResetDirtyProperties();

            RuntimeCache.ClearCacheItem(Cache.CacheKeys.GetEntityCacheKey <IInvoice>(entity.InvoiceKey));
        }