Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 12 June 2015
( Git )

ssh-keygen and save to C:\Users\David\.ssh\id_bitbucket

create a file named config in C:\Users\David\.ssh

Put SSH key into Bitbucket

Host bitbucket.org
IdentityFile ~/.ssh/id_bitbucket

The indent is important https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git

| | # 
# Thursday, 11 June 2015
( Git )
  • Work on own repo pushing up to GitHub (masterbranch workflow)
  • Work stuff / opensource Featurebranch

Master branch (create repo)

mkdir GitTest2 
Git init
copy .gitignore
npp README.txt  - have setup notepad++ to this shortcut
git add .
git commit –m “Adding first README”
git lga – shortcut for git log --graph --oneline --all –decorate
git commit –am “Second README message”  - shortcut for commit if file already there

git remote –v  - view remotes
git remote add origin git@github.com:djhmateer/GitTest2.git
git remote set-url origin new_url  - for updating the url
git checkout – readme.txt – throw away any changes (file not in staging or committed)git push –u origin master

git checkout – readme.txt – throw away any changes (file not in staging or committed)
git reset head readme.txt – bring out of staging

git reset –hard – throws away any uncommitted changes
git reset –hard HEAD~1  -- goes back to second last commit

Feature branch

git checkout -b feature1
git checkout master
git merge feature1
git branch -d feature1

git mergetool  --for when there are merge conflicts (I use vs difftool)

git commit --amend file2 – when you’ve made a commit, and want to add something to it or change the message
git commit --amend  -- just gets the last commit in text editor

Remote branches
git branch  -shows local branches
git branch --r  -shows remote branches

git push origin feature3  - push the local branch to the remote

Feature Branch (clone)

git clone git@github.com:djhmateer/GitTest2.git
git push origin feature3
git remote update - check to see if any changes on the remote branch

GitIgnore

http://www.kickasslabs.com/2010/02/06/quick-hits-when-your-gitignore-file-gets-ignored/
When you stage files, then realise the .gitignore file needs updating

| | # 
# Thursday, 04 June 2015

dsa.msc – Start AD Users

| | # 

Strengthen the guarantees of the methods with DirectoryInfo and FileInfo instead of strings

DirectoryInfo

public FileStore(DirectoryInfo workingDirectory)

Instead of accepting String, be more explicit.
public FileInfo GetFileInfo(int id)

Handy as can do
var file = this.GetFileInfo(id); if (!file.Exists)

FileInfo

public FileInfo GetFileInfo(int id) { // This can never be null as int is a value type, and workingDirectory is a pre-condition return new FileInfo(Path.Combine(this.WorkingDirectory.FullName, id + ".txt")); }

Again more prescriptive of the return type of the method.. so can use
FileInfo fileInfo = fileStore.GetFileInfo(id); string fileName = fileInfo.FullName;


Caching and Logging
public void Save(int id, string message){ Log.Information("Saving message {id}", id); var file = this.GetFileInfo(id); File.WriteAllText(file.FullName, message); this.cache.AddOrUpdate(id, MessageTemplate (i,s) => message); }

public Maybe<string> Read(int id){ var file = this.GetFileInfo(id); if (!file.Exists) return new Maybe<string>(); var message = this.cache.GetOrAdd(id, _ => File.ReadAllText(file.FullName)); //var message = File.ReadAllText(file.FullName); // Never want message to be Null - that is what the previous step is for return new Maybe<string>(message); }

Caching and Logging behaviour – common additions to code.

Where to Start – Logging

| | # 
# Monday, 01 June 2015
( SOLID )

 

  • A class should have only 1 reason to change  (do 1 thing and do it well)
  • Separation of concerns eg caching/logging
  • So a class should be 1 thing and do it well

How many reasons for change does the FileStore class have?

  • Logging
  • Caching
  • Storage (eg not filesystem use db)
  • Orchestration

image_thumb2

1. Logging - How to refactor – decompose each reason for change to other Classes

Move logging to another class – Serilog

public class StoreLogger { public void Saving(int id) { Log.Information("Saving message {id}.", id); } public void Saved(int id) { Log.Information("Saved message {id}.", id); }

and then:

public void Save(int id, string message) { this.log.Saving(id); //... this.log.Saved(id);

Test app working

http://xunit.github.io/docs/getting-started.html

xunit.net
xunit.runner.console

xUnit.net console test runner (64-bit .NET 4.0.30319.34209)
Copyright (C) 2015 Outercurve Foundation.

Discovering: SRPTests
Discovered:  SRPTests
Starting:    SRPTests
Finished:    SRPTests

=== TEST EXECUTION SUMMARY ===
   SRPTests  Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.292s

packages\xunit.runner.console.2.0.0\tools\xunit.console SRPTests\bin\Debug\SRPTests.dll

xunit.runner.visualstudio

Serilog – having trouble getting it to compile from nuget

install-package serilog

gets 1.5.6

Got it working by starting a new project, then adding in logging first.  SerilogTest

using Serilog; namespace SerilogTest { public class FileStoreLogger { public void Save(string message) { var log = new LoggerConfiguration() .WriteTo.ColoredConsole() .WriteTo.RollingFile(@"C:\Temp\Log-{Date}.txt") .CreateLogger(); log.Information("Saving"); } } }

and

using Xunit; namespace SerilogTest { public class Tests { [Fact] public void Thing() { var fileStoreLogger = new FileStoreLogger(); fileStoreLogger.Save("test"); Assert.Equal(1, 1); } } }

which dumped a log file into c:\temp\

Xunit2 not picking up tests

Create project
compile
restart vs
install-package xunit
compile
restart vs
install-package xunit.runner.visualstudio
restart vs

install-package seriolog

install-package AutoFixture.XUnit2

| | # 
# Friday, 29 May 2015
( Git )

git log -1 
This gives the latest SHA1

git diff
See the latest diffs

| | # 
( SOLID )

Supple/Pliable code – can change shape as we go along (as requirements always change)

  • Purpose of SOLID is to make you more productive
  • ..by making code more maintainable
  • ..through decomposition and decoupling

SOLID is a reaction to code smells

  • Rigidity – the design is difficult to change
  • Fragility – the design is easy to break
  • Immobility – the design is difficult to reuse
  • Viscosity – difficult to do the right thing
  • Overdesign – needless complexity

The 5 Principles (Uncle Bob first described)

  • Single Responsibility (SRP)
  • Open Closed (OCP)
  • Liskov Substitution (LSP)
  • Interface Segregation (ISP)
  • Dependency Inversion (DIP)
| | # 

To get XUnit2 working with AutoFixture using R# test runner:

Run powershell as administrator: Set-ExecutionPolicy RemoteSigned

Install-Package AutoFixture.XUnit2   **be careful not to install AutoFixture.Xunit
(This installs AutoFixture and XUnit2)

Install-Package xunit.runner.visualstudio
(Test runner for xunit)

Install-Package xunit

https://github.com/djhmateer/githubscratch/tree/master/MaybeTest2

| | # 
# Thursday, 28 May 2015

https://github.com/djhmateer/githubscratch/tree/master/maybe

I like the concept behind Maybe<T>..  Option Type in functional programming. 

  • List/Collection of 0 or 1 elements
  • eg if a TextFileRead operation is legal it returns the contents of the file as 1 element
  • eg if a TextFileRead operation fails it returns 0 elements

[Theory, AutoData] //[Theory] //[InlineData("test message")] public void ReadReturnsMessage(string message) { var fileStore = new FileStore(Environment.CurrentDirectory); fileStore.Save(44, message); Maybe<string> actual = fileStore.Read(44); Assert.Equal(message, actual.Single()); }

To get these tests to run, I had to install: (commented out text above shows how to run it not using AutoFixture)

https://www.nuget.org/packages/xunit/1.9.2 – not version 2

https://www.nuget.org/packages/xunit.extensions/1.9.2 – not version 2

https://www.nuget.org/packages/AutoFixture/ – used latest

https://www.nuget.org/packages/AutoFixture.Xunit/ – used latest

AutoFixture puts in random data into the string message.

Also found I had to use R#9 to get xunit2 runner working (although using xunit1 now)

| | #