Category: .NET

Architecting WPF - Part 1 - Closer Look at MVVM pattern

I've been spending a lot of time learning about WPF recently and its chief design pattern: MVVM. It is partly work-related, as we've been doing some major backend refactoring and could take the advantage to change some of the front-end too. We've been gradually adopting WPF because apparently, Blazor and co. are still unproven to

Continue Reading

Why is primitive obsession still a thing? - .NET Perspective

Imagine, .NET didn't have DateTime struct, what would you do then? Sure, some of you would find a library (or write the library), but I bet most of the time, we would simply pass a date as a string or an int (e.g. epoch time). This is known as a primitive obsession. 4 Good Reasons

Continue Reading

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