Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / XML

A Tough Nugget – NuGet Package Manager

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 May 2011CPOL4 min read 19.5K   11  
The NuGet Package Manager 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.

Introduction

A Tough Nugget – NuGet Package Manager

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 deployed 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 haven’t heard about NuGet, then I hope that this post will help you to get started with it.

What We Strive?

These days, building large applications includes 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 includes. 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:

NuGet in Extension Manager

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 sialog – use a VS dialog in order to manage the packages.

The Package Manager Console

From the two option management options, this is my preferred one. The Package Manager Console is a PowerShell console that helps to manage the packages that you install into your application.

The Package Manager Console

If you can’t see the console after the NuGet installation, you can go to 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 exist in your project then use the following syntax:
  • tetx
    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:

Using PowerShell Commands

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
<?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 item of the project. You can point the project name, press the right mouse button, and select the “Add Library Package Reference” to invoke NuGet's package management dialog:

Add Library Package Reference

In the dialog, you can see the installed packages, get packages from the online packages gallery, update the packages to their latest version, and more.

Add Library Package Reference Dialog

Summary

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.

This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
-- There are no messages in this forum --