Category: tinyTypes

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

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.