Category: .NET

Create a new exception–add fields and/or properties

This post is not about why we need custom exception (https://blogs.msdn.microsoft.com/jaredpar/2008/10/20/custom-exceptions-when-should-you-create-them/ ) . It is (more a rant ) about a specific  item in best practices in Exceptions( https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions )

It says:

In custom exceptions, provide additional properties as needed

Provide additional properties for an exception (in addition to the custom message string) only when there’s a programmatic scenario where the additional information is useful. For example, the FileNotFoundExceptionprovides the FileName property.

What I want to add : In EVERY exception that you create in code, DEFINE a custom field. It  is useless without !

Why this rant ? In Stankins I have to intercept KeyNotFoundException  and I want to find the Key that was not found ( problem with some dictionary ) to provide it (Yes, it is a flawed design – but this is not the point here) . The problem was the definition:

public class KeyNotFoundException : SystemException, ISerializable
{

public KeyNotFoundException();

public KeyNotFoundException(string message);

public KeyNotFoundException(string message, Exception innerException);

protected KeyNotFoundException(SerializationInfo info, StreamingContext context);
}

See the problem ? No way to find WHAT is the Key that was not found. So I ended up with this code:

name =innerKeyEx.Message;
// The given key 'nameColumn' was not present in the dictionary.
var first=name.IndexOf("'");
var last= name.IndexOf("'",first+1);
name= name.Substring(first+1,last-first-1);

Moral of the post ? Do NOT define a custom Exception without defining a field / property inside!

OpenSource library- publishing

Following the rules at https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/publish-nuget-package

Nr

Recomandation

AOP Roslyn

1

DO publish stable packages and pre-release packages you want community feedback on to NuGet.org.

Done

2

CONSIDER publishing pre-release packages to a MyGet feed from a continuous integration build.

No

3

CONSIDER testing packages in your development environment using a local feed or MyGet. Check the package works then publish it to NuGet.org.

Yes

4

DO use a Microsoft account to sign in to NuGet.

Yes

5

DO enable two-factor authentication for accessing NuGet.

Yes

6

DO enable email notification when a package is published.

Yes

For 1: Done already. However, I do not like pre-release packages

For 2: Too much hassle for a single developer. Using instead NuGet

For 3: Yes , I have already a build.bat to do this

For 4: Yes.(NuGet allows only that now)

For 5 : Yes

For 6: Yes.

OpenSource library–Strong naming

The strong naming is something that I have not have done usually, so it will be interesting following https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/strong-naming .

Nr

Recomandation

AOP Roslyn

1

CONSIDER strong naming your library’s assemblies.

Not –see below

2

CONSIDER adding the strong naming key to your source control system.

easy- not

3

CONSIDER incrementing the assembly version
on only major version changes to help
users reduce binding redirects, and how often they’re updated.

not

4

DO NOT add, remove, or change the strong naming key.

easy- not

5

DO NOT publish strong-named and non-strong-named versions of your library.

easy – not

I have tried to add signing – it is pretty easy in Visual Studio to generate a new pfx fiel to sign code. However, when compiling, it requires all dependencies to be signed

CSC : error CS8002: Referenced assembly ‘PortableConsoleLibs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ does not have a strong name. [C:\projects\aop-with-roslyn\AOPRoslyn\aopCmd\aop.csproj]

So , if one of your referenced libraries is not code signed, it is a big no

The other requirements are pretty easy..

My Async Await tutorials

Rule of thumb: just await / async from top to down.

 

To deeply understand async await in .NET Core , please follow the following resources:

 

1. https://channel9.msdn.com/Events/TechDays/Techdays-2014-the-Netherlands/Async-programming-deep-dive  – to gain inner knowledge about what code is async / await

2. Read https://blog.stephencleary.com/2012/02/async-and-await.html to have started into async await

3. Read MSDN for a better understanding : https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396

4. Common pitfalls in ASP.NET  Framework( not in console! ) with async await: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

5. No problem in ASP.NET Core: https://blog.stephencleary.com/2017/03/aspnetcore-synchronization-context.html

Happy reading !

Connections strings to config

I have the opportunity to work on some pretty old code . There were many projects , all that had a sort of connection string to the database.  This kind of code were in like > 11 places :

string connectionstring = “Data Source=.\\SqlServer;Initial Catalog=myDB;Integrated Security=SSPI;”;

The task was to modify this in something that could read from a config ( json) file . But more than that, was to verify
1.that is called everywhere – because sometimes it were in methods, other time in a static variable and so on .
2. that the json exists or not ( if not return the default string – but log the fact that are not in the settings)

So – how to monitor that your code is actually hit ? One idea is to make breakpoints. Other is to make a class:

So I came up with this class

public class ConnectionSql
{
public ConnectionSql(
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{

Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}
//TODO: if need speed, make this static - or strConnection...
public string ConnectionString()
{
var strConnection = ConfigurationManager.AppSettings["sql"];
if (string.IsNullOrWhiteSpace(strConnection))
{
Console.WriteLine("not found connection sql string in settings , going to default");
strConnection = ".\\SqlServer;Initial Catalog=myDB;Integrated Security=SSPI;";
}
return strConnection;

}

}

 

How we call ?


string connectionstring = new ConnectionSql().ConnectionString();

 

Because of the attributes https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information we can figure by looking at the trace if the code was hit or not and by which line.

Circular references on .NET , Entity Framework and WebAPI

Imagine having a class Department( ID, Name) and Employee ( ID , Name, IDDepartment) . You want to return in the WebAPI the Departments with the Employee . It is simple to wrote this code:


[HttpGet]
public IEnumerable<Department> Get([FromServices] MyTestDatabaseContext context)
{
var departments = context.Department.Include(iterator=>iterator.Employee).ToArray();
return departments;
}

But the problem is with circular references when serializing  : The Department has a list of  Employees  that have a Department that have a list of Employees that …

 

 

Solution 1 :  Delete/Comment from the EF generated code what you do not need ( in this case , the Department reference that the Employee has)


public partial class Employee
{
public int Idemployee { get; set; }
public string NameEmployee { get; set; }
public int Iddepartment { get; set; }

//public Department IddepartmentNavigation { get; set; }
}

Solution 2 : Nullify the reference


[HttpGet]
public IEnumerable<Department> Get([FromServices] MyTestDatabaseContext context)
{
var departments = context.Department.Include(iterator=>iterator.Employee).ToArray();
foreach (var dep in departments)
{
foreach (var emp in dep.Employee)
{
emp.IddepartmentNavigation = null;
}
}
return departments;
}

( maybe this should be add to the partial class of the employee ?)

 

 

Solution 3: Handle circular references in serialization


services
.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

Solution 4: Make a read DDD design. Read https://siderite.dev/2018/08/client-models-vs-data-transfer-objects.html

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);
        }
    }
}

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.