Mar 26, 2017

What's new in C# 7

C# 7 is the latest version of C# language and its available in Visual Studio 2017.
Let us check one by one what's new in C#.

1. Out Variables
You can declare out variable as a parameter to the method where they are used.

In the previous version, you need to separate the declare out variable and initialize into two statement.
int count;
if(int.TryParse(input, out count))
{
    Response.Write(count)
}

Now, You can write out variable as an argument so no need to write a separate statement.
if(int.TryParse(input, out int count))
{
    Response.Write(count)
}

2. Tuples
Tuples are lightweight data structures that contain multiple fields to represent the data members.

Tuples support more than one data element structure.

You can create a tuple by assigning each member to a value:
var letters = ("a", "b");
Tuples are most useful as return types for private and internal methods. Tuples provide a simple syntax for those methods to return multiple discrete values: You save the work of authoring a class or a struct that defines the type returned. There is no need for creating a new type.
private static (int a, int b) Range(int a, int b)
{
    return (a, b);
}
Call above method and set return values to the variables.
(int a, int b) = Range(a,b)

3. Pattern matching
Pattern matching is a feature that allows you to implement method dispatch on properties other than the type of an object.

Let's see is expression and switch expression

- is expression
public static int sumData(IEnumerable<object> values)
{
    var sum = 0;
    foreach(var item in values)
    {
        if (item is int val)
            sum += val;
    }
    return sum;
}
Above example, item check is int val then sum.

- switch statement updates
Switch statement already part old c# language.
public static int SumData(IEnumerable<object> values)
{
    var sum = 0;
    foreach (var item in values)
    {
        switch (item)
        {
            case int val:
                sum += val;
                break;
        }
    }
    return sum;
}
The match expressions have a slightly different syntax than the is expressions, where you declare the type and variable at the beginning of the case expression.

For other features and more detail visit the Microsoft websites.

Mar 11, 2017

Asp.Net Core Unit Test - Code Coverage using OpenCover


Very important thing is for any developer to write good code without any bugs and easy to understandable to other developers.

I'm not going to talk about coding standard and coding style because it's all depend on company standard and developer to developer.

Today, I'm more talk about Asp.Net Core Unit Test case Code Coverage.

I assumed you already know how to write xUnit test case in asp.net core. If you want to know about xUnit test case then please read this blog

For code coverage, I'm using OpenCover and for the report using ReportGenerator tool.

For both the mentions, OpenCover and ReportGenerator added into project.json file under the Test project.
{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "dependencies": {
    "Microsoft.EntityFrameworkCore.InMemory": "1.1.0",
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "NETStandard.Library": "1.6.0",
    "InMemoryDatabaseAspNetCore.Model": "1.0.0-*",
    "InMemoryDatabaseAspNetCore": "1.0.0-*",
    "OpenCover": "4.6.519",
    "ReportGenerator": "2.5.2"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}

Once you added both the package on project.json file. Now time to create one batch file with below code and place at Test project.
@echo off

SET dotnet="C:/Program Files/dotnet/dotnet.exe"  
SET opencover="C:\Users\%USERNAME%\.nuget\packages\OpenCover\4.6.519\tools\OpenCover.Console.exe"
SET reportgenerator="C:\Users\%USERNAME%\.nuget\packages\ReportGenerator\2.5.2\tools\ReportGenerator.exe"

SET targetargs="test"  
SET filter="+[*]InMemoryDatabaseAspNetCore.* -[.Model]* -[InMemory.Test]* -[.Startup]* -[.Test]* -[xunit.*]* -[FluentAssertions]* -[crypto]*"  
SET coveragefile=Coverage.xml  
SET coveragedir=Coverage

REM Run code coverage analysis  
%opencover% -oldStyle -register:user -target:%dotnet% -output:%coveragefile% -targetargs:%targetargs% -filter:%filter% -skipautoprops -hideskipped:All

REM Generate the report  
%reportgenerator% -targetdir:%coveragedir% -reporttypes:Html;Badges -reports:%coveragefile% -verbosity:Error

REM Open the report  
start "report" "%coveragedir%\index.htm" 


On above batch file, you can set all variables as per your system and project path.

Once setup variables then one important point are filters. Filters are that which mentions the which dll are included\excluded for code coverage.
SET filter="+[*]InMemoryDatabaseAspNetCore.* -[.Model]* -[InMemory.Test]* -[.Startup]* -[.Test]* -[xunit.*]* -[FluentAssertions]* -[crypto]*"  

After all the set, you are ready to run the batch file and it will show the result into the browser and it look like below. Green and red color based on your code coverage.


Hope you like it!!