Tech Blog

Unit Testing with Mocks

By: Chris Dunn on Tue Jul 16, 2019 (9 min read)

If you are interested in an academic, or more intellectual understanding of Mocks, Stubs, Fakes which collectively are known as Test Doubles, it's always a good idea to visit Martin Fowler. Stubs Aren't Fakes. For this post I want to talk about testing in isolation using Mocks and Dependency Injection.  The examples in this post will be using Moq(https://github.com/moq/moq) but there a number of other options out there to choose from. Making your code testable In order to unit test in isolation, ...

Password Hashing using Rfc2898DeriveBytes

By: Chris Dunn on Tue Jun 25, 2019 (6 min read)

With the recent data security breaches, we as developers need to make sure we are doing our best to secure the application data the best we can.  One very obvious problem is passwords and other sensitive data stored in plain-text in a user's database record on the server. For data that you must maintain intact, like perhaps a social security number or similar identifier, you need to encrypt the data completely.  It's always a good idea to take a long hard look at what data you must maintain.  Th ...

Initialize data before Angular App Starts

By: Chris Dunn on Tue Jun 11, 2019 (4 min read)

It's generally a good idea to separate client configuration options from the rest of your code and display logic. These options could include API Endpoints, Language options, logging configuration or display settings. It makes it easier to customize these options for the various environments in the development process (development, test, staging, production).   Angular supports the concept of client configuration with environment files.  At first this may seem like a perfect place for these conf ...

Many to Many Relationship in EF Core

By: Chris Dunn on Tue May 21, 2019 (5 min read)

A many-to-many relationship is one where you have two entities and entity x can have one or more of entity y, and entity y can have one or more of entity x.  Examples of this relationship are Products <-> Orders, BlogPosts <-> Tags, Albums <-> Artists, and in this post Courses <-> Students. In our example let's pretend we're writing course scheduling software for a University.  In that scenario, we might have two models for our Entities, Course and Student declared as fol ...

Multiple Authentication Schemes

By: Chris Dunn on Tue Apr 23, 2019 (6 min read)

If you haven't read my post on cookie authentication in .net core, I suggest you do that now since I will be referencing it in the following post. There will be times when you will want more than one sign in under a web application.  This usually involves a member section and an administrative section of your application.  You could write two separate applications, one for the front-end, and another for the back-end.  Sometimes this is overkill and tricky depending on your deployment infrastruct ...

Typed AppSettings in .net core

By: Chris Dunn on Tue Apr 9, 2019 (5 min read)

It's always easier to work with typed information in a program than trying to remember string keys in a configuration file. So when working with the appsettings.json file in .net core, I advise you load the configuration file into a class object and work with the object directly, instead of GetSection() repeatedly throughout your code. It's also a good idea to create the class object as a service so you can inject it into controllers for ease of use while still maintaining test-ability. I'll do ...

Watch your case in Linux Containers

By: Chris Dunn on Tue Mar 19, 2019 (3 min read)

I am subconsciously Windows biased.  Maybe not subconsciously, but I do take case sensitivity and the file system for granted.  I am use to certain path information, special folders names, and file naming conventions. Embarking on the cross-platform possibilities available in .net moving forward, I (and you) will need to rethink some things we take for granted in windows. I recently ran into a situation while deploying a .Net Core web application to a Linux Docker container. The application was ...

Determine Current Operating System in .Net Core

By: Chris Dunn on Tue Mar 5, 2019 (2 min read)

What Operating System is my .net application running on?  Seems weird to ask that question doesn't it.  But with .net core we now have the opportunity to more easily deploy cross-platform solutions using .Net and C#. So previously the question of which OS platform we were running for our application didn't really concern many .net developers. Fortunately, we now have to consider such things. (I'm not forgetting mono). Luckily .Net Core has us covered with OS information included with the Runtime ...

Linq First vs Single

By: Chris Dunn on Tue Feb 19, 2019 (3 min read)

What's the difference between LINQs Single method and First method? While they can both return the same result, they serve a different purpose depending on the underlying sequence of data. Using the wrong method can cause problems in your code now, and as your data grows. First() First() should be used on sequences of data with a count of one or more. This method is returning the "FIRST" of possibly "MANY". If there are no results an exception will be thrown. One option, in this case, is to use ...

Configure Sessions in .net core

By: Chris Dunn on Tue Feb 5, 2019 (4 min read)

With .net core, if you want to use functionality you will, in most cases, need to explicitly configure it yourself via middle ware.  This is actually a good thing.  It keeps your code and dependencies clean.  So if we want to utilize sessions in our application, we'll need to configure the middle ware. Configuring Sessions Even though you need to configure it yourself, it's not hard by any means. I'll explain the process and then show the code.  First you will need to add the following code in t ...

Canceling an Asynchronous Task

By: Chris Dunn on Tue Jan 22, 2019 (8 min read)

When we have a long running operation, it nice to have the option to cancel the operation.  Luckily when they designed the TASK they gave developers the ability to more cleanly exit long running operations.  A TASK is part of the task based asynchronous pattern, which is the suggested pattern moving forward.  With this pattern they have included the idea of a CancellationToken which allows us to signal to a running TASK, that is should cancel. In the following example I've written a simple conso ...

Understanding the Limits of AI: When Algorithms Fail

By: Chris Dunn on Tue Jan 8, 2019 (3 min read)

I am lucky to live close to a wonderful institute of learning, Indiana University. I decided it was time to take advantage of some of the opportunities available to me to grow as a developer. So, I have been making an effort this year to attend seminars and colloquiums sponsored by the Computer Science department. I recommend everyone take the time to see what's available locally. At the end of October I had the great pleasure of attending a lecture by Timnit Gebru, a postdoctoral fellow at Mic ...

Angular patchValue vs setValue

By: Chris Dunn on Tue Dec 4, 2018 (2 min read)

When creating model driven forms in Angular, for CRUD operations, you will need to map a data model to the FormGroup.  You can do this by manually setting each control value or you can use a function to map the entire data model to the FormGroup model.  At first glance of the documentation it looks like a good option would be setValue on the FormGroup object. myForm.setValue(formData, {onlySelf: true}); This will work if you have a matching control in the Form Group model (control name) for eac ...

Configuring Cors Policy in .net core

By: Chris Dunn on Tue Nov 20, 2018 (3 min read)

To allow sharing responses cross-origin and allow for more versatile fetches than possible with HTML’s form element, the CORS protocol exists. It is layered on top of HTTP and allows responses to declare they can be shared with other origins. Source They is a lot more in depth information on CORS than I could provide here in my post so I will rely on others if you are unfamiliar with what it's all about. See a good explanation from Mozilla. My goal for this post it to show you how to configure a ...

Cleanup relative paths in Angular/Typescript

By: Chris Dunn on Tue Nov 6, 2018 (3 min read)

Angular projects usually start out pretty simple with a similarly simple folder structure.   Most likely in the beginning you simple drop all components, directives and services in the /app folder just to get things working.  As your program grows, you realize (hopefully) that if you're going to survive you will need to apply some sort of structure moving forward. This can cause it's own problems by turning our nice, short import statements from this: Import { MyComponent } from './mycomponent.c ...

Cookie Authentication in .net core

By: Chris Dunn on Tue Oct 16, 2018 (5 min read)

Most web applications today, with personalization and integration, require authentication to access the site. One of the more basic and simplest forms of authentication is using cookies. If you have a history with .net, cookie authentication is what you will most be familiar with. As with all things, .net core has a slightly different approach to implementing cookie authentication, so I thought I would walk through a basic implementation you can use as a starting point. Setup Authentication With ...

Fluent Builder Pattern vs Constructor

By: Chris Dunn on Tue Oct 2, 2018 (7 min read)

Design patterns are important when developing applications. When people first discover GOF design patterns, they either reject them or are eager to rewrite their code base to make use of the new concepts. The same way I would caution about rejecting Design Patterns out of hand just beause they are trendy, I wouldn't rewrite for the sake of doing something new. I personally like the Fluent interface we find in LINQ, Entity Framework and other libraries. I first read about the interface from Marti ...

Why you can't pass property as out or ref parameter

By: Chris Dunn on Tue Sep 11, 2018 (3 min read)

Sometimes compiler or language rules seem arbitrary, but actually make much more sense if you know why they are in place.  It's never a bad idea to peak under the hood, from time to time to get some perspective. If you're writing a method or utilizing a method with a ref or out parameter, like TryParse, you'll soon realize that you can't pass in an object property as the out or ref parameter. You will meet with the following error "A property or indexer may not be passed as an out or ref paramet ...

FormatWithTags Extension Method

By: Chris Dunn on Tue Aug 14, 2018 (4 min read)

The string.format(..) method for building strings with variables is useful when you have a string template and want to replace values within the text. The only problem I have with this method is that it uses numeric placeholders for the values. That's fine if you have one or two values to integrate, but if you're dealing with more formatting of the text, it's problematic. string.Format("My name is {0} and I live in {1}.", "Chris", "Indiana");  Note: String interpolation will not work in this ca ...

Parse CVS Data with LINQ

By: Chris Dunn on Tue Jul 10, 2018 (4 min read)

In the world of BIG DATA we're dealing with more than just JSON, XML and SQL.  A lot of data comes in raw Comma Separated Values (CSV) format.  A number of times in the past I've imported CSV data into SQL server and queried the data that way.  With LINQ, we now possess that same concise query power (somewhat) in our code. It gives us some more options especially if we don't want the weight of a database (or server) for a client application. Also, if you're starting to get involved in machine le ...

Debug Stopwatch and Performance monitoring

By: Chris Dunn on Tue Jun 12, 2018 (4 min read)

If you're into performance analysis you will want to know about .net's Stopwatch class in the System.Diagnostics namespace.  It's great for monitoring the execution time of an operation and identifying performance black holes in your code.  It's also more precise and performant than using repeated DateTime.Now calls (which we're all guilty of doing).  Understand that Stopwatch is not your only option and in certain cases PerformanceCounters may be a better solution. Here's the basic code to get ...

StripHtml string extension

By: Chris Dunn on Tue Apr 10, 2018 (3 min read)

If you're working on a web project with customizable html content you will need, at some point, a way to strip the Html in order to access the raw text.   One scenario I use often is automatically generating an intro for a listing page for products, by using the html formatted product description.  I do this by cleaning the html out of the description, and then grabbing the first 255 characters to display.  In the case of extracting a preview or description of html text, I suggest you cache or p ...

Web Scraper Data Tool

By: Chris Dunn on Tue Mar 13, 2018 (3 min read)

A number of years ago I was migrating and merging a series of websites into a Content Management System.  We were attempting to organize a number of mixed technology sites, some hard-coded html, others ASP or PHP, under a single CMS.  Rather than manually translate each page into the new system, I decided to try parsing sites that followed a template pattern and import sections into the properties of the new system pages.  I developed a web scraping tool to extract that data based on a data map ...

Auto Sign out

By: Chris Dunn on Tue Feb 6, 2018 (3 min read)

This functionality seems to be more prominent on financial websites than others, but something all sites should consider.  Even though we automatically expire someones session or login, sensitive information can still exist on the page. Unless the page makes a call to the server, the page won't know the session has expired.  So, if you see auto-sign out show up in your specs, here's how you do it. Setup The following javascript code needs to function in cooperation with your server authenticatio ...

Highlight JS Syntax Highlighter

By: Chris Dunn on Tue Jan 9, 2018 (4 min read)

When you're writing a technical blog, you need a good syntax highlighter.  I didn't want to point to external files or even GitHub Gists for every sample or partial piece of code.  While there are probably hundreds of tools for the job, I ended up choosing Highlight JS, and couldn't be happier.  Here's how I went about setting it up including a minor customization I added for the display.   NOTE: If you have a code display option in your CMS or Wiki, I would try give that a try first and see if ...

Copyright 2019 Cidean, LLC. All rights reserved.

Proudly running Umbraco 7. This site is responsive with the help of Foundation 5.