Monday 12 October 2015

This blog has Moved!

This blog hasn't been particularly active for the last couple years also. My apologies. I have started a new blog where I will post my thoughts and opinions on various topic (mainly technical) from now on. I will also rewrite and tidy up some of my old posts from here. Thanks for reading!!!

https://kelvinbytes.wordpress.com

Thursday 7 November 2013

Stub VS Shim VS Mock. Differences between Microsoft.Fake framework and Moq framework.

using System;
using Microsoft.QualityTools.Testing.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MicrosoftFakesFrameworkPlayClassLibrary;
using Moq;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest2
    {
        [TestMethod]
        public void TestUsingStub()
        {
            IStockFeed stockFeed =
                new MicrosoftFakesFrameworkPlayClassLibrary.Fakes.StubIStockFeed()
                {
                    GetSharePriceString = (company) => 7890
                };

            var actual = stockFeed.GetSharePrice("bbc");
        }

        [TestMethod]
        public void TestUsingShim()
        {
            using (ShimsContext.Create())
            {
                System.Fakes.ShimDateTime.NowGet = () => new DateTime(2000, 1, 1);

                var now = DateTime.Now;

                Assert.AreEqual(2000, now.Year);
            }
        }

        // To use Mock, you need to deal with Mock Wrap object. While as MS Fakes framework already generated 
        // Stub and Shim objects for you.
        [TestMethod]
        public void TestUsingMock()
        {
            var mock = new Mock();
            mock.Setup(instance => instance.GetSharePrice(It.IsAny())).Returns(1234);

            //Action
            var actual = mock.Object.GetSharePrice("myCompany");

            //Assert
            Assert.AreEqual(1234, actual);
        }
    }
}
                                                                                                                       

Saturday 2 February 2013

Maybe class for null handling for reference types in C#

"Tony Hoare, the inventor of the null reference, called it his ‘billion dollar mistake.’ Null is a disaster because it means that every reference type has a magic value that will destroy your program. For value types, we now have ‘normal’ value types and nullable value types to express the difference between, say, ‘this function returns an integer’ and ‘this function might return an integer, or it might not.’ But we don’t have that for reference types."
For example, in C# we have int for a normal value and int? for a nullable value. But for reference type like a string, you just don't know until you check it or your program crashed on it.

Ivan Towlson has mentioned a good and compact way to handle nullable type with a Maybe class. Since I don't have his source code, I created my own. If you are interested to use it, just simply download the Maybe.cs class under the MaybeApplication folder from github and use it in your own program. Or you can download the whole project. The project is created in VS2010.

Why should you care? What does the class do?

Ok, instead of doing the null check the old fashion way
        static string DemoTheOldWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            if (contact == null)
                return null;

            if (contact.PersonalPet == null)
                return null;

            if (contact.PersonalPet.PetName == null)
                return null;

            var petName = contact.PersonalPet.PetName.Name;
            if (petName == null)
                return null;

            return petName;
        }

Now with some Linq awesomeness, you can do something like this
        static string DemoTheNewWayOfNullChecking()
        {
            Contact contact = new Contact();
            contact.Name = "Sean";
            contact.Phone = "12345678";
            contact.Age = 24;
            //contact.PersonalPet = new Pet()
            //{
            //    PetName = new PetName()
            //        {
            //            Name = "My Pet"
            //        }
            //};

            // Maybe class allow you to do something similar to int? for the reference types.
            Maybe maybeContact = new Maybe(contact);

            var maybePetName = from c in maybeContact
                          from cp in c.PersonalPet
                          from pn in cp.PetName
                          select pn.Name;

            if (maybePetName.HasValue)
                return maybePetName.Value;

            return null;
        }


Reference:
http://www.mindscapehq.com/blog/index.php/2012/03/27/5-12-f-features-every-c-programmer-should-lust-after/

Tuesday 11 September 2012

Copying files is extremely slow to a vmware vm

Solution 1:

After installing windows 7 64bit on my computer as the host operating system, copying files from the host windows to my linux virtual machine was painfully slow. After some research I found the fix to be :
Going to the control panel -> network and internet-> network connections-> right click on your physical interface -> click on configure-> click on the advanced tab.

In here make sure ipv4 checksum offloader, large send offload v2 ipv4, large send offload v2 ipv6 are disabled.

Solution 2:

You must disable the TCP Offload Engine features in the driver on the host.

To disable the TCP Offload Engine features:
Go to Start > Run.
Type regedit and press Enter.
Browse to:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters


Create a key named DisableTaskOffload, of type DWORD.
Set the value of the key to 1 .
Close the Registry Editor and restart the computer.

Reference:

Wednesday 22 August 2012

VMWare Performance - vmem file cause harddisk IO busy

I am doing my project work on a vmware virtual machine. I normally will let the vm running over night. The issue is that the VM will hang for at least 15 min everyday morning when I wake it up.

After some investigation, I found there are huge read and write activities on the the *.vmem file in the virtual machine folder. It turns out the vmem file is the backup of your VM's memory. If your VM's memory is big, e.g. 8Gb, there will be a lot of writing to backup the memory to harddisk. 


You can change it by ask vmware to save the memory backup in your host machine's RAM. The catch is that there is no GUI to help you therefore you have to manually modify the vmx file. However it is a easy change. All you need to have is the following line in you vmx file.

mainmem.useNamedFile = "FALSE"



Sunday 20 May 2012

Prevent a large textbox from getting put on the second page in SSRS

Problem:
When I render a SSRS report in PDF and the textbox is too big or long, it puts my large textbox on the second page and leave the first page with large empty space.

Solution:
In SSRS 2008 R2 and Visual Studio 2008:
Click (not-right click) a textbox and go to the properties window (lower right side of VS) -> KeepTogether = false.
The text will cleanly cut between a line and continue on the next page.

Reference:
http://stackoverflow.com/questions/3111071/dealing-with-very-tall-textboxes-and-pagination-in-ssrs-2005

Tuesday 15 May 2012

How to disable floating tabs in Visual Studio 2010


Solution1: Ctrl+DoubleClick, it will dock back to its last position.



Solution2: The current version of Productivity Power Tools does support disabling floating tabs: it's in Options > Productivity Power Tools > Document Tab Well > Floating Docs > [ ] Enable floating tab well. Unchecking this stopped all the SSDT Juneau tabs from opening floating for me. ProductivityPower Tools