C#

Transient fault handling in Sitecore Commerce

Posted on

Transient fault handling can be cumbersome and lead developers to write repetitive code. There could be many situations when you want your code to be retried in case of failures/exceptions etc. While working with a few external system integrations, we had to implement a retry mechanism for certain transactional requests. Payment integrations are one of the examples that I am using here. Polly is a library that allows developers to express resilience and transient fault handling policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Almost all the payment providers we integrated to Sitecore Commerce project e.g. Braintree/DiectBank/Openpay recommend to retry the transactional requests in case of timeouts or internet issues. For example when we request Braintree payment gateway to settle the payment if the request times out you have to retry the same request 3 times before you treat it as a failure. Since the retry could be handled […]

C#

Multiple Delivery Calculations – Sitecore Commerce

Posted on

While working with one of the clients recently, we had to implement a product-specific delivery calculation. Certain products sold online, are delivered by the company itself and they have fix delivery charges for each region. While certain products(depending on the size/nature) are delivered through other courier services. Since the delivery charges can differ depending on the courier service, we have to calculate/fetch delivery charges for particular products. Let’s say a customer adds two products to the cart Bluetooth Speaker Baby Cart Since speakers are delivered by the company itself, we use the internal delivery fee using a delivery fee book. For baby-cart, we integrate to the external courier delivery fee service. For the sake of this article, I am calling Speaker as Internal product while baby-cart as external product. In commerce engine solution we have a feature called fulfilment, this feature has CalculateCartDeliveryBlock which calculates the delivery fee for internal products. This […]

C#

Entity framework core and enhanced testability

Posted on

Entity framework aka EF is a powerful ORM tool. After using it for many years on full Net-framework(until EF6), I came across netCore version of it. If you have started working with netCore stack, this post will help a great deal to start consuming netCore EF. NetCore is an open source, cross-platform framework which can be used to develop .net applications and run them on almost all known operating systems e.g. Linux, Windows and macOS. To use EF in a netCore application, you have to go with netCore version of it. In addition to EF6 features, netCore EF has an OOTB(out of the box) inMomory database provider which can be used to create a logical relational database in the memory and use it until the life cycle of the application. This is very powerful when it comes down to writing integration tests. In netCore application, we can register the services on startup of the application. This means, our main application(could MVC web […]

C#

Http Caching for Web API Responses

Posted on

A Web API serves a client e.g. browser with an HTTP response upon receiving an HTTP request. Sometimes these responses could be huge size wise and this can lead to delays in displaying the response to the user. To cater such things, almost all the web browsers (Chrome, IE, Mozilla) have built-in capabilities to cache the http responses for a given timespan. A cache is an in memory store for data which is used to improve the performance. Read more about cache… Caching of a response at the browser is not automatic. In the response headers we have to raise max-age or expires flag to tell the browser, it has to cache the response for a given timespan. Until this cache remains valid, browser picks data from cached response rather going back to the server again. Validity of browser’s cache depends on the value of max-age or expires headers. You […]

C#

ExpandoObject – add properties dynamically

Posted on

ExpandoObject class allows to create an object of dynamic type. This dynamic type object allows to attach properties to it at runtime. While working on a project recently, I had to create a data service which builds response for a data request without knowing the schema and source of data. The request contains following information Request.TableName : “SegmentTable” Request.RequiredFields: [“SegmentKey”, “SegmentIndex”, “Attribute1”, “Attribute2”, “Attribute4”] SegmentTable looks like something SegmentID SegmentKey SegmentIndex Attribute1 Attribute2 Attribute3 Attribute4 ……. Data service builds a query like [SELECT * from {SegmentTable}] and then creates a collection of ExpandoObjects  against each row returned against this query. This code snippet shows how an ExpandoObject is created from a datatable row.

ExpandoObject uses dictionary underneath to achieve this but for the users its very seamless. We can attach the properties directly to the object as well. In this case since the names of those properties were also coming from database/request so we had to type cast […]