Databases Reference
In-Depth Information
public void FulfillOrder(Order order)
{
#if DEBUG
this.Validate(order);
#endif
// ...
}
You can use the Debug version of the business layer during the initial active development
and testing of your application, when finding problems faster is more important than
squeezing every bit of performance from the application. When the testing is finished and
you are ready to deploy the application to the production environment, it should be safe
to assume that the interaction between the presentation and business logic was imple-
mented correctly. At this point, you can deploy the Release version of the business layer,
which does not have the additional validation checks and will work faster.
If you do choose to perform additional validation in the release version of the application,
it is a good idea to verify that it has no significant effect on the application performance.
Remember that CPU and RAM resources of web servers can be increased easily, but addi-
tional database access can be costly and limits the application's ability to scale.
Saving Changes
When implementing business rule methods, such as the FulfillOrder method of the
NorthwindEntities context, you might be wondering if saving changes (by calling the
SaveChanges method of the ObjectContext ) is a good idea. If the SaveChanges method has
to be called anyway, why not simply call it from the FulfillOrder method itself?
A business rule method knows how to retrieve and manipulate a specific set of entities.
However, it has no way of saving only those entities it modifies. The SaveChanges method
of the ObjectContext class saves all changed entities, so calling it in the FulfillOrder
method could affect not only the Order and Product entities it modified, but also all other
entities that were changed before the method was called. This might produce unexpected
side effects further up the call stack, especially when business rule methods can be called
from other methods. From the viewpoint of a developer reading the code, it is also easier
to understand the code if it is responsible for both creating the ObjectContext and saving
changes. Because there is no way to tell if the FulfillOrder method saves changes by
reading this code alone, intuitively, you would assume that changes are not saved unless
the SaveChanges method is called:
using (var context = new NorthwindEntities())
{
int orderId = // ...
context.FulfillOrder(orderId);
context.SaveChanges();
}
 
 
Search WWH ::




Custom Search