The Blog

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

Custom type converters for MongoDB

I always try to keep my models seperate from the implementation or configuration. EF Core 2.1 added great support for custom type converters with HasConversion. MongoDB also has a support for custom type conversion, though it is not quite as well documented. The basic premise revolves around implementing IBsonSerializer<TType>. Let's suppose I have a Bitrate

Continue Reading

Clean Architecture in .NET Core 3.0 by Jason Taylor

This video is truly one of the best ones I've ever seen. I am not fully sold on using EF Context everywhere. There are quite a few databases (e.g. MongoDB, ElasticSearch) that do not have an EF Driver. However, since .NET Core has amazing support for integration testing and each handler is nicely isolated, so

Continue Reading