Maintaining history of data

 

What is this about?

I tend to quote Fowler: “Usually when we see properties on a class, they represent questions we can ask of an object now. However there are times when we don’t want to just ask questions about a property of an object now, we also want to ask these questions about some point in the past when things may have changed. “

For example , we want to track every change that occurs at an Employee ( changing name, salary) and see the previous versions( what was the salary 2 years ago ?)

So what are the software solutions to do an audit log ? First, we should have a history table for each table that we want to maintain an audit log. E.g. Employee => EmployeeHstory

Solution 1 : Coding / Event Sourcing

That means code should maintain history himself – i.e. adding to the history table every modifications.

For Entity Framework this can be done easy, by overiding SaveChanges.

Demo at https://youtu.be/YZ9GDjFApns 

Solution 2 : Table Triggers

This can be done from database . There are triggers for insert , update, delete – and those triggers insert the data in the history table

Demo at https://youtu.be/9xsvCJ-1f7Q

Solution 3, Temporal Tables, Microsot  Sql Server 2016 :

Documentation at https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables 

Limitations at https://docs.microsoft.com/en-us/sql/relational-databases/tables/temporal-table-considerations-and-limitations 

Demo at https://youtu.be/1Q3DmXJI5h8

Powershell vs console application

I have decided to make the utilities that I use in form of Powershell applications instead of Windows / Console applications

Pros:

There are simpler to use  – just run the powershell

The parsing of command line is powerfull in powershell

There are simpler to maintain ( just text scripts files)

There are no big dependencies( .NET Core/ .NET 4.x  to be installed)

This is a good tool under  my toolbelt

Everything you do in .NET you can do in Powershell ( sometimes it is much harder in .NET rather then powershell)

 

The repository is https://github.com/ignatandrei/PowershellUtils and the first item is to move the big files from C: into another drive to free space by making a junction dir ( e.g. C:\ProgramData\Package Cache and C:\Windows\Installer )

MVC 5 encrypt parameters–SEO

 

I have added SEO ( ok, a bullshit example of SEO ) to http://msprogrammer.serviciipeweb.ro/2017/03/20/mvc-5-encrypt-parameters/ .

This is pretty simple :

 public class EncDecSEOBullShit : IEncryptDecrypt
    {
        //public static string FullName = typeof(EncDecSEOBullShit).AssemblyQualifiedName;
        const string seo = "this-is-seo-";
        public string DecryptString(string value)
        {
            return value.Replace(seo, "");
        }

        public string EncryptString(string value)
        {
            return seo + value;
        }
    }

 

the action is

<a href=’@Url.ActionEnc(new EncDecSEOBullShit(), “TestEncryptSEO”, new {id=7, a = 1, b = “asd” })’>Test</a>

It generates url such as:

http://mvc5encrypt.apphb.com/Home/TestEncryptSEO/7?a=this-is-seo-1&b=this-is-seo-asd

The Action Filter to decrypt takes the full type name ( with assembly name) of the class that does SEO ( or encrypt / decrypt)

[MVCDecryptFilter(EncDecFullClassName = "MVCEncryptDemo.EncDecSEOBullShit, MVCEncryptDemo")]

Example at

http://mvc5encrypt.apphb.com/