There's one more feature of NuGet that we should look at, and it has to do with working in a team environment. When you're working with other developers on the same team, you're sharing source code – usually by checking into a centralized repository. This technique is typically called “Source Control.” It’s not only for centralized project sharing but also for keeping a version history of application allowing you to roll back changes to earlier versions within your application’s history.
There is a dilemma with Source Control as it relates to Package Management and that is whether or not downloaded packages and their dependencies, should be included in the version history. Packages shouldn’t change over time and aren’t meant to be changed by any member within the team. However, other team members will need these external dependencies in order to work with the project. Typically, the best practice is to exclude external Packages from Source Control, requiring each developer to get a local copy of those Packages from NuGet. There are a few ways you can achieve this “no commit workflow” with regards to Packages and Source Control.
We'll see a demonstration of this with a blank Windows Form App. Open up a New Windows Form App, calling it “NuGetNoCommitWorkflow”:
Now let’s add an arbitrary Package for this demonstration. Using the steps outlined in the previous lesson, install the “Elmah” Package:
Then from the Visual Studio menu select:
Tools > Options > NuGet Package Manager > General
And make sure the following options are checked. The key option is the latter which checks to see if the project has all of the Packages it needs and If not it downloads and installs them before performing a build:
Now, if you give your project – not including the installed Packages folder – to a common Source Control or to another developer, the Packages will automatically download to the user’s local project folder when a build is attempted. You can simulate this by deleting the “packages” folder in your project:
And then when you re-open the project in Visual Studio you will see the following message in the Package Manager Console:
You can click the “Restore” button here, or simply Build (ctrl+shift+b) the Solution and see the Output Window telling you that the NuGet packages were automatically downloaded to the local project:
The way this works is that Visual Studio is able to recognize the missing packages based on the references in the packages.config file:
You can get more information on a “no commit workflow” by visiting: http://bit.do/no-commit
Lesson 67 - NuGet No-Commit Workflow