-
Book Overview & Buying
-
Table Of Contents
Instant .NET 4.5 Extension Methods How-to
By :
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.
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.
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"); 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.
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.
Change the font size
Change margin width
Change background colour