Tuesday, November 22, 2011

Jump Start your next project with Nuget

In between the company end-year functions, demanding clients and of course my fabulous life :) , I've deceided to do some blogging again. In fact, it's been a while since any Ninja laid a finger on a keyboard here, (or an iThing)...

A while ago I came across a cool little thing called "NuGet" (pronounced new-get). No, it's not the kind of things you get from Mc Donalds. According to the website, NuGet is a Visual Studio extension that makes it easy to install and update [open source] libraries and tools in Visual Studio. But, I believe they're being modest. It's way more cooler than what meets the eye.

Okay, so whats all the fuss about? Well, the main idea behind it is to allow you to install any files (libraries, media, code files etc) into your project / solution in a managed way with versioning & dependency detection. In the NuGet world, these files are bundles into things called "packages".

When you use NuGet to install a package, it copies the [library] files to your solution and automatically updates your project (add references, change config files, etc). If you remove a package, NuGet reverses whatever changes it made so that no clutter is left

There is a wide variety of libraries ready to be downloaded & used (3680 to be exact) from the offical NuGet Package Source. You can even create your own package source that can be hosted to be available externally or internally on your intranet - I will get to this later.

So, let's install NuGet...

Obviosly, first you need to open every Ninja's biggest asset: Visual Studio 2010.

1. Open up the 'Extension Manager' from the Tools menu

2. Under the Online Gallery section, search for "NuGet" and click "Download"

After downloading, "reading" the legal stuff & installing, you need to restart VS for the changes to take effect.

That's it, now we have NuGet installed & we can get "Nugetting" :)

For this example, I'm going to create a blank web forms project (yes I said web forms) and install "EntityFramework.Patterns" onto it. This is only to show the functionality of NuGet.

1. After you've created a blank project, right-click on 'References' there is an additional item "Manage NuGet Packages" - Click it. (or if you're using a touch screen, I rest my case)

2. Next you will be presented by the main NuGet Manager

3. From the 'Online' section (on the left), search for "EntityFramework.Patterns" and hit "Install"

Note that EntityFramework 4.1 is required for this library to work (under dependencies). But have no fear NuGet handles it for us:

That's it... So if we go back to our little sample project you will notice that a few references were added including a packages.config file. (Also note that some content has been added to the web.config file)

There is of coarse an alternative "Ninja" solution to do all this, by using the Library Package Manager Console (Tools > Library Package Manager > Package Manager Console). Just type:

Install-Package EntityFramework.Patterns

You can now use the installed library in your code. Im not going into how we use EntityFramework.Patterns, but hopefully you get the idea.

But, where is the actual packages? If you go to your solution folder, you will note a "packages" folder. When you open it up, you'll notice all the packages that is installed in the solution. So best checking this folder into TFS!

Now, for the cool stuff... Creating your own package...

Creating your package is exactly like adding files to a ZIP file. But in this instance, the "zip" file is called a NuGet Package file (*.nupkg). There is also a specifications file called a NuSpec file (*.nuspec) that contains all the meta data of the package like version information & dependencies etc.

There are many ways of creating and publishing your packages:

The Ninja Way
The Easy Way

You can also host your own package feed

You can view a variety of videos available on this subject

So, Ninjas... Now you have no excuse to deliver the "best in the business" code. Get coding away... Get packaging away and make your life easier with NuGet.

Follow me on Twitter @FanieReynders

Until next time..... Cheers.

No comments:

Post a Comment