Databases Reference
In-Depth Information
NOTE
Yo u ca n r e a d a bo u t th e Fai l Fas t appr oac h i n an ar t i cl e w ritte n by Jim S ho re f or IEEE
and available online at
http://martinfowler.com/ieeeSoftware/failFast.pdf.
Although it
might seem counterintuitive at first, this approach significantly improves quality of
applications by allowing developers to discover expected error conditions quickly and
implement appropriate logic for handling them in a way that not only looks pretty, but
also makes sense from the business standpoint.
Whether or not an error condition is expected during normal execution of the application
depends on the perspective of the developer designing on the business layer. For instance,
the
FulfillOrder
method just shown, which takes an
OrderID
as a parameter, treats an
invalid order status value as an
expected
error condition based on an assumption that this
method will be called from an Order web page where the user could click a Fulfill button
more than once. This could happen if the user's network connection is interrupted after
the initial Order page is displayed and she clicks the browser's Back button to fulfill the
order again.
However, the overloaded version of the
FulfillOrder
method shown next, which takes an
Order
parameter, assumes that the caller is responsible for checking the current order
status before trying to fulfill it. If the order is not
Paid
when the method is called, it
throws the
InvalidOperationException
to indicate that there is a bug in the calling code.
public partial class NorthwindEntities
{
public void FulfillOrder(Order order)
{
if (order.OrderStatus != (byte)DataModel.OrderStatus.Paid)
{
throw new InvalidOperationException(
“Order status must be Paid for the order to be fulfilled”);
}
order.OrderStatus = (byte)DataModel.OrderStatus.Fulfilled;
order.ShippedDate = DateTime.Now;
IQueryable<Order_Detail> orderItems = this.Order_Details
.Include(“Product”)
.Where(od => od.OrderID == order.OrderID);
foreach (Order_Detail orderItem in orderItems)
orderItem.Product.UnitsInStock -= orderItem.Quantity;
}
}