Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, May 18, 2012
( NTP )

image

To get to this screen, press the icon on NTP Status tab.

needs port UDP 123 opened inbound

http://serverfault.com/questions/390061/windows-7-as-ntp-time-server

useful to sync up local servers clock with the ntpd service ie clock, internet time, point to localhost.

Comments [0] | | # 
# Thursday, May 17, 2012
( TekPub )

Any time you instantiate or reference a concrete type within a class, you take a Dependency and make your code harder to test.

public Licence(Instant expirey, IClock clock)
        {
            //variables or local variables
            this.expirey = expirey;
            this.clock = clock;
        }

we are injecting a dependency here.  More loosely bound here than


public Licence(Instant expirey)
{
    //variables or local variables
    this.expirey = expirey;
    this.clock = SystemClock.Instance;
}

tightly bound to a concrete implementation here.

We made another class here called Diary which uses a clock too:

//calendaring application which will want to know what time it is too!
    class Diary
    {
        private readonly LocalDatePattern outputPattern = LocalDatePattern.CreateWithInvariantInfo("yyyy-MM-dd");
        //fields
        private readonly IClock clock;
        private readonly CalendarSystem calendar;
        private readonly DateTimeZone timeZone;

        //by demanding the dependencies in the constructor, classes are more flexible
        //especially if they are interfacess
        public Diary(IClock clock, CalendarSystem calendar, DateTimeZone timeZone)
        {
            this.clock = clock;
            this.calendar = calendar;
            this.timeZone = timeZone;
        }

        public string FormatToday()
        {
            //in system default timezone, and system default calendar
            //DateTime dateTime = DateTime.Today;
            LocalDate date = clock.Now.InZone(timeZone, calendar).LocalDateTime.Date;
            if (date.Month == 4)
            {  //ncrunch showing black here - so tests are not covering this codepath
                return "April fool";
            }
            return outputPattern.Format(date);
        }
    }

 

//still a class lib so can't run it.. demonstrates concept
    public class Program
    {
        static void Main()
        {
            //manual dependency injection - this takes a lot of time and code to do
            //so people invented dependency injection frameworks aka Inversion of Control.
            IClock clock = SystemClock.Instance;

            Licence licence = new Licence(Instant.UnixEpoch, clock);
            Diary diary = new Diary(clock,CalendarSystem.Iso, DateTimeZone.GetSystemDefault());

            DiaryPresenter presenter = new DiaryPresenter(diary, licence);
        }
    }

    class DiaryPresenter
    {
        public DiaryPresenter(Diary diary, Licence licence)
        {
        }
    }

    class Licence
    {
        private readonly Instant expirey;
        private readonly IClock clock;

        public Licence(Instant expirey, IClock clock)
        {
            //variables or local variables
            this.expirey = expirey;
            this.clock = clock;
        }

        //property
        public bool HasExpired
        {
            get { return clock.Now >= expirey; }
        }
    }

    //calendaring application which will want to know what time it is too!
    class Diary
    {
        private readonly LocalDatePattern outputPattern = LocalDatePattern.CreateWithInvariantInfo("yyyy-MM-dd");
        //fields
        private readonly IClock clock;
        private readonly CalendarSystem calendar;
        private readonly DateTimeZone timeZone;

        //by demanding the dependencies in the constructor, classes are more flexible
        //especially if they are interfacess
        public Diary(IClock clock, CalendarSystem calendar, DateTimeZone timeZone)
        {
            this.clock = clock;
            this.calendar = calendar;
            this.timeZone = timeZone;
        }

        public string FormatToday()
        {
            //in system default timezone, and system default calendar
            //DateTime dateTime = DateTime.Today;
            LocalDate date = clock.Now.InZone(timeZone, calendar).LocalDateTime.Date;
            if (date.Month == 4)
            {  //ncrunch showing black here - so tests are not covering this codepath
                return "April fool";
            }
            return outputPattern.Format(date);
        }
    }

Manually doing dependency injection.  Setting up each component explicitly.  Each is loosely coupled

Licences don’t know about Diaries

Diaries don’t know about Licences..

DiaryPresenter knows about diary and licence, but not about a clock or timezone directly.

notice SystemClock.Instance is a singleton.

can get very hard to maintain

Comments [0] | | # 

Why are interfaces important?

not a pattern.. a language feature.

time zone changes..

We want to give the current Date and Time as a dependency.. so can test properly eg

what if our licence never expires in following 5 years.. want to inject what would happen in the future,  and using DateTime.Now in our licence code means can’t test that.

[TestFixture]
  public class LicenceTests
  {
      [Test]
      public void HasExpired_AnExpiredDate_ReturnFalse()
      {
          Licence licence = new Licence(new DateTime(2000,1,1,0,0,0));
          Assert.IsTrue(licence.HasExpired);
      }

      [Test]
      public void HasExpired_AFutureDate_ReturnTrue()
      {
          Licence licence = new Licence(new DateTime(2020, 1, 1, 0, 0, 0));
          Assert.IsFalse(licence.HasExpired);
      }
  }

  class Licence
  {
      //field
      private readonly DateTime expirey;

      public Licence(DateTime expirey)
      {
          //expirey is a variable or local variable
          this.expirey = expirey;
      }

      //property
      public bool HasExpired
      {
          //a dependency on a static method
          //nothing we can do to make it behave differtly in tests  
          get { return DateTime.UtcNow > expirey; }
      }
  }

 

StubClock and SystemClock both implement IClock

public interface IClock
    {
        /// <summary>
        /// Gets the current <see cref="Instant"/> on the time line according to this clock.
        /// </summary>
        Instant Now { get; }
    }

so thats how this code uses interfaces.. for testing we’ve got a StubClock that we can set the time on.  For production we pass in the actual time.

[TestFixture]
public class LicenceTests
{
    [Test]
    public void HasExpired_AnExpiredDate_ReturnTrue()
    {
        Instant expirey = Instant.FromUtc(2000, 1, 1, 0, 0, 0);
        StubClock clock = new StubClock(expirey + Duration.FromTicks(1));

        //injecting the dependency
        Licence licence = new Licence(expirey, clock);

        Assert.IsTrue(licence.HasExpired);
    }

    [Test]
    public void HasExpired_AFutureDate_ReturnTrue()
    {
        Instant expirey = Instant.FromUtc(2000, 1, 1, 0, 0, 0);
        StubClock clock = new StubClock(expirey - Duration.FromTicks(1));

        Licence licence = new Licence(expirey, clock);

        Assert.IsFalse(licence.HasExpired);
    }

    [Test]
    public void HasExpired_AtExactInstant_ReturnTrue()
    {
        Instant expirey = Instant.FromUtc(2000, 1, 1, 0, 0, 0);
        StubClock clock = new StubClock(expirey);

        Licence licence = new Licence(expirey, clock);

        Assert.IsTrue(licence.HasExpired);
    }

    [Test]
    public void HasExpired_NonExpiredLicenceBecomesExpires_ReturnTrue()
    {
        Instant expirey = Instant.FromUtc(2000, 1, 1, 0, 0, 0);
        StubClock clock = new StubClock(expirey - Duration.FromTicks(1));

        Licence licence = new Licence(expirey, clock);

        Assert.IsFalse(licence.HasExpired);
        clock.AdvanceTicks(1);
        Assert.IsTrue(licence.HasExpired);
    }
}

class Licence
{
    private readonly Instant expirey;
    private readonly IClock clock;

    public Licence(Instant expirey, IClock clock)
    {
        //variables or local variables
        this.expirey = expirey;
        this.clock = clock;
    }

    //property
    public bool HasExpired
    {
        get { return clock.Now >= expirey; }
    }
}

public class BigApplicatiion
{
    static void Main ()
    {
        //a natural singleton!  there is only 1 system clock
        //but there is no way to test this business code...want dependency injection
        Licence licence = new Licence(Instant.FromUtc(2012,4,19,0,0), SystemClock.Instance);
        if (licence.HasExpired)
        {
            Console.WriteLine("Licence expired");
            return;
        }
    }
}
Comments [0] | | # 
( NCrunch | Testing )

http://www.ncrunch.net/download.htm

image

Just sits there and runs any test that I change.

Comments [0] | | # 
( Patterns | TekPub )

Singleton: A class which only allows a single instance

Very controversial in general.

NodaTime – Jons open source library, which implements good practices

namespace Sandbox
{
    public class Singleton
    {
        public void DoSomething()
        {
        }
    }

    public class SingletonClient
    {
        public void UseSingleton()
        {
            Singleton s1 = new Singleton();
            s1.DoSomething();
            Singleton s2 = new Singleton();
            s2.DoSomething();
        }
    }
}

Ahh – can make 2 of the singleton class!

public static class Singleton
    {
        public static void DoSomething()
        {
        }
    }

    public class SingletonClient
    {
        public void UseSingleton()
        {
            //Singleton s1 = new Singleton();
            //s1.DoSomething();
            //Singleton s2 = new Singleton();
            //s2.DoSomething();
            Singleton.DoSomething();
        }
    }

but want to instantiate class.. just not have 2 possible

public class Singleton
{
    //global state - bad
    private static Singleton instance;

    private Singleton()
    {
    }

    public static Singleton CreateInstance()
    {
        if (instance == null)
        {
            instance = new Singleton();
        }
        return instance;
    }

    //raison d'etre for the class
    public void DoSomething()
    {
    }
}

[TestFixture]
public class SingletonClient
{
    [Test]
    public void UseSingleton()
    {
        Singleton s1 = Singleton.CreateInstance();
        Singleton s2 = Singleton.CreateInstance();
        Assert.AreSame(s1,s2);
    }
}

However if on multiple threads this could be bad as both could get to the new Singleton() line at the same time.

mutex locks..complex

public class Singleton
{
    //clr manages locking, mutex and volatile stuff
    private static readonly Singleton instance = new Singleton();

    //empty static constructor - chaanges when the clr is allowed to initialise this...
    //will only initialise before it is first used.
    static Singleton() {}

    private Singleton()
    {
        //stuff that must only happen once
        Console.WriteLine("Singleton constructor");
    }

    public static Singleton Instance { get { return instance; } }

    public static void SayHi()
    {
        Console.WriteLine("hi");
    }

    //raison d'etre for the class
    public void DoSomething()
    {
        // This must be thread-safe... it can be got at from multiple threads... thats its purpose
    }
}

[TestFixture]
public class SingletonClient
{
    [Test]
    public void UseSingleton()
    {
        Singleton.SayHi();
        Console.WriteLine("Start of test");
        Singleton s1 = Singleton.Instance;
        Singleton s2 = Singleton.Instance;
        Assert.AreSame(s1,s2);
    }
}

asdf

image

but say we wanted real laziness ie only creating the singleton when needed:

public class Singleton
    {
        //nested class
        private static class SingletonHolder
        {
            internal static readonly Singleton instance = new Singleton();
            //empty static constructor - forces laziness!
            static SingletonHolder() {}
        }

        private Singleton()
        {
            //stuff that must only happen once
            Console.WriteLine("Singleton constructor");
        }

        public static Singleton Instance { get { return SingletonHolder.instance; } }

        public static void SayHi()
        {
            Console.WriteLine("hi");
        }

        //raison d'etre for the class
        public void DoSomething()
        {
            // This must be thread-safe... it can be got at from multiple threads... thats its purpose
        }
    }

    [TestFixture]
    public class SingletonClient
    {
        [Test]
        public void UseSingleton()
        {
            Singleton.SayHi();
            Console.WriteLine("Start of test");
            Singleton s1 = Singleton.Instance;
            Singleton s2 = Singleton.Instance;
            Assert.AreSame(s1,s2);
        }
    }

.NET4 could use Lazy<T>

Comments [0] | | # 
# Wednesday, May 16, 2012

Do be able to multiple delete files in a folder

image

Comments [0] | | # 
# Tuesday, May 08, 2012
( Scrum )

From wikipedia:

During the meeting, each team member answers three questions:[11]

  • What have you done since yesterday?
  • What are you planning to do today?
  • Any impediments/stumbling blocks?
Comments [0] | | # 
# Wednesday, April 18, 2012
( Orchard )

Goals:
    website demoing projects I've done over many years
        to give people ideas about what can be done easily
        to sell myself
        for it to become my 'CV'
            look at stackoverflow CV - link to it
            actual CV - link to it
       
    CMS that I can use in many places
    large codebase to explore

Examples

image

nice and simple.

Comments [0] | | #