NuGet using

Today I would like to talk about the use of NuGet. Surely you’ve heard something about this system of management of external dependencies. Recently, the system grows and more and more developers are putting their systems in the wrapper for distribution with the help of NuGet.

NuGet – a manager to control the dependencies on external libraries. With this tool you can install, update and remove according to your project with great ease. This applies both for the desktop software and the web. In particular, when expanding the CMS Orchard, she drags the half assembly during installation of the library on their own packages. There are examples of NuGet for Silverlight.

Notes from the back seats

When studying materials NuGet, a plus appears availability of shared libraries to your projects. Conditional opponents NuGet, or principled opposition asks why you need NuGet for their shared libraries, if there can be external to SVN, and other such things for VCS? So you can refer to the stable branch sources.

Developers NuGet answer that

  • do not control whether access to the version control system,
  • source code is not displayed to other groups
  • no need to monitor and switch to other audits by hand (maybe you want to jump from branch Release, to an earlier, or you are referring to an audit in trunk)

Quite good advantages. Especially now that your favorite library does not get lost among repositories and projects.

Installation

Get package manager NuGet You can either set yourself ASP.NET MVC 3.0, or download it from the site nuget.codeplex.com. Then in Visual Studio will have a new menu item for viewing and downloading packages NuGet.

Tools > Library Package Manager > Add Library Package Reference…

Usage

The package manager is out of NuGet looks if you used the add-ons manager for the studio.

All the same. A list of available packages, brief description of the allocation package. On accordion tab Online list of available places where packages are NuGet. Now there is only one official resource, but you can easily add a local directory, online directory, your own online store packages NuGet.

Installing and updating packages, up to a disgrace simple – you simply click on the Install \ Update when you select a package from the list.

Little is need to be said about the search package. I’m in the studio search periodically disappears and I use the search at the official site nuget.org. At the moment it contains 958 packages. About finding and installing with the help of the console will talk about below.

All actions can be performed from the console package manager, which appears when you install NuGet

Actions carried out in the console for a specific project that is selected as DefaultProject. The screenshot is selected WindowsFormsApplication1, and all the packages that I will show in the console will be installed for this project if the project is not specifically listed.

Searching

Displays a list of available pacts in the console is not very convenient, so you better to find him before on the site. For example add to the project StructureMap. Go to the official website and look for the name. As a result of searching you are likely to bring up a page of the form:

Installing packages

Here it is specified by a command to execute to install. Copy and paste the text into the console.

Execute the command and get all the necessary libraries added to the project. This will download and install all the dependencies needed to run the component. So most likely you will not be in a frenzy of hand-parse all dependencies. Unless of course the author of the package have all true))

In the console we can see what packages have been established and how successfully. Once it is installed, you will see that among the files of the project, a new element packages.config. This file specifies which packages are installed and which version. General view of the file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<packages>
   <package id="structuremap" version="2.6.2" />
</packages>

The newly installed package appears only in that project, which exposed “by default”. If you want to add the same package in other projects that will have to choose projects from the drop-down list and then install it.

In this package will not be re-downloaded from a remote resource, NuGet smart enough to know what has already been downloaded in the current software solution, so it just adds a link to the library. In the console you will see the following:

PM> Install-Package structuremap
'structuremap 2.6.2' already installed.
Successfully added 'structuremap 2.6.2' to TestProject1.

 

Unfortunately, that can not be specified when adding the package several projects for the integration. The general syntax of the installation is as follows:

Install-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version <string>] [-Source <string>] [<CommonParameters>]

Packages are downloaded to the root directory of software solutions to the folder packages, so be sure to add it to your version of control system.

Installed packages can be viewed as out of the package manager

As well as from the console. Operations on the update, and delete as you can make in two ways. For all the package manager is crystal clear with all the console a bit more complicated but more flexible.

Console

The basic commands for working with the console:

Package – This command lists all packages installed for the selected “default” project.

PM> package

Id                             Version              Description
—                             ——-              ———–
structuremap                   2.6.2                StructureMap is a Dependency Injection / Inversion of Control tool for .Net that can be used to improve the architectural qualities of an object ori…
UpdatePackage – с помощью этой команды осуществляется обновление установленых пакетов. Для обновления зависимостей в проекте «по умолчанию» необходимо написать
UpdatePackage – with this command by updating the installed packages. To update the dependencies in the project “default” to write

PM> Update-Package -Id structuremap

 

If you have the latest version, then NuGet will say:

PM> Update-Package -Id structuremap
No updates available for 'structuremap'.

The full syntax looks like this:
Update-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version <string>] [-Source <string>] [<CommonParameters>]

 

Unfortunately you can not check for updates all the packages at once and update them. It is necessary to carry out a refresh operation for each packet NuGet. The international community and backstage hope that this opportunity will appear in an upcoming release. In the meantime, knowledgeable people have written here is a script for PowerShell

ForEach ($project in get-project -all)
{
get-package | update-package -Project $project.Name
}

 

The result of running:

PM> ForEach ($project in get-project -all)
>>  {
>>  get-package | update-package -Project $project.Name
>>  }
No updates available for 'structuremap'.

No updates available for 'structuremap'.

PM>

The feedback to the script, people write that the confusion about “too many requests to the resource”, so I have warned you.

Uninstall-package – remove a package ,it is not difficult to guess =)

The result:

PM> Uninstall-Package -Id structuremap
Successfully removed 'structuremap 2.6.2' from TestProject1.

General syntax:
Uninstall-Package [-Id] <string> [-RemoveDependencies] [-Force] [-Version <string>] [<CommonParameters>]

 

Get-help – the most important team at work!

Example

PM> Package get-help
PM> Package -?

Interesting packets

Scratch in the Internet and looking at examples, we can say that there are the following interesting packages:

nUnit – framework for testing
log4net – a rich library for logging
structureMap – IoC container
SqlServerCompact – 4th version of SQL CE
mvcMailer – a handy library for mail
jQuery – just all the family in mind when working in VS2010
moq – a lightweight library for object mocking

Most likely your favorite library from the world of OpenSource is also in the library NuGet!

The next time I’ll tell you how you can create NuGet bags and place them in libraries.

Tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>