Book Image

Instant .NET 4.5 Extension Methods How-to

By : Shawn Ricardo Mclean
Book Image

Instant .NET 4.5 Extension Methods How-to

By: Shawn Ricardo Mclean

Overview of this book

.NET extension methods is an essential feature to know and understand for all .NET developers. Usage of extension methods is found in applications ranging from small to large scale enterprise systems built using the .NET framework. Create and use extension methods the correct way to save your development time and maintainability costs. Instant .NET 4.5 Extension Methods How-to is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises that will help you take advantage of the real power that is behind extension methods and gives you good knowledge of how to use them in your .NET applications. This book covers how to create, write, and use different types of extension methods. It will take you through a number of clear, practical recipes that will help you take advantage of the power of extension methods in the quickest possible way. You will also learn exactly how to create extension methods on strings, interfaces, classes such as IQueryable and IEnumerable, and so on. You will write them from scratch and then use them practically in your application. You will also learn the most suitable scenarios for using these extension methods.You will learn everything you need to know about creating your own extension methods and using them and other external extension methods.
Table of Contents (6 chapters)

Extension methods on interfaces (Should know)


Extension methods can also be used to extend interfaces. This may seem unorthodox when thinking with a strict OOP mindset, as extension methods include implementation while interfaces will contain the method prototypes. In this recipe, we have extracted an interface from the User class in the previous recipes and written extension methods for the interface itself.

Getting ready

Refer to the UserExtensions.cs and Models/IUser.cs files in the ExtensionMethods.Library project for the extension methods and interfaces. These methods are used in the Program.cs file in the ExtensionMethods.Console project.

How to do it...

The following code shows the two rewritten extension methods:

/// <summary>
/// Hashes and set a password with a generated salt.
/// </summary>
/// <param name="value"></param>
/// <param name="password"></param>
/// <returns></returns>
public static IUser SetHashedPassword(this IUser value, string password)
{
    string generatedSalt = CreateSalt(25);

    return SetHashedPassword(value, password, generatedSalt);
}

/// <summary>
/// Hashes and set a password with the salt argument.
/// </summary>
/// <param name="value"></param>
/// <param name="password"></param>
/// <param name="salt"></param>
/// <returns></returns>
public static IUser SetHashedPassword(this IUser value, string password, string salt)
{
    value.Password = Hash(string.Concat(password, salt));
    value.PasswordSalt = salt;
    return value;
}

The interface extracted from the class:

public class Student : IUser
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string PasswordSalt { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class Teacher : IUser
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string PasswordSalt { get; set; }
    public string TaughtSubject { get; set; }
}

The following code shows the use of the extension methods:

Teacher user = new Teacher();
user.SetHashedPassword("samplepassword");

Student user1 = new Student();
user1.SetHashedPassword("samplepassword", "myownsalt"); 

How it works...

Our first step was to extract the IUser interface from our previous User class. We then created two classes, Student and Teacher, which inherits from the IUser interface. Extension methods (SetHashedPassword) were created based on IUser. Calling the extension can be done on any object that implements the IUser interface.

Note

The compiler will use the closest method to the object type, a concept similar to what we did in the Overriding extension methods recipe. If we create an extension method for the Student class, then the compiler will select that extension method to be called instead of the method for the interface.

Extension methods on interfaces are straightforward if you understand how to create them for classes.