Visual Studio Tips

Jeremy Hutchinson listed out series of tips to be more productive in Visual Studio.

I was not aware of tip #2 - $exception Pseudovariable - Viewing uncaptured exception while debugging. Visual studio has a pseudo variable $exception in the debug mode which will hold the details of the last exception. To explore $exception, let’s create a small project:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
static void Main(string[] args)
{
    Object foo = null;

    string bar;
    try
    {
        bar = foo.ToString();
    }
    catch (Exception)
    {
        // Oh! What cause this exception?
        bar = null;
    }
}

Put a breakpoint at line 13 and run above code. The code will throw an exception and pause the execution in catch block. Now here you can use $exception variable to inspect exception details.

Visual Studio - $exception variable

Visual Studio - $exception variable

SQL SERVER - Stored Procedure sp_datatype_info to Get Supported Data Types

Sometimes you need to know more information about various data types supports by your SQL Server version. Information like if the specific data type is searchable or not, or it’s maximum precision, or if it’s case sensitive or not. sp_datatype_info system SP comes handy to retrieve such information.

1
2
3
4
5
USE master;
GO

EXEC sp_datatype_info;
GO
sp_datatype_info

ZarahDB - JSON based flat file database

The relational database management system is the de facto choice for any standard application to persist data on the disk. It provides all interfaces one requires to store, optimize, and query data for the client. But sometimes persisting data requirement for the application is so basic that we don’t need whole RDBMS engine on the backend to store data - storing in some form of flat file is sufficient enough to serve application data persistence need.

Creating such a backend layer to store data as flat files and querying data from these files can be a bit tedious which can become a project in itself. But there are many open source wrappers already around which can help you to kickstart building such data access layer. ZarahDB is one such open-source project.

ZarahDB is a flat file database engine which uses the combination of the root folder (per database), child folders (per table) and JSON files (per record) to persist data. Storing data as JSON files makes ZarahDB more structured and reliable compare to others. With that, storing data as files make it easy to backup or restore as it become just a matter of copying and pasting application data folder. As with other flat file based database platforms, you don’t need to install any database software on your server.

Project URL: http://zarahdb.com/
NuGet URL: https://www.nuget.org/packages/ZarahDB.Library
Source Code: https://github.com/MikeReedKS/ZarahDB

Using ZarahDB library in your project is easy - Simply add ZarahDB.Library through NuGet and your are ready.

ZarahDB - Nuget

ZarahDB - Nuget

Enough talk, let’s create a very basic .NET Core console application which will store and retrieve an employee information.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using ZarahDB_Library;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var instance = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Data");
            var table = "Employees";

            // Save an employee to the database
            var key = "0001";
            ZarahDB.Put(instance, table, key, "Name", "Mike Smith");
            ZarahDB.Put(instance, table, key, "City", "Chicago");

            // Retrive employee from the database
            var name = ZarahDB.Get(instance, table, key, "Name").Value;
            var city = ZarahDB.Get(instance, table, key, "City").Value;

            Console.WriteLine("Name: " + name);
            Console.WriteLine("City: " + city);

            Console.ReadKey();
        }
    }
}

JSON stored in the Employees data folder (Location: Data\Employees\0\0\0\1\0001.json) will look like following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{
  "Keys": [
    {
      "Key": "0001",
      "ColumnValues": [
        {
          "Column": "Name",
          "Value": "Mike Smith",
          "PreviousValue": "",
          "Updated": "636935583251802007"
        },
        {
          "Column": "City",
          "Value": "Chicago",
          "PreviousValue": "",
          "Updated": "636935583256218064"
        }
      ]
    }
  ]
}
ZarahDB - Output

ZarahDB - Output

As you probably noticed, ZarahDB stores data values as string, which means you will require to explicitely convert non-string data type or object type as string before passing it to ZarahDB method to save it. This goes other way around as well: you will retrive stored data as string from ZarahDB, you will then require to convert string value to column data type.

Here is the example which explains how DateTime data value to stored and retrived in ZarahDB.

1
2
3
4
5
// Convert non-string data type value to string
ZarahDB.Put(instance, table, key, "BirthDate", (new DateTime(1998, 09, 12)).ToString());

// Convert string to column specifc data type
var birthDate = Convert.ToDateTime(ZarahDB.Get(instance, table, key, "BirthDate").Value);

To store complex object using ZarahDB, you will require to serialize it to Json or Xml string. And as you guessed, you will then require to deserialize returned Json or Xml string to object type.

To conclude this brief overview of ZarahDB, following is the code snippet which demonstrates taking a complex object like a blog post and store and retrive using ZarahDB through Json serialization.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Newtonsoft.Json;
using System;
using ZarahDB_Library;

namespace BlogEngine
{
    internal class Post
    {
        public string Title { get; set; }
        public string Content { get; set; }
        public string Author { get; set; }
        public string Category { get; set; }
        public DateTime Publish { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var instance = new Uri(AppDomain.CurrentDomain.BaseDirectory + "Data");
            var table = "Posts";

            string key = "quick-start-tutorial";
            var newPost = new Post
            {
                Title = "Quick Start Tutorial",
                Content = "The basic concept of a scattered database is ...",
                Author = "Mike Reed",
                Category = "Database",
                Publish = DateTime.Now
            };
            var jsonPost = JsonConvert.SerializeObject(newPost);

            // Save blog post to the database
            ZarahDB.Put(instance, table, key, "PostData", jsonPost);

            // Retrive blog post from the database
            var resultJson = ZarahDB.Get(instance, table, key, "PostData").Value;
            var resultPost = JsonConvert.DeserializeObject<Post>(resultJson);

            Console.WriteLine("Title: " + resultPost.Title);
            Console.WriteLine("Content: " + resultPost.Content);
            Console.WriteLine("Author: " + resultPost.Author);
            Console.WriteLine("Category: " + resultPost.Category);
            Console.WriteLine("Publish: " + resultPost.Publish.ToString("MM/dd/yyyy"));

            Console.ReadKey();
        }
    }
}
ZarahDB - Blog Example

ZarahDB - Blog Example

As I mentioned in the early part of this article, ZarahDB is great if you have a basic data persistence requirement but surely it’s not recommended for the more complex or data-heavy requirement. I hope you will find this short overview helpful.

Hunters Head, Acadia

Sony ILCE-7RM3 • FE 16-35mm F2.8 GM
ƒ/8.0 • 16mm • 0.8s • ISO 100

Precarious rocks in the foreground and with the smooth and calm horizon in the background give a good contrast in this composition.

It was a huge gamble to wake up before dawn and drive to this spot for sunrise shots when the night before all weather apps on my iPhone were showing a cloudy morning. I was actually anticipating a cloud break-out in the sky for a very short time window which always creates inverted sunlight illuminating cloud cover turning them to the glowing orange-red-pink fireball. And as you can see in this photo, that gamble does payout that morning. The cloud break-out was not more than 5 mins but it gives me a whole lot of time to take multiple compositions. With the expected cloud-covered sky for the rest of the day, I decided to return back to my hotel and complete my sleep.

Why software projects take longer than you think – a statistical model

Erik Bernhardsson analysis SiP Effort Estimation dataset to illustration why software estimations turn out to be inaccurate and summaries his conclusion:

The mean turns out to be substantially worse than the median, due to the distribution being skewed (log-normally).

Tasks with the most uncertainty (rather the biggest size) can often dominate the mean time it takes to complete all tasks.

randr - Rename And Replace

I thought spending this weekend to kickstart learning Go. As a committed .NET/C# developer for 15 years, working with the Go syntax is a different kind of experience. I am still trying to well verse myself with Go syntax and development

Once I got the hang of Go basic syntax, I thought why not build a small and useful tool in Go which could resolve a daily annoyance. Folk, this is what I come up with, randr.

randr is a command line tool which recursively searches files content for source string and replaces it with the target string. With that, randr will also recursively rename files or folders name which matches source string to target string. You can also specify a command flag, if you want to rename or replace based on case sensitive or case insensitive search. And that is all!

randr - Help

randr - Help

To illustrate the usage of randr, consider you have a project folder with the following structure:

// Project Folder: c:\Projects\Payroll\

// c:\Projects\Payroll\Models\Worker.cs
public class Worker {}

// c:\Projects\Payroll\Services\WorkerService.cs
public class WorkerService {}

// c:\{rojects\Payroll\Repositories\WorkerRepository.cs
public class WorkerRepository {}

// c:\Projects\Payroll\Helpers\Worker\Extensions.cs
public static class Extensions {}

Let’s refactor above project using randr and change Worker entities to Employee. randr command to refactor will look like following which will replace any files text which contains Worker to Employee as well as will rename any file or folder with contains Worker to Employee.

randr.exe -find=Worker -replace=Employee -match=true -location=c:\Projects\Payroll\
randr - Example

randr - Example

After running randr successfully, our example project folder will look like following:

// Project Folder: c:\Projects\Payroll\

// c:\Projects\Payroll\Models\Employee.cs
public class Employee {}

// c:\Projects\Payroll\Services\EmployeeService.cs
public class EmployeeService {}

// c:\Projects\Payroll\Repositories\EmployeeRepository.cs
public class EmployeeRepository {}

// c:\Projects\Payroll\Helpers\Employee\Extensions.cs
public static class Extensions {}

Please go over README.md in the project repository for parameter usage details.

Project Repository: randr

randr is currently a bare minimum tool and it does require some more polishing. I hope you will find randr useful.