Category: .NET

Interpreter–part 7 of n–thank you

In our days you cannot build a project without help from other projects.  So it is your project – it is build on the shoulder of others( https://en.wikipedia.org/wiki/Standing_on_the_shoulders_of_giants )

Add a third party notice and thanks others that contributes with components to your project.

I have discovered https://github.com/KrystianKolad/DotnetThx  – and running it gives me that :

—————————————————————
Package:InterpreterDll
Page:https://github.com/ignatandrei/interpreter
Andrei Ignat
—————————————————————
—————————————————————
Package:McMaster.Extensions.CommandLineUtils
Page:https://github.com/natemcmaster/CommandLineUtils
Nate McMaster
—————————————————————
—————————————————————
Package:McMaster.Extensions.CommandLineUtils-max
Page:https://github.com/natemcmaster/CommandLineUtils-max
Nate McMaster
—————————————————————
—————————————————————
Package:McMaster.Extensions.CommandLineUtils-max2
Page:https://github.com/natemcmaster/CommandLineUtils-max2
Nate McMaster
—————————————————————
—————————————————————
Package:CommandLine.Core.CommandLineUtils
Page:https://github.com/mthamil/CommandLine.Core
Matt Hamilton
—————————————————————
—————————————————————
Package:CommandLineUtils.Extensions
Page:https://github.com/mthamil/CommandLine.Core
Matt Hamilton
—————————————————————
—————————————————————
Package:FormatWith
Page:https://github.com/crozone/FormatWith
Ryan Crosby
—————————————————————
—————————————————————
Package:netfx-System.StringFormatWith
Page:http://netfx.codeplex.com/
Henri Wiechers,  Daniel Cazzulino,  kzu,  Clarius
—————————————————————
—————————————————————
Package:netfx-System.StringFormatWith.Tests
Page:http://netfx.codeplex.com/
Henri Wiechers,  Daniel Cazzulino,  kzu,  Clarius
—————————————————————
—————————————————————
Package:DocumentFormat.OpenXml
Page:https://github.com/OfficeDev/Open-XML-SDK
Microsoft
—————————————————————
—————————————————————
Package:ZXing.Net
Page:https://github.com/micjahn/ZXing.Net/
Michael Jahn
—————————————————————
—————————————————————
Package:Google.Protobuf
Page:https://github.com/google/protobuf
Google Inc.
—————————————————————
—————————————————————
Package:ExcelNumberFormat
Page:https://github.com/andersnm/ExcelNumberFormat
ExcelNumberFormat developers
—————————————————————
—————————————————————
Package:NUnit.Runners
Page:http://nunit.org/
Charlie Poole
—————————————————————
—————————————————————
Package:WebP.Touch
Page:https://github.com/molinch/WebP.Touch
Daniel Luberda, Molinet Fabien, Cosmin Gordea
—————————————————————
—————————————————————
Package:BclContrib-ParseFormat
Page:http://code.google.com/p/bclcontrib-parseformat/
Sky Morey
—————————————————————
—————————————————————
Package:SmartFormat.NET
Page:https://github.com/scottrippey/SmartFormat.NET
Scott Rippey,axuno gGmbH,Bernhard Millauer and other contributors.
—————————————————————
—————————————————————
Package:SFD.StringFormat
Page:
AdamSpeight2008
—————————————————————
—————————————————————
Package:ActionMessageFormat
Page:https://github.com/mntone/Data.Amf
mntone and qwerty
—————————————————————
—————————————————————
Package:FileFormatWavefront
Page:https://github.com/dwmkerr/file-format-wavefront
Dave Kerr
—————————————————————
—————————————————————
Package:String.Format.Js
Page:http://mstr.se/sffjs
Daniel Mester Pirttijärvi
—————————————————————
—————————————————————
Package:ActionMessageFormat.UWP
Page:https://github.com/tor4kichi/Data.Amf
tor4kichi
—————————————————————
—————————————————————
Package:jQuery.dateFormat
Page:https://github.com/phstc/jquery-dateFormat
Pablo Cantero
—————————————————————
—————————————————————
Package:TMU-BioTextMining.i2b2.Format
Page:https://sites.google.com/site/hongjiedai/projects/tmuclinicalnet
Hong-Jie Dai
—————————————————————
—————————————————————
Package:Estat.Sri.Ws.Format.Sdmx
Page:https://webgate.ec.europa.eu/CITnet/stash/projects/SDMXRI/repos/nsiws.net/browse
Eurostat
—————————————————————
—————————————————————
Package:FormatProviders
Page:
AdamSpeight2008
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Workspaces
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Scripting
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Features
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Extensions
Page:https://github.com/denis-tsv/Microsoft.CodeAnalysis.CSharp.Extensions
Denis Tsvettsih
—————————————————————
—————————————————————
Package:Sawmill.Microsoft.CodeAnalysis.CSharp
Page:https://github.com/benjamin-hodgson/Sawmill
benjamin.hodgson
—————————————————————
—————————————————————
Package:Forked-MS.CodeAnalysis.CSharp-KeywordAlias
Page:https://github.com/rickardp/roslyn
Rickard
—————————————————————
—————————————————————
Package:Roslynator.CodeFixes
Page:http://github.com/JosefPihrt/Roslynator
Josef Pihrt
—————————————————————
—————————————————————
Package:Roslynator.Analyzers
Page:http://github.com/JosefPihrt/Roslynator
Josef Pihrt
—————————————————————
—————————————————————
Package:Ben.Demystifier-RoslynScriptingCompatibility
Page:https://github.com/molinch/Ben.Demystifier-RoslynScriptingCompatibility
ben_a_adams molinch
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.Compilers
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.Workspaces.MSBuild
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Scripting
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Ben.Demystifier-RoslynScriptingCompatibility
Page:https://github.com/molinch/Ben.Demystifier-RoslynScriptingCompatibility
ben_a_adams molinch
—————————————————————
—————————————————————
Package:Newtonsoft.Json
Page:https://www.newtonsoft.com/json
James Newton-King
—————————————————————
—————————————————————
Package:Amplified.ValueObjects.Newtonsoft.Json
Page:https://github.com/Nillerr/Amplified.ValueObjects
Nicklas Jensen
—————————————————————
—————————————————————
Package:CommonSerializer.Newtonsoft.Json
Page:https://github.com/BrannonKing/CommonSerializer
Brannon King
—————————————————————
—————————————————————
Package:Remote.Linq.Newtonsoft.Json
Page:https://github.com/6bee/Remote.Linq
Christof Senn
—————————————————————
—————————————————————
Package:aqua-core-newtonsoft-json
Page:https://github.com/6bee/aqua-core
Christof Senn
—————————————————————
—————————————————————
Package:RestSharp.Newtonsoft.Json.NetCore
Page:https://github.com/Alterdata/RestSharp.Newtonsoft.Json.NetCore
Bernardo Esbérard
—————————————————————
—————————————————————
Package:Newtonsoft.Json.FSharp
Page:
Henrik Feldt,  Logibit AB
—————————————————————
—————————————————————
Package:Orleans.Serialization.Newtonsoft.Json
Page:https://github.com/OrleansContrib/orleans.serialization.json
Daniel Marbach
—————————————————————
—————————————————————
Package:Newtonsoft.Json.Net20.dll
Page:
wx1983@gmail.com
—————————————————————
—————————————————————
Package:Sfa.Core.Newtonsoft.Json
Page:https://github.com/SkillsFundingAgency/Core
Skills Funding Agency
—————————————————————
—————————————————————
Package:NServiceBus.Newtonsoft.Json
Page:https://docs.particular.net/nuget/NServiceBus.Newtonsoft.Json
Particular Software
—————————————————————
—————————————————————
Package:RestSharp.Newtonsoft.Json
Page:https://github.com/adamfisher/RestSharp.Serializers.Newtonsoft.Json
Adam Fisher
—————————————————————
—————————————————————
Package:Bridge.Newtonsoft.Json
Page:https://github.com/bridgedotnet/Bridge.Newtonsoft.Json
Object.NET, Inc.
—————————————————————
—————————————————————
Package:Gu.SerializationAsserts.Newtonsoft.Json
Page:https://github.com/JohanLarsson/Gu.SerializationAsserts
Johan Larsson
—————————————————————
—————————————————————
Package:RestSharp.Newtonsoft.Json.Extensions
Page:https://github.com/i4004/RestSharp.Newtonsoft.Json.Extensions
Alexander Krylkov
—————————————————————
—————————————————————
Package:Mgazza.Newtonsoft.Json
Page:https://github.com/mgazza/Optional
Mark Gascoyne
—————————————————————
—————————————————————
Package:Newtonsoft.Json.Akshay
Page:
Akshay.Shingnapurkar, Yuvraj.Repe
—————————————————————
—————————————————————
Package:WampSharp.NewtonsoftJson
Page:https://wampsharp.net/
CodeSharp
—————————————————————
—————————————————————
Package:Pollock.Newtonsoft.Json
Page:
wallymathieu
—————————————————————
—————————————————————
Package:Lykke.WampSharp.NewtonsoftJson
Page:https://github.com/LykkeCity/WampSharp
Lykke
—————————————————————
—————————————————————
Package:coverlet.msbuild
Page:http://github.com/tonerdo/coverlet
tonerdo
—————————————————————
—————————————————————
Package:Microsoft.CodeAnalysis.CSharp.Scripting
Page:https://github.com/dotnet/roslyn
Microsoft
—————————————————————
—————————————————————
Package:Ben.Demystifier-RoslynScriptingCompatibility
Page:https://github.com/molinch/Ben.Demystifier-RoslynScriptingCompatibility
ben_a_adams molinch
—————————————————————
—————————————————————
Package:Microsoft.NET.Test.Sdk
Page:https://github.com/microsoft/vstest/
Microsoft
—————————————————————
—————————————————————
Package:MSTest.TestAdapter
Page:https://github.com/microsoft/testfx
Microsoft
—————————————————————
—————————————————————
Package:MSTestX.TestAdapter
Page:https://github.com/dotMorten/MSTestX
Morten Nielsen
—————————————————————
—————————————————————
Package:MSTest.TestFramework
Page:https://github.com/microsoft/testfx
Microsoft
—————————————————————
—————————————————————
Package:MSTest.TestFramework
Page:https://github.com/microsoft/testfx
Microsoft
—————————————————————
—————————————————————
Package:MSTest.TestAdapter
Page:https://github.com/microsoft/testfx
Microsoft
—————————————————————
—————————————————————
Package:Microsoft.UnitTestFramework.Extensions
Page:https://github.com/Microsoft/mstest-extensions/wiki
Microsoft
—————————————————————
—————————————————————
Package:Nosnitor.TestFramework.Extensions.MSTest
Page:
Nosnitor Corporation
—————————————————————
—————————————————————
Package:Riganti.Selenium.MSTest2Integration
Page:https://github.com/riganti/selenium-utils
Ladislav Šesták
—————————————————————
—————————————————————
Package:LightBDD.MsTest2
Page:https://github.com/LightBDD/LightBDD
Wojciech Kotlarski
—————————————————————
—————————————————————
Package:Shouldly
Page:http://shouldly.github.com/
Shouldly
—————————————————————
—————————————————————
Package:Sollertes.Bdd.Shouldly
Page:https://github.com/Sollertes/BDD
Marcin Markowski
—————————————————————
—————————————————————
Package:ITLibrium.BDD.Shouldly
Page:https://github.com/itlibrium/BDD
Marcin Markowski
—————————————————————
—————————————————————
Package:Mgazza.Shouldly
Page:https://github.com/mgazza/Optional
Mark Gascoyne
—————————————————————
—————————————————————
Package:Shouldly.EqualityExtensions
Page:https://github.com/whortleberrybearer/shouldly
whortleberrybearer
—————————————————————
—————————————————————
Package:NorwegianShouldly
Page:https://github.com/eaardal/norwegian-shouldly
This language wrapper: Eirik Årdal. Original Shouldly: See Shouldly NuGet package

A whole bunch of packages – that it is clear that helps my project.

Obtain data from ANAF(local IRS)

In ROmania ANAF is providing a WebAPI that allows access to some of the information that any enterprise should provide. The API is described at https://webservicesp.anaf.ro/PlatitorTvaRest/api/v3/ ,

I have made a C# console and a node,js script.

The differences:

1.it is easiear in node to make an http request.

2. For C# – I think in components. For node – I think in application.

Without further ado, here is the code

using RestSharp;
using System;

namespace InfoAnafWS
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length == 0)
            {
                Console.WriteLine("lipseste CUI ");
                return;
            }
            var client = new RestClient("https://webservicesp.anaf.ro");
            var request = new RestRequest("PlatitorTvaRest/api/v3/ws/tva", Method.POST);
            request.RequestFormat = DataFormat.Json;
            string date = DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd");
            string req = "";
            foreach (var item in args)
            {
                req += string.Format(@"{{'cui': {1}, 'data':'{0}'}}", date, item);
            }
            req = req.Replace("'", "\"");
            req = "[" + req + "]";

            request.AddParameter("application/json", req, ParameterType.RequestBody);
            var data = client.Execute(request);
            Console.WriteLine(data.Content);
        }
    }
}


const start = async () => {
	
let request = require('request');

const d = require('delay');
await d(1000);
var fs = require('fs');
var array = fs.readFileSync('CUI.txt').toString().split("\r\n");
var start=0;

while(start<array.length){
	var nr = Math.floor(Math.random() * Math.floor(495))+1;
	var jsonData=[];
	console.log('start at ' + start+  ' for nr ' + nr);
	var i=0;
	while(i<nr && i+start<array.length){
		jsonData.push({"cui": array[i+start], "data":"2018-03-26"});
		i++;		
	}
	
	var finish=false;
	console.log('array :'+ jsonData[0].cui + '-'  + jsonData[nr-1].cui);
	request.post(
    'https://webservicesp.anaf.ro/PlatitorTvaRest/api/v3/ws/tva',
    { json: jsonData },
    function (error, response, body) {
		finish=true;
        if (!error && response.statusCode == 200) {
            console.log(body);
			fs.writeFile(`text${start}_${nr}.json`, JSON.stringify( body ), function (err) {
            if (err) {
                return console.log(err);
            }

            console.log("The file was saved!" + `text${start}_${nr}.json`);
        });

        }
    });
	start = start+nr;
	while(!finish){
		console.log('waiting');
		await d(13000);
	}
	

}
console.log('finish')
return;

}
start();

Tiny Types–documentation–part 4

This is a series

  1. http://msprogrammer.serviciipeweb.ro/2018/03/12/tiny-types-in-cpart-1/
  2. http://msprogrammer.serviciipeweb.ro/2018/03/19/tiny-types-part-2adding-iequatable/
  3. http://msprogrammer.serviciipeweb.ro/2018/03/26/tinytypesadding-equality-operatorpart-3/

  4. http://msprogrammer.serviciipeweb.ro/2018/04/02/tiny-typesdocumentationpart-4/

tiny types in C#

Build Status

Tiny types is a NuGET dll , https://www.nuget.org/packages/TinyTypesObjects/

Also , you can find the construction here: http://msprogrammer.serviciipeweb.ro/category/tinytypes/

The documentation is copied shameless from https://github.com/jan-molak/tiny-types

Installation

To install the module from nuget : … Install-Package TinyTypesObjects …

Defining Tiny Types

An int on its own is just a scalar with no meaning. With an object, even a small one, you are giving both the compiler and the programmer additional information about what the value is and why it is being used.

Jeff Bay, Object Calisthenics

Single-value types

To define a single-value TinyType – extend from TinyType<T>() :

If you want operator ==, please use TinyTypeOf or TinyTypeOfString

using TinyTypesObjects;

public class Age : TinyTypeOf<int>
    {
        public Age(int nr) : base(nr)
        {

        }
    }
public class FirstName : TinyTypeOfString
    {
        public FirstName(string str) : base(str)
        {

        }
    }

Every tiny type defined this way has a get property value of type T, which you can use to access the wrapped primitive value. For example:

var firstName = new FirstName("Jan");

Assert.AreEqual(firstName.value , "Jan");
        
Converting from / to original values

There are defined conversions between type T and the class

public void TestConvert()
        {
            string s = "http://msprogrammer.serviciipeweb.ro";
            TinyTypeOfString tt = s;
            Assert.AreEqual(s, (string)tt);

            int nr = 7;
            TinyTypeOf<int> tt1 = nr;

            Assert.AreEqual(nr, (int)tt1);
        }
        

so the following code should work for the class with constructor string

class TestConstructor
    {
        public TestConstructor(string firstName)
        {
            FirstName = firstName;
        }

        public string FirstName { get; }
    }
[TestMethod]
        public void TestConstructor()
        {
            var firstName = new FirstName("Jan");
            TestConstructor tc = new TestConstructor(firstName);
            Assert.AreEqual(tc.FirstName, "Jan");
        }
Equals or ==

Each tiny type object has an equals method, which you can use to compare it by value:

int nr = 7;
            TinyTypeOf<int> tt1 = nr;
            TinyTypeOf<int> tt2 = nr;

            Assert.AreEqual(tt1, tt2);
            Assert.IsTrue(tt1 == tt2);
Links:

GitHub: https://github.com/ignatandrei/tinyTypes

Blog About: http://msprogrammer.serviciipeweb.ro/category/tinytypes/

NuGet: https://www.nuget.org/packages/TinyTypesObjects/

TinyTypes–adding equality operator–part 3

This is a series

  1. http://msprogrammer.serviciipeweb.ro/2018/03/12/tiny-types-in-cpart-1/
  2. http://msprogrammer.serviciipeweb.ro/2018/03/19/tiny-types-part-2adding-iequatable/
  3. http://msprogrammer.serviciipeweb.ro/2018/03/26/tinytypesadding-equality-operatorpart-3/

  4. http://msprogrammer.serviciipeweb.ro/2018/04/02/tiny-typesdocumentationpart-4/

Now we have come to the interesting part – the equality operator.

We have already operator equals, but not ==

 [TestMethod]
        public void TestSimpleIntOperatorEqual()
        {
            int nr = 7;
            TinyType<int> tt1 = nr;
            TinyType<int> tt2 = nr;

            Assert.AreEqual(tt1, tt2);
            Assert.IsFalse(tt1 == tt2);
        }
        [TestMethod]
        public void TestSimpleStringOperatorEqual()
        {
            string s = "http://msprogrammer.serviciipeweb.ro";
            TinyType<string> tt1 = s;
            TinyType<string> tt2 = s;

            Assert.AreEqual(tt1, tt2);
            Assert.IsFalse(tt1 == tt2);
        }

Because we primary want this TinyTypes for structs( int, double, bool, and so on) we can define a new class

For this we could implement operator  ==  by using the Equals operator ( because we KNOW that a struct cannot be null)

public class TinyTypeOf<T> : TinyType<T>
        where T:struct
    {
        public TinyTypeOf(T tDef) : base(tDef)
        {
        }

       
        public static bool operator ==(TinyTypeOf<T> lhs, TinyTypeOf<T> rhs)
        {
            if(lhs is null)
            {
                return rhs is null;
            }
            return lhs.t.Equals(rhs.t);
        }

        public static bool operator !=(TinyTypeOf<T> lhs, TinyTypeOf<T> rhs)
        {
            return !(lhs==rhs);
        }

CODE FOR NEW CLASS

Also, because the string is not a struct, but a class, we need for String also:

public class TinyTypeOfString : TinyType<string>

And the tests are

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TinyTypesObjects;

namespace TinyTypesTest
{
    [TestClass]
    public class TestTinyTypeOperatorEqual
    {
        [TestMethod]
        public void TestSimpleIntOperatorEqual()
        {
            int nr = 7;
            TinyType<int> tt1 = nr;
            TinyType<int> tt2 = nr;

            Assert.AreEqual(tt1, tt2);
            Assert.IsFalse(tt1 == tt2);
        }
        [TestMethod]
        public void TestSimpleStringOperatorEqual()
        {
            string s = "http://msprogrammer.serviciipeweb.ro";
            TinyType<string> tt1 = s;
            TinyType<string> tt2 = s;

            Assert.AreEqual(tt1, tt2);
            Assert.IsFalse(tt1 == tt2);
        }
        [TestMethod]
        public void TestSimpleStringOperatorEqualWorks()
        {
            string s = "http://msprogrammer.serviciipeweb.ro";

            TinyTypeOfString tt1 = s;
            TinyTypeOfString tt2 = s;

            Assert.AreEqual(tt1, tt2);
            Assert.IsTrue(tt1 == tt2);
        }

        [TestMethod]
        public void TestSimpleIntOperatorEqualWorks()
        {
            int nr = 7;
            TinyTypeOf<int> tt1 = nr;
            TinyTypeOf<int> tt2 = nr;

            Assert.AreEqual(tt1, tt2);
            Assert.IsTrue(tt1 == tt2);
        }
        [TestMethod]
        public void TestSimpleIntNrOperatorEqualWorks()
        {
            int nr = 7;
            TinyType<int> tt1 = nr;
            
            Assert.AreEqual(tt1, nr);
            Assert.IsTrue(tt1 == nr);
        }
    }
}

Tiny Types part 2–adding IEquatable

This is a series

  1. http://msprogrammer.serviciipeweb.ro/2018/03/12/tiny-types-in-cpart-1/
  2. http://msprogrammer.serviciipeweb.ro/2018/03/19/tiny-types-part-2adding-iequatable/
  3. http://msprogrammer.serviciipeweb.ro/2018/03/26/tinytypesadding-equality-operatorpart-3/

  4. http://msprogrammer.serviciipeweb.ro/2018/04/02/tiny-typesdocumentationpart-4/

As always, the bigger problem is adding equality. The Tiny Type should be equal with the inner value – and with the other type with the same value. And, in C#, when you implement equality , there is a whole theory – see https://msdn.microsoft.com/en-us/library/336aedhh(v=vs.100).aspx .

So the code to define equality is 60 lines long just for defining equality for

public class TinyType<T>:IEquatable<T>, IEquatable<TinyType<T>>

   

But this is not all. This is the code for testing equality

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TinyTypesObjects;

namespace TinyTypesTest
{
    [TestClass]
    public class TestTinyTypeEquals
    {
        [TestMethod]
        public void TestSimpleStringEquals()
        {
            #region arrange
            string s = "http://msprogrammer.serviciipeweb.ro";
            #endregion
            #region act
            TinyType<string> tt1 = s;
            TinyType<string> tt2 = s;
            #endregion
            #region assert
            Assert.IsTrue(tt1.Equals(tt2));
            Assert.AreEqual(tt1, tt2);
            Assert.AreEqual<string>(tt1, tt2);
            Assert.AreEqual<TinyType<string>>(tt1, tt2);
            Assert.AreEqual<string>(s, tt2);
            #endregion
        }
        [TestMethod]
        public void TestSimpleStringWithNull()
        {
            #region arrange
            string s = null;
            #endregion
            #region act
            TinyType<string> tt1 = s;
            TinyType<string> tt2 = null;
            #endregion
            #region assert
            Assert.IsFalse(tt1.Equals(tt2));
            Assert.AreNotEqual(tt1, tt2);
            Assert.AreEqual<string>(tt1, tt2);
            Assert.AreNotEqual<TinyType<string>>(tt1, tt2);
            Assert.AreEqual<string>(s, tt2);
            #endregion
        }
        [TestMethod]
        public void TestSimpleStringNull()
        {
            #region arrange
            string s = null;
            #endregion
            #region act
            TinyType<string> tt1 = s;
            TinyType<string> tt2 = s;
            #endregion
            #region assert
            Assert.IsTrue(tt1.Equals(tt2));
            Assert.AreEqual(tt1, tt2);
            Assert.AreEqual<string>(tt1, tt2);
            Assert.AreEqual<TinyType<string>>(tt1, tt2);
            Assert.AreEqual<string>(s, tt2);
            #endregion
        }
        [TestMethod]
        public void TestSimpleIntEquals()
        {
            #region arrange
            int s = 1;
            #endregion
            #region act
            TinyType<int> tt1 = s;
            TinyType<int> tt2 = s;
            #endregion
            #region assert
            Assert.IsTrue(tt1.Equals(tt2));
            Assert.AreEqual(tt1, tt2);
            Assert.AreEqual<int>(tt1, tt2);
            Assert.AreEqual<TinyType<int>>(tt1, tt2);
            Assert.AreEqual<int>(s, tt2);
            
            #endregion
        }

    }
}

Tiny types in C#–part 1

This is a series

  1. http://msprogrammer.serviciipeweb.ro/2018/03/12/tiny-types-in-cpart-1/
  2. http://msprogrammer.serviciipeweb.ro/2018/03/19/tiny-types-part-2adding-iequatable/
  3. http://msprogrammer.serviciipeweb.ro/2018/03/26/tinytypesadding-equality-operatorpart-3/

  4. http://msprogrammer.serviciipeweb.ro/2018/04/02/tiny-typesdocumentationpart-4/

I have read about tiny types in Javascript  – at https://darrenhobbs.com/2007/04/11/tiny-types/ and at https://janmolak.com/tiny-types-in-typescript-4680177f026e . It was an interesting idea – especially in this world of REST API .

I decided to make the same on C# – so here it is: https://github.com/ignatandrei/tinyTypes and at https://www.nuget.org/packages/TinyTypesObjects .

For the moment , the tests are minimal – just to get working :

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TinyTypesObjects;

namespace TinyTypesTest
{
    [TestClass]
    public class TestTinyType
    {
        [TestMethod]
        public void TestConvert()
        {
            string s = "http://msprogrammer.serviciipeweb.ro";
            TinyType<string> tt = s;
            Assert.AreEqual(s, (string)tt);
        }
        [TestMethod]
        public void TestBehaviour()
        {
            #region arrange + act
            Author a1 = new Author("andrei", "ignat");
            Author a2 = new Author(
                new FirstName("andrei"),new LastName( "ignat"));

            Author a3 = new Author(firstName:"andrei",lastName: "ignat");
            #endregion
            #region assert
            Assert.AreEqual(a2.FullName(), a3.FullName());
            Assert.AreEqual(a2.FullName(), a3.FullName());
            #endregion

        }
    }
}

Next time , I will do IComparable / Iequatable /Equals and others

Making any call to a function of an object thread safe

 

 

I was wondering how to modify old code to support threads /task .

So I was making a small project about making any function of an object thread safe.

NuGet Package at : https://www.nuget.org/packages/ThreadSafeObject/

The solution contain tests and 2 console project for .NET Core and .NET Framework 4.5.1

The usage is pretty easy

Let’s say you have this

Calculation c = new Calculation();
c.Add();

And you want

c.Add

to be thread safe

In the Package Manager Console write:

Install-Package ThreadSafeObject

Then modify your code to:

Calculation c = new Calculation();
dynamic ts = new ThreadSafe(c);
ts.Add();

It is a toy project- however, it has tests to prove it is correct.

You can download the code source from https://github.com/ignatandrei/ThreadSafeObject/

Task and generic list

Could you spot the problem in transforming one code that is sequential into a Task ( threaded ) one?

First , this is the sequential one:

 

            var ret = new List<Indicator>();
            var jsonData = await data.JsonData();
            var jo = JArray.Parse(jsonData);
            var page = jo[0].ToObject<Pagination>();
            var array = jo[1].ToObject<Indicator[]>();
            ret.AddRange(array);
            var currentPage = 1;
            while (currentPage < page.pages)
            {
                currentPage++;
                jsonData = await data.JsonData(currentPage);
                jo = JArray.Parse(jsonData);
                array = jo[1].ToObject<Indicator[]>();
                ret.AddRange(array);

            }
            Debug.Assert(ret.Count == page.total, $"{nameof(ret.Count)} : {ret.Count} should be equal {nameof(page.total)} : {page.total}");
            return ret.ToArray();


 

This is the modified with task:

 

            var jsonData = await data.JsonData();
            var jo = JArray.Parse(jsonData);
            var page = jo[0].ToObject<Pagination>();
            var array = jo[1].ToObject<Indicator[]>();
            var ret = new List<Indicator>(array);

            var currentPage = 1;
            var downloads = new List<Task>();
            while (currentPage < page.pages)
            {
                currentPage++;
                var itemPage = currentPage;
                var task = data.JsonData(itemPage)
                    .ContinueWith(it =>
                    {
                        var data = JArray.Parse(it.Result);
                        var pageNr = data[0].ToObject<Pagination>();
                        var indicators = data[1].ToObject<Indicator[]>();
                       ret.AddRange(indicators);

                    }
                );

                downloads.Add(task);
            }
            await Task.WhenAll(downloads);
            Debug.Assert(ret.Count == page.total, $"{nameof(ret.Count)} : {ret.Count} should be equal {nameof(page.total)} : {page.total}");
            return ret.ToArray();

Ok. If you do not know , here is a helper:
Sometimes, at line

 ret.AddRange(indicators)

it gives the error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.ArgumentException: Source array was not long enough. Check srcIndex and length, and the array’s lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.List`1.set_Capacity(Int32 value)
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)

Ok. I let you think.
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..
…..

The answer is simple : List < T > is not Thread safe. If you have small quantities of data, does not matter. I was having 16174 records – and sometimes , when multiple threads have come to .AddRange , the internal re-dimensioning of array it does not fulfill the task ( imagine one thread redim to 100, then other 3 threads coming with request of 50+ data)

Solution 1.

Use ConcurrentBag< T > instead of List < T > . This is thread safe and does the trick

Solution 2.
Add to the array in the final of data ( not use ContinueWith) . The code is slightly low performant

           var jsonData = await data.JsonData();
            var jo = JArray.Parse(jsonData);
            var page = jo[0].ToObject<Pagination>();
            var array = jo[1].ToObject<Indicator[]>();
            var ret = new List<Indicator>(array);

            var currentPage = 1;
            var downloads = new List<Task<string>>();
            while (currentPage < page.pages)
            {
                currentPage++;
                var itemPage = currentPage;
                var task = data.JsonData(itemPage);
                downloads.Add(task);
            }
            await Task.WhenAll(downloads);

            foreach (var t in downloads)
            {
                var data = JArray.Parse(t.Result);
                //var pageNr = data[0].ToObject<Pagination>();
                var indicators = data[1].ToObject<Indicator[]>();
                //Console.WriteLine($"reading {pageNr.page} with {item.Length}");
                ret.AddRange(indicators);

            }
            //Console.WriteLine($"total records {ret.Count}");
            Debug.Assert(ret.Count == page.total, $"{nameof(ret.Count)} : {ret.Count} should be equal {nameof(page.total)} : {page.total}");
            return ret.ToArray();

You will find the code at IndicatorRepository in https://github.com/ignatandrei/WorldBankAPi

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

Five common mistakes for ASP.NET MVC accesing resources : css, js, images, ajax

This is a Razor /MVC5 variant of older post
http://msprogrammer.serviciipeweb.ro/2010/10/09/five-common-mistakes-for-asp-net-mvc-accesing-resources-css-js-images-ajax/

To have once for all the link to show to people, because too much makes the same error again and again. (From here – you can use ResolveUrl or Url.Content – it’s the same for me. I use ResolveUrl because I used first …)

Case 1 The image does not display

Please check you have the following :

 <img src="@Url.Content("~/Content/images/YOURIMAGE.jpg" )" alt="image" style="border:0" /> or 
 
<img src="~/Content/images/YOURIMAGE.jpg" alt="image" style="border:0" /> 

Case 2 The css does not show

Please check you have the following :

 @string.Format("<link href="{0}" type="text/css" rel="stylesheet" />", Url.Content("~/Content/your.css"))  

or

 <style type="text/css">
@import '@Url.Content("~/Content/your.css")';

</style>  
  <style type="text/css">
<link href='~/Content/your.css' type='text/css' rel='stylesheet' />

</style>  

Case 3 The js does not execute (undefined error)

3.1 Please check you have the following :

 <script type="text/javascript" src="@Url.Content("~/Scripts/yourjs.js")"></script> 

or

 <script type="text/javascript" src="~/Scripts/yourjs.js"></script> 

This should be either in _Layout.cshtml, or in the scripts section

 @section scripts {   } 

in your .cshtml file.
3.2 please check in browser console ( press F12 ) for any javascript error that code may have

Case 4 The js does execute in .cshtml page, but did not execute under js file

Please check you DO NOT have the following @something in the js file. The js file is not interpreted by the same engine as cshtml, so can not interpret asp.net tags. Instead , add a parameter to your function for each @ variable that you have. Simple example : Let’s say in cshtml you have :

 <script type=”text/javascript”>

function Generate(){

window.open('@Url.Action(“About”)’);
}

</script> 

and you call Generate() from .cshtml file. Now you want the same in a .js file. The function Generate will have a parameter instead of Razor variable

 function Generate(url){  window.open(url); } 

and call like this from .cshtml file:

 Generate('@Url.Action("About")'); 

Case 5 The ajax request gives you a 404 error.

Please ensure that you have

 @Url.Content("~/path”) 

and not

 /path 

when you call the Ajax URL

Bonus 1: T4MVC , http://mvccontrib.codeplex.com/releases

Bonus 2: Edit the project file and put <MvcBuildViews>true</MvcBuildViews>

( short url: http://bit.ly/asp5Mistakes)

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.