Archive for the Category ◊ .Net ◊

Tuesday, September 06th, 2016

When using official C# mongo driver any field with name ‘Id’ and type “ObjectId” is mapped to _Id field in mongo db.What that means is that If you don’t supply some value for “Id” field default value would be generated (using default Id Generator)and the same will be deserialized into Id field of your POCO while fetching the data.

Lets take a simple POCO class as shown below .


 public class Post
    {
        public ObjectId Id { get; set; }
        public string Title { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
    }

Saving this class without providing value for Id field would automatically generate the object id value as shown below.

image

But for most of the real world applications you cannot constrain yourself with the restriction of having type of Id field as “ObjectId” and name as “Id”.

In these cases we need to do few extra things to ensure that mapping and id generation happens properly.

Mapping a property to _Id field

We can use any property of our POCO class as Id property.Lets see what happens when I rename Id field to PostId.


public class Post
    {
        public ObjectId PostId { get; set; }
        public string Title { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
    }

Trying to save this POCO we get following error “System.InvalidOperationException: No IdGenerator found.”   To map this property we can use BsonId  attribute as shown below.


    public class Post
    {
        [BsonId]
        public ObjectId PostId { get; set; }
        public string Title { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
    }

And it will map to _Id value correctly.

Changing type of Id Property

Lets say I want to have my Id field as a string i.e. I want to have Id saved as ObjectId in mongodb but should be string in my POCO class.If I try this with current code again I will get same error i.e. “System.InvalidOperationException: No IdGenerator found.” To make this work you need to pass a parameter to BsonId attribute as shown below.This specifies the IdGenerator to use which in our case is StringObjectIdGenerator.


    public class Post
    {
        [BsonId(IdGenerator=typeof(StringObjectIdGenerator))]
        public string PostId { get; set; }
        public string Title { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
    }

This is an inbuilt id generator which is used for string represented as ObjectId in MongoDb. MongoDb provides couple of inbuilt Id Generators as shown below.

  • BsonObjectIdGenerator
  • CombGuidGenerator
  • GuidGenerator
  • NullIdChecker
  • ObjectIdGenerator
  • StringObjectIdGenerator
  • ZeroIdChecker<T>

For more details visit this link.

Custom Id Generator

Finally there may be situations where you want to generate your own ids.This can be accomplished by implementing a custom id generator.Let say we want to generate  custom ids which will have following format <MyApplicationName>_<GUID>.

To create custom id generator we need a class that implement IIdGenerator interface and use that as Id Generator type.

Custom Id Generator:


 public class BlogIdGenerator : IIdGenerator
    {

        public object GenerateId(object container, object document)
        {

            return "Blog_" + Guid.NewGuid().ToString();
        }

        public bool IsEmpty(object id)
        {
            return id == null || String.IsNullOrEmpty(id.ToString());
        }
    }

Using Id Generator in our POCO class:


 public class Post
    {
        [BsonId(IdGenerator = typeof(BlogIdGenerator))]
        public string PostId { get; set; }
        public string Title { get; set; }
        public string Tags { get; set; }
        public string Description { get; set; }
    }

Results:

image

I had to struggle initially  to understand how all these scenarios worked (may be because I didn’t pay enough attention to basics).Hope this post will  help other to get started smoothly with these basic concepts.

Category: .Net | Tags: ,  | Leave a Comment
Tuesday, August 02nd, 2016

ASP.NET request validation is ASP.NET’s built in way of saving our applications from dangerous script injection attacks or XSS attacks.The default validation provided by asp.net  is quite restrictive i.e. it doesn’t allow any html tag to be part of request.For more details on ASP.NET request validation go to this MSDN article.

Default request validation is good for simple application which does not deal with any HTML formatting but for a real world website this may not work.For example if you have an application where you allow user to input text with basic html formatting like bold or italic text or allow changing the color of the text ,the default validation will not allow this and you will see the familiar message.

A potentially dangerous Request.Form value was detected from the client.

To get around this problem we usually tend to disable the request validation which means we have to take care of all the input validation ourselves.

ASP.NET 4.0 provides another option to get around this problem by plugging your custom request validation logic.Lets see how we can do this.

For the purpose of this blog post I want to put a request validation logic which allows basic html formatting  and throws error if try to add some scripts as input.

Plugging your custom request validation

Below are the step required to plugin you custom request validation.

  • Request validation in ASP.NET is taken care by RequestValidator  class.To create our custom logic first step is to create a class which inherits form RequestValidator class.We will call this class HtmlRequestValidator.

public class HtmlRequestValidator : RequestValidator 
 { 
     public HtmlRequestValidator() {}
}

  • Next step is to override  IsValidRequestString method.This method is called on each of the value which asp.net wants to validate.

 protected override bool IsValidRequestString( HttpContext context, string value, RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex) 
    {

    }

Below is a brief explanation on what all the parameters to this method are :

  1. context : HttpContext instance of current request.
  2. value :  value which needs to be validated.
  3. requestValidationSource :   Enum describing what is the source of the value which is being validated e.g. Is this a cookie or Form value etc.
  4. collectionKey : In case the value is coming from a collection this key identifies the key of that collection e.g. if the value is from form collection then collectionKey would be the key of the value being validated.
  5. validationFailureIndex: This gives the index into the value string where dangerous value was detected.

For our purpose we will  just validate the values coming in form collection and call a method IsSafeHtml  which is suppose to return me boolean value indicating whether or not the value which is coming is a safe html.For all the other type of input like cookies ,files etc we will fall back to default request validation.


public class HtmlRequestValidator : RequestValidator 
 { 
     public HtmlRequestValidator() {}

   
        protected override bool IsValidRequestString( HttpContext context, string value, RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex) 
    {
        validationFailureIndex = -1; 
        
        if ( requestValidationSource == RequestValidationSource.Form   ) 
        {
            return IsSafeHtml(value,out validationFailureIndex);
        } 
        else 
        { 
            return base.IsValidRequestString( context, value, requestValidationSource, collectionKey, out validationFailureIndex); 
        } 
    }
	//Just a sample representative method
     bool IsSafeHtml(string s,out int outIndex)
     {
         outIndex = s.ToUpper().IndexOf("<SCRIPT");
         return outIndex == -1;
     }
 }

NOTE :

  1. IsSafeHtml method is just an example method to demonstrate the concept and by no means a foolproof method to safeguard against XSS attacks.For a production application you should consider using some of the Anti XSS libraries available.
  2. Another important point is that ASP.NET request validation is not triggered in case of AJAX based JSON POST requests like the one shown below.

$.ajax({
        url: '/Controller/Action',
        data: JSON.stringify({data:data}),
        type: "POST",
        dataType: "json",
        contentType: "application/json",
      }); 

So in these cases even if you have Request Validation enabled you should take care of validating such requests.

  • Last step in this whole process is to plug our custom validator.For this we need to add requestValidationType attribute in web.config file with fully qualified name of our custom validator.

<httpRuntime targetFramework="4.5" requestValidationType ="CustomValidationSample.HtmlRequestValidator" />

That’s all.Lets see how it works.

Demo

I have created a simple form with few fields where I will first try to put some html text explicitly.

Add Html and Save

image

Saved without any request validation errors !!!

image

Lets try to create another entry by adding some scripts .

Add Script and Save

image

Error !!!

image

So our custom logic works.You can use this logic to validate not only form collection value but the values coming from cookes,files,headers etc.

Category: .Net | Tags: , , ,  | Leave a Comment
Monday, August 01st, 2016

Recently when I was trying to learn Entity framework, I came across the “Code First” Approach which I felt was pretty cool. Code first approach mainly follows Domain Driven Design wherein you can create classes as per your requirement rather than design your database first and then create the classes which match your database design. Code first approach will help in creating the database based on your entity class.

Let’s see a practical example of how code first works.

Prerequisites:

Install entity framework as a nugget package to your project

Right click on your project -> Manage NuGet Packages -> Search for “Entity Framework” -> Install Package

Now assuming you have set up a basic project, let’s go ahead and add the below class:

1

 

As you can see from the above piece of code, we are creating a concert class which has properties that describe the following:

  • Id: Used to identify the concert [Primary key]
  • ArtistId: Would tell which artist is performing at the concert. It has a foreign key reference to the ApplicationUser table. By decorating this property as required, we are ensuring that it’s a not null column in the DB.
  • Venue: Is again a not null column and also has a limit of 255 characters. This would be translated to varchar (255)
  • Datetime: Time at which the concert starts.
  • GenreId: It has a foreign key reference to the Genre table. It’s not null column in the DB.

 

In order to generate a migration script, you have to follow the below steps. This will generate a class that would contain the migration script for your entity class

  • Run the “Enable-Migrations” command in Package Manager Console. This has to be done only once i.e. at the beginning of your project
  • After creating/modifying your entity class, you can add a migration script. Advantage of this is that you can track the table changes and also every database change can be version controlled. To do this, run “Add migration <name>” in Package Manager Console. This would create a class file.

2

  •  These migration classes can be found within the migration folder:

3

 

  • In order to execute this migration script, run “Update Database” in Package Manager Console. You would notice that the changes are applied to the database.

4

 

Now, the next question that would be in your mind is “how can I populate this table”? It’s very simple:

 

5

 

Upon saving the changes, a record would be inserted to your concerts table.

 

6

 

In this way, without creating a database first, you can start writing an application that will eventually create the database from your domain classes.

Category: .Net  | Leave a Comment
Author:
Tuesday, July 05th, 2016

What is a Deadlock?

You would have encountered deadlock in the real life situation like

  • It takes money to make money or,
  • You can’t get a job without experience and you can’t get experience without a job. 

Similarly, in concurrency, when two or more competing actions wait for each other to finish and if neither ever does, then it leads to a deadlock.

Generally, we have a myth about deadlock that it is an an issue which sometimes occurs in multiprocessing systems, parallel computing and distributed systems which involves more than one process and resources. But in actual, deadlock is a condition which occurs when one process holds a resource (e.g. R1) and waits for another (e.g. R2) to finish the task wherein the other process holds the same resource (R2) and wait for resource (R1) and so on.

 

graph

Example of Deadlock

  • Deadlock in SQL Server

Sometimes we have observed it takes lot of time for transactions to complete if there are large number of records in the database.

For example, let’s assume that the transaction B is in the process of modifying a row in a table T which causes Intent-Exclusive lock on both the table and the page that contains the row.

Simultaneously a process A needs to read a few pages on the same table. However, Process B already holds a lock on one of the pages that contains some of the rows which Process A needs. This situation causes both the processes to be in waiting queue resulting in a deadlock. 

 

Necessary Condition for a deadlock

Coffman identified 4 conditions that hold simultaneously to have a deadlock in a system.

  •     Mutual Exclusion

It means if one process is holding on to one or more than one resource, it cannot be shared by another process

e.g.: if one process acquires a memory, no other process can acquire it before the process is finished

  •       Hold and wait

It means one process is holding a resource and is waiting for another one.

  •       No-Preemptive Condition

It means if a process locks a resource, it cannot be taken away by a different process by any means until and unless it is finished.

  •         Circular wait

Suppose we have a set of processes such as {P0, P1, P2, P3…. Pn} and P1 is waiting for Resource X, P2 is waiting for Resource Y, and Pn is waiting for Resource X.

The above situation creates circular wait where in every process in the list is waiting for a process held by the next process in the list.

 

Referring to the diagram which appears earlier in the passage

  • Mutual Exclusion

R1 and R2 are non-sharable

  • No Preemptive               

                       Cannot force the processes to release the lock for Resource R1 and R2.

  • Hold and wait

Process T1 is holding R1 and waiting for R2. The process T2 is holding R2 and waiting for the Resource R1.

  • Circular wait

Third situation creates circular wait.

 

Handling Deadlock

There are 3 ways with which we can handle the deadlock

  • Deadlock Detection

In this approach we allow the system to enter deadlock state and then recover. There are different ways to detect deadlock. One of ways is through trace flag.

Below is the deadlock information once the trace flag was on

picture

When the deadlock is detected, the database engine ends the deadlock by choosing one of thread as deadlock victim.

  •   Deadlock Prevention

Ensure that the system will never enter a deadlock state. This can be ensured if we can prevent any of the conditions which is required for deadlock.

For example:

Hold and Wait:

            We should guarantee that whenever a process request for resources, it should not be holding any other resources.

No Preemption:

It means process should release the resources being held if it can’t get additional resource. Preempted resources will be added to the list of resources for which the process is waiting and a process can be restarted only if it regains its old resource as well as the new one which it is requesting.

Circular Wait

It means impose an order for all the resource type in a system and then process can request a resource based on order of its resource type.

E.g. provide an order to the resource type of hardware in a computer system

            1 – Card Reader

2 – Printer

3 – Plotter

4 – Tape Drive

Now the process may request for printer and then Tape drive but it cannot request for Tape drive and then Printer.        

  • Deadlock avoidance

                                    The deadlock avoidance approach handles deadlock before they occur.

There are methods like ‘Wait-for graph’ which can can be used to detect deadlock in advance.

Wait-for graph:

 

In this method, a node is created for each transaction starting in the system. Suppose Transaction T1 request for a resource e.g. X to acquire a lock which is acquired by some other transaction say T2 a direct edge is created from T2 to T1. If T2 release the resource, the edge is dropped.

pic

The system maintains the wait-for graph for every transaction waiting for resource and keeps checking whether there is cycle in the graph.

We can use following approaches to avoid deadlock:

  • Do not allow any transaction which request for resource which is already acquired by some other transactions. One way to accomplish this is to make sure the sequence of the transactions is in such a way that it does not allow transaction to go in waiting state.
  • Other way is to rollback one of the transaction. It is always feasible to rollback younger transaction. It is generally done by the database engine when the deadlock is detected.
Category: .Net, General  | 2 Comments
Tuesday, June 07th, 2016

 

Introduction

When we are trying to solve a large business problem, a good software architecture would help focus on the real business problem rather than the technical complexities due faced due to bad design.

In this article we will look at the DDD approach for solving a big business problem. There is a very good article on code-project about what is BDD and some basics: http://www.codeproject.com/Articles/339725/Domain-Driven-Design-Clear-Your-Concepts-Before-Yo

We will use this article and see how we can implement the technical challenges. We will look at the how we can implement different entities explained the article.

We will be using a framework called Highway.Data is one of the fastest and smoothest way to great architecture specially when working with DDD.

The first step in the design is to define a domain(s). If it’s a very small application just a single domain will suffice but the real power of DDD comes when we have divided our large application many independent segregated smaller domains. Each domain use its own DataContext, Entities, Value objects, Repositories, and domain service.

 

Problem Statement

Lest look at a real world business problem and try to solve it with our approach. Lets take an example of

“An e-commerce website where there are multiple consumers can browse through different products, add to cart, and purchase and then later the order is shipped”

When we analyses the above business problem the domain that comes to our mind is “Ecommerce”. But if we start our application with “Ecommerce” as our domain we might end up with a huge very complex unstructured application. But what if we could divide this application in smaller domains for example:

  • Users – the end consumers of the application
  • Login/Authentication – Login to application
  • Products – products listed for purchase
  • Order-Management – creating the order from the users cart
  • Payment – Handle the payment for the orders, discounts and other business logic
  • Shipping – Once order is completed handles the shipping, order tracking

Each of these domains can be independently developed, changed and maintained as a separate project.

For this article we will look at one domain (Users domain) in depth how we can implement it end to end.

 

Application domain

Since each domain is application in our context lets create a domain for Users called UserAppDomain. Let’s start by first installing Highway.Data NuGet package

 

1

Depending on which data access technology we want to use, select the package accordingly. In this article we will be using Entity framework for our example.

2

 

Entities & Value objects

When working with DDD it makes sense to define the entities for each domain. There will be situations where there can be common entities used across domain. But it’s not a good idea to use common entities. Let’s talk why not?

Let’s take an example,

A user entity (End consumer) would have all the information such as Username, Full-Name, Address, Phone, Age, Sex etc. But when we are in Login Domain which is responsible for authentication uses User entity but it does not need anything else other that some basic properties such as username, full name and say password etc. hence it makes sense to create an entity User specific to Login domain with just these few properties.

 

3

 

Depending on the ORM we need to map the entity to the corresponding table in DB.

4

 

So now we have our entity and it is mapped to respective table we can move on to next step.

 

Data Domain

So now we create a data domain through which we can access the data across the application

5

 

Repository

Highway.Data provides a generic repository which can be used to execute a typed query to execute. Let us look at a query first before we use the repository.

With highway data there are few default queries which can be used for querying most of the data

Eg.

var users = _repository.Find(new FindAll<UserEntity>()).ToList();

var user = _repository.Find(new GetById<UserEntity>(10));

Or we can create custom queries for more complex querying

Eg.

6

 

And then use it like

var users = _repository.Find(new UserComplexQuery<UserEntity>(“john”,”1st street”));

The repository can be directly injected in any controller or class where we wish to query a repository Eg.

 

7

 

 

Author:
Friday, April 01st, 2016

C# Interactive !! Seems interesting. What is that ?

The C# interactive window provides an easy way
to experiment with code, API, classes, external dll without writing code in
some .cs file. Instead you run the code in this window. It supports intelligence/
code completion.

Where and how I can found C# interactive window?

Well either you should have VS 2015 update from here:
https://www.visualstudio.com/downloads/download-visual-studio-vs
or If you are still using VS 2012 , you can install Microsoft “Roslyn” CTP from here:
https://www.microsoft.com/en-in/download/details.aspx?id=34685

csharp_interactive_1

 

I just read “Roslyn”. What is that ?

The .NET Compiler Platform (“Roslyn”) provides open-source C#  compilers with rich code analysis APIs.

What I can do with C# interactive window ?

1.)    Run simple code :

SimpleCode

2.)    Run Multi Line Code:

To add code in multiline where each line itself is complete; just press ctrl+enter other wise press shift+Enter and write code in subsequent line. Keep doing that until code is over and finally press enter ket to get output.

MultiLineCode

3.)    Define types and call their methods:

CustomTypeDefining

4.)    Test LINQ queries:

LinqQuery

5.) Reference External dlls:

DllReference

Anything else ?

Yes, there are a few commands too to help you out:

Commands:

#help If you need any help just type #help and it will show all available options.

#reset  used to reset the execution environment

 

That’s all folks. Thanks for reading. Hope you enjoy it.

 

 

 

 

 

Sunday, March 06th, 2016

Pipes are a mechanism for inter process communication in windows.Pipes come in two varieties viz. Anonymous pipes and Named pipes.Anonymous pipes as name suggest do not have a name and can be used to communicate between threads or two related processes i.e. when one process initiates another process.

Below are major differences between named and anonymous pipes.

Anonymous

Named

Communicate between threads or related process i.e. parent –child  processes on same machine. Communicate between any kind of process (related or unrelated) on a machine or over network.
One way communication (Half-duplex) i.e. either Server to client or client to server.* Two way communication (Duplex)
Byte based i.e. data is written and read as stream of bytes (or characters) Message based i.e.data is written and read as stream of messages(or string)

 * You can always use two anonymous pipes for bi-directional communication.

In this blog post I will talk about anonymous pipes and in upcoming post will be talking more about named pipes.

To communicate using anonymous pipes within the process or between the process we need a mechanism to pass pipe handle to client so that it can connect to the pipe created by server.Below are the steps.

  1. Parent creates Pipe hence its called pipe server.
  2. Get the handle for the pipe.
  3. Parent creates child and pass pipe handle to child
  4. Child uses handle to connect to the pipe

Now communication can start.Based on what communication direction server and client specified one end of pipe would be considered Writer end and another one reader.

Lets see how this works in two different scenarios.

Intra-Process Communication

You can use anonymous pipes to communicate between two threads in the same process.Below is the code for the same.


using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace AnonymousPipesIntraProcess
{
    class Program
    {
        
        static void Main(string[] args)
        {
            using (AnonymousPipeServerStream pipedServer = new AnonymousPipeServerStream(PipeDirection.Out))
            {

                Thread child = new Thread(new ParameterizedThreadStart(childThread));
                child.Start(pipedServer.GetClientHandleAsString());
                
                using (StreamWriter sw = new StreamWriter(pipedServer))
                {
                    var data = string.Empty;
                    sw.AutoFlush = true;
                    while (!data.Equals("quit", StringComparison.InvariantCultureIgnoreCase))
                    {
                        pipedServer.WaitForPipeDrain();
                        Console.WriteLine("SERVER : ");
                        data = Console.ReadLine();
                        sw.WriteLine(data);
                    }


                }

            }
        }

        public static void childThread(object parentHandle)
        {
            using (AnonymousPipeClientStream pipedClient = new AnonymousPipeClientStream(PipeDirection.In, parentHandle.ToString()))
            {
                using (StreamReader reader = new StreamReader(pipedClient))
                {
                    var data = string.Empty;
                    while ((data = reader.ReadLine()) != null)
                    {
                        Console.WriteLine("CLIENT:" + data.ToString());
                    }
                    Console.Write("[CLIENT] Press Enter to continue...");
                    Console.ReadLine();
                }
            }
        }

       
    }
}

Code is self explanatory only thing to note is when we define the Pipe server and Pipe client we specify direction i.e. whether in or out and further communication can take place only in that specified one direction.

Inter-Process Communication

Little variation of the same code can be used to communicate between two related processes i.e. when one process creates another process.

Server code:


using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;

namespace AnonymousPipesServer
{
    class Program
    {
        static void Main(string[] args)
        {
            
            
            using (AnonymousPipeServerStream pipedServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
            {
                var startInfo = new ProcessStartInfo(@"AnonymousPipesClient.exe");
                startInfo.UseShellExecute =false;
                
                startInfo.Arguments = pipedServer.GetClientHandleAsString();
                var client = Process.Start(startInfo);
                pipedServer.DisposeLocalCopyOfClientHandle();
                using (StreamWriter sw = new StreamWriter(pipedServer))
                {
                    var data = string.Empty;
                    sw.AutoFlush = true;
                    while (!data.Equals("quit", StringComparison.InvariantCultureIgnoreCase))
                    {
                        pipedServer.WaitForPipeDrain();
                        data = Console.ReadLine();
                        sw.WriteLine(data);
                    }
                }
            }
        }
    }
}

Client Code:


using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AnonymousPipesClient
{
    class Program
    {
        static void Main(string[] args)
        {
            
            var parentHandle = args[0];
            using (AnonymousPipeClientStream pipedClient = new AnonymousPipeClientStream(PipeDirection.In, parentHandle.ToString()))
            {
                using (StreamReader reader = new StreamReader(pipedClient))
                {
                   
                    var data = string.Empty;
                    while ((data = reader.ReadLine()) != null)
                    {
                        Console.WriteLine("CLIENT:" + data.ToString());
                    }
                    Console.Write("[CLIENT] Press Enter to continue...");
                    Console.ReadLine();
                }
            }

        }
    }
}

Two changes in this code are use of handle inheritance and DisposeLocalCopyOfClientHandle.

While creating object of AnonymousPipedServerStream  the second parameter specified in the constructor indicates whether or not child process can inherit the handles from parent process.This value should be HandleInheritability.Inheritable so that child process can use the pipe handle we are passing.Refer this link to know more about handle inheritance concept.

Also in case of inter process communication we need to call DisposeLocalCopyOfClientHandle method after passing handle to client as this makes sure server gets notification when client disposes its PipeStream object.Suppose you don’t call this method and client disposes its PipeStream object.In this case when server sends message to client ,server process will hang indefinitely. On the other hand by calling this method we insure that server will throw appropriate exception

Conclusion

Anonymous pipes provide an easy way of communicating between related processes without much overhead as in case of named pipes.There are also ways to make it work for unrelated process but that is not in scope of this post and also in such cases it is better to use named pipes.

Thursday, February 11th, 2016

Unit test your MVC views using Razor Generator

Yes , we can unit test our MVC views . This is possible using  RazorGenerator which  generates classes which can further be used to instantiate views and unit test them.The generated class makes it possible that the views are pre compiled.

Razor Generator is a Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution.

But what’s the advantage of using MVC views?

If I (or another dev) removes some field from the view, I don’t want to wait for the Acceptance test to discover this. I want it to be discovered and flagged in a unit-test that runs multiple times daily.

If we are setting up CD pipeline we generally have test configured on different pipelines. So we can have unit test of views for testing all positive and negative scenarios and Acceptance test can be configured with few scenarios to check the basic overall functionality.

 

Steps to Write Unit Tests:

1. Install RazorGenerator from visual studio.

2. Create an MVC  project, using the ‘Internet Application’ template and including the unit test project.

3. Use NuGet to install the RazorGenerator.Mvc package in your MVC project.

4. Set the custom tool on Views\Home\Index.cshtml to  ‘RazorGenerator’ and press enter, Index.generated.cs is generated under it. Please check the image below for reference.

1           generatedcs

5. Use NuGet again to add the RazorGenerator.Testing package to the unit test .And that’s all it takes to get set up!

Below is a sample Unit test code

Remove the code given by default in Index.cshtml with the code below.

<h2>@ViewBag.Message</h2>

Now we  can write a unit test for our precompiled Index.cshtml view  to test if our value is rendered  in the html document as expected.

e.g. create a Views\HomeViewsTest.cs (in the unit test project):

unittestMVC 

And this is how we can write multipe cases to test our views.

References :

http://razorgenerator.codeplex.com/

http://blogs.planbsoftware.co.nz/?p=469

Category: .Net  | One Comment
Sunday, January 31st, 2016

If you write unit tests or follow TDD approach for your projects it is absolutely essential that your unit tests should be readable and clearly outlines the exact intent of the test.Also your unit tests should be able to act as documentation for your application logic.

You can always achieve these goals with usual asserts but it is difficult to keep it readable and very easy to mess it up to the extent that no body can understand what the test is trying to do.

To address these problems there are couple of fluent assertion framework which provide fluent APIs for doing asserts.One such framework which I found useful for .NET is Fluent Assertions.

Below is an Account class representing a bank account which I will use to demonstrate fluent assertions API.Some of the code is intentionally incorrect so that we can fail the tests.


 public enum TransactionType
    {
        Credit,
        Debit
    }
    public class BankTransaction
    {
        public BankTransaction(TransactionType type, double amount,DateTime transactionDate)
        {
            TypeOfTransaction = type;
            Amount = amount; 
            TransactionDate = transactionDate;
        }
        public TransactionType TypeOfTransaction { get;  }
        public double Amount { get;  }
        public DateTime TransactionDate { get;  }

        public override bool Equals(object obj)
        {
            
            var transaction = obj as BankTransaction ;
            if (obj == null) return false;

            return this.Amount == transaction.Amount &&
                this.TypeOfTransaction == transaction.TypeOfTransaction &&
                this.TransactionDate == transaction.TransactionDate;



        }
        public override int GetHashCode()
        {
           return  RuntimeHelpers.GetHashCode(this);
        }
    }
    public class Account
    {
        private string customerName;
        private double balance;
        private Account()
        {
        }

        public Account(string customerName, double balance)
        {
            this.customerName = customerName;
            this.balance = balance;
            Transactions = new List<BankTransaction>();
        }

        public string CustomerName
        {
            get { return customerName; }
        }

        public double Balance
        {
            get { return balance; }
        }

        public List<BankTransaction> Transactions { get; }

        public void Debit(double amount)
        {
            if (amount > balance)
            {
                throw new ArgumentOutOfRangeException("Invalid Amount");
            }

            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("Invalid Amount");
            }

            balance += amount; // INCORRECT CODE

            Transactions.Add(new BankTransaction(TransactionType.Debit, amount,DateTime.Now));
        }

        public void Credit(double amount)
        {

            if (amount < 0)
            {
                throw new ArgumentOutOfRangeException("Invalid Amount");
            }

            balance += amount;
            Transactions.Add(new BankTransaction(TransactionType.Debit, amount,DateTime.Now)); // INCORRECT CODE
        }



    }

First step is the install nuget package for Fluent Assertions (called FluentAssertions) library.

image

To start with lets see how simple assertion and failure message looks like.


[TestMethod]
        public void DebitAmount_ShouldBe_DeductedFrom_Overall_Balance()
        {
            Account acc = new Account("Jack", 1000);
            acc.Debit(50);

            //usual Notation 
            //Assert.AreEqual(950, acc.Balance);

            //Fluent assertions.
            acc.Balance.Should().Be(950); //SHOULD FAIL

        }

 

Below is the result you get when the above test fails.

Usual Assert:

image

Fluent Assertion:

image

You see how the assertion statement and failure message are much more descriptive.

Lets see how you can assert on collections.


 [TestMethod]
        public void AllTransaction_Should_Be_Recorded()
        {
            Account acc1 = new Account("Jack", 1000);
            acc1.Credit(50);
            acc1.Debit(50);

            Account acc2 = new Account("Matt", 1000);
            acc2.Credit(50);
            acc2.Debit(150);

            acc1.Transactions.Should().BeInAscendingOrder(x => x.TransactionDate);
            acc2.Transactions.Should().BeInAscendingOrder(x => x.TransactionDate);
            //Test each member to see it exists in other collection.Uses overridden equals method
            acc1.Transactions.Should().BeEquivalentTo(acc2.Transactions); //SHOULD FAIL

        }

Last assertion fails and below is the description which comes out.

image

This kind of shows how powerful this kind of API can be in debugging failing tests.

Below is one more example of asserting on exception that should be thrown.


  [TestMethod]
        public void Credit_Amount_LessThanZero_ShouldNotBeAllowed_AndThrowsException()
        {
            Account acc1 = new Account("Jack", 1000);
            
            Action creditAction = () => acc1.Credit(-50);

            //Test the type of exception and message (using wildcard)
            creditAction.ShouldThrow<ArgumentOutOfRangeException>().WithMessage("*Invalid Amount*");

        }

Fluent Assertions is very well documented and has a vast API covering each and every scenario which you might encounter in your tests.Here is the link to documentation wiki for Fluent assertions library.

This is by no means the only library available.There are other fluent assertion libraries like AssertJ (for Java), NFluent, Shouldly etc.

In the end, the library you use can be your choice but by means of this post I want to highlight the approach of fluent assertions which can go long way in keeping your test code maintainable.

Author:
Wednesday, January 20th, 2016

How often we have seen settings defined in config file like this :

 

Problem starts when they are a lot and there is no organized/logical way of putting them in config file and reading in application.

The more organized way of implementing custom app setting is via custom configSection.

Let’s first have a glimpse of config file to know what we want to achieve.

1 2

As it’s apparent now we have organized our mail server related settings in more organized way. Now by creating a strongly typed class for these settings we can read these setting directly by using our custom type. How ? Let’s understand this process step by step.

Step 1.) First we need to create a custom SettingsConfiguration class or any name you like inherited from IConfigurationSectionHandler interface. This is the class which we will use to register our custom config section in config file. For now just add a method called Create as highlighted below. We will discuss rest of the code in subsequent steps.

3

Step 2.) Now we will register this type in to our config file. How? Here is the way:

4

Here the name “settings” should be same as defined in our mailSetting parent node. In type first argument is our config class name with full namespace and second parameter is the assembly name in which this config class is defined.

5

So our basic config skeleton is ready. Let’s fill it with mailSetting that we want to achieve.

Step 3.) As seen earlier we already have our mailSetting defined in config file and to read it via XmlSerializer we need to create mapping classes for each node [if node does contain any other node(s)].

So here are our classes.

6

Here we can have different class name but name defined in XmlRoot should match with what is defined in config file.

Now there are different way of defining classes and their members based on their representation.

A.)   <name>default</name>: in this case we can have a property defined decorated with attribute XmlElement where name in attribute should match with name defined in config file.

B.)    <description companyName=”Prowareness” /> : for mapping this kind of node we need to have a separate class where CompanyName will be decorated with attribute XmlAttribute instead of XmlElement.

7

C.)    <to>

        <email description=”stakeholder” value=”reciever1@prowareness.com”/>

 <email description=”security admin” value=”reciever2@prowareness.com”/>    

      </to>  : Now It’s little tricky what if we want to define an email collection like above in our config file.

Here is the solution:

8

In this case List<Email> will be our XmlElement and the Email class can be defined as above.

So now our all the mapping classes are ready. Let’s see how to read it in our application.

Step 4.)  Now let’s go back to our class SettingsConfiguration’s Read method.

9

Here we are reading our custom config section named “settings” which we registered in step2. We get our “mailSetting” node using SelectSingleNode method and then using XmlSerializer we deserialize it to our MailSettingElement class. And then now onwards we can use this class to get the settings from config file and apply wherever needed.

So what advantages I’m getting out of this ?

As we can see even if sectionNode is null in above code (meaning you missed defining settings in config file due to various reason, may be you are not sure it’s value in development or test environment), you can still pass some default value in code as shown above.

By using this approach we can logically organize our settings in different sections which can be reused across applications if this class is developed as an assembly.

And here is the way to read/use these settings:

10

That’s all folks for this article. Please let me know your views/comments.