The Easiest Way to Share Code: NuGet
You want to use a DLL in multiple projects (it's even possible that other developers at your company might find your DLL helpful). The easiest way to distribute and deploy that DLL, or any other combination of files, is with NuGet. Really.
In an earlier column driven by reader input, I built an ASP.NET MVC HtmlHelper that simplified creating the standard combination of label, textbox and validation message. That helper also supported a client-side "Dirty" flag that would be set when there was a change on the page. (The solution even handled those situations when a user makes a change in a page and then backs the change out.)
There are two parts to using NuGet, though: Creating a NuGet package from an existing project's components and then making that package available to Visual Studio. I'll start with creating a NuGet package, using my HtmlHelper as an example, and then show the easiest way to make your package available to the Visual Studio NuGet Package Manager.
Documenting a NuGet Package
While you can create a NuGet package from the command line, typing obscure commands isn't my first choice -- I prefer to use a GUI. If that's your preference also then your first step is to download the NuGet Package Explorer. Once the Package Explorer is downloaded, it will open automatically (you'll also find it in your Start menu under NuGet Package Manager when you want to use it again).
NuGetPackage Manager Explorer splits its screen in two: on the left is a pane displaying the package's documentation (labeled "Package metadata") and on the right is a pane listing the files in your package (labeled "Package contents"). To start creating a new package, pick New from the File menu. After that, you can use either pane, but I'd suggest you start on the left with the Package metadata by entering the documentation the developer will see when he picks your package from the NuGet Package Manager. My reasoning is that, if you start with the documentation, you'll have a chance to revise it after you've added your files to your package and have a better understanding of what your package consists of.
To create your package's documentation, click on the document icon in the top left corner of the Package metadata pane. While there's lots of information you can enter, the four critical items are:
- Id (which must be unique)
- Authors (a comma-delimited list of the package's authors)
The Title and Description are what developers will see in the NuGet Package Manager, so you want to provide all the information developers might need to find your package, recognize it and know what it does. Optionally, you can also enter into the Tags textbox any keywords that developers might use when looking for your package that don't appear in your Title or Description. To save your changes, click the green checkmark in the upper-left corner of the Package metadata pane.
Creating a NuGet Package
If you find that your folders aren't nested correctly in the Package contents pane, you can drag them to where you want them. If you want to check that you've got all of your packages in all the right folders, from the Package Explorer File menu, select Export and pick a folder in the resulting Choose a folder dialog. Package Explorer will unpack your package into that folder for you to review.
Adding a NuGet Source
Now it's time to save your package somewhere that you (and other developers) can find it. The easiest solution is to tell Visual Studio to use a folder on a network share when looking for NuGet packages. This is the one part of the process where'll you need the cooperation of the other developer because he'll need to tell his Visual Studio about your new folder. You'll also need to save your package to that folder using the Save option on the Package Manager File menu, of course.
To get your copy of Visual Studio to search for NuGet packages in your new folder, from the Tools menu, select Options. Then, from the left-hand list in the resulting dialog, expand the NuGet Package Manager choice to display the Package Sources entry. Select that entry and you'll see your current list of package sources on the right side of the Options dialog (you can also get to this dialog from the Manage NuGet Packages dialog box -- just click the Settings button in the lower-left corner that you've been ignoring).
To add your NuGet package folder to Visual Studio, click the plus sign button in the upper-right hand corner of the dialog. This adds a new source item to the list already filled in with some default values. To update those default entries, make sure your new source is selected and enter your values in the textboxes at the bottom of the dialog. You need to give your source a name (for example, PHVIS Package Source) and a source, which is the full path to your NuGet package folder (for example, C:\NuGet).
Now, in the Visual Studio Manage NuGet Packages dialog, you'll find your new Package Source listed under the Online tab. Picking your source will list all the packages in the folder. If you select the All option, you'll even find your package mixed into the resulting list with the packages from the regular NuGet sources. When you select your package and click Install, your package will be added to the current project.
Personally, I find that pretty cool -- makes me feel like I'm playing with the big boys. If you'd prefer to keep your NuGet packages in a Web site (like Microsoft does), you can do that, too. Fortunately, that's so simple I can cover it in a tip later this month.
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.