How to use Autofac’s interceptor feature to apply the DRY principle for session and transaction management with NHibernate

Introduction

My past articles here on Medium covered fairly low-level topics, more in the form of Lego blocks that can be used as elements for building larger components. For example, we looked at how to use transactions with NHibernate. We also introduced dependency injection (DI) with Autofac.

In this article, we will combine two elements, i.e., we’ll combine NHibernate transactions with Autofac. At a high level the benefits of this approach are:

  • Handle transaction-related exceptions in a single place
  • Retry transactions in a single place
  • Change transactional behavior of classes by changing a single line…


How to use fakes and mocks to test classes in isolation at a much faster speed

Introduction

How fast is the test suite for your backend? Can you run 10,000 tests per minute on a laptop? If you can’t then in this article, we will look at techniques that will assist getting towards that speed. And yes, I am involved in at least one project where the test suite runs at a speed of up to 10k tests per minute.

How is this possible? There are several fundamental factors to consider:

  1. Eliminate cross-process interaction
  2. Test each class and method in isolation

The first factor means that you don’t want tests to access a drive…


How to reduce complexity while making testing easier in .NET 5

In this article:

  • What is the problem to be solved?
  • Basic DI concepts
  • DI in a console application
  • Autofac in an ASP.NET web application in .NET 5.0

Introduction

As software engineers we are continuously tackling complexity. Any technique that makes our lives easier is welcome. Dependency injection (DI), which is one way to achieve Inversion of Control (IoC), is one such technique. It helps us breaking complex software components down into smaller, much more manageable pieces of code. In this article I’ll provide a gentle introduction to using Dependency Injection in…


How to handle parent-child relationships in .NET with Fluent Migrator’s fluent interface

A todo-list can have many to-do list items

In this article:

  • Extending the domain model
  • Updating the object-relational mapping code
  • Resolving common NHibernate and database exceptions
  • Lazy Loading
  • Simplifying mapping code by using conventions

Introduction

Recap

In previous articles, we introduced NHibernate as the Object-Relational Mapper (ORM) to access data in a relational database. We then added transactions to keep data consistent. This will become more important in this article because we will expand our domain model with a one-to-many relationship.

Running Example

In this article, we’ll again be using as a running example a fictitious product named “Mahi”. …


Keeping your data consistent while avoiding code duplication

Boilerplate Code for Transactions

In this article:

  • Transactions in NHibernate
  • Rolling back transactions
  • Removing duplicated boilerplate code
  • Some house keeping

Introduction

Recap

In the previous article we introduced NHibernate as the Object-Relational Mapper (ORM) to access data in a relational database. We managed to write and read objects, which was a good first step. In this article we’ll build on this and look at how we can keep data consistent.

Running Example

In this article we’ll again be using as a running example a fictitious product named “Mahi”. The word Mahi means “task” in Te Reo…


Storing and reading data in a relational database without writing SQL

Repository using NHibernate

In this article:

  • A simple domain class
  • Using an object-relational mapper (ORM): NHibernate
  • Mapping classes to tables with Fluent NHibernate
  • Using the repository pattern to encapsulate database access

Introduction

Recap

In previous articles we looked at how to set up the development environment with Docker including a separate container for the relational database. We also looked at maintaining the database schema with Fluent Migrator and we updated the dev container to .NET 5.0. …


Changing the target framework and other dev environment improvements

Source: https://devblogs.microsoft.com/dotnet/introducing-net-5/

In this article:

  • Adding a missing tool required by some VS Code extensions
  • Adding an entrypoint script to improve running as non-root
  • Installing dotnet-outdated-tool for keeping nuget packages up-to-date
  • Switching the target framework to .NET 5.0

Introduction

Previously, we looked at how to create a dev container for .NET Core 3.1. Since then .NET 5.0 has been released. Therefore, in this article we’ll look at how upgrade the dev container to reflect those changes.

Running Example

As always, we’ll again be using as a running example a…


How to avoid issues caused by missing file change events when mounting Windows directories into a container

In this article:

  • Installing Windows Subsystem for Linux, version 2 (WSL2)
  • Installing Linux distro in WSL2
  • Configuring git for use in Linux distro
  • Launching VS Code in Linux distro
  • Launching and connecting to dev container
  • Avoiding common pitfalls

Introduction

Originally, I though this article would be about object-relational mapping (ORM) on .NET Core. Then I ran into what I thought were issues with VS Code extensions when I ran up dev containers that made use of NTFS folders mounted into the dev container.


Database Schema Maintenance with Fluent Migrator

In this article:

  • Writing and applying database schema migrations
  • A Database class to keep things simple
  • Using up-only migrations to simplify migrations

Introduction

Recap

In a previous article, we created a dev container (see article “Building a Dev Container for .NET Core”). Then we added a second container to run a database server we can use during development (see article “Dockerized SQL Server for Development”). This also served as an example how we can use pre-built container images to make our lives easier as developers.

At this point we…


How to quickly spin up and use a SQL Server instance cross-platform.

Compose file with database container

In this article:

  • Extending the dev container with a database server container
  • Automatically starting the database server
  • Automatically creating the database
  • Connecting to the database from the host

Introduction

Recap

In my previous article I described how to create a full-time development container for .NET Core 3.1 with VS Code. The key point is that the development container (or short “dev container”) not only runs for specific tasks such like compiling, debugging, etc. The idea is to keep the dev container running all the time.

In…

Manfred Lange

I’m a Principal Consultant at boutique firm HYPR Innovation in New Zealand. Currently, my main focus is helping clients to build scalable SaaS products.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store