Click here to Skip to main content
Click here to Skip to main content

Building a Shared Code Library as a VS.NET Plug-in

By , 30 Mar 2005
 

Introduction

The "CodeManager" is a plug-in for VS.NET that stores code snippets in a database. From the plug-in you can add code, search the database for code snippets to insert into your code. That is really the core of it. The reason why I took all the effort of creating this, was that we (the developer collective) needed a way to share code in our organization. Hence, one very misspent vacation, writing this! In an attempt to justify the time spent on it, I am now inflicting it upon you.

Having done some analysis on what was out there (looking to buy a third party solution - turns out they were overpriced), I thought I could get similar results in just a few days, at a fraction of the cost. I built this during a short vacation over an year ago, hence the code quality is not something I would be proud of, but it does the job it was designed to. To me its a good example of re-use, and leveraging code/classes that already exists, and was able to reduce the development time by orders of magnitude.

This would be interesting to you if;

  • You want to learn how to create a real world VS.NET plug-in.
  • You need a shared code library that you can maintain and extend internally.

Here are a couple of screenshots of it in action:

Adding code to the library

Searching for code

Background

This book gave me some crucial tips in order to get this done quickly: Writing Add-ins for Visual Studio .NET.

I have used and extended the code from Julijan's add-in to include a versioning controlled build in the project, as detailed in the article Versioning Controlled Build.

Using the code

You can either use the installation and DB script to set up a working installation, or set up the code in your IDE. I recommend the latter, though it requires more effort it gives you the opportunity to really examine what is going on.

The overall concept is the same for using ToolWindows in .NET. The VSUserControlHost DLL needs registering (if you don't go through the installer). This is then used to host a usercontrol, which contains all the functionality for the ToolWindow (as seen in the main screenshot). This user control presents an interface to SQL Server database which contains code snippets. Other integration points are the main menu entries, the toolbar entries, an Options dialog and the output windows (all are displayed below in images). Users are validated using Windows Authentication/Active Directory and added dynamically. I have also included an updated version of the version updater (sorry about that sentence), as listed in the references by Julijan Sribar.

The Context Menu

The Main Menu

The Output window

Toolbar items

Setting up

  • You need a SQL DB server to use. Create a database, call it RADDev, and install the supplied script there.
  • You also need to register VSUserControlHostLIb - the project is provided with the code. Open a command prompt, navigate to the folder where the VSUserControlHost.dll is, and type in regsvr32 VSUserControlHost.dll.
  • Now, open the solution, and you will see three projects.
  • WinUserControlTestProject - this is the project used to test the functionality in the main user control during development. It is convenient while testing add-ins to have a separate test application to test the core functionality, as reloading the add-in becomes a drain on debugging time.
  • RadDevSetup - this is the setup program, which builds the redistributable. Nothing remarkable about this.
  • Lastly, RadDev - this is the add-in project itself, which does have lots of interesting things about it.

Points of interest

  • clsCodeDBFunctions – this interfaces with the data accessor class below clsDBTier – this is an accessor block type class. It is quite out of date now, but it still does the job.
  • clsNodeTag: This stores the information for a tree node, it is just a holder class (should probably be a struct, but who knows what more needs to be added in the future).
  • clsRegistration – handles registration of licenses, new users, and decrypts licenses for checking. The registration isn't being checked anymore in the current code set. The build I made for the company was limited to the company's domain.
  • clsWinAuthentication – this class authenticates the user against their Windows domain and user, then checks against the database to see if they have been already added. If they are a new user, they are prompted to enter more details, and subsequently added to the database. It is important to know who is adding articles, as only the user who has added can delete it. Also I have written controls in ASP.NET applications that create content based on the contents of the database, so you need names for the articles.
  • Crypto.vb – this is used for encryption/decryption of license keys. All credit for this goes to Stan Schultes.
  • I am not going into greater details of the code as it is a lot interesting, but nothing in particular stands out to me. You really need to pick out the things that interest you the most from the code set itself.

History

It really needs a thorough cleaning up in terms of removing unused code. What it needs the most is someone who really needs this tool, to take ownership of it (I have too many other projects/commitments), and do a cleaning and rinsing on it. Hence I am freeing it to the world. There are lots and lots of areas where it can be improved. There are some redundant classes, like VSS integration and maintain database versioning, which are still good ideas, but could not make it into this version. My plan was to create a tool that also did integrated change management, linking to a web based system that I was already using. I think there is a call for an open source plug-in that covers these areas. I believe the market prices of the commercial options are over-priced.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

paul heap
Web Developer
Thailand Thailand
Member
Spent my whole life developing, having worked in C++, Delphi, ASP, then finally settling down to working solely in ASP.Net. Also a forex day trader.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalplease move to a new database :)memberJoe Robe27 Jul '05 - 15:31 
Hi,
 
do you now db4o ?? That sql stuff is a pain. If you use db40 you save yourself 90 % of the work and users only need to copy one dll, thats all !
 
Check it out at www.db4o.com.
 
I have LOTS of trouble with that database, but only because I tried to use it in client/server mode for a professional application. They are not ready for this yet, but for embedded applications its a dream !
 
Let me know if such a move is feasable. I dont want to install and mess anymore with sql !
 
Thanks !
 

 
Joe Robe
GeneralTry to get it to workmemberHaichih Hwa14 Apr '05 - 10:21 
I install the Code Mange from the setup and created the database as instructed. Setup the user RADDev and added the registry entry. I am able to open the Code Manager's FormAddin Tools window in the IDE. When I try to test the connection, it say user "bugspray" is not connected to the db. So I added the user account "bugspray" and password "bugspray" in the the RADDev database. I am able to add category to the code lib and add code. I was able to edit the snippet and it shows the saved code. But when I try to add a save code into my project I get blank under the "Code" table and my "Description" tab shows the text I entered when I saved the snippet. Nothing was inserted when "insert" button was click.
 
How can I get the add-in to work???
 
Thanks
 
Haichih (CAI)
GeneralRe: Try to get it to workmemberkScope14 Apr '05 - 21:56 
Hi, the view "proc_SelectCodeEntry" retuns a null value for the field "CodeEntryDescription"(the table tblCodeEntryType has no values), this causes an exception in the form "frmViewCode.vb" (row 221) that causes the function "InitialiseCodeDetails" to exit without warnings. A fast (and dirty) solution is to update the view this way:

ALTER PROCEDURE dbo.proc_SelectCodeEntry
@CodeEntryID int AS
Select
longdesc, dateadded, shortdesc, firstname, lastname, '' as CodeEntryDescription, CodeText
from
tblCodeEntry
left join tbluser on tbluser.user_id = tblCodeEntry.userid
where entryid = @CodeEntryID

 
hope it helps
kScope

Generalnot able to set database settings in Raddev optionmemberashu_sharma2112 Apr '05 - 0:07 
Hi Paul,
I came across this tool of yours today and it seems to be very helpful. While trying to run it I am facing this problem while making Database settings through Raddev option. when i set server and database, I get this error "Connection Failed : Login failed for user 'Bugspray'".
 
Please help!
 
Ashu Sharma
Web Analyst
QuestionHow to get it workingmemberSteve Hurcombe8 Apr '05 - 1:00 
Hi,
You need to follow these steps to get it working:
 
  • Run the setup
  • Create a database RadDev
  • Create an SQL login raddev with a password of raddev (this is hardcoded)
  • Make the raddev login a user in the RadDev DB and make it the dbowner. I think that there's a problem with the SQL script as the permissions were insufficient.
  • Run the SQL script in the RadDev db
  • Add the following settings to the registry
 
[HKEY_CURRENT_USER\Software\VB and VBA Program Settings\RadDev\General]
"DBServer"="yourservernamehere"
"Database"="RadDev"
 
If these values are not here then the toolbar won't appear, so you can't change the options to add the values...catch-22...
 
When you run VS, and start the addin, create a new user. It should pop a box to say that it has done this.
 
Then restart Visual Studio.
 
That's as far as I've got so far but it looks to be OK now.
 
Best regards
 
Steve
http://stevehurcombe.blogspot.com
AnswerRe: How to get it workingmemberpaul heap12 Apr '05 - 5:32 
Thanks for providing the instructions steve, the script i had was injecting into another database hence the wrong username. Sorry im flat out i would create the right script but i dont have time right now. If someone would like to send it me i will update the downloadable.
 
This is my rifle
There are many like it, but
This rifle is mine
GeneralWrite DRY CodememberAl Einstien4 Apr '05 - 7:23 
DRY (Don't repeat yourself) code means that when things change or you have a defect to fix you only have to make a change in one place. Tools like this fly in the face of writing DRY code because they enable the coping of code. Whenever you are coping code you are probably doing something wrong. A better alternative is to create a core object library to include in all your projects. My problem isn't with the add in itself (I'm sure it's a nice add in) just the concept. I also thought it would make for an interesting discussion.
GeneralRe: Write DRY Codememberpaul heap4 Apr '05 - 17:29 
I hear you and i agree with the principle (read that acronym in a book, programmer as craftsman, 'the pragmatic programmer'?). We did have a core library, which got used a lot more. In reality its more useful for javascript/html/TSQL; the code that cant naturally go in your object library. But it was used a lot. There is a review and edit mechanism in there too so you can improve code. Code tends to move out of the plug-in and into the object library over time. There are many ways to reuse code;
-The shared code library (look at codeproject/internet resources itself, think of it as a smaller/local (but faster) version of that)
-Object libraries
-3rd party component libraries
-Code from older projects that achieves a given techniques
-Code generation (ie CodeSmith)
 
This is written in the past tense as actually the company in question have no idea whatsoever about software development, so i left very recently. Its a dilberts history of mismanagement which i may relate one day.
 
This is my rifle
There are many like it, but
This rifle is mine
GeneralRe: Write DRY CodememberDot Netter5 Apr '05 - 13:44 
I agree with your comment entirely. However, I wrote myself a tool much like this, myself. Not because I like the idea of having code snippets I can copy and paste, but to have purely as an algorithm reference. I would have a category for "Sorting Algorithms" for example, in which I would have some implementation of a Bubble Sort, a Merge Sort, etc... It would just be a reference, and more like a Knowledge Base than a whole lot of code I could re-use in production. Maybe it's different in that respect, because the code in there was always written to be "illustrative and educational" rather than "off the shelf reusable."
GeneralDoesn't work :-(memberNiteman3 Apr '05 - 23:18 
Hi Paul,
I'm really keen on getting this plugin to work, because I was searching for something like this a long time. But it doesn't seem to work - probably only the way I install it Wink | ;-) ?
Here the steps I did:
1.) Made a new SQL-DB called "RADDev"
2.) set up new user "raddev" pw "raddev" with admin-rights (for testing)
3.) run the sql-db-script you provided - successfully.
4.) installed your application
5.) registered the vsusercontrol.dll via regsvr32
6.) started Visual Studio .NET 2003
a.) a Dialog appeared, asking me to add myself as a user. So I did, completed the registration Form, pressed ok. This operation seemed to take a few seconds to complete, but the initial dialog doesn't disappear. I pressed ok. Nothing happened. I pressed ok the second time, then the dialog disappeared, VS started normally.
 
As I then used VS and loaded a project, everything was the way it was before - no trace from the addin. But in Addin-Manager, it appeared and was checked to be loaded. How do I "activate" it? Even as I checked the contents of the tblUsers in SQL, there was no entry.
 
Could you give me a hint how I could get it to work? Would be GREAT!
 
Thx in Advance,
Eddy

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 31 Mar 2005
Article Copyright 2005 by paul heap
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid