Wednesday, September 24, 2008

Exception handling - Patterns, Classification, Best Practices

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of some condition that changes the normal flow of execution.

Exception safety

A piece of code is said to be exception-safe if run-time failures within the code will not produce ill effects, such as memory leaks, garbled stored data or invalid output. Exception-safe code must satisfy invariants placed on the code even if exceptions occur.

There are several levels of exception safety:
Failure transparency, also known as the no throw guarantee: Operations are guaranteed to succeed and satisfy all requirements even in presence of exceptional situations. If an exception occurs, it will not throw the exception further up. (Best level of exception safety)

Commit or rollback semantics, also known as strong exception safety or no-change guarantee: Operations can fail, but failed operations are guaranteed to have no side effects so all data retain original values.

Basic exception safety: Partial execution of failed operations can cause side effects, but invariants on the state are preserved. Any stored data will contain valid values even if data has different values now from before the exception.

Minimal exception safety also known as no-leak guarantee: Partial execution of failed operations may store invalid data but will not cause a crash, and no resources get leaked.

No exception safety: No guarantees are made. (Worst level of exception safety)

Exception handling Patterns

Here is the list of patterns and questions defining the problems ( For the complete exposition visit here)

Error Object
What characterizes an error? How to structure and administrate error information?

Exception Hierarchy

How to structure error types? What role does inheritance play in the structuring of errors?

Error Traps
What indicators are useful to detect erroneous situations and where to install the traps in the application code?

Assertion Checking Object

How to implement Error Traps in an object oriented language without using a generative approach?

Backtrace

How to collect and trace useful information for the system developers or the maintenance team, so that it supports them by the analysis of the error situation? Especially, if we have no or limited access to the stack administered by the system itself.

Centralized Error Logging

How do you organize exception reporting so that you can offer your maintenance personnel good enough information for analyzing the branch offices problems?

Error Handler
Where and how do you handle errors?

Default Error Handling
How do you ensure that you handle every possible exception correctly (no unhandled exception and limited damage)?

Error Dialog

How to signal errors to an application user?

Resource Preallocation
How to ensure error processing although resources are short?

Checkpoint Restart
How do you avoid a complete rerun of a batch as a result of an error?

Exception Abstraction
How do you generate reasonable error messages without violating abstraction levels?

Exception Wrapper

How do you integrate a ready-to-use library into your exception handling system?

Multithread Exception Handling

How to schedule exceptions in a multithread environment?

Eric Lippert's classification


Writing good error handling code is hard in any language, whether you have exception handling or not. When Eric Lippert first classifies every exception into one of four buckets which he labels fatal, boneheaded, vexing and exogenous. You can read the entire article here.

Fatal exceptions are not your fault, you cannot prevent them, and you cannot sensibly clean up from them.

Boneheaded exceptions are your own darn fault, you could have prevented them and therefore they are bugs in your code. These are all problems that you could have prevented very easily in the first place, so prevent the mess in the first place rather than trying to clean it up.

Vexing exceptions are the result of unfortunate design decisions. Vexing exceptions are thrown in a completely non-exceptional circumstance, and therefore must be caught and handled all the time.

And finally, exogenous exceptions appear to be somewhat like vexing exceptions except that they are not the result of unfortunate design choices. Rather, they are the result of untidy external realities impinging upon your beautiful, crisp program logic.

Best practices

Here is the gist of what Daniel Turini talks in his article Exception Handling Best Practices in .NET.


Plan for the worst

-Check it early
-Don't trust external data
-The only reliable devices are: the video, the mouse and keyboard.
-Writes can fail, too

Code Safely

-Don't throw new Exception()
-Don't put important exception information on the Message field
-Put a single catch (Exception ex) per thread
-Generic Exceptions caught should be published
-Log Exception.ToString(); never log only Exception.Message!
-Don't catch (Exception) more than once per thread
-Don't ever swallow exceptions
-Cleanup code should be put in finally blocks
-Use "using" everywhere
-Don't return special values on error conditions
-Don't use exceptions to indicate absence of a resource
-Don't use exception handling as means of returning information from a method
-Use exceptions for errors that should not be ignored
-Don't clear the stack trace when re-throwing an exception
-Avoid changing exceptions without adding semantic value
-Exceptions should be marked [Serializable]
-When in doubt, don't Assert, throw an Exception
-Each exception class should have at least the three original constructors
-Be careful when using the AppDomain.UnhandledException event

Bulk inserts and updates

A common development task is transferring data between disparate data sources. In this post have outlined a few approaches that can help you to do bulk inserts and updates.

1. Bulk inserts and updates by using the OpenXML method.
OpenXML method is one of the approaches to do bulk inserts and updates with different Microsoft .NET data providers.

Following are two good articles on this
http://support.microsoft.com/kb/315968
http://dotnet.org.za/ncode/archive/2007/05/14/Bulk-Update-and-Insert-of-Object-State-Stored-by-SQL-Server-2000-Using-.NET-C_2300_.aspx

2. SqlBulkCopy to streamline data transfers
The .NET Framework 2.0's SqlBulkCopy class allows you to easily move data programmatically from any data source to a SQL Server table.

Here are some articles that throw light on this
http://video.techrepublic.com.com/5100-10878_11-6187181.html
http://www.codeproject.com/KB/database/TransferUsingSQLBulkCopy.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

3. Bulk copy large files using bcp
The bcp (bulk copy) command allows you to quickly bulk copy large files into SQL Server tables or views. With .NET Framework 1.1, you can utilize bcp via a SqlCommand object.

Following is an article that talks of bcp:

10 things you should know of Microsoft's SharePoint Services

SharePoint Services is touted as a document management system, and there's a built-in problem with that concept, because we all have a pretty fixed and mundane idea of what a document management system is. SharePoint's Web-centric orientation, however, gives it some unexpected punch, and may change your thinking. Here are some points to consider.

1. SharePoint extends Exchange Server
If you're using Exchange Server to handle your email traffic, SharePoint can greatly simplify distribution.

2. SharePoint collaboration solutions are scalable
Creating sites for team interaction, sharing and management of project-specific documents and files, testing, and other collaborative functions are a natural application of SharePoint. A less hyped aspect of SharePoint is that this collaborative utility is highly scalable.

3. SharePoint sites are highly customizable
SharePoint Services comes fully integrated with FrontPage 2003, so all of FrontPage's WYSIWYG Web editing tools are available for use in crafting SharePoint sites.

4. SharePoint extends InfoPath
InfoPath 2003 is Microsoft's desktop application technology for integrated forms management and data transport. Specifically, you’ll find it useful to publish InfoPath forms directly to a SharePoint library.

5. Metadata can be used to create dynamically parsed storage systems

Metadata is critical to the SharePoint Server concept, and comes in several flavors. With metadata you can effectively create customized search arguments that permit you to organize information dynamically, and to use search criteria from one document library to retrieve information from another.

6. SharePoint can be a data transport mechanism
Depending on what your organization's sites contain, content-wise, and the role(s) the sites are playing in your system, you can actually distribute data from server to server by means of SharePoint's site-moving utilities (see #10).
For instance, if you have SharePoint sites deployed internally to represent data in different workflow stages, the SharePoint content databases of those sites can be rotated in a de facto batch process using these utilities (which are Command Line programs and therefore scriptable).

7. Use the Task Pane to turn Word libraries into collaborative systems with built-in administration
You have a Task Pane that ties documents to libraries, and within it lie a number of important features that take you from the simple management of documents to real collaboration and administration. Through the Task Pane, you can:
track status and versioning of documents
define and track who has site/document access
do task monitoring
create alerts
You can, of course, save from all Office applications—not just Word—to SharePoint.

8. SharePoint can pull data from external databases and other data sources
Data View Web Parts allow you to add views to your sites from a variety of data sources. You can create views specific to your SharePoint sites and link views together. Data sources can be databases, Web services, or any XML source (InfoPath documents, etc.).

9. Leverage Excel for data management
Exporting data to Excel is well-supported in SharePoint and makes graphing and printing convenient (via the Print with Excel and Chart with Excel options). But it's also possible (and may often be desirable) to export data to Excel just for the sake of manageability. The Excel Export function creates an Excel Web query linking to the original data. In this way, you can create spreadsheets that will accept data, and then push that data to SharePoint.

10. Sites and entire site collections can be backed up in a single operation
The ability to move a site, lock-stock-and-barrel (and even more so a site collection, which includes primary site, sub-sites and all their contents), should not be under-appreciated. Anyone who's migrated sites the hard way knows it can be maddeningly frustrating. SharePoint Services includes two utilities that will greatly reduce the frustration: STSADM and SMIGRATE.

SMIGRATE is for backup/restore and for moving sites wholesale. It's a command line utility, so it's tailor-made for scripting, and can simplify the process of moving a site and its contents to the point that it can conceivably be a content distribution tool in some scenarios.

This is a cut down version of the very good article by Scott Robinson and can be read here.

Wednesday, September 17, 2008

Reporting Services 2008

Reporting Services provides companies with the ability to fill a variety of reporting scenarios.

Managed Reporting

Also often referred to as enterprise reporting - supports the creation of reports that span all aspects of the business and delivers them across the enterprise to provide every employee real time access to information relevant for their business area and enable better decision making.

Ad-Hoc Reporting

Enables users to create their own reports on an ad-hoc basis and provides them with the flexibility to quickly get the information that they need, in the format that they need it without submitting a request and waiting for a report developer to create the report for them.

Embedded Reporting

Enables organizations to embed reports directly into business applications and web portals, enabling users to consume reports within the context of their business process. Deep integration with Microsoft Office SharePoint Server 2007 also enables organizations to deliver reports through a central report library or to use new web parts for thin rendering of reports directly within SharePoint enabling easy creation of dashboards. In this way organizations are able to bring all business critical data, structured as well as unstructured, from across the company together in one central location providing one common experience for information access so that users can see key business performance information at a glance.

Authoring Reports
Report authoring is a major activity in many organizations. Executives, business analysts, managers, and increasingly information workers throughout the enterprise rely on timely and accurate information from easy to understand reports to perform their job effectively. SQL Server 2008 Reporting Services includes comprehensive report authoring tools, and a range of report format innovations that make it easy to create reports that bring data to life and provide the information that employees need in whatever format is most effective for your organization.

Using Report Development Tools

In most organizations, there are two distinct groups of people who create reports; experienced business intelligence solution developers who are used to working in a comprehensive development environment, and business users who are unfamiliar with database schema designs and need an intuitive report design environment that abstracts the underlying technical complexities.

SQL Server 2008 meets both of these needs by providing distinct report development tools specifically designed to meet the needs for these two audiences. This enables developers to create sophisticated reporting solutions for the entire enterprise, while making it easy for business users to focus on the specific data relevant for their business area.

Dundas visualizations platform

With the arrival of SSRS 2008 users will now gain out-of-the-box access to the Dundas visualizations platform. SSRS 2008 (as of the February 2008 CTP build) contains both Dundas Gauge and Dundas Chart products. In addition, according to the prior mentioned Dundas press release it states that Dundas Calendar will also be included into SSRS 2008.

This fairly recent Microsoft purchase of the Dundas source code for integration into SSRS 2008 is a great move as the Dundas suite of SSRS add-ons have become the premier choice for such advanced visualization needs. By including the Dundas technologies into SSRS 2008, Reporting Services customers will not only gain access to a much improved Report Server Architecture (without requiring IIS) but also an enhanced Visualization platform. There are literally about three times as many chart types in SSRS 2008 as compared to SSRS 2005. Some of the brand new chart types include the Funnel, Range, Pyramid, and Polar. In addition to the added chart types customers will also gain access to the Dundas Gauge capabilities via a new Gauge Data Region. Finally, we get a few other ‘goodies’ with the inclusion of Dundas suite including:
Secondary Axes
Runtime Calculated Series
WYSIWYG Chart Editor (design-time)

GRASP Patterns

GRASP stands for General Responsibility Assignment Software Patterns (or sometimes Principles). It is used in object oriented design, and gives guidelines for assigning responsibility to classes and objects...

Larman claims that GRASP can be used as a methodical approach to learning basic object design. These are patterns of assigning responsibilities. He also says that there are two types of responsibilities:

knowing responsibilities that include knowing about private encapsulated data, about related objects, and things it can derive or calculate

doing responsibilities include doing something itself, like creating another object or doing a calculation, initiating action in other objects, and controlling and coordinating activities in other objects.

The full set of GRASP patterns are:
Information Expert
Creator
Controller
Low Coupling
High Cohesion
Polymorphism
Pure Fabrication
Indirection
Protected Variations

Information Expert

The Information Expert pattern provides the general principles associated with the assignment of responsibilities to objects. The information expert pattern states that responsibility should be assigned to the information expert—the class that has all the essential information. Systems which appropriately utilize the information expert pattern are easier to understand, maintain and expand as well as increase the possibility that an element can be reused in future development.

Related patterns are
Low Coupling/High Cohesion : The Expert pattern promotes low coupling by putting methods in the classes that have the information that the methods need. Classes whose methods only need the class’ own information have less need to rely on other classes. A set of methods that all operate on the same information tends to be cohesive.

Creator

The Creator pattern solves the problem of who should be responsible for the creation of a new instance of a class. The creator pattern is important because creation of objects is one of the most ubiquitous activities in an object-oriented system. A system that effectively utilizes the creator pattern can also support low coupling, increased understandability, encapsulation and the likelihood that the object in question will be capable of sustaining reuse. Given two classes, class B and Class A, class B should be responsible for the creation of A if class B contains or compositely aggregates, records, closely uses or contains the initializing information for class A. It could then be stated that B is natural object to be a creator of A objects.

The Factory pattern is a common alternative to Creator when there are special considerations, such as complex creation logic. This is achieved by creating a Pure Fabrication object (see below), called Factory that handles the creation.

Controller

The Controller pattern assigns the responsibility of dealing with system events to a non-UI class that represent the overall system or a use case scenario. A use case controller should be used to deal with all system events of a use case, and may be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate use case controllers). It is defined as the first object beyond the UI layer that receives and coordinates ("controls") a system operation. The controller should delegate to other objects the work that needs to be done; it coordinates or controls the activity. It should not do much work itself. The GRASP Controller can be thought of as being a part of the Application/Service layer (assuming that the application has made an explicit distinction between the App/Service layer and the Domain layer) in an object-oriented system with common layers.

Related patterns are
Pure Fabrication: The Controller pattern is a specialized form of the Pure Fabrication pattern.
Mediator: The Mediator pattern is used to coordinates events from a GUI. Like controller objects, a highly coupled and incohesive mediator object may involve less overall complexity than an arrangement that distributes the same responsibilities over more objects.

Low Coupling

Low Coupling is an evaluative pattern, which dictates how to assign responsibilities to support:
low dependency between classes;
low impact in a class of changes in other classes;
high reuse potential;

Related patterns are
Interface: One form of coupling between classes is the coupling between a subclass and its superclass. It is often possible to avoid subclassing by using the Interface pattern.
Mediator: It is not necessary or even always desirable for all of the classes in a design to have low coupling and high cohesion. Sometimes the overall complexity of a class can be reduced by concentrating complexity in one class. The Mediator pattern provides an example of that.
Composed Method: It is possible for methods to be uncohesive and difficult to work with. Some common causes are excessive length or too many execution paths within a method. The Composed Method pattern provides guidance of breaking up such methods into smaller, simpler and more cohesive methods.

High Cohesion

High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable. High cohesion is generally used in support of Low Coupling. High cohesion means that the responsibilities of a given element are strongly related and highly focused. Breaking programs into classes and subsystems is an example of activities that increase the cohesive properties of a system. Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities. Elements with low cohesion often suffer from being hard to comprehend, hard to reuse, hard to maintain and adverse to change.

Related patterns same as related patterns for Low Coupling

Polymorphism

According to the Polymorphism pattern, responsibility of defining the variation of behaviors based on type is assigned to the types for which this variation happens. This is achieved using polymorphic operations.

Related patterns are
Dynamic Linkage You can implement plug-ins or pluggable software components using a combination of polymorphism and the Dynamic Linkage pattern.

Pure Fabrication

A pure fabrication is a class that does not represent a concept in the problem domain, specially made up to achieve low coupling, high cohesion, and the reuse potential thereof derived (when a solution presented by the Information Expert pattern does not). This kind of class is called "Service" in Domain-driven design.

Related patterns are
Low Coupling/High Cohesion The point of the Pure Fabrication pattern is to maintain the low coupling and high cohesion of the classes in an object oriented design.

Indirection

The Indirection pattern supports low coupling (and reuse potential) between two elements by assigning the responsibility of mediation between them to an intermediate object. An example of this is the introduction of a controller component for mediation between data (model) and its representation (view) in the Model-view-controller pattern.

Related patterns are
Low Coupling/High Cohesion: The fundamental motivation for the Don’t Talk to Strangers pattern is to maintain low coupling.
Pure Fabrication: There are sometimes good reasons for calls made to classes added to a design using the Pure Fabrication pattern to violate the guidelines of the Don’t Talk to Strangers pattern.
Mediator: The Mediator pattern provides an example of a class created through pure fabrication that receives direct method calls from classes unrelated to it with a benefit that outweighs the disadvantages of the direct calls.

Protected Variations
The Protected Variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability with an interface and using polymorphism to create various implementations of this interface.

"The critical design tool for software development is a mind well educated in design principles. It is not the UML or any other technology" (Larman, Craig. Applying UML and Patterns - Third Edition). Thus, GRASP is really a mental toolset, a learning aid to help in the design of object oriented software.