
Choosing a Notification Subscription Strategy Using the SqlClient Managed Provider
There are two ways to request a notification subscription. A dependency provides a high-level interface to query notifications. An application simply creates an instance of a dependency class (for example, SqlDependency in ADO.NET), registers a handler to receive the notification event, and adds the dependency object to a query command. The dependency object calls the handler when the notification occurs.
A notification request provides a lower-level, more flexible interface to query notifications. Like a dependency, an application creates a notification request (for example, an instance of SqlNotificationRequest in ADO.NET) and attaches the notification request to a query command. Unlike a dependency, a notification request simply requests a notification subscription for the query. The application itself must retrieve notification messages from the database, process the messages, and react accordingly. This allows more flexibility in application design, but requires slightly more effort. A notification request is suitable for applications that require flexibility in handling notifications. For example, an application that runs on a scheduled basis cannot use a dependency, since the dependency object is destroyed when the application exits. With a notification request, however, the application can check the queue on startup, and refresh the local data cache if necessary.
Applications use a dependency when the application that registers the notification subscription is also the application that will process the notification, when the application will remain running continuously, and when connectivity to the database is available while the application is running. For applications that meet these requirements, dependencies provide an easy way to use query notifications. Applications use a notification request when the application cannot use a dependency. In this case, the application developer must design a strategy to receive and process messages that makes sense for the application.