Search and Replace with Regular Expressions in VSCode

One of the little gems in Visual Studio Code (VSCode) that I like, is the regular expression support in the search box. It is very powerful, but the thing that I like most is how simple it is.

There isn’t a large dialog with several options. It’s just a small button that changes a simple text search into a pattern matching search.

Here is the link to the video

Note: I wanted to try uploading a video on Youtube for this. I’d like to produce content on Youtube in the future, but I have no experience with it. The embedded video is basically me experimenting with the platform.

Cake build system: Command line arguments

I keep on forgetting how to form the command line in PowerShell for a Cake build script with multiple command line arguments.

In Cake, command line arguments are available in the build script as shown below:

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var version = Argument("build_version", "1.0.0.0");
var releaseCandidate = Argument("release_candidate", "0");

As you can see, the arguments can have a default value. For example, the default target is “Default”. That looks like this in the Cake script

Task ("Default")
    .IsDependentOn ("BuildSolution")   
    .IsDependentOn ("Run-Unit-Tests")
    .IsDependentOn ("BuildPackages");

Let’s say that we want to pass into the script the build version and a release candidate number.

.\build.ps1 -ScriptArgs "--build_version=1.0.0.12","--release_candidate=1"  

In Cake. You define a default build script called “build.cake”. Cake’s main script is called build.ps1, which bootstraps Cake and runs the “build.cake” script.

The example repository will teach you everything you need to know about Cake in about 15 minutes.

Merge Query for C#

I’m in the process of getting MergeQueryObject setup on github. This was one of my personal projects. Previously, I had been maintaining it in a private repository, but I think it is in a form where it would be usable by others.

https://github.com/JasonDV/MergeQueryObject

https://www.nuget.org/packages/ivaldez.Sql.SqlMergeQueryObject/

List of features:

  • Support for surrogate or natural primary keys
  • Update from a target table given a collection of DTOs.
    • Option to preform only Updates and no Inserts or Deletes.
  • Insert from a target table given a collection of DTOs.
  • When a collection of DTOs is “merged” into the target table, any records in the target table not in the collection of DTOs will be deleted.
    • Option 1: Ignore Deletes
    • Option 2: Delete from a target table given a collection of DTOs.
    • Option 3: Mark for Delete (“soft delete”) on records in target table given a collection of DTOs.
  • Target updates to only specific fields by using a DTO that partially matches the target table
  • Target changes to a portion of a very large table.
    • For example, target changes to a specific date range
  • All features of BulkLoader are supported.
  • Create a temporary source table or Create an concrete source table.

MergeQueryObject’s primary use is performing updates, inserts, and deletes into a large target table using minimal C# syntax. The basic “merge” concept comes from the fact that a source temp table is setup with the wanted values and the Merge statement executes the necessary actions to make the target table match the source table. This can be done for the entire table or portions of a table.

It is a low level utility that sits just above the database and works with POCO DTOs, using my other project BulkLoader to load the temp table.

Creating Query Objects is something that should be done for complex SQL. It helps with maintainability of the code and makes it easier to conceptually test and verify, since the query object has only one responsibility.

Cross-platform targeting

SQL BulkLoader was my first attempt at multi-targetting a nuget package.

https://github.com/JasonDV/SQLBulkLoader

There are some interesting best practices around this concept.

https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/cross-platform-targeting

Normally, when working on line-of-business applications, I have a lot of control over what .NET target I’m using. I choose mostly based on which targets are current and what is installed on the deployment infrastructure. Getting configuration changes to deployment environments is often time consuming; therefore, I avoid it unless upgrades justify the time.

In the future, I’ll explore some of my justifications when putting together a solution and choosing technologies, structure, and environment.

Coursera – something new

I just discovered Coursera.

https://www.coursera.org/courses

I’ve been aware of the progression toward low-cost or no-cost online education, but I had felt like I hadn’t the time to really examine it.

Recently, I had been looking for avenues to grow my skillsets. I decided to give Coursera a try. I’m just putting my toe into the water, though. I don’t feel ready to commit to a lot because I’m not sure how it will work with time constraints. I also have personal projects that I’ve been wanting to get off the ground for a while.

Cryptography seems to be a good starting point.

LINQ Practice project on GitHub

Note: as of 3/2019 this has been retired

While doing project work for business or pleasure. I often find myself in a position where I need to test out a thought or experiment with some technology. Usually, I simply spin-up a throwaway project.

Linq is one of those technologies that I have used extensively, but in a limited scope. It has been over a year since I had to perform a join operation in Linq, which left me doing some research that reminded me that Linq is rather elaborate and extensive.

To fill out the back story here, I was coding a service recently and implemented a loop to accomplish what could have been done with a Linq join. The reason it had not occurred to me was because I had forgotten the specific join syntax.

For that reason, I put up a project on GitHub for Linq experiments.

https://github.com/JasonDV/LinqPractice (I took this link down)

I plan to expand the project as needed.

Removing strings when using INotifyPropertyChanged

Update: I wrote this article a long time ago, while I was playing with an idea. Another possibility in C# 6.0 is to use the nameof() utility method.

I had an interesting idea this morning about adding an extension that would allow me to get a method name without using a string value.

The intended use was to add some safety to the use of the INotifyPropertyChanged interface. Most implementations of this interface use a string when calling OnPropertyChanged(“…”). I wanted to do something that would allow me to use refactoring tools to change the name of a property without having to update a string value manually.

As a novelty, I decided to implement it as an extension to all objects. It could just as easily be a utility method.

///
/// UtilExtensions is a class of utility extensions
///
public static class UtilExtensions
{   /// The ToName extensions is added to all non value-type objects.
    public static string ToName<T, P>(this T obj, Expression<Func<T, P>> action) where T : class
    {
        string retVal;

        if (action.Body is MethodCallExpression)
        {
            retVal = ((MethodCallExpression)action.Body).Method.Name;
        }
        else if (action.Body is MemberExpression)
        {
            retVal = ((MemberExpression)action.Body).Member.Name;
        }
        else
        {
            throw new Exception("Unable to determine use: " + action.Body.GetType().ToString());
        }

        return retVal;
    }
}

It would be used like this:

public string MyName
{
    get { return _myName; }
    set {
        _myName = value;

        OnPropertyChanged(this.ToName(x => x.MyName));
    }
}

Apparently, this isn’t a new idea.

While I was researching this I discovered (from other blogs) that there is a new attribute in .NET 4.5 that would help with this issue. The [CallerMemberName] attribute can be used to automatically enter an optional parameter value with the name of the calling method.

It would look like this:

/// Execute the PropertyChanged event.
/// The [CallerMemberName] attribute is used to automatically supply the caller method name.
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

Links:

MVVM Light Toolkit – Initial

I’ve been looking over the MVVM Light Toolkit.

I haven’t had the opportunity to work with WPF extensively, in the past. For the most part, I’ve created custom controls or worked on maintenance of existing applications. Both of these activities keep me away from the architectural aspects of the MVVM. I know that there were several frameworks for the MVVM pattern that had been developed over the years. With my recent change in jobs, I have had the opportunity to start learning about MVVM Light Toolkit.

I’ve only started today, so this post serves mostly as a bookmark.

Toolkithttp://mvvmlight.codeplex.com/

Notes:

  • The CodePlex site has some getting started info, but I found it a bit outdated.
  • You can download an MSI from the CodePlex website, which will add the templates (project, viewmodel, etc.) to Visual Studio.
  • I haven’t tried it, but it should be possible to add the toolkit to an existing project using NuGet.
  • Overview: http://www.galasoft.ch/mvvm/

Getting started videohttp://www.youtube.com/watch?v=s-goeb2u3oA

Notes:

  • The ViewModelLocator is very interesting