What’s New in C# 6.0 using examples.

The null-conditional operator avoids having to explicitly check for null before calling a method or accessing a type member.

System.Text.StringBuilder sb = null;
string result = sb?.ToString(); // result is null

Expression-bodied functions allow methods, properties, operators, and indexers that comprise a single expression to be written more tersely, in the style of a lambda expression:

public int TimesTwo (int x) => x * 2;
public string SomeProperty => "Property value";

Property initializers let you assign an initial value to an automatic property:

public DateTime TimeCreated { get; set; } = DateTime.Now;

Read-only properties can also be set in the constructor, making it easier to create immutable (read-only) types.
Index initializers allow single-step initialization of any type that exposes an indexer:

var dict = new Dictionary()
{
[3] = "three",
[10] = "ten"
};

String interpolation offers a succinct alternative to string.Format:

string s = $"It is {DateTime.Now.DayOfWeek} today";

Exception filters let you apply a condition to a catch block:

string html;
try
{
html = new WebClient().DownloadString ("//asef");
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.Timeout)
{
...
}

The using static directive lets you import all the static members of a type, so that you can use those members unqualified:

using static System.Console;
...
WriteLine ("Hello, world"); // WriteLine instead of Console.WriteLine

The nameof operator returns the name of a variable, type, or other symbol as a string. This avoids breaking code when you rename a symbol in Visual Studio:

int capacity = 123;
string x = nameof (capacity); // x is "capacity"
string y = nameof (Uri.Host); // y is "Host"

You’re now allowed to await inside catch and finally blocks.

Reference: C# 7.0 in a nutshell by Oreilly publications

Continue Reading

What’s New in C# 7.0 using examples.

Numeric literal improvements
Numeric literals in C# 7 can include underscores to improve readability. These are ignored by the compiler:

int million = 1_000_000;

Out variables and discards
You can now declare out variables on the fly:

bool successful = int.TryParse ("123", out int result);

And when calling a method with multiple out parameters, you can discard ones you’re uninterested in with the underscore character:

SomeBigMethod (out _, out _, out _, out int x, out _, out _, out _);

Patterns
You can also introduce variables on the fly with the is operator(pattern variables):

if (x is string s)

The switch statement also supports patterns, so you can switch on type as well as constants. You can also switch on the null value:

switch (x)
{
case bool b when b == true: // Matches only when b is true
..........
break;
case null:
...............
break;
}

Local methods
A local method is a method declared inside another function. Local methods are visible only to the containing function, and can capture local variables in the same way that lambda expressions do.

void WriteCubes()
{
Console.WriteLine (Cube (3));
int Cube (int value) => value * value * value;
}

More expression-bodied members
C# 6 introduced the expression-bodied “fat-arrow” syntax for methods, read-only properties, operators, and indexers. C# 7 extends this to constructors, read/write properties, and finalizers:

public class Person
{
string name;
public Person (string name) => Name = name;
public string Name
{
get => name;
set => name = value ?? "";
}
~Person () => Console.WriteLine ("finalize");
}

Deconstructors
C# 7 introduces the deconstructor pattern. Whereas a constructor typically takes a set of values (as parameters) and assigns them to fields, a deconstructor does the reverse and assigns fields back to a set of variables:

public void Deconstruct (out string firstName, out string lastName)
{
int spacePos = name.IndexOf (' ');
firstName = name.Substring (0, spacePos);
lastName = name.Substring (spacePos + 1);
}

Deconstructors are called with the following special syntax:

var joe = new Person ("Joe Bloggs");
var (first, last) = joe; // Deconstruction
Console.WriteLine (first); // Joe
Console.WriteLine (last); // Bloggs

Tuples
Tuples provide a simple way to store a set of related values:

var tuple = (Name:"Bob", Age:23);
Console.WriteLine (tuple.Name); // Bob

With tuples, functions can return multiple values without resorting to out parameters:

static (int row, int column) GetFilePosition() => (3, 10);
static void Main()
{
var pos = GetFilePosition();
Console.WriteLine (pos.row); // 3
Console.WriteLine (pos.column); // 10
}

Tuples implicitly support the deconstruction pattern, so they can easily be deconstructed into individual variables.

static void Main()
{
(int row, int column) = GetFilePosition(); // Creates 2 local variables
Console.WriteLine (row); // 3
}

throw expressions
With C# 7, throw can also appear as an expression in expression-bodied functions:

public string Foo() => throw new NotImplementedException();

A throw expression can also appear in a ternary conditional expression:

string Capitalize (string value) => value == null ? throw new ArgumentException ("value") : value == "" ? "" : char.ToUpper (value[0]) + value.Substring (1);

Reference: C# 7.0 in a nutshell by Oreilly publications

Continue Reading

Send emails using SendGrid Email provider and ASP.Net Core

Sending emails using SendGrid email provider and ASP.Net Core is very straightforward.
Firstly install SendGrid package through Package Manager Console using the following command

install-package SendGrid

Now create a account in Sendgrid.com and add an API key to use the SendGrid provider service.  Add this key to your AppSettings.Json or AppSettings.Development.Json under a new SendGridEmailSettings=>SendGridKey node.

image

AppSettings.Json:

{
"SendGridEmailSettings": {
"SendGridKey": <SendGrid Private API Key>
}
}

StartUp.cs:

public void ConfigureServices(IServiceCollection services)
{
……………………..
services.AddTransient<IEmailSender, EmailSender>();
services.Configure<AuthMessageSenderOptions>(options =>
Configuration.GetSection("SendGridEmailSettings").Bind(options));
}

EmailSender .cs

using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Options;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;

public class AuthMessageSenderOptions
{
public string SendGridUser { get; set; }
public string SendGridKey { get; set; }
}
public class EmailSender : IEmailSender
{
public EmailSender(IOptions optionsAccessor)
{
Options = optionsAccessor.Value;
}
public AuthMessageSenderOptions Options { get; } //set only via Secret Manager
public Task SendEmailAsync(string email, string subject, string message)
{
return Execute(Options.SendGridKey, subject, message, email);
}
public Task Execute(string apiKey, string subject, string message, string email)
{
var sendGridClient = new SendGridClient(apiKey);
var sendGridMessage = new SendGridMessage()
{
From = new EmailAddress("ajitgoel@gmail.com", "Simpler Products-Social Media Scrubber"),
Subject = subject,
PlainTextContent = message,
HtmlContent = message
};
sendGridMessage.AddTo(new EmailAddress(email));
// Disable click tracking.See //sendgrid.com/docs/User_Guide/Settings/tracking.html
sendGridMessage.SetClickTracking(false, false);
return sendGridClient.SendEmailAsync(sendGridMessage);
}
}

If you send an email and you do not receive it in your mailbox check the spam folder. My emails landed in the spam folder when I was testing the application.

Continue Reading

NotSupportedException: No IUserTwoFactorTokenProvider named ‘Default’ is registered.

If you have changed the AddDefaultIdentity in the Startup.cs file to AddIdentity and you receive a

An unhandled exception occurred while processing the request.
NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named ‘Default’ is registered.
Microsoft.AspNetCore.Identity.UserManager<TUser>.GenerateUserTokenAsync(TUser user, string tokenProvider, string purpose)

error when registering a user, there is a easy fix. You need to a add a AddDefaultTokenProviders() when adding identity() to services.

public void ConfigureServices(IServiceCollection services)
  {
  ………………
  services.AddIdentity<User, UserRole>()
   .AddEntityFrameworkStores<ApplicationDbContext>()
   .AddDefaultTokenProviders();
…………………….

Continue Reading

FizzBuzz using C# yield

Yield.

[TestClass]
public class FizzBuzzTest
{
    [TestMethod]
    public void Test1()
    {
        var iEnumerableString = new FizzBuzz().GenerateFizzBuzzUpto(100);
        var list = iEnumerableString.ToList();
        Assert.IsTrue(list[0] == "Fizz:3");
        Assert.IsTrue(list[1] == "Buzz:5");
        Assert.IsTrue(list[6] == "FizzBuzz:15");
    }
}
public class FizzBuzz
{
    public IEnumerable<string> GenerateFizzBuzzUpto(int numberUpto)
    {
        for (var counter = 1; counter < numberUpto; counter++)
        {
            if (counter%3 == 0 && counter%5 == 0)
            {
//The use of the yield keyword controls the iterator’s behavior. If the consumer requests an iterator 
//to produce a value, it runs until a yield statement is encountered. At that point, the iterator is 
//suspended until the consumer asks for the next value.
                yield return $"FizzBuzz:{counter}";
            }
            if (counter % 3 == 0)
            {
                yield return $"Fizz:{counter}";
            }
            if (counter % 5 == 0)
            {
                yield return $"Buzz:{counter}";
            }
        }
    }
}
Continue Reading

“Develop something useful in under 100 lines of code” contest-Improved version

My article here was well received. Various people pointing out several flaws in the application. Some of these points that were discussed were:

a. Should the application use decimal instead of double since it is a financial application?
Answer: The application has been changed to use decimals to calculate the monthly payment amount, Interest amount, total interest amount. However based on the article here, “the results when computed in doubles are going to be off by a few billionths of a penny one way or the other”. I therefore believe that I need to find a actual use case where using a decimal produced a result that was more correct than using a decimal.

b. The application will not behave correctly when the “loan Date” is passed with a “DateTime.MaxValue” value.
Answer: This has been fixed and a unit test case added for the same.

c. The application needs to consider business rules related to “loan date” and “Loan Period in years”
Answer: This has been fixed and two unit test cases added for the same.

d. What advantages will the system get if the return type of the Get function is a “IList” instead of a “List”?
Answer: This has been fixed in the application. The advantages that we get is that the AmortizationSchedule.Get and IAmortizationSchedule.Get signature does not need to change when the internals of the AmortizationSchedule.Get method is changed to return a collection that implements a IList (example return a Array instead of a List collection).

e. How will you optimize the creation of a the List class on line 30? Will it make sense to pass the created object from the client application if the creation of the list instance is expensive?
Answer: If the List creation is an expensive operation then we can consider using the Object pooling design pattern in the application.
The Object pool design pattern is a creational design pattern that can improve performance when working with classes that are slow to instantiate. Rather than constructing new objects, reusable objects are retrieved from, and released to, a pool as required.
An example implementation is listed here.

f. Will the application need to take care of multiple processes creating “Amortization Schedule”? If yes then how will you change the application?
Answer: It was decided that the application does not need to change to take care of multiple processes creating the “Amortization Schedule”.

g. Why was the “IAmortizationSchedule” interface created? It is not used anywhere in the application
Answer: This is true, the “IAmortizationSchedule” interface is not used in the application but I always create an interface because it helps later down the road when I need to write unit tests by mock the various parts of the application.

h. Is there a possibility that the “toThePowerOfVariable” variable on line 22 will become 1? What happens on line 23 if the “toThePowerOfVariable” variable becomes 1?
Answer: It was decided that there is no possibility of the “toThePowerOfVariable” variable on line 22 becoming 1. If the “toThePowerOfVariable” variable becomes 1, no “DivideByZeroException or other exception will occur.

Here is the changed application:

public class AmortizationSchedule: IAmortizationSchedule
{
    public IList<AmortizationScheduleRow> Get(double annualPercentageRate, 
        double loanAmount, int loanPeriodInYears, DateTime loanStartDate)
    {
        if (annualPercentageRate <= 0 || 
         loanAmount <= 0 || 
         loanPeriodInYears <= 0 || 
         loanStartDate <= DateTime.MinValue || 
         loanStartDate == DateTime.MaxValue)
        {
            throw new ArgumentException("Invalid arguments");
        }
        //If the code below does not throw an exception then 
        //the "loanStartDate" has been validated against the "loanPeriodInYears"
        DateTime addYears = loanStartDate.AddYears(loanPeriodInYears);
        
        #region Calculate monthly payment rounded to 2 digits
        const int NoOfMonthsInYear = 12;
        var loanPeriodInMonths = loanPeriodInYears * NoOfMonthsInYear;
        var effectiveInterestRateInDouble = annualPercentageRate / (NoOfMonthsInYear * 100);
        var toThePowerOfVariable = Math.Pow(1 + effectiveInterestRateInDouble, -loanPeriodInMonths);
        var monthlyPayment = (decimal) (loanAmount * (effectiveInterestRateInDouble / (1 - toThePowerOfVariable)));
        #endregion

        #region Calculate payment schedule based on monthly payment and loan start date
        //Initialize the collection(for performance) based on the 
        //no of elements that it is supposed to hold
        var amortizationSchedule = 
        new List<AmortizationScheduleRow>(loanPeriodInMonths);
        var currentMonthCounter = loanStartDate.AddMonths(1);
        var currentBalance = (decimal) loanAmount;
        decimal totalInterestAmount = 0;
        for (var counter = 0; counter < loanPeriodInMonths; counter++)
        {
            var interestAmount = (decimal) (effectiveInterestRateInDouble * (double) currentBalance);
            totalInterestAmount = (totalInterestAmount + interestAmount);
            var amortizationScheduleRow = new AmortizationScheduleRow
            {
                Date = currentMonthCounter,
                MonthlyPaymentAmount = monthlyPayment,
                InterestAmount = interestAmount,
                TotalInterestAmount = totalInterestAmount
            };
            amortizationSchedule.Add(amortizationScheduleRow);

            currentBalance = currentBalance - amortizationScheduleRow.GetPrincipalAmount();
            currentMonthCounter = currentMonthCounter.AddMonths(1);
        }
        #endregion
        return amortizationSchedule;
    }
}
public interface IAmortizationSchedule
{
    IList<AmortizationScheduleRow> Get(double annualPercentageRate, double loanAmount, 
        int loanPeriodInYears, DateTime loanStartDate);
}
public class AmortizationScheduleRow
{
    public decimal GetMonthlyPaymentAmount()
    {
        return Math.Round(MonthlyPaymentAmount, 2, MidpointRounding.AwayFromZero);
    }
    public decimal MonthlyPaymentAmount { private get; set; }
    public decimal GetInterestAmount()
    {
        return Math.Round(InterestAmount, 2, MidpointRounding.AwayFromZero);
    }
    public decimal InterestAmount { private get; set; }
    public decimal GetTotalInterestAmount()
    {
        return Math.Round(TotalInterestAmount, 2, MidpointRounding.AwayFromZero);
    }
    public decimal TotalInterestAmount { private get; set; }
    public decimal GetPrincipalAmount()
    {
        var principalAmount = MonthlyPaymentAmount - InterestAmount;
        return Math.Round(principalAmount, 2, MidpointRounding.AwayFromZero);
    }
    public string GetDate()
    {
        return Date.ToString("MMM yyyy");
    }
    public DateTime Date { private get; set; }
}
 [TestClass]
public class AmortizationScheduleTester
{
    const double annualPercentageRate = 3.5;
    const double loanAmount = 300000;
    const int loanPeriodInYears = 15;
    readonly DateTime loanStartDate = new DateTime(2016, 7, 1);
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest()
    {
        const int NoOfMonthsInYear = 12;
        var monthlyPaymentSchedule = new AmortizationSchedule().Get(annualPercentageRate, loanAmount,
            loanPeriodInYears, loanStartDate);
        Assert.IsFalse(monthlyPaymentSchedule == null);
        Assert.IsFalse(monthlyPaymentSchedule.Count > 1);
        var secondMonthPaymentSchedule = monthlyPaymentSchedule[1];
        Assert.AreEqual(monthlyPaymentSchedule.Count, 15 * NoOfMonthsInYear);
        Assert.AreEqual(secondMonthPaymentSchedule.GetMonthlyPaymentAmount(), 
        (decimal) 2144.65);
        Assert.AreEqual(secondMonthPaymentSchedule.GetDate(), "Sep 2016");
        Assert.AreEqual(secondMonthPaymentSchedule.GetInterestAmount(), 
        (decimal) 871.30);
        Assert.AreEqual(secondMonthPaymentSchedule.GetTotalInterestAmount(), 
        (decimal) 1746.30);
        Assert.AreEqual(secondMonthPaymentSchedule.GetPrincipalAmount(), 
        (decimal) 1273.35);
    }
    [ExpectedException(typeof(ArgumentException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest1()
    {
        new AmortizationSchedule().Get(0, loanAmount,
            loanPeriodInYears, loanStartDate);
    }
    [ExpectedException(typeof(ArgumentException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest2()
    {
        new AmortizationSchedule().Get(annualPercentageRate, 0,
            loanPeriodInYears, loanStartDate);
    }
    [ExpectedException(typeof(ArgumentException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest3()
    {
        new AmortizationSchedule().Get(annualPercentageRate, loanAmount, 0, loanStartDate);
    }
    [ExpectedException(typeof(ArgumentException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest4()
    {
        new AmortizationSchedule().Get(annualPercentageRate, loanAmount,
            loanPeriodInYears, DateTime.MinValue);
    }
    [ExpectedException(typeof(ArgumentException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest5()
    {
        new AmortizationSchedule().Get(annualPercentageRate, loanAmount,
            loanPeriodInYears, DateTime.MaxValue);
    }
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    [TestMethod]
    public void GetAmortizationScheduleExceptionTest6()
    {
        new AmortizationSchedule().Get(annualPercentageRate, loanAmount,
            loanPeriodInYears, DateTime.MaxValue.AddYears(-1));
    }
}

Unit Test Project-Improved

Continue Reading

Concurrent and immutable Collections in C#-Primer

Concurrent(or Threadsafe) collections allow multiple threads to update them simultaneously in a safe way. Concurrent collections are more efficient than implementing a regular collection with a lock.
An immutable collection cannot actually be modified. Instead, to modify an immutable collection, you create a new collection that represents the modified collection.

The .Net framework has several different collection types.
ImmutableList<T> Use this data structure when
a. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. They allow indexing like Lists but they have lower performance than Lists.

ImmutableStack<T> Use this data structure when
a. you need a first-in, first-out data structure
b. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. ImmutableStack have lower performance than Stack data structures.

ImmutableQueue<T> Use this data structure when
a. you need a first-in, last-out data structure
b. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. ImmutableQueue have lower performance than Queue data structures.

ImmutableDictionary<T, TValue> is a key-value collection of items. Use this type when you
a. need a key-value collection of items.
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items in ImmutableDictionary have an unpredictable order.
Performance of ImmutableDictionary is faster than ImmutableSortedDictionary.

ImmutableSortedDictionary<T, TValue> is a key-value collection of items sorted by key. Use this type when you
a. need a key-value collection of items.
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items are sorted in the collection.

ImmutableHashSet<T> is just a collection of unique items. Use this type when you
a. do not need to store duplicates
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items in the ImmutableHashSet have an unpredictable order.
Performance of ImmutableHashSet is faster than ImmutableSortedHashSet.

ImmutableSortedSet<T> is a sorted collection of unique items. Use this type when you
a. do not need to store duplicates
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items are sorted in the collection.

ConcurrentDictionary<T, TValue> is a key-value collection of items. Use this type when you
a. need a key-value collection of items.
b. can be accessed by multiple threads safely.
Use the ConcurrentDictionary<T, TValue> when you have multiple threads reading and writing to the shared collection. If the updates are more rare then the ImmutableDictionary<T, TValue> is a better choice.

Continue Reading

Encapsulate and vary algorithm from the client that uses it(Strategy Pattern)

Strategy pattern is a software design pattern that enables an algorithm’s behavior to be selected at runtime. It
a. defines a family of algorithms,
b. encapsulates each algorithm, and
c. makes the algorithms interchangeable within that family.

Example:
Strategy Design Pattern

using System.Collections.Generic;

class StrategyDesignPatternClient
{
    static void Main()
    {
        var sortedList = new SortedList();
        sortedList.Add("name 1");
        sortedList.Add("name 2");
        sortedList.Add("name 3");
        //The client passes the "sort strategy" to the SortedList 
        //so the SortedList can use that passed strategy to sort the internal list
        sortedList.SetSortStrategy(new QuickSort());
        sortedList.Sort();
    }
}
public class SortedList
{
    private List<string> list = new List<string>();
    private SortStrategy sortstrategy;
    public void SetSortStrategy(SortStrategy sortstrategy)
    {
        this.sortstrategy = sortstrategy;
    }
    public void Add(string name)
    {
        list.Add(name);
    }
    public void Sort()
    {
        //Ask the "SortStrategy" class to sort the list. 
        sortstrategy.Sort(list);
    }
}
public interface SortStrategy
{
    void Sort(List<string> list);
}
public class QuickSort : SortStrategy
{
    public void Sort(List<string> list)
    {
        // Default is Quicksort
        list.Sort(); 
    }
}
public class ShellSort : SortStrategy
{
    public void Sort(List<string> list)
    {
        // ShellSort the "list" collection here. 
    }
}
public class MergeSort : SortStrategy
{
    public void Sort(List<string> list)
    {
        // MergeSort the "list" collection here. 
    }
}
Continue Reading

Use ProtoBuf.Net to improve serialization performance

Our application relied on data that did not change often. It had therefore implemented a solution that cached this data as xml files. These files were written to the web server on the first request and subsequently read whenever they were needed.
Some of the problems that arose due to this was:
a. Since the application was load balanced, a web server servicing a web request was not guaranteed to serve the next web request. This lead to the data in the various web servers quickly getting out of sync.
b. Our application was very slow. Profiling the application showed that showed that reading and writing(i.e serialization and de-serialization) the XML cache files was one of the biggest bottleneck in the application.
XML serialization is one of the slowest serializationde-serialization formats in the .Net framework. See the excellent article where Maxim Novak compares the various types of serialization in the .Net framework.

We therefore decided to change the serialization format from XML to ProtoBuf.Net. Protobuf-net is a contract based serializer for .NET code, that writes data in the “protocol buffers” serialization format engineered by Google. This framework component is written by Marc Gravell, considered as a .Net guru.
We also decided to change the application to write the cache files to a single file server and create the cache files in a background thread. I will write a separate article for the same in the future.
Changing the application to ProtoBuf.Net protocol was pretty straightforward.
a. Use Nuget to install ProtoBuf.Net framework component.

Install-Package protobuf-net

b. Add [ProtoContract] attribute to each class that needs to be serialized and [ProtoMember] Attribute with a unique integer to identify each member that needs to be serialized.

using ProtoBuf;
[ProtoContract]
class Person 
{
    [ProtoMember(1)]
    public int Id {get;set;}
    [ProtoMember(2)]
    public string Name {get;set:}
}

c. Serialize and deserialize your data

using (var file = File.Create("person.bin")) 
{
    Serializer.Serialize(file, person);
}
Person person;
using (var file = File.OpenRead("person.bin")) 
{
    person = Serializer.Deserialize<Person>(file);
}

As you can see from the charts below, the change from
a. XML to ProtoBuf.Net serialization
b. caching the files in a single file server instead of multiple file servers
c. offloading the work of generating the cache files to a background thread
was a huge success.

ProtoBuf.NetPerformanceImprovement-Part1

 

ProtoBuf.NetPerformanceImprovement-Part2

Using ProtoBuf.Net Serialization: The .Net profiler shows “HotSpots” due to serialization and de-serialization are much reduced. File sizes are also much reduced.

ProtoBufNetSerialization1

ProtoBufNetSerialization2

Using XML Serialization: The .Net profiler shows high CPU usage i.e “HotSpots” due to XML serialization and de-serialization. File sizes are also much larger.

XMLSerialization1

XMLSerialization2

Continue Reading