Wednesday 18 November 2015
Recommended Reading
These are the best books I have read so far that I think every software developer should read. Every one of them has really helped me to understand and improve the way I work.
Tuesday 17 November 2015
Git Aliases
Git Aliases
You can add extra git commands in the command line to make your life easier using git aliasif you type git config --global -e you will be able to edit these.
they should go inside the [alias] section (add one if it's not there)
Basic Commands
These commands are just simple aliases for anything that I use a lot, mainly because I have to type less to use them.
s = status
See the status of the repositoryc = commit
Commit changes to the local repositorycm = commit -m
Commit with a messageExample:
git cm "Updated some code stuff"
aa = add -A
Stages all the changes in the workspace.co = checkout
Checkout a branch or commit.Example:
git co master
Viewing History
lga = log --graph --oneline --all --decorate
Shows the history for the repository in a nice way.
lgab = log --graph --oneline --decorate
Shows the history for the current branch in a nice way.Pushing & Pulling
plr = pull --rebase
pull but rebase instead of merge, find this useful when working on a branch that have others working in it to avoid merge commits. Makes the history much cleaner. See the internet for arguments about this.
pushup = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\""
Push a local branch to the origin remote, does what it says on the tin. Use this lots when working within a feature branch/pull request workflow.Stash & Reset
Most of these I found on Phil Haacks blogwip = !git add -A && git commit -m 'WIP'
Work in progress commit, commits anything to the current branch so it can be reverted later. Useful if you need to change what you are working on. You could use git stash but I prefer having a proper commit.
load = reset HEAD~1 --mixed
Used to undo the commit that was done with the wip command and put the changes in the working directory.wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
Really like this one, use it instead of "git reset --hard" to clear the current working directory. Much better because it still commits the changes before wiping so they will remain in your local repository until they are garbage collected.References
http://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-mergehttp://haacked.com/archive/2014/07/28/github-flow-aliases/
Tuesday 10 November 2015
Running a Release Retrospective
We just shipped the first release of a project that I have been working on out to customers and it was decided it would be a good time to run a release retrospective with all of the teams involved.
This was shorter than hoped but it worked well to help frame the period we were talking about.
Read out the prime directive and explained that the meeting is supposed to be a positive thing looking at what we can do better and improve on rather than blame.
The discussion was very interesting though some of the topics were very developer focused, it was suggested that we should have a developer only meeting first next time to avoid bringing the other team members to boredom.
The hardest part of these things always seems the be actually getting something practical out of as many items as possible. To try to help we assigned owners to each item and I will try to chase the owners to make sure progress is made over the coming weeks.
While this is probably not the best way to judge the release it was interesting to see the information that came in. In future I would suggest maybe using a more simple system (Well, OK, Poor) or something along those lines.
Format of the session
3 hours in the afternoon with roughly 30 people attending.Introduction
Some senior managers came in and just say a few words to kick off the meeting in a positive way. Sometimes retrospectives can turn quite negative so this helped make the point that the release was a success and we are there to look for improvements in the coming versions.Discussion of outcomes
Discussion of the expected outcomes of the meeting, this was a list of different items with actions we are going to take and owners for those actions. The items were:
- Recommendations (What we did well)
- Knowledge (What we learnt)
- Mysteries (Things that need investigation)
- WTF's (Things that we can improve on)
Next time I would rename recommendations to something else as they came out as recommended things we can do better rather that things we did well that we recommend we keep doing and pass on.
Energizer
We tried energizer for this meeting as described in parts of a retrospective. After some reading we decided to try the Candy Love Energizer (whats better to get people talking than chocolate?).
This worked well and got everyone being talkative, would really recommend this energizer (think its a bit more interesting as most people haven't done it before).
Story of the project
This was a brief presentation with some key dates and statistics of the project, to help focus people on what we were going to be discussing the following breakout section.This was shorter than hoped but it worked well to help frame the period we were talking about.
Retrospective Prime Directive
"Regardless of what we discover, we understand and truly believe that everyone did the best job they could, given what they knew at the time, their skills and abilities, the resources available, and the situation at hand."Read out the prime directive and explained that the meeting is supposed to be a positive thing looking at what we can do better and improve on rather than blame.
Breakout
We broke up in to 4 teams each with flip board paper and pens with each team to focus on a different area. Each team should identify Recommendations, Knowledge, Mysteries and WTF's with possible actions.Quality & QA
Looking at our quality management & testing
Process
This is essentially the story of a story, from customer to shipped.
Planning & Release
Looking at how the project was planned and released.
Work & Environment
How the teams interact and work together, inside and outside of the team. as well as generally at working within the company & skill sharing.
Discussion
Each team presents their findings and we discuss as a whole group the proposed actions.The discussion was very interesting though some of the topics were very developer focused, it was suggested that we should have a developer only meeting first next time to avoid bringing the other team members to boredom.
Actions & Owners
Review the actions and assign owners.The hardest part of these things always seems the be actually getting something practical out of as many items as possible. To try to help we assigned owners to each item and I will try to chase the owners to make sure progress is made over the coming weeks.
Release Success
Each person gets a piece of paper and writes a 1-10 on it to say how well they thought it went. This provides an anonymous way to gather information on how well everyone who worked on the release believes it has gone.While this is probably not the best way to judge the release it was interesting to see the information that came in. In future I would suggest maybe using a more simple system (Well, OK, Poor) or something along those lines.
Conclusion
The session went pretty well most of the feedback was very positive. We identified several points and actions that we will be looking at over the period of the next release.
I would highly recommend playing around with the different subjects to suit the number of people you have joining in and areas you want to cover. Hopefully when we do another release retrospective we can try another set and compare the results.
http://blog.crisp.se/2013/01/22/henrikkniberg/how-to-run-a-big-retrospectives
http://joakimsunden.com/2013/01/running-big-retrospectives-at-spotify/
http://www.retrospectives.com/pages/retroPrimeDirective.html
https://www.thoughtworks.com/insights/blog/7-step-agenda-effective-retrospective
I would highly recommend playing around with the different subjects to suit the number of people you have joining in and areas you want to cover. Hopefully when we do another release retrospective we can try another set and compare the results.
References
http://www.funretrospectives.com/candy-love/http://blog.crisp.se/2013/01/22/henrikkniberg/how-to-run-a-big-retrospectives
http://joakimsunden.com/2013/01/running-big-retrospectives-at-spotify/
http://www.retrospectives.com/pages/retroPrimeDirective.html
https://www.thoughtworks.com/insights/blog/7-step-agenda-effective-retrospective
Tuesday 4 August 2015
Protocol Buffers & Lists
Introduction
For this example I will be using the following example .proto and looking at serializing large repeated data sets in C#.
.proto
Serialization Code
Deserialization Code
Comparison
For a list of over 7k items Json.net serializes about 10ms slower on my machine, not quite what I expected from what is supposed to be a super fast binary serializer. Upon further reading it appears Protocol Buffers are not designed to handle large data sets, but this can be worked around by serializing the items individually.
Fix
To fix this we will serialize each item induvidually but add them all to the same stream with the length of the item added before each item.
Fixed Serialize
Fixed Deserialize
This updated code resulted in a significant difference (160ms -> 25ms) between JSON.Net and Protocol Buffers.
References
https://developers.google.com/protocol-buffers/
https://developers.google.com/protocol-buffers/docs/techniques#large-data
https://github.com/google/protobuf
For this example I will be using the following example .proto and looking at serializing large repeated data sets in C#.
.proto
syntax = "proto2";
package pcdf;
message Boiler
{
required string BoilerId = 1;
required int32 TableNumber = 2;
required string TableName = 3;
required string BoilerData = 4;
required string BrandName = 5;
required string Qualifier = 6;
required string ModelName = 7;
}
message AllBoilers
{
repeated Boiler Boiler = 1;
}
Serialization Code
var listBuilder = new AllBoilers.Builder();
for (var i = 0; i < 7000; i++)
{
var builder = new Boiler.Builder
{
BoilerId = "ID",
TableNumber = 2,
TableName = "Name",
BoilerData = "Data",
BrandName = "Brand",
ModelName = "Model",
Qualifier = "Qualifier"
};
listBuilder.AddBoiler(builder);
}
var list = listBuilder.Build();
var bytes = list.ToByteArray();
var fileStream = new FileStream(OUTPUT_FILE, FileMode.Create);
fileStream.Write(bytes, 0, bytes.Length);
For the actual tests real test data was used.Deserialization Code
var fileStream = new FileStream(OUTPUT_FILE, FileMode.Open);
var boilers = new AllBoilers.Builder();
boilers.MergeFrom(fileStream);
Comparison
For a list of over 7k items Json.net serializes about 10ms slower on my machine, not quite what I expected from what is supposed to be a super fast binary serializer. Upon further reading it appears Protocol Buffers are not designed to handle large data sets, but this can be worked around by serializing the items individually.
Fix
To fix this we will serialize each item induvidually but add them all to the same stream with the length of the item added before each item.
Fixed Serialize
var fileStream = new FileStream(OUTPUT_FILE, FileMode.Create);
for (var i = 0; i < 7000; i++)
{
var builder = new Boiler.Builder
{
BoilerId = "ID",
TableNumber = 2,
TableName = "Name",
BoilerData = "Data",
BrandName = "Brand",
ModelName = "Model",
Qualifier = "Qualifier"
};
var bytes = builder.Build().ToByteArray();
var intBytes = BitConverter.GetBytes(bytes.Length);
fileStream.Write(intBytes, 0, intBytes.Length);
fileStream.Write(bytes, 0, bytes.Length);
}
Fixed Deserialize
var fileStream = new FileStream(OUTPUT_FILE, FileMode.Open);
var list = new List<Boiler>();
int size;
while ((size = GetSize(fileStream)) != -1)
{
var bytes = new byte[size];
fileStream.Read(bytes, 0, bytes.Length);
var boiler = new Boiler.Builder();
boiler.MergeFrom(bytes);
list.Add(boiler.Build());
}
This updated code resulted in a significant difference (160ms -> 25ms) between JSON.Net and Protocol Buffers.
References
https://developers.google.com/protocol-buffers/
https://developers.google.com/protocol-buffers/docs/techniques#large-data
https://github.com/google/protobuf
Protocol Buffers in C#
Installation
Protocol Buffers can be installed via Nuget,
Tools
As well as installing the protocol buffers library this will also download the tools required to compile .proto files and generate C# files from them. The tools will be located where the nuget package was installed to.
.proto
A .proto file will be required to generate the required files for usage. This example .proto defines a message called TestMessage with 3 properties. The package is used like a namespace and will also be used to generate the namespace of the classes in C#.
Protoc
To compile a .proto file locate protoc.exe in the tools dir and run it with the following parameters.
ProtoGen
The next step is to generate the .cs files that will contain the generated code that can be used to create and serialize the data.
Serialization
Deserialization
Other Methods
message.ToJson() & message.ToXml()
These can be used to visualize the message in other formats.
message.WriteTo(Stream stream)
This is used to write the bytes to a stream.
References
https://developers.google.com/protocol-buffers/
https://developers.google.com/protocol-buffers/docs/reference/proto2-spec
https://github.com/google/protobuf
Protocol Buffers can be installed via Nuget,
PM> Install-Package Google.ProtocolBuffers
Or search for Protocol Buffers, there is also a Protocol buffers lite package that is smaller and does not use relection (better for some lighter platforms like mono). Note: an option must be set in the .proto file to compile the proto for use with the lite package.Tools
As well as installing the protocol buffers library this will also download the tools required to compile .proto files and generate C# files from them. The tools will be located where the nuget package was installed to.
.proto
A .proto file will be required to generate the required files for usage. This example .proto defines a message called TestMessage with 3 properties. The package is used like a namespace and will also be used to generate the namespace of the classes in C#.
syntax = "proto2";
package test_proto;
message TestMessage
{
required int32 Id = 1;
required string Name = 2;
optional string Details = 3;
}
For more information on defining a .proto file see the protocol buffer v2 spec.Protoc
To compile a .proto file locate protoc.exe in the tools dir and run it with the following parameters.
protoc -oitem test.proto
where item is the name of the output file and test.proto the name of the .proto file. This outputs a compiled version of the .proto file.ProtoGen
The next step is to generate the .cs files that will contain the generated code that can be used to create and serialize the data.
.\ProtoGen.exe item
This should generate a .cs file containing everything you need to read/write to the protocol buffers.Serialization
var builder = new TestMessage.Builder();
builder.Id = 2;
builder.Name = "Test";
builder.Details = "Some Detials";
var message = builder.Build();
var bytes = message.ToByteArray();
You must create a builder as messages are immutable (Each message generates and internal Builder class), a message can be turned back in to a builder easily wit the ToBuilder() method.Deserialization
var builder = new TestMessage.Builder();
builder.MergeFrom(bytes);
var message = builder.Build();
You could just read the data from the builder if you wish.Other Methods
message.ToJson() & message.ToXml()
These can be used to visualize the message in other formats.
message.WriteTo(Stream stream)
This is used to write the bytes to a stream.
References
https://developers.google.com/protocol-buffers/
https://developers.google.com/protocol-buffers/docs/reference/proto2-spec
https://github.com/google/protobuf
Protocol Buffers
Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data (official site).
In short protocol buffers are googles in house binary serialization format, designed to serialize small messages fast and with a small data size.
Where to use them
Protocol buffers are great when you need fast serialization and a small payload (where efficiency/speed is important). They are not so good when data formats are likely to change as this will mean you need to discard and recreate schemas and drop backwards compatibility or have multiple schemas for different versions.
Pros
Proto Format
Protocol Buffer messages are first defined in a .proto file using the proto language. This can then be used to generate data access objects for your language of choice.
Proto3
The current version is proto2, but Proto3 is current in alpha and being developed (Source code on GitHub) The update will include a new version of the .proto syntax.
Binary vs Text Serialization
References
https://developers.google.com/protocol-buffers/
https://github.com/google/protobuf
https://developers.google.com/protocol-buffers/docs/reference/proto2-spec
In short protocol buffers are googles in house binary serialization format, designed to serialize small messages fast and with a small data size.
Where to use them
Protocol buffers are great when you need fast serialization and a small payload (where efficiency/speed is important). They are not so good when data formats are likely to change as this will mean you need to discard and recreate schemas and drop backwards compatibility or have multiple schemas for different versions.
Pros
- Fast
- Small Payload
- Many cross platform implementations (c++, Java, C#, python, JS)
- Generators a provided to generate data access classes
Cons
- Schema Enforced (Need to define .proto file)
- More complicated API than some other serializers
Proto Format
Protocol Buffer messages are first defined in a .proto file using the proto language. This can then be used to generate data access objects for your language of choice.
message Boiler
{
required string BoilerId = 1;
required int32 TableNumber = 2;
required string TableName = 3;
required string BoilerData = 4;
required string BrandName = 5;
required string Qualifier = 6;
required string ModelName = 7;
}
Fields are described with options (required, optional, repeated, etc..) followed by Type (int32, int64, string), field name then = and an Id number that must be unique. For more information read the v2 spec.Proto3
The current version is proto2, but Proto3 is current in alpha and being developed (Source code on GitHub) The update will include a new version of the .proto syntax.
Binary vs Text Serialization
{
"Id": "21",
"Name": "Test User",
"address": {
"streetAddress": "Test Street",
"city": "Birmingham"
}
}
Some formats serialize to text (JSON, XML, etc..) this can be really useful if you need to quickly view and edit the documents without special tooling. Binary serializations will require a tool that can read the document before you can begin to read and manipulate the data. Binary serializations will normally be smaller and more efficient though.References
https://developers.google.com/protocol-buffers/
https://github.com/google/protobuf
https://developers.google.com/protocol-buffers/docs/reference/proto2-spec
Tuesday 23 June 2015
Encapsulation
Encapsulation
Encapsulate - "To enclose or be enclosed in or as if in a capsule". [the free dictionary]
In software
Encapsulate - "To enclose or be enclosed in or as if in a capsule". [the free dictionary]
In software
Encapsulation is the practice of having classes/objects not
expose their data but expose methods to act upon the data. It also relates to
how we choose to put methods and data into classes (Putting data with the relevant methods that use it).
There are many ways we can choose to decompose systems into
objects but encapsulation is an effective way of making systems easier to
understand and maintain. By restricting access to data we can change the
internals of a class/module without requiring changes in the code that uses
that class/module.
Example
Example
I have a class “Person”
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now take the following method:
public string FullName(Person person)
{
return person.FirstName + person.LastName;
}
If this method is not in the Person class the class must
expose the firstname and lastname data for this method to work. So any other piece
of code then has access to firstname and lastname and can use it. This means
that if at a later point you decide to store the person’s name as just a
fullname string you must change code that is outside of the person class. If this
method is part of the person class you only have to update the person class and
as long as its public interface is well designed and not affected by the change
you should not have to change any other classes.
Design
Design
By carefully thinking about how we compose our classes we
can hide the details (implementation) from the outside world, this can be a
massive advantage when maintaining large systems as we can completely rewrite
the internals of a class and have it not affect the rest of the system. When
classes are encapsulated well it can lead to a better conceptual model (set of
classes that represent the system), and often a better set of abstractions
making the system easier to code against.
How to achieve
You can use Getters and Setters to make it easier to encapsulate data, in .net an auto property can achieve this quickly. This will let you change the data later and still use the same property. Generally I would prefer using methods to act upon a class though.
Put methods in the same class as the data they are operating on, this should help achieve cohesion and make modification easier later.
Avoid
Classes exposing their data and their internal workings, this can even include the way you design your methods. Sometimes you can encapsulate how the object works by avoiding sharing of implementation details. For example return amount of fuel as a percentage left rather than gallons.
Having your method access and change data within another object (see if you can move the method to within the other object, where hopefully it will make more sense)
Put methods in the same class as the data they are operating on, this should help achieve cohesion and make modification easier later.
Avoid
Classes exposing their data and their internal workings, this can even include the way you design your methods. Sometimes you can encapsulate how the object works by avoiding sharing of implementation details. For example return amount of fuel as a percentage left rather than gallons.
Having your method access and change data within another object (see if you can move the method to within the other object, where hopefully it will make more sense)
Scope
Spend time thinking about anything that is not private, is there a way you could increase encapsulation by reducing the amount of data shared. Is your publicly accessible interface clean and free of clues as to how the object is implemented.
Also be wary of protected members, it can seem natural for inherited objects to have access to data but this can lead to problems refactoring later. Loose coupling between inheritance trees is hard to achieve but important to consider.
Also be wary of protected members, it can seem natural for inherited objects to have access to data but this can lead to problems refactoring later. Loose coupling between inheritance trees is hard to achieve but important to consider.
Benefits of
encapsulation
- Increase cohesion (Relation of data/methods within a class, high cohesion is good!)
- Decrease coupling (High coupling of classes can make them hard to reuse, replace and test)
- Better separation of concerns (when classes are highly cohesive a class is more likely to only focus upon itself)
- Easier to reuse (A loosely coupled class will be easier to reuse in other places)
- Easer to make changes (Only changes to the public interface require changes in other places)
- Reduces complexity (By only requiring you to think about one class at a time)
Disadvantages of
encapsulation
- Can require more code (Boiler plate, getters/setters etc.)
Inheritance and
encapsulation
Inheritance can be seen to be breaking encapsulation as you
are exposing the workings of the class to the class that is sub classing. Over
use of inheritance where it is not required will often lead to the breaking of
encapsulation. So we should favor composition (Containing another object) over
inheritance as a mechanism for code reuse.
Sunday 22 February 2015
Generic Fun : Generic Implicit Operators
Just playing around with generics, thinking about using them for things like lazy loading wrappers and potentially for dirty tracking and things like that. Lets start with a simple generic wrapper that doesn't do anything.
One of the problems is that you always have to add in a .Value for reading out and new Wrapper<T>(someData) for creating. One solution to this is to use implicit operators. Thus allowing you to create by just assigning a instance of T to the variable, and read out by assigning in to a T variable.
This will allow you to do things like this:
There are some drawbacks to this, as if you are reading in to implicitly typed local variables (var), it would treat it as a Wrapper<T> unless you specify the type of the variable. Causing the properties of T not to be available without accessing the value of the wrapper putting in another variable.
It could be useful to help reduce the amount of code you need to write when using a generic wrapper, I was thinking maybe this could be used for lazy loading?
Even if it's not useful, still interesting :)
public class Wrapper<T>
{
public T Value { get; }
public Wrapper(T value)
{
Value = value;
}
}
(Using C# 6)One of the problems is that you always have to add in a .Value for reading out and new Wrapper<T>(someData) for creating. One solution to this is to use implicit operators. Thus allowing you to create by just assigning a instance of T to the variable, and read out by assigning in to a T variable.
public class Wrapper<T>
{
private T _value;
public Wrapper(T value)
{
_value = value;
}
public static implicit operator Wrapper<T>(T value)
{
return new Wrapper<T>(value);
}
public static implicit operator T(Wrapper<T> value)
{
return value._value;
}
}
This will allow you to do things like this:
Wrapper<string> textWrapper = "Text";
string text = textWrapper;
Wrapper<int> numberWrapper = 23;
int number = numberWrapper;
There are some drawbacks to this, as if you are reading in to implicitly typed local variables (var), it would treat it as a Wrapper<T> unless you specify the type of the variable. Causing the properties of T not to be available without accessing the value of the wrapper putting in another variable.
It could be useful to help reduce the amount of code you need to write when using a generic wrapper, I was thinking maybe this could be used for lazy loading?
public class LazyLoaded<T>
{
private T _value;
private bool _loaded;
public LazyLoaded(T value)
{
_value = value;
}
public T Value()
{
if (!_loaded)
{
// Load data
_loaded = true;
}
return _value;
}
public static implicit operator LazyLoaded<T>(T value)
{
return new LazyLoaded<T>(value);
}
public static implicit operator T(LazyLoaded<T> value)
{
return value._value;
}
}
Even if it's not useful, still interesting :)
Tuesday 20 January 2015
Autofac
Basic Setup
Autofac is a nifty dependency injection container that comes as a PCL (Portable Class Library) so it can be used in Xamarin projects.To set it up you need to build a container using the ContainerBuilder class:
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
// Register Types
_container = containerBuilder.Build();
You can then register types like this: container.RegisterType<First>().As<IFirst>();
To use the container to resolve objects you need to get a lifetime scope from it: using (var scope = _container.BeginLifetimeScope())
{
// Reslove Types
}
You can then resolve types like so: var first = scope.Resolve<IFirst>();
If you are using MVC/Web.Api you can add additional nuget packages that will enable constructor injection on your controllers!Automatic resolution of concrete types
If you want the container to automatically resolve concrete types you can add this to the container builder. This will mean that if it can't find a registration and the type is a concrete (non-interface, non-abstract) class it will just new up the class and return it. containerBuilder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
Passing some parameters
You can pass in some parameters and the container will automatically resolve the non passed ones. This means you can have a constructor like this and still pass in some of the parameters and have the container resolve the others automatically. public Test(IFirst first, ISecond second)
To pass in the IFirst parameter just do: var test = scope.Resolve<Test>(new TypedParameter(typeof(IFirst), first));
This will then pass first in and create a new Second instance. delegate Factories
You can use a Func<T> as a factory, this will then pass in a function that will resolve instances from the container so you can create multiple instances. public NeedAFactory(Func<Test> factory)
{
_test1 = factory();
_test2 = factory();
}
Delegate Factories with parameters
You can also add a Func that takes parameters and this will be passed in to the constructor of the class when it is constructed. internal class Parent
{
private IChild _child;
public Parent(Func<Parent, IChild> factory)
{
_child = factory(this);
}
}
internal class Child : IChild
{
private readonly Parent _parent;
public Child(Parent parent)
{
_parent = parent;
}
}
When autofac creates the child class it will pass the varable specified in the parent (this).Friday 9 January 2015
C# 6 (Part 2)
String Interpolation
String interpolation is a replacement/alternative to string.format.
This will output "Hello Rob"
This is quite a nice feature as I find the syntax a lot easier to read than string.format.
Resharper 9.1 does not support this yet and will highlight it as an error, but it still compiles and runs.
https://roslyn.codeplex.com/discussions/540869
https://roslyn.codeplex.com/discussions/570614
nameof Operator
Quick and easy way of getting the name of something
Another useful little feature for when you are setting things using reflection and don't want to use strings, you can use an expression tree but this is simpler and I assume more efficient.
Resharper 9.1 does not support yet but it still compiles and runs
https://roslyn.codeplex.com/discussions/570551
Expression Bodied Members
You can now define fields with lambda expressions.
Can also be used to define methods.
Nice little feature for quick definition of simple methods.
Exception Filters
It is now possible to filter exceptions without affecting the stack trace.
This can also be used (abused) to log exceptions.
String interpolation is a replacement/alternative to string.format.
var name = "Rob";
var output = "Hello \{name}";
Console.WriteLine(output);
This will output "Hello Rob"
This is quite a nice feature as I find the syntax a lot easier to read than string.format.
Resharper 9.1 does not support this yet and will highlight it as an error, but it still compiles and runs.
https://roslyn.codeplex.com/discussions/540869
https://roslyn.codeplex.com/discussions/570614
nameof Operator
Quick and easy way of getting the name of something
Console.WriteLine(nameof(test)); // varable outputs: "test"
Console.WriteLine(nameof(TestProperty)); // property outputs: "TestProperty"
Console.WriteLine(nameof(MyClass.TestProperty)); // property of class outputs: "TestProperty"
Console.WriteLine(nameof(System.Text)); // namespace outputs: "Text"
Console.WriteLine(nameof(MyClass.Method)); // method outputs: "Method"
Another useful little feature for when you are setting things using reflection and don't want to use strings, you can use an expression tree but this is simpler and I assume more efficient.
Resharper 9.1 does not support yet but it still compiles and runs
https://roslyn.codeplex.com/discussions/570551
Expression Bodied Members
You can now define fields with lambda expressions.
private int _total => _first + _second;
private static Program CreateProgram => new Program();
Can also be used to define methods.
public int Add(int x) => _number += x;
Nice little feature for quick definition of simple methods.
Exception Filters
It is now possible to filter exceptions without affecting the stack trace.
try
{
ThrowException();
}
catch (Exception e) if (e is InvalidDataException || e is InvalidOperationException)
{
// Handle it
}
This can also be used (abused) to log exceptions.
try
{
ThrowException();
}
catch (Exception e) if (Log(e))
{
// Handle it
}
This seems to be an acceptable abuse of the feature as it is described in the language spec :)
Thursday 8 January 2015
Asp.net Mvc Image Helpers (Part 3)
Image helper with lazy loading
This article looks at creating a html helper to lazy load images using the jQuery lazy load plugin.
It follows on from 2 earlier articles on creating html helpers for image loading:
There are a few different ways of making an image lazy load, but this seems to be the simplest to get working I've found so far.
First of all you need to install the Lazy load plugin for jQuery (search "lazyload") using the Nuget package manager. There are 2 versions available, I used 1.8.4. You will also need some additional images for this part, a ajax style loading spinner (create your own here!) and a 1x1 pixel spacer image (create one or download from here).
First we will create the html helper that will generate the html code that lazy loads the image.
public static MvcHtmlString LazyLoadImage(
this HtmlHelper helper,
string image,
string alternative,
int width,
int height)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.Action("ImageResize", "Image", new { image, width, height });
var spaceUrl = urlHelper.Action("Image", "Image", new { image = "spacer.gif" });
var html = String.Format("<div class='lazyWrapper' style='height:{0}px; width:{1}px'>", height, width);
html += String.Format("<img class='lazy' src='{0}' data-original='{1}' alt='{2}'/>", spaceUrl, url,
alternative);
html += String.Format("</div>");
return MvcHtmlString.Create(html);
}
It uses the Image Controller re-size method created in part 2 of this series.We create a div element to wrap the img element, this enables us to set the background of the div to the loading spinner element and easily center the spinner. We set the img tags src attribute to a single pixel transparent image so that it will not show until updated by jQuery lazy load. The data-original tag holds the image that we want to lazy load.
Done!
C# 6
Been digging in to some of the new C#6 features, a few examples/thoughts
Auto Property Initializers
Auto property initializers allow you to set the initial value without having to do it in a constructor
This can also be used for get only properties
Seems like a non-static field cannot access a method to set the value:
When changing the method to static it appears to work, interesting...
This appears to be because the property initializers are called before the constructor.
Think I will be using this stuff, especially setting the get only property. Nice little bits of syntactic sugar.
Constructor Assignment to get only Auto Properties
Does what it says on the tin,
Also seems reasonably useful, though not sure if I will favour this or setting it on the property.
Static Using
Pretty simple, just imports the static methods for use without their class name.
Not sure if using this stuff will be a good idea as it may reduce readability, but then maybe there will be cases where this would be useful. Advise using with caution.
Dictionary Initializers
There is a new dictionary initialzer format, the old one like the following still works
The new format is as follows
So why the new format, well the only reason that I can see is this allows you to use the new format initializer just by implementing a this property on your own class:
You can then use the new initializer when creating instances
Think this stuff will be useful for people making custom collections, good to know it exists so that I can use it when necessary. Though i'm now not sure which initializer I should be using when initializing dictionaries...
Auto Property Initializers
Auto property initializers allow you to set the initial value without having to do it in a constructor
public string TestString { get; set; } = "Hello";
This can also be used for get only properties
public string TestString { get; } = "Hello";
Seems like a non-static field cannot access a method to set the value:
Cannot access non-static method 'GetString' in static context
When changing the method to static it appears to work, interesting...
public string TestMethodReturn { get; } = GetString();
public static string GetString()
{
return "Method";
}
This appears to be because the property initializers are called before the constructor.
Think I will be using this stuff, especially setting the get only property. Nice little bits of syntactic sugar.
Constructor Assignment to get only Auto Properties
Does what it says on the tin,
public string TestString { get; }
public Program()
{
TestString = "Hello";
}
Also seems reasonably useful, though not sure if I will favour this or setting it on the property.
Static Using
Pretty simple, just imports the static methods for use without their class name.
using System.Console;
WriteLine("Hello");
Not sure if using this stuff will be a good idea as it may reduce readability, but then maybe there will be cases where this would be useful. Advise using with caution.
Dictionary Initializers
There is a new dictionary initialzer format, the old one like the following still works
var dictionaryOld = new Dictionary<string, string>
{
{"first", "First"},
{"Second", "Second"},
{"Third", "Third"},
};
The new format is as follows
var dictionaryNew = new Dictionary<string, string>
{
["First"] = "First",
["Second"] = "Second",
["Third"] = "Third",
};
So why the new format, well the only reason that I can see is this allows you to use the new format initializer just by implementing a this property on your own class:
public class MyCollection
{
private readonly Dictionary<string, string> _store = new Dictionary<string, string>();
public string this[string key]
{
get { return ""; }
set { _store[key] = value; }
}
}
You can then use the new initializer when creating instances
var custom = new MyCollection
{
["First"] = "First",
["Second"] = "Second",
["Third"] = "Third",
};
Think this stuff will be useful for people making custom collections, good to know it exists so that I can use it when necessary. Though i'm now not sure which initializer I should be using when initializing dictionaries...
Wednesday 7 January 2015
New Toys
Looks like there has been a load of new stuff for Visual Studio and .NET Recently. So I thought I would have a play. Link.
Visual Studio Ultimate 2015 Preview
Download and installation pretty standard, havent noticed anything exceptionally different, lambda debugging looks like it will be pretty cool. Will spend a bit more time playing with the features after I have played with C#6.
List of features
Roslyn (The new C# compiler platform)
Pretty easy to install in to a project just put open a console and type:
Install-Package Microsoft.CodeAnalysis -Pre
Roslyn Website
R#
New platform installer for 9.1 picked up vs2015 and installed, works fine. Looks like some of the c# 6 support is already added as it suggests adding the ?. (Null Propagation) operator.
Resharper Platform Installer
C# 6
Tried out some of the new features including Null propagation, using Static Members ,auto Property Initializers,Getter only auto properties,Exception Filters. Lots of fun :) hopefully will have time to look at a few of these more in depth.
Here is a list of c#6 features:
Source
Xamarin
At first the Xamarin project templates did not appear, but I updated Xamarin studio to v 3.9 (In the alpha/Beta Channel) which features Visual Studio 2015 support and the templates appeared.
Looks like there is lots of fun ahead for .net!
Visual Studio Ultimate 2015 Preview
Download and installation pretty standard, havent noticed anything exceptionally different, lambda debugging looks like it will be pretty cool. Will spend a bit more time playing with the features after I have played with C#6.
List of features
Roslyn (The new C# compiler platform)
Pretty easy to install in to a project just put open a console and type:
Install-Package Microsoft.CodeAnalysis -Pre
Roslyn Website
R#
New platform installer for 9.1 picked up vs2015 and installed, works fine. Looks like some of the c# 6 support is already added as it suggests adding the ?. (Null Propagation) operator.
Resharper Platform Installer
C# 6
Tried out some of the new features including Null propagation, using Static Members ,auto Property Initializers,Getter only auto properties,Exception Filters. Lots of fun :) hopefully will have time to look at a few of these more in depth.
Here is a list of c#6 features:
Feature | Example | C# | VB |
---|---|---|---|
Auto-property initializers | public int X { get; set; } = x; | Added | Exists |
Getter-only auto-properties | public int Y { get; } = y; | Added | Added |
Ctor assignment to getter-only autoprops | Y = 15 | Added | Added |
Parameterless struct ctors | Structure S : Sub New() : End Sub : End Structure | Added | Added |
Using static members | using System.Console; … Write(4); | Added | Exists |
Dictionary initializer | new JObject { ["x"] = 3, ["y"] = 7 } | Added | No |
Await in catch/finally | try … catch { await … } finally { await … } | Added | No |
Exception filters | catch(E e) if (e.Count > 5) { … } | Added | Exists |
Partial modules | Partial Module M1 | N/A | Added |
Partial interfaces | Partial Interface I1 | Exists | Added |
Multiline string literals | "Hello<newline>World" | Exists | Added |
Year-first date literals | Dim d = #2014-04-03# | N/A | Added |
Line continuation comments | Dim addrs = From c in Customers ' comment | N/A | Added |
TypeOf IsNot | If TypeOf x IsNot Customer Then … | N/A | Added |
Expression-bodied members | public double Dist => Sqrt(X * X + Y * Y); | Added | No |
Null propagation | customer?.Orders?[5]?.$price | Added | Added |
String interpolation | $"{p.First} {p.Last} is {p.Age} years old." | Added* | Planned |
nameof operator | string s = nameof(Console.Write); | Added* | Planned |
#pragma | #Disable Warning BC40008 | Added | Added |
Smart name resolution | N/A | Added | |
ReadWrite props can implement ReadOnly | Exists | Added | |
#region inside methods | Exists | Added | |
Overloads inferred from Overrides | N/A | Added | |
CObj in attributes | Exists | Added | |
CRef and parameter name | Exists | Added | |
Extension Add in collection initializers | Added | Exists | |
Improved overload resolution | Added | N/A |
Xamarin
At first the Xamarin project templates did not appear, but I updated Xamarin studio to v 3.9 (In the alpha/Beta Channel) which features Visual Studio 2015 support and the templates appeared.
Looks like there is lots of fun ahead for .net!
Subscribe to:
Posts (Atom)