The Blog

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

Internals Visible To in *.csproj

Making internal methods and properties visible in tests gives a finer level granularity of tests hopefully making them less coupled and more cohesive as well as more focused on what they are testing. This is without making the API visible to the outside. In the older days, we used add into the 'Assembly.cs'

But

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

MongoDB and C# decimals

MongoDB used to serialize C# decimals to strings. This wasn't an issue when simply storing and retrieving values. However, if you'd like to search or sort by the value, it makes the value pretty useless. The decimal value would need to be stored both as a decimal (precise number) for use and a double for

Continue Reading

Unit testing MongoDB serialization

If you added custom serializers to your model, you almost certainly want to be able to unit test it. TDD is strongly encouraged here, simply because it is very easy to make mistakes. IBsonSeralizers themselves are not so easy to test and you can make a very legitimate argument they should be made internal and

Continue Reading

Adding custom type converter to MongoDB in C#

I covered how to impelement a custom type converter for MongoDB in my previous article. In this article, I want to cover adding a serializer to MongoDB 'serializer'. There are couple of tricks you can use to make your life easier. Unlike EF Core with its IEntityTypeConfiguration<TType>, MongoDB driver is more static. You can specify

Continue Reading