Managing Nuget Packages – Installing Correct / Old Versions

Nuget packages are great. Concept is to “plug-in” whole libraries or frameworks with correct configurations and files at right place. So that it is all central and ready to use. As you install package, it add required assemblies to project, and add all the related configuration to config file automatically using one command. Sometime it can get messy in terms of package version management. if your code is not compatible with new version of a package, or you want to un-install or simply revert to an old version of package. I will go through how to install, un-install a package, and how to install specified version. Easiest way to install package is through Nuget Package Manager GUI, but if you want to have a bit more control, best way to manage packages in projects or solution is through Package Manager Console.

To access Nuget Package Manager Console:

How to access Nuget Package Manager Console

 



Package Manager Console looks like following. It has two major options along with Console:

Nuget Package Manager Console

Package Source: By Default it is set to nuget.org, You can add / remove package sources in your list.
Default Project: Select the project on which you want to manage Nuget packages. It displays list of projects from opened Solution.

To get or search list of available packages in Package Source Library (e.g. Nuget.org)

Package Sources like Nuget.org contains library of packages. To see the List of all Available Packages on package source

get-package –ListAvailable
will retrieve the list of all available packages.

Normally there are so many packages, that going through the whole list is of no benefit. So you can use filter the list by –filter parameter. Such as to search all packages which have word ‘Mapper’ in name. Use:

get-package -ListAvailable -filter Mapper
will retrieve the list of only packages with name having ‘Mapper’  in it.

get-package command with filter



If you remove –ListAvailable parameter, get-package will show the list of packages already installed and available locally.

Install an available package

Once you know the name of package. Command for installing package is:

install-package <packageName>
for example, if we want to install latest version of EntityFramework, we would use
install-package EntityFramework
will install latest version of EntityFramework compatible with your Visual Studio and .NET framework

Install-package command

 

Uninstalling an installed package

You have realized that you have installed wrong version of package, best way is to first remove installed package. For any other reason, if you want to remove a package, use following command:

uninstall-package <packageName>
We want to uninstall above installed EntityFramework 6.0.1. it would be
uninstall-package EntityFramework

Uninstall-package command


This would uninstall package. Some time it require a Visual Studio restart, if package manage is unable to delete any existing file in use.

Installing a Specified Version of package

You can mention version number along with package name to install any old package. For example, in above example, we have uninstalled EntityFramework version 6.0.1, and we want to install version 5.0.0. Command would be:
install-package EntityFramework -version 5.0.0

install-package with version command


This should install Version 5.0.0 of EntityFramework. In above case, since Version 5 was already installed on another project in solution, so it just enabled version 5 for TestConsoleApp.

Summary:

Nuget packages are great for productivity and to re-use already developed libraries. For package management, best tool is to use Package Manager Console which gives you more control over details such as versions. 

For exploring more about Nuget Packages, please refer to official documentation.

Action, Func and Predicate - Predefined Generic Delegates

Action, Func and Predicate are pre-defined Generic delegates. So as delegate they can point to functions with specified signature.

Action<>

Does not return any value, and can take from none up to 16 generic parameters. So there are total of 17 overloads for Action delegate, from no parameter to up to 16 generic parameters. As it does not return any value, so conventionally named as Action.

- Action
- Action<T>
- Action<T1, T2>
- Action<T1, T2, T3>
- Action< ……

static void Shout(string arg1, string arg2)
{
    Console.WriteLine(string.Format("{0} {1}!", arg1, arg2));
}
static void Main(string[] args)
{
    //Create an Action 
    Action<string, string> action =  new Action<string,string>( Shout );
    
    //OR You can initialize it as
    Action<string, string> moreAction = Shout;

    //Invoke method.
    moreAction("Hello", "World");
    //will output 'Hello World!'
}

Actual delegate definition of Action<T1, T2> would be:

public delegate void Action<T1, T2>(T1 param1, T2 param2);

Func<>

Return a generic parameter, and has 17 overloads to support from none up to 16 generic parameters. Last parameter is the Return type, and is required. As it does return a parameter, so conventionally named as Func derived from Function.

- Func<TResult>
- Func<T1, TResult>
- Func<T1, T2, TResult>
- Func<T1, T2, T3, TResult>
- Func< ……

Actual delegate definition of Func<T1, T2, out TResult> with two parameters would be:

public delegate TResult Func<T1, T2, out TResult>(T1 param1, T2 param2);

Predicate<>

As name indicate, this delegate defines a method with return type bool, and exactly one generic parameter. Method is pretty much used as a function to affirm or deny certain input for specific operation. i.e Either provided input is true or false based on specific method implementation. Mostly used as a filter method for given data.
For example, FindAll method for List<T> takes Predicate<T> method as parameter. FindAll method use provided method against Predicate<T> to apply and filter against each value of the collection based on Predicate response.

static bool IsEvenNumber(int num)
{
    return num % 2 == 0;
}
static void Main(string[] args)
{
    //Predicate
    Predicate<int> isEvenPredicate = IsEvenNumber;
    List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    List<int> evenNumbers = numbers.FindAll(isEvenPredicate);
    // output will be 2,4,6,8
}

Actual delegate definition for Predicate would be:

public delegate bool Predicate<T>(T input);

What are the advantages of pre-defined delegates?

  1. To save time from defining a delegates of similar definition yourself.  Use the predefined one which is also more commonly used, and well recognized.
  2. With the invent of Extension methods and LINQ which introduced loads of functions with the requirement of delegate parameter. A presence for general globally defined pre-defined delegates was required.

Hope that clarify the concept.

Welcome

Finally a long started, low priority background thread in my head succeeded, and I started a blog.

Hello everyone! and Welcome to my Blog.

"Now, fair Blogging, our publishing hour Draws on apace; many happy blogging days bring in another moon; but oh methinks, how slow this old moon wanes! it lingers my desires, like to a step-dame or a dowager, long withering out a young developer's revenue."

- Shakespeare, about blogging ;)

Hope you will get benefited in some way, by some of my blogs. Subscribe to stay in touch.

About Bajwa

Bajwa Thumbnail Ali Haider is a software developer and technology enthusiast based in London. He has over nine years of hands on experience in software design and development using Microsoft technologies on various fronts. He attempts to keep learning and applying cutting edge technologies and best practices, and his passion is to work on complex challenging projects and to develop data crunching frameworks.

Projects

    Tag Cloud