As mentioned earlier, the add-in creates a class that
implements IDTExtensibility2. In this section we will discuss some of the
methods that need to be implemented in the class. The methods that need to be
implemented are:
OnConnection - Receives notification that the Add-in is
being loaded. The parameters are:
Application - The Root object of the host
application
connectMode - How the add-in is loaded
addInInst - Represents the Add-In
OnDisconnection - As the name suggests, the Add-in is being
unloaded. The parameters are:
disconnectMode - How the add-In is unloaded
Custom - Array of parameters specific for the host
application
OnAddInsUpdate - Receives notification that the collection
of Add-Ins has changed. Parameters are:
Custom - Array of parameters specific to the host
application
OnStartupcomplete - Receives notification that the host
application has completed loading. Parameters are:
Custom - Array of parameters specific to the host
application
OnBeginShutdown - Receives notification that the host
application is being unloaded.
Custom - Array of parameters specific to the host
application.
For our application we are mainly concerned with
OnConnection. For SSMS add-in we will write our code in the OnConnection
method. As we were writing the Add-In for recognizing the database object or
table object we needed to have an event handler that would recognize the object
that was selected. So, we wrote an event handler called OnSelectionChanged.
This event handler was provided to handle the IObjectExplorerEventProvider's
SelectionChanged event. So, now our code for OnConnection looks like:
Listing 1
_ApplicationObject = Ctype(Application, DTE2)
_addInInstance = Ctype(AddINInst, AddIn)
_ObjectExplorer = ServiceCache.GetObjectExplorer
Dim typeOfProvider as Type = GetType(IObjectExplorerEventProvider)
Dim provider as IObjectExplorerEventProvider = _
Ctype(ObjectExplorer.GetService(typeOfProvider), IObjectExplorerEventProvider)
AddHandler provider.SelectionChanged, AddressOf OnSelectionChanged
The _ApplicationObject, _addInINstance, _ObjectExplorer are
declared in the class as follows:
Listing 2
Private _ApplicationObject as DTE2
Friend _addINInstance as AddIn
Private _ObjectExplorer as IObjectExplorerService
In our SQL SP Generator application, we have declared
another class called SQLObjectNode. The scope of this class is Friend. So, we
can call the functions through the SelectionChanged event handler that is
declared in the Connect class. The SelectionEventHandler code looks like:
Listing 3
Dim objSQLDBNode as SQLObjectNode = SQLObjectNode.GetInstanceOnSelection(Me)
This is all we need to do on this class. This class just
implements the IDTExtensibility2 and lets the application know what to do in
it.