Vssharp: A vs Extension development framework (top) Prev: Design One, Introduction
In the process of developing the SSMs plug-in Sqlsharp (er) since 2008, it was found that most of the code was the same one day, like this.
Commands2 commands = (COMMANDS2) _applicationobject.commands; String toolsmenuname = "Tools"; Place the command on the Tools menu. Find the MenuBar command bar, which is the top-level command bar holding all the main menu Items:microso Ft. VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars) _ Applicationobject.commandbars) ["MenuBar"]; Find the Tools command bar on the MenuBar command Bar:commandbarcontrol Toolscontrol = menuBarCommandBar . Controls[toolsmenuname]; CommandBarPopup toolspopup = (CommandBarPopup) Toolscontrol; This try/catch block can is duplicated if you wish to add multiple commands to being handled by your add-in, Just make sure your also update the Querystatus/exec method to include the new command names. try {//add a command to THe Commands collection://Add + (int) vscommandstatus.vscommandstatusenabled If we want the default STA Te to is enabled command command = commands. AddNamedCommand2 (_addininstance, "Formatsql", "Format SQL", "format SQL", True,, ref contextguids, (int) vscommandstatus.vscommandstatussupported + (int) vscommandstatus.vscommandstatusenabled, (int) Vscommandstyle.vscommandstylepictandtext, Vscommandcontroltype.vscommandcontroltypebutton); Add a control for the command to the Tools menu:if (Command! = null) && (toolspopup! = null )) {command. AddControl (Toolspopup.commandbar, 1); }} catch (System.ArgumentException) {//if We is here, T Hen the exception is probably because a command with that name//already exists. If so there are no need to recreate the command And we can//safely ignore the exception. }
So the idea of developing a framework emerged.
So there is a framework called Ssmssharp, after formally named Sqlsharp released to the CodePlex.
At the same time, the code that manipulates EnvDTE is separated from the code of SSMs objects, and the code that manipulates EnvDTE forms the vssharp of this article.
Later, when I formally used the Vssharp development vs extension, and brought up some new problems such as source code checkout, a vs with multiple extensions, to solve these problems, Vssharp began to mature.
Second, design ideas
1. Target
Apply the command pattern to define the behavior of each control. Sets the properties and behaviors of each control in a configuration file, automatically loads the control when vs starts, and triggers the corresponding command by reflection when the control is clicked.
2. Process
User: End users (that is, you yards farm)
Host:vs instances, providing global EnvDTE object accessors, registering plugin, responding to various events in the IDE (such as opening and closing documents)
Plugin: Plug-in developed based on VSSHARP (here to avoid the same name as Envdte.addin, named Plugin)
This leads to Vssharp's responsibilities.
- Responsible for configuring the load
- Register the control with VS
- Respond to user clicks and other events
Three, outline design
1. Object Design
1.1 Based on the above definition of responsibilities, abstract the following objects:
- Commandconfig: Responsible for command and control configuration description
- Commandmanager: Responsible for configuring the load, and interacting with VS
- Commandbaraccessor: Objects interacting directly with VS, implementing the Icommandbaraccessor interface, primarily to isolate vs version differences
- Host: Host, Singleton, representing the VS instance of the current operation; Commandaccessor interacts with EnvDTE through it
- PlugIn: The main attribute is the assembly of the Commandconfig and connect portals
Commandbaraccessor's behavior:
Public interface icommandbaraccessor { void AddControl (Commandcontrol control); void Resetcontrol (Commandcontrol control); void Enablecontrols (ienumerable<string> IDs, bool enabled); void Delete (); }
- Addcontro: Adding a control
- Resetcontrol: Resetting a control, such as a control's submenu, can depend on a specific configuration or data source, and reloading the control when the configuration or data source changes
- Enablecontrol: Enable (disable) controls, such as certain controls that are used to manipulate documents, are enabled when a document is open
- Delete: Deletes the control that is triggered when the Disconnect method is executed when vs exits
1.2 Command interface
Public interface ICommand { void Execute (object arg = null); }
Command type:
Public enum Commandactiontype { Menu, program , Window, Dialog }
- Menu: Default type, no behavior
- Program: Execute a procedure
- Window: Open a form
- Dialog: Open a modal form
1.3 Command Control description
There are two main types of controls:
- Commandmenu: Includes the main menu bar of the custom menu, context menu, its subordinate can have a submenu
- CommandButton: The main is to insert ToolStrip ToolStripButton, its subordinate can also have sub-menu
The parent class of the abstract class Commandcontrol:commandmenu and CommandButton, which describes the control's ID, text, icon, command type, location, owning parent control, and so on.
The following code fragment is the full property of Commandcontrol.
which
ClassName is the name of the action type used for reflection, and when Commandactiontype is program, the type is required to implement the ICommand interface.
View Code
Commandmenu inherits Commandcontrol, which is unique to submenu-related properties.
Where the submenus property can be manipulated at programming time, Subgeneratortype is a sub-menu generator type defined in the configuration file for reflection and is automatically generated at startup based on a specific data source.
View Code
2. Class Diagram
Call Relationship:
- The Connect object loads Commandconfig when it starts, generates a plugin object to Commandmanager, and register with host.instance; Commandmanager load all controls described by Commandconfig
- Connect.onconnection method Call Commandmanager.load method
- Connect.exec method Call Commandmanager.execute method
- Connect.ondisconnection method Call Commandmanager.disconnect method
Iv. Source Code
http://vssharp.codeplex.com/
---------------------------------------------------
The next article will explain the use of the framework with an example, so please look forward to it.
VS Extended Development framework