Once upon a time when a .Net developer wanted to install a third-party/open source component the amount of work he had to do was enormous. Things like downloading the component with all its dependencies, installing the latest component version, saving the component into source control, deploying the component with the application and more made this mission very hard and cumbersome. In other platforms such as Java and Ruby there were solutions for this problem which wrapped components into packages and deploy those packages. NuGet is an open source package manager that is integrated into Visual Studio 2010 and helps to do exactly that. If you are a .Net developer and you didn’t hear about NuGet then I hope that this post will help you to get started with it.
What We Strive?
These days, building large applications include a lot of parts/components or third party packages. For example, when you start writing a web application you’ll probably want to integrate jQuery, Modernizr and other components that will help you to build a better application. When you want to add an assembly you also want to add all the dependencies that it include. Without the dependencies (such as configuration files, images and etc.) the component won’t work at all. But there you start to hit a wall. How to download the latest component version? How to get all the dependencies? How to manage the whole thing? Not doing a good job in these issues can lead to a messy application that holds out of date dependencies that break it apart. This is why NuGet is so important.
NuGet Package Manager
NuGet is a package manager that is integrated into Visual Studio 2010. It is an open source project that can be downloaded from CodePlex but it can be added to Visual Studio also through the extension manager as you can see in the next figure:
When you install NuGet you will have the ability to use it in two main ways:
- The Package Manager Console – use a PowerShell console with commands that help to do the package management.
- The Add Library Package Reference Dialog – use a VS dialog in order to manage the packages.
The Package Manager Console
If you can’t see the console after the NuGet installation you can go the Tools menu in VS –> Library Package Manager –> Package Manager Console in order to open it. There are a few PowerShell commands in NuGet that you will have to know such as:
- Get-Package – Gets the set of packages available from the package source. The default source is the current project. If you like to get a package that doesn’t exists in your project then use the following syntax:
Get-Package [package-name] -ListAvailable
- Install-Package – install a package and all its dependencies.
- Update-Package – updates a package and its dependencies to the latest package version.
- Uninstall-Package – uninstall a package that you installed in your solution.
There are other commands as well such as Add-BindingRedirect but the main ones were written previously. Here is an example that shows how to get the available packages in the solution and how to install the Modernizr package:
You can explore the commands and their syntax using the PowerShell Get-Help command. After you install the packages NuGet will add a packages.config file into your project that will include the details of the package name and version that are available in the project. Here is an example of such a file:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Modernizr" version="1.7" /> <package id="jQuery" version="1.6" /> </packages>
Also it will do all the relevant integration for you. For example adding a Scripts directory in your application and adding the full and minimized version when you install Modernizr. Another thing that can help a lot is the ability to build PowerShell script files that manage automatic installation of packages. Then, you can add the solution to source control with those files and when other developers get the latest version they can run the script and get all the packages and their dependencies.
The Add Library Package Reference Dialog
This is a graphic dialog for doing the same thing like in the Package
Manager Console. When you install NuGet it adds a new menu item into
the menu items of the project. You can point the project name, press
right mouse button and select the “Add Library Package Reference” to
invoke NuGet's package management dialog:
NuGet is a great extension to Visual Studio’s stack of extensions. It helps to manage the open source component packages and their dependencies and by that decrease the amount of cumbersome work that the developer will need to do. One of its better qualities is that it is so simple and easy to use. If you ask me, this is a must know tool for .Net developers.