Archive for the ‘Development’ Category.

Git Cheatsheet

Clone

git clone <path or url to repo>

Create an empty repo

git init

Check if upstream has updates

git fetch

Create a branch

git branch mybranch

Switch to another branch

git checkout mybranch

Create and switch to another branch in 1 command

git checkout 0bf7e9a915a15be0bdd6b97e79642b76aa0bf3ff

Switch to a previous commit (earlier state)

Want to get your code from before one or more changes? Find the commit id and use it.

git checkout mybranch

You can’t do much more than look around, but it can be useful, especially after a major architecture change that broke one tiny thing and you need to know why.

Pull upstream updates

git pull

Add a file

git add filename

Move a file

git mv sourcefile destinationfile

Note: You can move a directory or source file or destination file can include directories.

Delete a local branch

git branch -d mybranch

Status

git status

Revert uncommitted changes to a file

git checkout path\to\file.ext

Remove all untracked files

This makes the repository clean again.
Do a dry run first with -n.

git clean -n

Then do it for real with -f.

git clean -fxd

git diff

git diff

git merge

git merge myBranch

Take all upstream source files

git checkout --ours .
git add .

Keep all local files

git checkout --theirs .
git add .

Abort the merge

git merge --abort

git reset or undoing a local commit

Reset your local branch to head, but keep all the changes. Use this to undo a commit.

git reset HEAD^

git rebase

This at first looks easy. But there is complexities, especially if you have already pushed.

git rebase master

If a merge conflict occurs, fix it and then run:

git rebase --continue

If you have already pushed, run this to push once rebase is complete.

git push --force-with-lease

git squash all commits

This is a multistep process. The assumption is that you are in your feature branch.
Make sure you have no lingering changes and everything is committed before starting.
Branch name in example: FeatureA

git checkout master
git pull
git checkout -b FeatureA_2
git merge --squash FeatureA

Now if you want the branch named the same, you can delete FeatureA and rename FeatureA_2 to FeatureA.

Delete local branch

git branch -d yourbranch

To force, just use a capital D.

git branch -d yourbranch

Rename local branch

git branch -m newBranchName

If you are in master and want to rename a feature branch without checking it out:

git branch -m oldBranchName newBranchName

Git conflict with Visual Studio .sln file

Often, when multiple developers are working on the same solution and adding new projects to it, git will conflict easily.
Instead of trying to merge the .sln, it is often much faster, especially if you have only added a project or two, to just take the current master’s .sln and re-add your projects to the sln.

So imagine you are working on branch FeatureA.
Note: Remember, where “ours” and “theirs” points to is opposite of where they point to on a merge.

git checkout master
git pull
git checkout FeatureA
git rebase master
git checkout --theirs /path/to/yourproj.sln
git rebase --continue

You will then have to save your commit as the commit text will open. Remember to press “Esc + Shift + :” and then type wq! and hit enter.
Now, if your branch has many check-ns, you may have to repeat the process to keep the master (theirs) .sln file.

Once your rebase is completed, make your changes to your .sln and check them in.

Change the git editor from ‘vim’ to ‘notepad++’

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Reuseable Building Block Development

If you haven’t paid attention to the development world, you might have missed the current movement called “Reuseable Building Block development.” You know, as a kid, we could get blocks are build anything with them. We only had to stack them. Well, having a n-tier stack is very common, now, so stacking isn’t the issue. It is having blocks that are easy to stack. Some are calling it the open source movement, and while most reusable building blocks are open source, not all of them are. Many of the building blocks don’t have to be open source, but can simply be well-documented and work well.

With NuGet and Npm, building blocks are being created and published daily. The problem now is helping other developers recognize this movement. Changing our mindset from, “we can’t use if it wasn’t invented here,” to something more like, “this is our unique stack of building blocks for a unique problem and this stack was invented here.”

I have created a bunch of building blocks for C#. Check out my github account at https://github.com/rhyous. You will see a few reusable building blocks:

  • Rhyous.Collections – You know all those pesky extension methods your write for collections that are missing from the collections or from linq. I have a lot of them in here.
  • Rhyous.EasyCsv – A simple tool for working with csv files.
  • Rhyous.EasyXml – A simpel tool for working with Xml. (You might ask why I don’t have one for JSON, and that is because Newtonsoft.Json and fast.jsona already exist , so another one isn’t needed.)
  • Rhyous.EntityAnywhere – Wow, have a full rest api and only have to create the model class. Are you kidding, this is probably the coolest project for Web Service APIs since the REST pattern was introduced.Rhyous.SimplePluginLoader – Easily load plugins in your app.
  • Rhyous.SimpleArgs – Writing a tool with command line arguments? This tool allows you to configure your arguments in a model class and be done. It will output usage and force required parameters and allow for events when a parameter is set, etc.
  • Rhyous.StringLibrary – You know all those pesky extension methods you write for string manipulations missing from .NET Framework. They are in this library, along with a pluralization tool. Every heard of the The oft forgotten Middle Trim, well, it is in this library, too.
  • WPFSharp.Globalizer – The best localization library for WPF that exists, allowing you to change language and style (including left to right flow for certain languages) at runtime.

I actually have many more building blocks. Take a look.

DateTime Within Extension Method

I wrote and extension method to DateTime today. I want to call something simple to see if one date is within a two days of another date. There isn’t a within method. I set out to create one and this what I came up with.

Let me know what you think.

using System;
using System.Collections.Generic;

namespace Rhyous.WebFramework.Handlers.Extensions
{
    public enum DateTimeInterval
    {
        Miliseconds,
        Seconds,
        Minutes,
        Hours,
        Days,
        Weeks,
        Months,
        Years
    }

    internal class DateTimeIntervalActionDictionary : Dictionary<DateTimeInterval, Func<double, TimeSpan>>
    {
        #region Singleton

        private static readonly Lazy<DateTimeIntervalActionDictionary> Lazy = new Lazy<DateTimeIntervalActionDictionary>(() => new DateTimeIntervalActionDictionary());

        public static DateTimeIntervalActionDictionary Instance { get { return Lazy.Value; } }

        internal DateTimeIntervalActionDictionary()
        {
            Add(DateTimeInterval.Miliseconds, TimeSpan.FromMilliseconds);
            Add(DateTimeInterval.Seconds, TimeSpan.FromSeconds);
            Add(DateTimeInterval.Minutes, TimeSpan.FromMinutes);
            Add(DateTimeInterval.Hours, TimeSpan.FromHours);
            Add(DateTimeInterval.Days, TimeSpan.FromDays);
            Add(DateTimeInterval.Weeks, (double d) => { return TimeSpan.FromDays(d * 7); });
            Add(DateTimeInterval.Months, (double d) => { return TimeSpan.FromDays(d * 30); });
            Add(DateTimeInterval.Years, (double d) => { return TimeSpan.FromDays(d * 365); });
        }

        #endregion
    }

    public static class DateExtensions
    {
        public static bool IsWithin(this DateTime dateTime, double interval, DateTimeInterval intervalType, DateTime comparisonDateTime)
        {
            TimeSpan allowedDiff = DateTimeIntervalActionDictionary.Instance[intervalType].Invoke(interval);
            TimeSpan diff = dateTime - comparisonDateTime;
            return allowedDiff <= diff;
        }
    }
}

The problems with the Tiobe Index in regards to .Net

The Tiobe index is really missing one piece of information about .Net for its users. Java is #1. So users should use Java, right? Well, maybe not. Let’s talk about the problems with it before we move on.

I am going to make an argument that:

  1. Java is actually a more clear #1 than suggested.
  2. .Net is #2 behind Java, but not as far behind as the Tiobe index makes it appear.

Problem 1 – DotNet Framework is not listed as one a language

.Net has more languages writing against it than just one. That makes it appear less popular because the language is more fragmented. In fact, two of them are in the top 5 or 6. However, the fact that a dll compiled in either language can be consumed by either language is really not described here. I am not saying this should be on the same list of programming languages, but Tiobe should make it clear that the combined .Net languages show .Net as being used more heavily. Similary for Java, there are other languages that compile to the JVM. Perhaps there should be a page on compile target: What percent of languages compile to .Net’s Common Intermediary Language or compile to the Java Virtual Machine or to machine code or don’t compile at all?

As for intermediary languages, there are only two that stand out: Java and .Net. And Java is #1 but it only has 1 in the top 10. .Net has two in the top 10 and the combined languages are easily a rival to the combined JVM languages.

Look at the Tiobe index and add up the .Net Framework languages:

.Net Framework Languages

Language 2019 Tiobe Rating
Visual Basic .Net 5.795%
C# 3.515%
F# 0.206%
Total 9.516%

Notice that combined, the number of the three main .Net languages is %9.516. That puts .Net in the #3 position behind Java, C, and C++.

Problem 2 – Some .Net languages are missing and may be lumped in other languages

What about Visual C++? Yes, you can write .Net code in C++. However, that language is completely missing from Tiobe. Or is it? Is all Visual C++ searches lumped in with C++? If so, shouldn’t Visual C++ be separate out from C++. What is the Tiobe raiting Visual C++ would get? That would be hard to guess. But it is a language has been around for almost two decades. Let’s assume that a certain percentage of C++ developers are actually doing Visual C++. Let’s say it is more than F# but quite a lot less than C#. Let’s just guess because unlike Tiobe, I don’t have have this data. Let’ say it was .750. Again, this is a wild guess. Perhaps Tiobe could comment on this, perhaps they couldn’t find data on it themselves.

.Net Framework Languages

Language 2019 Tiobe Rating
Visual Basic .Net 5.795%
C# 3.515%
F# 0.206%
F# 0.206%
Total 10.266%

As you see, .Net combined is clearly #3 just by combining the .Net languages. Well past Python, which in fact can be used to both code for .Net (IronPython) and for the Java JVM (Jython). What percent of python is used for that?

Here is a wikipedia list of .Net-based languages: https://en.wikipedia.org/wiki/List_of_CLI_languages.

Similarly, for Java, languages like Groovy up it’s score. Here is a wikipedia list of Jvm-based languages: https://en.wikipedia.org/wiki/List_of_JVM_languages.

Problem 3 – Visual Studio is Awesome

For all the problems and complaints of bloat, Visual Studio is the move feature rich IDE by such a long ways that I doubt any other IDE will ever catch up to it, except may Visual Studio Code, which, however, is just as much part of the Tiobe index problem as Visual Studio is.

The better the tool, the less web searching is needed. The breadth of the features in Visual Studio is staggering. The snippets, the Intellisense, the ability to browse and view and even decompile existing code means that .Net developers are not browsing the web as often as other languages. My first search always happens in Intellisense in Visual Studio, not on Google. The same features and tooling in other IDEs for other languages just isn’t there. Maybe Exclipse, but only with hundreds for plugins that most developers don’t know about.

After Visual Studio 2012 released, the need to search the web has decreased with every single release of Visual Studio. I am claiming that C#, which is the primary .Net Framework language microsoft code for in Visual Studio, is used far more than Visual Basic .Net. Tiobe has Visual Basic .Net at 5.795% and C# at 3.515%, but reality doesn’t match Tiobe’s statististics. C# is used far more than Visual Basic .Net.

I am making the hypothesis that as the primarily coded language in Visual Studio, C# would appear to go down in the Tiobe index since the release of Visual Studio 2012. Let’s test my hypothesis by looking at the Tiobe year-by-year chart for C#. Do we see the Tiobe index going down starting with the release of VS 2012?

After looking at the Tiobe index, I am upgrading my claim from a hypothesis to a theory.

Other .Net languages may not experience the same as C# as the tooling in .Net is primarily focussed around C#.

So the reality is that the Tiobe index is showing the data it can find from search engines, but the data for C# is just not there because a lot of the number of ways C# deflects the need to search.

I hypothesise that C# reached a peak Tiobe index of 8.763% and it’s usage has not actually gone down. Instead, it has gone up. However, the data doesn’t exist to prove it. Assuming the hypothesis is correct, and C# usage has gone up, then the rate it should be is closer to 9 or 10. That means the C# is probably #3 on it’s own.

If we adjust to take this problem into account, simply by using the 2012 index and not assuming the the usage rate has gone up, we see the following:

.Net Framework Languages

Language 2019 Tiobe Rating
Visual Basic .Net 5.795%
C# 8.7%
F# 0.206%
F# 0.206%
Total 17.606%

Now, I am not saying .Net is above Java with my hypothesized 17.505% adjusted rating. Java has other languages as well that compile to the JVM that would similarly raise it and it is still #1.

Problem 4 – Direct linking to or searching on Microsoft.com

Microsoft has done a great job with a lot of their documentation. Some of this could be attributed to Visual Studio as well. After clicking a link in Visual Studio, we are taking directly to a site like https://msdn.microsft.com where I do a lot of my language searches.

Also, Microsoft has built a community where customers can ask questions and get data.

Tiobe has a nice document that clearly states which search enginers did not qualify and what the reason they didn’t qualify was.

  • Microsoft.com: NO_COUNTERS

See: https://www.tiobe.com/tiobe-index/programming-languages-definition/

I would argue that a significant amount of searches for .Net languages are done primarily on Microsoft.com. I can only provide personal data. I often go directly to the source documentation on Microsoft.com and search on Microsoft’s site. And once I am there almost all further searches for .Net data occur there.

Microsoft has more C# developers in their company that many programming languages have world wide. Are they doing web searches through the list of qualified search engines?

Problem 5 – Better documentation

I hypothesize that the better the documentation, the less searching on the web is required. I also hypothesize that Microsoft is one of the best at providing documentation for it’s languages.

Because the documentation for .Net framework is so excellent, the question is usually answered in a single search instead of multiple searches that languages that are less well documented may require.

Problem 6 – Education

Colleges are teaching certain languages. Python and C++ are top languages taught in college. I would estimate that because of these, the languages primarily taught in college have far higher good search rates. Unfortunately, .Net languages, because of their former proprietary nature (which is no longer the case with the open source of .Net Core), were shunned by colleges.

It would be interesting to filter out searches by college students. Unfortunately, how would Tiobe know that a search came from a college student or not.

Problem 7 – Limited Verbage

Tiobe is only looking at certain words. The words that are being queried are:

  • C#: C#, C-Sharp, C Sharp, CSharp, CSharp.NET, C#.NET

Further, Tiobe says:

The ratings are calculated by counting hits of the most popular search engines. The search query that is used is

+"&lt;language&gt; programming"

This problem piggy backs on Problems 3, 4, and 5. Visual Studio is so awesome, that we know exactly what we are looking for. As a C# developer, I don’t type C# into my searches hardly at all. I type something like: WebApi, WCF, WPF, System.Net.Http or Entity Framework or LINQ, Xamarin, and many other seaches. Microsoft documentation is so clear and specific (Problem 5) that we can do highly specific searches without including the word C#.

Yes, other languages have libraries, too, but do other languages have Microsoft’s marketing department that brands libraries with trademarks and logos and makes that brand the goto phrase to search? I don’t think there is a single other programming language other than C# that does this. Microsoft is lowing the web searches for C# by their awesome marketing.

This is further evidence to explain why the actual usage of C# has gone way up while the Tiobe index has gone way down. Asp.Net, Ado.Net, Razor, WCF, WebApi, WPF, WF,  etc. What other language has logos and brands around specific parts of a language?

Problem 8 – Is C# always seen as C# in search engines

I don’t always add C# to my google searches. However, when I do, it is somehow changed to just C. The sharp symbol, #, is often removed. This recently stopped happening on Google, but it used to happen with every search in every browser. It was frustrating.

Has this been addressed in search engine stats?

Conclusion

The belief that C# is in the 3% range is an unfortunate error of circumstances. And .Net should be looked at is the second most important tool for a programmer, second only to Java, and above all other programming languages.

 

 

How to Replace WCF Serialization with Json.Net without Wrapping and with UriTemplate Support

First, yes, I am still using WCF. Let’s move passed that concern to the real concern.

There are a dozen blog posts out there that explain how to replace the WCF serializer with Json.Net, however, every last one of them says that you must use wrapping and using parameters in the UriTemplate is not supported. https://blogs.msdn.microsoft.com/carlosfigueira/2011/05/02/wcf-extensibility-message-formatters

Just search the internet for WCF IDispatchMessageFormatter Json.Net. You will find all the articles that only work without UriTemplate support.

Well, I needed it to work with UriTemplate support without wrapping.

Turns out that this solution is far easier than I expected. I came accross this solution only after spending hours browsing Microsoft’s code.

So, to start, using parameters in the UriTemplate means that your Url or Url parameters will be specified in the UriTemplate and will have parameters.

For example, the Odata spec says that you should access an entity by Id with this a Url similar to this one:

https://somesite.tld/some/service/Users(1)

Then the method for the WCF service is like this:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "Users({{id}})", ResponseFormat = WebMessageFormat.Json)]
OdataObject Get(string id);
public virtual OdataObject Get(string id) 
{
    // code here
}

That is fine for a GET call as it doesn’t have a body. But what about a POST, Patch, or PUT call that does have a body? And what about now that the world is realizing that a GET sometimes needs a body?

Also, the examples provided a lot of code to figure out if it is a GET call and not even use the custom Json.Net IDispatchMessageFormatter. None of that code is necessary with this solution.

Let’s look at a PUT call that updates a single property of an entity as this has two parameters in the UriTemplate as well as a message body.

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "Users({{id}})/{{Property}} ResponseFormat = WebMessageFormat.Json)]
string UpdateProperty(string id, string property, string value);

public virtual OdataObject Put(string id, string property, string value)
{
// code here to update user
}

So there are two parameters in the UriTemplate, id and property, and the last parameter, value, is in the message body. Not a single solution for replacing the WCF serializer with Json.Net supports this scenario. Until now.

The goal is to deserialize the request with Json.Net. But the solutions provided break UriTemplate parameters in trying to reach the goal. The goal is not to replace the default WCF UriTemplate parameter work.

So now we can define a new problem: How do we deserialize the body with Json.Net but still have the UriTemplate parameters handled by WCF? The code to deserialize is the same code for both the parameters and the message body. We need to get the parameters without having WCF use the default deserializer for the message body.

Turns out, this problem is easy to solve.

Microsoft published their WCF code. Look at this code, lines 50-54: https://github.com/Microsoft/referencesource/blob/master/System.ServiceModel.Web/System/ServiceModel/Dispatcher/UriTemplateDispatchFormatter.cs

If you notice in line 50, WCF has the number of parameters from the Url and Url parameters and it subtracts that from the total list of parameters. If the message has not body, the subtraction result is always 0. If the message has a body, the subtraction always results in 1, telling WCF to deserialize the body. Well, I want WCF to do what it normally does with UriTempalte parameters, so if there is no body, use the WCF default stuff (which all the blogs say to do, but they do it the hard way).

Solution:

  1. In the custom EndPointBehavior, on the override, store the default IDispatchMessageFormater and pass it into the CustomDispatchMessageFormatter.
protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
    var parentFormatter = base.GetReplyDispatchFormatter(operationDescription, endpoint);
    return new CustomDispatchMessageFormatter(this, operationDescription, parentFormatter);
}
  1. If there is no body, use the WCF default DeserializeRequest method. This vastly simplifies the code on the blogs out there. The other examples had masses of code upstream that just wasn’t needed when message.IsEmpty could be used.
  2. If there is a body but no parameters, just use Json.Net.
  3. If there is a body and there are UriTemplate parameters, create a temparary parameter array 1 size smaller and pass that into the default serializer.
  4. Copy the temp array to the orignal array.
  5. Then just deserialize with Json.Net.
public void DeserializeRequest(Message message, object[] parameters)
{
     if (message.IsEmpty || parameters.Length == 0)
         ParentFormatter.DeserializeRequest(message, parameters);
     else
         DeserializeMessageWithBody(message, parameters);
} 

private void DeserializeMessageWithBody(Message message, object[] parameters)
{
     if (parameters.Length > 1)
     {
         object[] tmpParams = new object[parameters.Length - 1];
         ParentFormatter.DeserializeRequest(message, tmpParams);
         tmpParams.CopyTo(parameters, 0);
     }
     if (message.GetWebContentFormat() != WebContentFormat.Raw)
         throw new InvalidOperationException("Incoming messages must have a body format of Raw.");
     byte[] rawBody = message.GetRawBody();
         var type = OperationDescription.Messages[0].Body.Parts.Last().Type;
         parameters[parameters.Length - 1] = RawBodyDeserializer.Deserialize(rawBody, type);
}

The deserializer becomes vastly simplified now that it isn’t trying to also handling wrapped parameters.

public class RawBodyDeserializer : IRawBodyDeserializer
{
    public object Deserialize(byte[] rawBody, Type type)
    { 
        using (MemoryStream ms = new MemoryStream(rawBody))
        using (StreamReader sr = new StreamReader(ms))
        {
            JsonSerializer serializer = new JsonSerializer();
            return serializer.Deserialize(sr, type);
        }
    }
}

Debugging Open Source dependencies included as NuGet packages

You may encounter the need to debug into a dependency that is NuGet package. If this NuGet package is proprietary, you need to contact the vendor. However, if the NuGet package is open source, perhaps on GitHub, then you have all the tools you need to debug into it. Debugging into an open source NuGet package is what this article is about.

We are going to use Rhyous.StringLibrary for this example. It is a simple open source project that provides some common extensions to strings. These are extensions that are often found duplicated in many different projects and sometimes multiple times in the same project.

Step 1 – Check out the Source

Check out the repo from GitHub. You need a Git client. If you don’t have one, you can use GitHub Desktop or the one that is included in the Windows install of Git.

  1. Check out the repository: 
    git fetch https://github.com/rhyous/StringLibrary.git 

Step 2 – Compare Assembly Versions

Some NuGet packages have different assembly versions than the code. I know, they shouldn’t be it happens. Make sure that the assembly version of the dll reference via the nuget package is the same as the assembly version in the downloaded source.

  1. In your project that references the NuGet package, expand the references, highlight the dll that came from the NuGet package, and note the assembly version.

  2. In the download NuGet package source project, check the Assembly version. This is different in .NET Framework and .Net Standard, but it should be easy to figure out in both.

Step 3 – Build the Solution

  1. Open the StringLibrary.sln in Visual Studio.
  2. Click Build.
  3. Go to the output directory and copy the dll and pdb files.

Step 4 – Copy the dll and pdb to your solution

If you go to your project that references the dll, find and highlight the reference and go to properties, you can see the full path to the referenced dll.

  1. Go to the solution folder of the project you are working on.
  2. Go to your project that references the dll.
  3. Under References, locate the dll.
  4. Go to Properties of the dll reference by pressing F4.
  5. Note the path to the dll.
  6. Go into the Packages directory.
  7. Find the folder for Rhyous.StringLibrary.
  8. Locate the dll folder. 
  9. Rename the existing rhyous.stringlibrary.dll to rhyous.stringlibrary.dll.orgininal.
  10. Copy the compiled dll and pdb from Step 2 to this folder.
  11. Clean and build your solution.

Step 5 – Add a breakpoint

You should now be able to step into the Rhyous.StringLibrary source from your project.

Note: If you have two instances of Visual Studio open, one for your project and one for Rhyous.StringLibrary project, you may think you put the break point in on the one with the SimplePluginLoader project. You don’t.  You don’t even need the Rhyous.StringLibrary project open, unless you need to make a change and recompile and recopy the dll and pdb to the packages directory. You simply need to step into the code in order to set a break point.

Note: One trick is to go to Tools | Options | Debugging | General and turn off Step over Property operators (Managed Only).

  1. Debug your poject.
  2. Put a break point on the call to Rhyous.StringLibrary you would like to step into.
  3. Step into the call to Rhyous.StringLibrary.
    Once you have stepped into the call, you should see it’s source.
    Continue stepping into or over or whatever you would like.
    Once you are in the source, you can add breakpoints.
    Note: If you know how to add a break point without first stepping into the project, let me know.

You should now be easily debugging your NuGet package.

Why long emails should be avoided as a Dev Lead

I keep failing to a avoid a common mistake as a leader. Sending long emails. It seems so easy. For whatever reason, as the dev lead, I cannot talk to a person face-to-face so I write a long email.

I could spend time talking about why email is bad, or I could show you how emails make people feel by showing you an email dialogue.

Why long emails should be avoided:

Dev Lead: I’m being a good mentor. Write a nice long email that will help the team grow on a subject A, that includes tons of info on Subject A, including its 5 benefits. I send this email to Dev1 and CC the other two members of my team.
Feels good about his leadership.

Dev 1: What the dev thinks: Uh, oh. The dev lead is having a hissy fit again. Looks like he is pissed at something I did. What a jerk.
Feels angry.

Dev 2: Oh no. I have no idea what the dev lead is talking about. Do I know my stuff? Googles and tries to learn what the dev lead is talking about.
Feels shamed.

Dev 3: Ugh! Why is he trying to teach me crap I already know.
Feels patronized.

Manager: Hey, the team didn’t appreciate that email.

Dev Lead: Feels like a poor leader.

Manager: Feels like he is losing his team.

Why it would have happened better face-to-face:

Dev Lead: Hey devs. I want to discuss subject A. What do you know about it already?

Dev 1: I’ve used it before

Dev 2: Stays silent.

Dev 3: I know all about Subject A.

Dev Lead: OK, Dev 3, tell us about subject A.

Dev 3: Gives four excellent points about subject A. One of them the dev lead didn’t know.

Dev Lead: Adds two points about subject A that Dev 3 didn’t know. Changes his list from 5 to 6 adding the one item Dev 3 did knew.
Feels impressed by Dev 3.

Dev 1: Feels growth.

Dev 2: Feels good to be introduced to a new subject.

Dev 3: Impressed that the dev lead let him educate the team.
Feels more respect for dev lead. Also notes that the Dev Lead knew things he didn’t and thinks he should listen more.

Manager: Feels good about the team.

It is all about the feelings, and there is something about face-to-face team interaction that leads to good feelings and something about long emails that always leads to bad feelings.

So, if you look at the face-to-face interaction, you can see that it all started with a short question. You could simulate this in a short email:

Dev Lead: Who can give me all the benefits of Subject A using only the knowledge in your head. No browser search allowed until after you respond.

Dev 1: Responds with the single most common benefit if subject A.

Dev 2: Doesn’t respond.

Dev 3: Responds with four items, one that the dev lead didn’t now about.

Dev Lead: Interesting. Here are the items that the team responded with. I added two more benefits for a total of 6. Should we use subject A to get those 6 benefits in our project?

Now imaging the response was crickets.

Dev Lead: Who can give me all the benefits of Subject A.

Dev 1: Doesn’t respond.

Dev 2: Doesn’t respond.

Dev 3: Responds with one item.

Dev Lead: Subject A is interesting and important to our project. I am going to create a quick training on it.

Dev Lead: Writes a doc on it and sends it to the team.

Team: Feels good to learn something new.

Manager: Feels like the team is running itself.

Tips

  1. Keep emails short.
  2. Use many short emails.
  3. Ask questions, preferable one-liners:
    1. Start by asking your team what they already know first.
    2. Ask follow-up questions second
  4. Compile responses into a bulleted list
    1. Add to the list if you can
    2. Ask questions about the list
  5. Thank the team

I am going to put these tips into practice next time I feel like sending a long email.

Code Review – Quick Reference

This is a simple check-list to make code reviews more valuable. Simply check these rules.

Download a single page word document: Code Review Cheat Sheet

What % of code reviews should pass the first time?

About 10% should pass the first time. The other 90% of the time, a code review should fail. Now this low 10% percentage of first-time successful code reviews might increase as a team matures and members follows these rules as a behavior, but if you start to see really high first-time successful reviews, you might need to re-focus on this article.

However, if 100% or even 80% of code reviews are passing without comments, then you are likely not code reviewing correctly.

Does the code follow the 10/100 Rule?

This is a quick check rule that isn’t extremely rigid. See the 10/100 rule of code

This rule is what I call the S.O.L.I.D. training wheels. For more on S.O.L.I.D., see below. Code often follows all other rules naturally if it follows the 10/100 Principle.

Method has less than 10 lines

Is the method that was added or changed 10 lines or less? (There are always exceptions such as Algorithms)

100

Is the class 100 lines or less?
Note: Model classes should have zero functions and be closer to 20 lines.

Existing code that breaks the 10/100 Rule

Did the class get smaller? We don’t make it bigger.

Is the code S.O.L.I.D.

S.O.L.I.D. is an acronym. See this link: https://en.wikipedia.org/wiki/SOLID

Single Responsibility Principal

Does each class have a single responsibility? Does each method have a single responsibility?
Is this the only class that has this responsibility? (No duplicate code or D.R.Y. (Don’t Repeat Yourself)

Open/Closed Principle

Can you extend the functionality without modifying this code? Config, Plugins, event registration, etc.
Is there configuration in this code? If so, extract it. Configuration does not belong in code.
Encapsulation. Is everything private or internal except the few things that should be published as public?

Liskov substitution principle

Is inheritance used? If so, does the child type cause issues the parent type wouldn’t cause?

Interface segregation principle

Does the code use interface-based design?
Are the interfaces small?
Are all parts of the interface implemented without throwing a NotImplementedException?

Dependency inversion principle

Does the code reference only interfaces and abstractions?
Note: If new code references concrete classes with complex methods, it is coded wrong.

Cyclomatic Complexity

Cyclomatic Complexity is the number of linearly-independent paths through a program module. Basically, if statements, conditions, etc. Higher is bad.

Are there too many conditions or branches in a piece of code? If so, that can make it buggy. Usually you never have high Cyclomatic Complexity if you follow the 10/100 Principle.

Is the code Unit Tested

99% coverage

Is the Code 99% covered? Is code not covered marked with the ExcludeFromCodeCoverageAttribute?

Test Names

Are tests using proper names: <ClassName>_<MethodName>_<State>_<Result>

AAA Pattern

Tests should be written with Arrange, Act and Assert parts clearly marked, often with comments.

Parameter Value Tests for methods with parameters

Are all parameter values that could cause different behavior covered?
See these links:
Unit testing with Parameter Value Coverage (PVC)
Parameter Value Coverage by type

Test Example

Example in C#: Imagine a class called MyClass with a method: string DoSomething(string s);

public void MyClass_DoSomething_ParameterSNull_Throws()
{
   // Arrange
   var myClass = new MyClass();
   var expected = "SomeExpectedValue";
   // Act
   var actual = myClass.DoSomething();
   // Assert
   Assert.AreEqual(expected, actual);
}

Naming things

Typos

Are your names typo free?

Naming convention

Do your file names, class names, method names, variable names match existing naming conventions?

Specific Names

Are the names specific. Avoid general words “SomethingManager” or “AbcHelper”. General names should be avoided. Specific names are self-documenting.

Big O

Do you have any glaringly obvious Big O problems? n or n2 vs when it could be constant or log n.
See: https://en.wikipedia.org/wiki/Big_O_notation

String Handling

Are you handling strings safely?

Are there magic strings? Do you understand the difference between volatile (untrusted strings) and nonvolatile (trusted strings).

Documentation

Did you write documentation? Is every public method/property/field documented?

Self-review

Has the submitter reviewed their own code. I understand that some tools don’t allow you to submit a review for your own change. That is not what I am talking about. I am talking about you have reviewed your own code, added comments anticipated questions from reviewers, and made sure the code meets the code review check-list yourself.

You should be failing your own reviews once or twice before anyone else even has a chance to look at them. If the tool doesn’t support a self-review, then leave a comment, saying: I have reviewed this myself and it is ready for others to review.

Parameter Value Coverage by Type

This article is a reference to Unit Testing with Parameter Value Coverage (PVC).

Primitive or Value Types

See this reference.

Short Name .NET Class Type Width Range (bits)
byte Byte Unsigned integer 8 0 to 255
sbyte SByte Signed integer 8 -128 to 127
int Int32 Signed integer 32 -2,147,483,648 to 2,147,483,647
uint UInt32 Unsigned integer 32 0 to 4294967295
short Int16 Signed integer 16 -32,768 to 32,767
ushort UInt16 Unsigned integer 16 0 to 65535
long Int64 Signed integer 64 -9223372036854775808 to 9223372036854775807
ulong UInt64 Unsigned integer 64 0 to 18446744073709551615
float Single Single-precision floating point type 32 -3.402823e38 to 3.402823e38
double Double Double-precision floating point type 64 -1.79769313486232e308 to 1.79769313486232e308
char Char A single Unicode character 16 Unicode symbols used in text
bool Boolean Logical Boolean type 8 True or false
object Object Base type of all other types
string String A sequence of characters
decimal Decimal Precise fractional or integral type that can represent decimal numbers with 29 significant digits 128 ±1.0 × 10e−28 to ±7.9 × 10e28

byte

  1. Zero, 0, which is also byte.MinValue.
  2. A positive byte between 0 and 255.
  3. byte.MaxValue or 255

sbyte

  1. Zero, 0, which is also sbyte.MinValue.
  2. A positive sbyte between 0 and 127.
  3. A negative sbyte between -128 and 0.
  4. sbyte.MaxValue or 127
  5. sbyte.MinValue or -128

int

  1. A positive int between 0 and 2,147,483,647
  2. A negative int between -2,147,483,648 and 0
  3. Zero, 0
  4. int.MaxValue or 2,147,483,647
  5. int.MinValue or -2,147,483,648

uint

  1. Zero, 0, which is also uint .MinValue.
  2. A positive uint between 0 and 4,294,967,295.
  3. uint .MaxValue or 4,294,967,295

short

  1. A positive short between 0 and 32,767
  2. A negative short between -32,768 and 0
  3. Zero, 0
  4. short.MaxValue or 32,767
  5. short.MinValue or -32,768

ushort

  1. Zero, 0, which is also ushort .MinValue.
  2. A positive ushort, such as 1 through 65,535.
  3. ushort.MaxValue or 65,535

long

  1. A positive long between 0 and 9,223,372,036,854,775,807
  2. A negative long between -9,223,372,036,854,775,808 and 0
  3. Zero, 0
  4. long.MaxValue or 9,223,372,036,854,775,807
  5. long.MinValue or -9,223,372,036,854,775,808

ulong

  1. Zero, 0, which is also ulong.MinValue.
  2. A positive ulong between 0 and 18,446,744,073,709,551,615.
  3. ulong.MaxValue or 18,446,744,073,709,551,615

float

  1. A positive float between 0 and 3.402823E+38
    1. Note: This includes the float.Epsilon, but you could test double.Epsilon separately
  2. A negative float between -3.402823E+38 and 0
  3. Zero, 0.0
  4. float.MaxValue or 3.402823E+38
  5. float.MinValue or -3.402823E+38
  6. float.NaN
  7. float.PositiveInfinity
  8. float.NegativeInfinity

double

  1. A positive double between 0 and 1.79769313486232E+308
    1. Note: This includes the double.Epsilon, but you could test double.Epsilon separately
  2. A negative double between -1.79769313486232E+308 and 0
  3. Zero, 0.0
  4. double.MaxValue or 1.79769313486232E+308
  5. double.MinValue or -1.79769313486232E+308
  6. double.NaN
  7. double.PositiveInfinity
  8. double.NegativeInfinity

decimal

  1. A positive double between 0 and 79,228,162,514,264,337,593,543,950,335
  2. A negative double between -79,228,162,514,264,337,593,543,950,335 and 0
  3. Zero, 0
  4. double.MaxValue or 79,228,162,514,264,337,593,543,950,335
  5. double.MinValue or -79,228,162,514,264,337,593,543,950,335

string

  1. A null string
  2. An empty string, String.Empty, or “”
  3. One or more spaces ” “
  4. One or more tabs ” “
  5. A new line or Environment.NewLine
  6. A valid string.
  7. An invalid or junk string
  8. A string with many special characters: `~!@#$%^&*()_-+=,.<>/\?[]{}|
  9. Unicode characters such as Chinese
  10. An long string, over 256 characters, or even 1 million characters.
  11. (Occasionally) Case sensitivity. For example, for string comparisons, case sensitivity of a string is a required Parameter Value Coverage test.

Struct

  1. This one takes thought. You need to define this per struct you create. For example, if your struct is a point with int values X and Y, then it is simply the int list above twice, once for X and once for Y.

Enum

  1. Any of the enums.
  2. You may need to do each of the enums, depending on how your enum is used.

Class or Reference Types

Class Object

Objects that are defined with the class keyword need the following tested:

  1. Null (This might go away or become optional in .NET 4.8)
  2. Instantiated
  3. Class properties can be primitive or value types, reference types, etc., and may need to be tested according to the type of the property.

Array, List, Dictionary, and other collections

Array, List, Collection

  1. Null
  2. Empty (instantiated with no items)
  3. Not empty but values of array are tested according to the value type. For example, an int[] would need to have the values tested in the ways listed above for int.
    1. Pay attention to how the code you are testing uses teh items in an array or list. If the items are objects, do you need to check if the list has a null item in the list?

Dictionary

  1. Null
  2. Empty (instantiated with no items)
  3. Key exists
  4. Key doesn’t exist
  5. Value at key is tested according to its value type. For example, a Dictionary<string, int> would need to have the values tested in the ways listed above for int.

Amazon Ec2 Instance Management with C#: Part 3 – Uploading and Importing a Key Pair

Before getting started

Skill Level: Beginner

Assumptions:

  1. You have completed Part 1 and 2 of Managing Amazon AWS with C# – EC2

Additional Information: I sometimes cover small sub-topics in a post. Along with AWS, you will also be exposed to:

  • .NET Core 2.0 – If you use .NET Framework, the steps will be slightly different, but as this is a beginner level tutorial, it should be simple.
  • Rhyous.SimpleArgs

Details

We may already have a key pair that we want to use, so we don’t want to create a new one. If that is the case, it can be uploaded.

Step 1 – Get key in the correct format

I used OpenSSL to do this.

  1. Download OpenSSL.
  2. Run this command:
    [sh]
    .\openssl.exe rsa -in c:\users\jbarneck\desktop\new.pem -pubou
    t -out c:\users\jbarneck\desktop\new.pub
    [sh]

Step 2 – Edit InstanceManager.cs file

We’ve created InstanceManager.cs in Part 1. Let’s edit it.

  1. Add a method to read the key file from disk and upload and import the key pair.
  2.         public static async Task ImportKeyPair(AmazonEC2Client ec2Client, string keyName, string keyFile)
            {
                var publicKey = File.ReadAllText(keyFile).Trim().RemoveFirstLine().RemoveLastLine();
                string publicKeyAsBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(publicKey));
                await ec2Client.ImportKeyPairAsync(new ImportKeyPairRequest(keyName, publicKeyAsBase64));
            }
    

Notice: We are calling RemoveFirstLine() and RemoveLastLine(); This is because key files have a header and footer that must be removed before sending up to AWS. We’ll do this in the next section.

Step 3 – Add methods RemoveFirstLine and RemoveLastLine

  1. By the time this publishes, you should only need to install Rhyous.String.Library. Otherwise, add this class file:
    namespace Rhyous.AmazonEc2InstanceManager
    {
        public static class StringExtensions
        {
            public static string RemoveFirstLine(this string text, char newLineChar = '\n')
            {
                if (string.IsNullOrEmpty(text))
                    return text;
                var i = text.IndexOf(newLineChar);            
                return i > 0 ? text.Substring(i + 1) : "";
            }
    
            public static string RemoveLastLine(this string text, char newLineChar = '\n')
            {
                var i = text.LastIndexOf(newLineChar);
                return (i > 0) ? text.Substring(0, i) : "";
            }
        }
    }
    

Step 4 – Configure command line Arguments.

We already have an Actions arguments to edit.

  1. Add DeleteKeyPair as a valid action to the Action argument.
  2. Add an additional argument for the key file.
                . . .
                new Argument
                {
                    Name = "Action",
                    ShortName = "a",
                    Description = "The action to run.",
                    Example = "{name}=default",
                    DefaultValue = "Default",
                    AllowedValues = new ObservableCollection<string>
                    {
                        "CreateKeyPair",
                        "DeleteKeyPair",
                        "ImportKeyPair"
                    },
                    IsRequired = true,
                    Action = (value) =>
                    {
                        Console.WriteLine(value);
                    }
                },
                . . .
                new Argument
                {
                    Name = "KeyFile",
                    ShortName = "pem",
                    Description = "The full path to a public key already created on your file system in PEM format. The full Private key won't work.",
                    Example = "{name}=c:\\My\\Path\\mykeyfile.pem",
                    CustomValidation = (value) => File.Exists(value),
                    Action = (value) =>
                    {
                        Console.WriteLine(value);
                    }
                }

You can now upload a public key file for use on the Amazon Cloud.

Next: Part 4

Return to: Managing Amazon AWS with C#

Interviewing: A developer should have a portfolio

I recently started interviewing for some contract positions, one a Software Developer in Test position and one a Senior Software Developer position. I am deeply surprised by the candidates complete lack of having an online presence. As I thought more about this, I realized that we have reached a point of maturity in the Software Developer roles that portfolios are now expected. I expected every candidate to have an active account on some open source source control repository, i.e. GitHub, and have a portfolio of code there.

Portfolio

When it is time to interview for a position as a developer, you should have a portfolio. The days of coding on a whiteboard should be over. Instead, an interviewer should be able to easily see your code and what you have or haven’t done.

There shouldn’t be a question about whether you can write code. Instead, the question should be: Based on the code we can see this individual has written, can they be a good fit for our team?

Proprietary code

Your portfolio cannot include proprietary code. End of discussion. If you are a developer and you can’t find code that isn’t proprietary to put into your portfolio, then what are you doing?

Open Source/Non-proprietary code

Even when working with proprietary code, there are many pieces of code that are so ubiquitous that they probably should be part of the .NET framework. You may use this code in every project you work on. Such as common string extensions in C#, or a more complete string check in javascript that checks if a string is undefined, null, empty, or whitespace.

Even better is if your code is not just stored, but it is available to be used, such as with NuGet, npm, Maven, or other code or library packaging tool. This shows that you not only have a portfolio, but you aren’t going to waste your hours rewriting code you have already written.

Where to keep your portfolio

I used to have mine on SourceForge but have since switched to GitHub. Visual Studio online is another option. Where you store your portfolio of your work does not matter as much as the fact that you do store it.

GitHub is where I chose. But you can easily Google for GitHub competitors if you want it to be elsewhere.

Brand your portfolio

My internet handle is Rhyous. Every piece of code I write that is part of my portfolio (Non-proprietary or not for someone else’s open source project) is now branded with Rhyous. Some of my older code may not be, but my new code is. For example, all my namespaces in C# now start with Rhyous. That makes it very easy to differentiate projects I have forked vs projects that I have developed.

What your portfolio must show

It must show:

  • You have skills as a developer.
  • SOLID principals.
  • An understanding of the importance of Unit Tests.
  • You refuse to waste time writing the same code twice.**
  • You can work on projects with other developers.
  • You bring more than just your skill set, you bring your ready-made building blocks.

** I find this to be so very important!

My Portfolio

My portfolio shows my skills as a developer. My code uses SOLID principals. Much of my code is Unit Tested.

I don’t like to write the same code twice. I for one, will never have to write a CSV parser in C# again as I have a good quality one: Rhyous.EasyCsv. Parsing arguments? I’ll never write an argument parser again because I have Rhyous.SimpleArgs. I will never have to write many of my string extensions again as I can easily grab them for any C# project from my Rhyous.StringLibrary NuGet package. Tired of using TryGetValue to get values from your dictionary? Try Rhyous.Collections and use the NullSafe dictionary, which still uses the TryGetValue but moves it inside the indexer so you don’t have to worry about it.

What a lack of portfolio shows

It has never clicked for you. What I mean by “It” is the idea of code reuse. The idea of object-oriented programming. The idea of standing on the shoulders of giants. The simple idea of using building blocks as a kid and make things from building blocks.

Go out and make your portfolio and fill it with building blocks so every time you build something new, you can build on foundation building blocks that are SOLID and just get better.

Amazon Ec2 Instance Management with C#: Part 2 – Deleting a Key Pair

Before getting started

Skill Level: Beginner

Assumptions:

  1. You have completed Part 1 of Managing Amazon AWS with C# – EC2

Additional Information: I sometimes cover small sub-topics in a post. Along with AWS, you will also be exposed to:

  • .NET Core 2.0 – If you use .NET Framework, the steps will be slightly different, but as this is a beginner level tutorial, it should be simple.
  • Rhyous.SimpleArgs

Step 1 – Edit InstanceManager.cs file

We’ve created InstanceManager.cs in Part 1. Let’s edit it.

  1. Add a method to delete the key pair.
  2.         public static async Task DeleteKeyPair(AmazonEC2Client ec2Client, string keyName)
            {
                await ec2Client.DeleteKeyPairAsync(new DeleteKeyPairRequest { KeyName = keyName });
            }
    

Step 5 – Configure command line Arguments.

We already have an Actions arguments to edit.

  1. Add DeleteKeyPair as a valid action to the Action argument.
  2.                 . . .
                    new Argument
                    {
                        Name = "Action",
                        ShortName = "a",
                        Description = "The action to run.",
                        Example = "{name}=default",
                        DefaultValue = "Default",
                        AllowedValues = new ObservableCollection<string>
                        {
                            "CreateKeyPair",
                            "DeleteKeyPair"
                        },
                        IsRequired = true,
                        Action = (value) =>
                        {
                            Console.WriteLine(value);
                        }
                    },
                    . . .
    

Next:

  • Part 3 – Uploading and Importing a Key Pair
  • Return to: Managing Amazon AWS with C#

    Amazon Ec2 Instance Management with C#: Part 1 – Creating a Key Pair

    Before getting started

    Skill Level: Beginner

    Assumptions:

    1. You already have Visual Studio installed.
    2. You are familiar with creating projects in Visual Studio.
    3. We assume you have already gone to AWS and registered with them. If you haven’t done that already, stop and go there now. Amazon has a free tier and you can create an account here: https://aws.amazon.com/free
    4. Some of the points that were discussed in the Amazon S3 article series will not be repeated here.

    Additional Information: I sometimes cover small sub-topics in a post. Along with AWS, you will also be exposed to:

    • .NET Core 2.0 – If you use .NET Framework, the steps will be slightly different, but as this is a beginner level tutorial, it should be simple.
    • async, await, Task
    • Rhyous.SimpleArgs
    • Reflection

    Note: As this is the first post of a series, there is going to be some setup. If you just want the quick and code, look at the code in Step 3.

    EC2 Instance Prerequisites

    Before creating an instance, there are some prerequisites. This Part 1 discusses the first of these prerequisites: creating a Key pair.

    Step 1 – Create the project

    1. Open Visual Studio.
    2. Go to File | New Project.
    3. Choose Console Application.
      Give it any name you want.
      I am going to call my project Rhyous.AmazonEc2InstanceManager.

    Step 2 – Add NuGet Packages

    1. Right-click on your project and choose Management NuGet Packages.
    2. Search for AWSSDK.EC2.
    3. Install the NuGet package and all the dependencies.
    4. Search for System.Configuration.ConfigurationManager.
    5. Install it.
    6. Search for Rhyous.SimpleArgs.
    7. Install it.

    Step 3 – Create an InstanceManager.cs file

    1. Create a new class file called InstanceManager.cs.
    2. Add a method to generate the key pair.
    3. Add a method to save the key pair to disc as a .pem file.
    4. using Amazon.EC2;
      using Amazon.EC2.Model;
      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Threading.Tasks;
      
      namespace Rhyous.AmazonEc2InstanceManager
      {
          public class InstanceManager
          {
              public static async Task&amp;lt;KeyPair&amp;gt; CreateKeyPair(AmazonEC2Client ec2Client, string keyName, bool writePem = false, string keyOutputDirectory = null)
              {
                  var keyPair = (await ec2Client.CreateKeyPairAsync(new CreateKeyPairRequest { KeyName = keyName }))?.KeyPair;
                  if (writePem)
                      await SaveKeyPairToDisc(keyName, keyOutputDirectory, keyPair);
                  return keyPair;
              }
      
              public static async Task SaveKeyPairToDisc(string keyName, string keyOutputDirectory, KeyPair keyPair)
              {
                  var path = Path.Combine(keyOutputDirectory, $&amp;quot;{keyName}.pem&amp;quot;);
                  await File.WriteAllTextAsync(path, keyPair.KeyMaterial);
                  Console.WriteLine($&amp;quot;They key pair was saved to: {path}&amp;quot;);
              }
          }
      }
      

      Notice: To create the Key pair, we use an instance of an AmazonEC2Client with a simple CreateKeyPairRequest model.
      Notice: Saving the Key pair has really nothing to do with Amazon EC2 Instance management. This is just a simple File.WriteAllTextAsync call.

    Step 4 – Create/Edit the App.config

    Of course, it is important to know what Amazon Web Services account we are working with, so we will store this in the app.config.

    1. If there isn’t an app.config in your project, create one.
    2. Right-click on your project and choose Add | New Item.
    3. Search for Application Configuration File.
      Rename it to app.config.
    4. Add an appSetting for your AWS profile name.
    5. Add an additional appSetting for your chosen AWS region.
      &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot; ?&amp;gt;
      &amp;lt;configuration&amp;gt;
        &amp;lt;appSettings&amp;gt;
          &amp;lt;add key=&amp;quot;AWSProfileName&amp;quot; value=&amp;quot;yourprofilename&amp;quot;/&amp;gt;
          &amp;lt;add key=&amp;quot;AWSRegion&amp;quot; value=&amp;quot;us-west-2&amp;quot; /&amp;gt;
        &amp;lt;/appSettings&amp;gt;
      &amp;lt;/configuration&amp;gt;
      

    Step 5 – Configure command line Arguments.

    We are going to be adding to this program in subsequent posts. For this reason, we are going to use Rhyous.SimpleArgs library for our command line arguments as it provides ready-made command line argument features. If you have been through the S3 series, you will already be familiar with Arguments and the convention we chose to match command line arguments to the method parameter names.
    Note: You should have already installed the Rhyous.SimpleArgs NuGet package.

    1. Create an ArgsHandler.cs file to define the arguments:
      Note: If you used a .NET core project you have to create this file. If you created a .NET Framework file, this file should have been created for you and you have but to edit it.
    2. Add an Action argument.
      Note: We are going to keep the exact same convention of having an Action argument that allows us to choose which method to run and for each method and have an Argument to match each method parameter name.

    3. Add an Argument for each dynamic parameter in the CreateKeyPair method.
      • string keyName
      • string keyOutputDirectory

      Note: The AmazonEC2Client parameter is not a dynamic parameter. It is fixed and doesn’t change.

    4. Also, make sure to call Program.OnArgsHandled() in the HandleArgs method.
    5. using Amazon.EC2.Util;
      using Rhyous.SimpleArgs;
      using System;
      using System.Collections.Generic;
      using System.Collections.ObjectModel;
      
      namespace Rhyous.AmazonEc2InstanceManager
      {
          public class ArgsHandler : ArgsHandlerBase
          {
              public override void InitializeArguments(IArgsManager argsManager)
              {
                  Arguments.AddRange(new List&amp;lt;Argument&amp;gt;
                  {
                      new Argument
                      {
                          Name = &amp;quot;Action&amp;quot;,
                          ShortName = &amp;quot;a&amp;quot;,
                          Description = &amp;quot;The action to run.&amp;quot;,
                          Example = &amp;quot;{name}=default&amp;quot;,
                          DefaultValue = &amp;quot;Default&amp;quot;,
                          AllowedValues = new ObservableCollection&amp;lt;string&amp;gt;
                          {
                              &amp;quot;CreateKeyPair&amp;quot;
                          },
                          IsRequired = true,
                          Action = (value) =&amp;gt;
                          {
                              Console.WriteLine(value);
                          }
                      },
                      new Argument
                      {
                          Name = &amp;quot;KeyName&amp;quot;,
                          ShortName = &amp;quot;k&amp;quot;,
                          Description = &amp;quot;A key pair name.&amp;quot;,
                          Example = &amp;quot;{name}=MyKeyPair&amp;quot;,
                          Action = (value) =&amp;gt;
                          {
                              Console.WriteLine(value);
                          }
                      },
                      new Argument
                      {
                          Name = &amp;quot;keyOutputDirectory&amp;quot;,
                          ShortName = &amp;quot;k&amp;quot;,
                          Description = &amp;quot;A key pair name.&amp;quot;,
                          Example = &amp;quot;{name}=MyKeyPair&amp;quot;,
                          Action = (value) =&amp;gt;
                          {
                              Console.WriteLine(value);
                          }
                      }
                  });
              }
      
              public override void HandleArgs(IReadArgs inArgsHandler)
              {
                  base.HandleArgs(inArgsHandler);
                  Program.OnArgumentsHandled();
              }
          }
      }
      

    Step 6 – Add the MethodInfoExtension

    This has nothing to do with AWS, but we built it in the S3 series, so were are going to use it for this series as well. It is a class file that uses reflection to dynamically detect the needed parameters of our action methods. I am not going to go into it any more that that in this post. I’ve modified this file to work for this project. Just add it to your project. If you want to understand it more, go through the S3 posts.

    1. Create a MethodInfoExtensions.cs.
      using Amazon;
      using Amazon.EC2;
      using Rhyous.SimpleArgs;
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Reflection;
      
      namespace Rhyous.AmazonEc2InstanceManager
      {
          public static class MethodInfoExtensions
          {
              public static List&amp;lt;object&amp;gt; DynamicallyGenerateParameters(this MethodInfo mi)
              {
                  var parameterInfoArray = mi.GetParameters();
                  var parameters = new List&amp;lt;object&amp;gt;();
                  var region = RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings[&amp;quot;AWSRegion&amp;quot;]);
                  foreach (var paramInfo in parameterInfoArray)
                  {
                      if (paramInfo.ParameterType == typeof(AmazonEC2Client))
                          parameters.Add(Activator.CreateInstance(paramInfo.ParameterType, region));
                      if (paramInfo.ParameterType == typeof(string))
                          parameters.Add(Args.Value(paramInfo.Name));
                  }
      
                  return parameters;
              }
          }
      }
      

    Step 7 – Edit the Program.cs

    1. Update Program.cs as follows:
      using Rhyous.SimpleArgs;
      using System;
      
      namespace Rhyous.AmazonS3BucketManager
      {
          class Program
          {
              static void Main(string[] args)
              {
                  new ArgsManager&amp;lt;ArgsHandler&amp;gt;().Start(args);
              }
      
              internal static void OnArgumentsHandled()
              {
                  var bucketName = Args.Value(&amp;quot;Bucket&amp;quot;);
                  var task = BucketManager.CreateBucket(bucketName);
                  task.Wait();
              }
          }
      }
      

    Now for fun, you can delete the app.config and change them to parameters.

    Next: Deleting a key pair
    Return to: Managing Amazon AWS with C#

    Understanding async and await, Task.WaitAll, Task.Run, and parallelism: part 2

    So another issue with async, await came up. I was using Task.WaitAll to make it parallel, but it wasn’t parallel.

    var task1 = Method1();
    var task2 = Method2();
    Task.WhenAll(task1, task2)
    

    Now Method1() and Method2() had some code that took some time. Now my code was making some large web service calls, but we can demonstrate this more easily just by sleeping for 10 seconds.

    public async Task Method1()
    {
        Thread.Sleep(10000); // Ten seconds
    }
    
    public async Task Method2()
    {
        await Task.Delay(10000); // 10 second task
    }
    
    

    I expected these to run in parallel, but they didn’t. Let me explain why.

    When calling a method that returns task, it doesn’t actually return until the first await. Notice Method1() doesn’t have an await. So Method1() will run synchronously until the first await.

    Method2(), however, is pretty much the same, but does have an await. So it returns immediately.

    Getting parallelism is easy. Wrap the method in Task.Run().

    public async Task Method1()
    {
    Task.Run(Thread.Sleep, 10000)); // Ten seconds
    }

    Amazon S3 Bucket Management with C#: Part 10 – Uploading all files in a directory recursively to an S3 Bucket

    Before getting started

    Skill Level: Intermediate

    Assumptions:

    1. You already gone through Parts 1-9 of Managing Amazon AWS with C#.

    Additional information: I sometimes cover small sub-topics in a post. Along with AWS, you will also be exposed to:

    • Rhyous.SimpleArgs
    • Single Responsibility Principle (S in S.O.L.I.D.)
    • async, await, parallelism
    • 10/100 rule

    Doing things by convention.

    Step 1 – Add a method to get the list of files in a local directory

    This isn’t the focus of our post, however, in order to upload all files in a directory recursively, we have to be able to list them. We are going to create a method that is 10 lines of code. The method has one single repsponsibility, to return all files in a directory recursively. It is not the responsibility of BucketManager.cs to do this. Hence we need a new class that has this responsibility.

    Another reason to move this method to its own file is that this method is itself 10 lines of code. While you can have methods longer than ten lines, more than ten lines is usually the first sign that the Single Responsibility principal is broken. Most beginning developers have a hard time seeing the many ways a method may be breaking the single responsibility principle. So a much easier rule, is the 10/100 rule. In the 10/100 rule, a method can only have 10 lines. This rule is pretty soft. Do brackets count? It doesn’t matter. What matters is that the 10 line mark, with or without brackets, is where you start looking at refactoring the method by splitting it into two or more smaller and simpler methods. This is a a Keep It Super Simple (K.I.S.S.) rule.

    1. Add the following utility class: FileUtils.cs.
      using System.Collections.Generic;
      using System.IO;
      using System.Linq;
      using System.Threading.Tasks;
      
      namespace Rhyous.AmazonS3BucketManager
      {
          public static class FileUtils
          {
              public static async Task&amp;lt;List&amp;lt;string&amp;gt;&amp;gt; GetFiles(string directory, bool recursive)
              {
                  var files = Directory.GetFiles(directory).ToList();
                  if (!recursive)
                      return files;
                  var dirs = Directory.GetDirectories(directory);
                  var tasks = dirs.Select(d =&amp;gt; GetFiles(d, recursive)).ToList();
                  while (tasks.Any())
                  {
                      var task = await Task.WhenAny(tasks);
                      files.AddRange(task.Result);
                      tasks.Remove(task);
                  }
                  return files;
              }
          }
      }
      

    Notice: The above class will get all files and directories, recursively. It will do it in parallel. Parallelism likely isn’t needed most of the time. Any non-parallel code that could list files and directories recursively would work. But if you were going to sync directories with tens of thousands of files each, parallelism might be a huge benefit.

    Step 2 – Add an UploadFiles method to BucketManager.cs

    1. Edit file called BucketManager.cs.
    2. Enter this new method:
              public static async Task UploadFiles(TransferUtility transferUtility, string bucketName, string directory)
              {
                  var files = await FileUtils.GetFiles(directory, true);
                  var directoryName = Path.GetFileName(directory); // This is not a typo. GetFileName is correct.            
                  var tasks = files.Select(f =&amp;gt; UploadFile(transferUtility, bucketName, f, f.Substring(f.IndexOf(directoryName)).Replace('\\', '/')));
                  await Task.WhenAll(tasks);
              }
      

    Notice 1: We follow the “Don’t Repeat Yourself (DRY) principle by having UploadFiles() forward each file to the singular UploadFile().
    Notice 2: We don’t use the await keyword when we redirect each file UploadFile. Instead we capture the returned Task objects and then we will await the completion of each of them.

    Step 3 – Update the Action Argument

    We should be very good at this by now. We need to make this method a valid action for the Action Argument.

    1. Edit the ArgsHandler.cs file to define an Action argument.
                          ...
                          AllowedValues = new ObservableCollection&amp;lt;string&amp;gt;
                          {
                              "CreateBucket",
                              "CreateBucketDirectory",
                              "CreateTextFile",
                              "DeleteBucket",
                              "DeleteBucketDirectory",
                              "ListFiles",
                              "UploadFile",
                              "UploadFiles"
                          },
                          ...
      

    Note: There are enough of these now that I alphabetized them.

    Step 4 – Delete the Parameter dictionary

    In Part 4, we created a method to pass different parameters to different methods.We took note in Part 8 and Part 9 that we now have more exceptions than we have commonalities. It is time to refactor this.

    Another reason to refactor this is because the OnArgumentsHandled method is seriously breaking the 10/100 rule.

    Let’s start by deleting what we have.

    1. Delete the Dictionary line from Program.cs.
              static Dictionary&amp;lt;string, object[]&amp;gt; CustomParameters = new Dictionary&amp;lt;string, object[]&amp;gt;();
      
    2. Delete the section where we populated the dictionary.
                  // Use the Custom or Common pattern
                  CustomParameters.Add("CreateBucketDirectory", new object[] { s3client, bucketName, Args.Value("Directory") });
                  CustomParameters.Add("CreateTextFile", new object[] { s3client, bucketName, Args.Value("Filename"), Args.Value("Text") });
                  CustomParameters.Add("DeleteBucketDirectory", new object[] { s3client, bucketName, Args.Value("Directory") });
                  CustomParameters.Add("DeleteFile", new object[] { transferUtility, bucketName, Args.Value("Filename") });
                  CustomParameters.Add("UploadFile", new object[] { transferUtility, bucketName, Args.Value("File"), Args.Value("RemoteDirectory") });
      

    Step 5 – Implement parameters by convention

    To refactor the parameter passing, To refactor this, we are going use a convention.

    A convention is some arbitrary rule that when followed makes the code work. You have to be very careful when using conventions because they are usually not obvious. Because they are not obvious, the first rule of using a convention is this: Conventions must be documented.

    The convention is this: Make the Argument names match the method parameters. Argument names are not case sensitive, so we don’t have to worry about case. Just name.

    There are two exceptions to this convention. AmazonsS3Client and TransferUtility. We will handle those exceptions statically in code.

    Now, let’s implement our convention.

    • For each Argument, make sure the associated parameter is the same name.
      • Change bucketName to bucket in all methods.
      • Change file to filename in the DeleteFile method.
      • Change UploadLocation to RemoteDirectory in the UploadFile method.
      • Change directory to LocalDirectory in the UploadFiles method.
    • Create the following MethodInfoExtension.cs.
      using Amazon;
      using Amazon.S3;
      using Amazon.S3.Transfer;
      using Rhyous.SimpleArgs;
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Reflection;
      
      namespace Rhyous.AmazonS3BucketManager
      {
          public static class MethodInfoExtensions
          {
              public static List&amp;lt;object&amp;gt; DynamicallyGenerateParameters(this MethodInfo mi)
              {
                  var parameterInfoArray = mi.GetParameters();
                  var parameters = new List&amp;lt;object&amp;gt;();
                  var region = RegionEndpoint.GetBySystemName(ConfigurationManager.AppSettings["AWSRegion"]);
                  foreach (var paramInfo in parameterInfoArray)
                  {
                      if (paramInfo.ParameterType == typeof(AmazonS3Client) || paramInfo.ParameterType == typeof(TransferUtility))
                          parameters.Add(Activator.CreateInstance(paramInfo.ParameterType, region));
                      if (paramInfo.ParameterType == typeof(string))
                          parameters.Add(Args.Value(paramInfo.Name));
                  }
      
                  return parameters;
              }
          }
      }
      

      Notice this class will dynamically query the parameters. AmazonS3Client and TransferUtility are exceptions. The rest of the parameters are created using a convention and pulled from Argument values.

    • Update Program.cs to use this new extension method.
              internal static void OnArgumentsHandled()
              {
                  var action = Args.Value("Action");
                  var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                  MethodInfo mi = typeof(BucketManager).GetMethod(action, flags);
                  List&amp;lt;object&amp;gt; parameters = mi.DynamicallyGenerateParameters();
                  var task = mi.Invoke(null, parameters.ToArray()) as Task;
                  task.Wait();
              }   
      

     

    Notice: Look how simple Program.OnArgumentsHandled method has become. By using this convention, and by moving the parameter creation to an extension method, we are down to six lines. The total size for the Program.cs class is 25 lines, including spaces.

    You can now move a directory to an Amazon S3 bucket using C#.

    <h3>Design Pattern: Facade</h3>

    Yes, we have just implement the popular Facade design pattern.

    Our project, and most specifically BucketManger.cs, represent an entire system: Amazon S3. When code is written to represent an entire system or substem, that code is called a Facade.

    Go to: Rhyous.AmazonS3BucketManager on GitHub to see the full example project from this 10 part tutorial.

    Return to: Managing Amazon AWS with C#