Przejdź do głównej zawartości

How MongoDB + good programming practices saved my time

Today I'll show you how in I'm using MongoBD in web service part of Perfect Life application. I'll show how using good programming practices saves time.
I switch database to brand new instance of mongoDB. And I did it. And all that you'll see today was made within one hour. Interested? Keep reading...

As you know I tend to use interface to connect components in my application. It helps me to keep components loosely coupled.
For example I use dependency injection where controller uses IWeightRecord Repository.


Using interfaces this way makes it easy to switch my repository to something else.
In my case I'd like to create a new repository that depends on Mongo database instead of in-memory repository that uses Dictionary.
As the reault my data won't be deleted in case of web application restart. This is my target.
So first I'm adding a new Nuget package to my WebAPI application. The package is called MongoDB.Driver.
Next step is to create a new repository that I'm going ot use in my controller:

As you know my style of work I think the functionality is only halfway done without automated tests.
We need good integration test to make sure the application works fine:

The final step is to configure service provider in my ASP.NET Core MVC application in Startup.cs class.
I simply changed this line:
services.AddSingleton();
to this line:
services.AddSingleton();

This is what we managed to discover:
1) Using mongo in C# is really easy to configure - we operate on objects, not RDBMS using SQL - time savings when we don't need to create and maintain DB schema
2) When you use built-in Dependency Injection container in ASP.NET Core MVC, it's a matter of seconds to switch service. - Saving time!
3) When we keep in mind loosely coupling we can created a new repository and used it instead of the old one without a line of code in the controller class - saving time as we change only what we need.
4) With good suite of integration tests we can make significant changes in the logic of our application and be sure it's working without even running the app. Integration tests + Dependency Injection = huge time savings!
What is the summary thoughts? If we keep using good practices we can do amazing things in short time.
Not only maintenance is easy but with goot integration tests, you can be sure the app is working correctly always. There's need to run app and do manual tests while we use unit and integration tests.
I hope you'll use those techniques too and will create better applications in shorter time. Good luck?



Komentarze

Popularne posty z tego bloga

Unleash the power of ChatGPT in your C# applications with NGpt - C# OpenAI GPT client

Meet NGpt - OpenAI GPT C# client library that will make your conversations with ChatGPT a breeze! Have you ever been struggling building your own smart AI application in C#? Well, now you can use ChatGpt inside your application! Introducing NGpt, the powerful .NET library that lets you integrate ChatGPT seamlessly into your C# applications. No more nigts spent on building your own AI solitions, no more convoluted logic and building complex business rules - just pure AI bliss. In this blog post, we'll dive into the wonderful world of NGpt and show you how easy it is to use. You might even find a few laughs along the way. The Magic of NGpt NGpt is a transient fault-tolerant .NET 6 OpenAI client that simplifies ChatGPT integration for C# developers. It's like your friendly neighborhood Spider-Man - always there to help you out when you need it most. With just your OpenAI API key, you can start coding AI applications in C# faster than you can say "Peter Parker." Let...

Specflow czyli BDD w praktyce

Dzisiaj pokażę w praktyce w jaki sposób zbieram wymagania korzystając z narzędzia Specflow. Na początku przypomnę nasze historie użtkownika. Potem skonfigurujemy dodatek Specflow Visual Studio. Na końcu pokażę jak wygląda język Gherkin i zaimplementujemy pierwszą historię użytkownika US1. Oto lista funkcjonalności, które będę implementował w aplikacji Perfect Life: US1: Jako użytkownik aplikacji mobilnej chcę ekran logowania aby mieć możliwość zapisywania swoich postępów US2: Jako użytkownik aplikacji mobilnej chcę mieć kalkulator wskaźnika BMI aby móc mierzyć poziom swojej kondycji US3: Jako użytkownik aplikacji mobilnej chcę widzieć wykresy aby móc wizualnie interpretować swoje postępy US4: Jako użytkownik aplikacji mobilnej chcę mieć możliwość rozwiązania quizu, żeby zbadać wskaźnik Body Age US5: Jako użytkownik aplikacji mobilnej chcę pobierać pliki PDF aby mieć dostęp do planów dietetycznych US6: Jako administrator aplikacji chcę zapisywać dane telemetryczne aby móc an...

Running Microsoft SQL Server on Linux with Docker

Probably you heard that Microsoft Sql Server can only run on Windows machines. Good news - This is not true anymore. You can easily install it on Linux machine too. In this article I show you 2 ways of installing Sql Server on Ubuntu 16 and demonstrate how to connect to the database from remote machine. OPTION 1: Slow way - Manual installation This is the most straightforward way of installing. You download packages and configure Sql Server interactively(you set password, port and choosing installation option - free or paid). This is the step by step instruction from microsoft https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-ubuntu OPTION 2: Fast way - Running on docker with just 2 pure-command-line commands. My assumption is that your Ubuntu machine has already docker installed. You can check if Docker is already installed by tunning the command: docker. If you don't have docker installed, no problem: before continuing below instruction just install...