Category: .NET

System.IO.FileLoadException : Could not load file or assembly 'MongoDB.Driver.Legacy, Culture=neutral, PublicKeyToken=null'. Invalid pointer (0x80004003 (E_POINTER))

I setup a set of automated tests for one of my pet projects but annoyingly I came across the following error today: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.IO.FileLoadException : Could not load file or assembly 'MongoDB.Driver.Legacy, Culture=neutral, PublicKeyToken=null'. Invalid pointer (0x80004003 (E_POINTER)) The solution is to install

Continue Reading

EF Core - empty inverse property list despite using Include and ThenInclude

Yesterday, I created a new project to play around with Blazor and .NET 5. However, my fun was cut short when EF Core refused to fetch dependent child properties. This is despite using eager loading with Include. Clean projects, unit tests, nullable reference types, init properties, the code was super simple with just 2 classes

Continue Reading

EF Core - using letters instead numbers for enums in the database

Sometimes, I actually prefer to store letters for rather than integers in the database. Yes, it can be less performant (as you do need to implement a custom converter), but can make the database a lot easier to read. Consider the example below that TradeDirection. It might be more preferable to see B and S

Continue Reading

Extremely simple Producer Consumer Pattern in C#

.NET has introduced a library called System.Threading.Tasks.Dataflow which is now included with .NET Core. Library contains BufferBlock<T> which is great data structure to implement an extremely simple Producer-Consumer pattern.

Continue Reading

GUID as an Alternative Key in EF Core

EF Core supports GUIDs as IDs. However, I've run into a couple of instances where it is/not not supported by 3rd party libraries (like https://github.com/artiomchi/FlexLabs.Upsert). I also prefer to use numerical IDs in the database as they take less space and are far more convenient to work with. However, exposing sequential numeric IDs can lead

Continue Reading

IEnumerable Any() and First() - Fun C# Code 5

I ran into an interesting problem today. Using CsvHelper, I was getting a list of records from a file, then check if there are any records to process. If not then skip processing of that file, otherwise, pick the first record and do something with the data. I've added a couple of unit tests to

Continue Reading

Deconstructing KeyValuePair into a tuple in a foreach - Fun C# Code 4

Did you know you can deconstruct in a foreach loop? Suppose you have a dictionary

If used a lot, it might be worth adding it to System namespace; thereby avoiding adding using statements everywhere.

Continue Reading

Using LocalDB for Integration/Unit Tests

Long did I hold this perception that integration tests are hard. With EF core, they are actually really easy. Instead of using SQL Sever, we can substitute with LocalDB. Run this before running the tests and you'll have your db setup. After calling EnsureCreated you can add things into the database.

Continue Reading

Select Multiple Properties using Linq - Fun C# Code #3

C#'s Linq is an simply amazing. However, as far as I know, there is no built-in ability to select multiple fields (of the same type) and return them, so here you go

Continue Reading

ASP.NET Core - Optimisation Hacks

Google has developed a tool called PageSpeed. It highlighted couple of quick wins I could do to improve website's loading time. Website loading times are one of the most deceptive metrics to measure during development. Website development is frequently done on a local machine when download times are neligiable. It is only once the website

Continue Reading