✅ Expandability of the ASP.NET WEB API - Mongo tutorial code

Hello there, I am currently following this tutorial: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-8.0&tabs=visual-studio This tutorial explores the integration of mongo and c#. What in this tutorial is done is that they set collection, database and connection in the env-like file. This means that when you have multiple collections you need to add them here and expend all the setting classes. Another issue I see with this is that https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-8.0&tabs=visual-studio#add-a-crud-operations-service is not maintainable in the long run, becuase of this line:
_booksCollection = mongoDatabase.GetCollection<Book>(
bookStoreDatabaseSettings.Value.BooksCollectionName);
_booksCollection = mongoDatabase.GetCollection<Book>(
bookStoreDatabaseSettings.Value.BooksCollectionName);
Would an EF-like approach be more suitable? So you have a MongoDBContext class that holds the collections and DI this Context class into the Service classes?
Create a web API with ASP.NET Core and MongoDB
This tutorial demonstrates how to create an ASP.NET Core web API using a MongoDB NoSQL database.
46 Replies
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
ive read your thoughts about the code/article and you are definetly right that the code provided is very bad. Putting the connection in the appsettings is somewhat reasonable but not the collection(s). because when you want another service (for example AuthorService) you need to add an extra definition in the appsettings.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
Both options are fine indeed. Even though I only have 1 collection in my project, I want to think ahead just in case.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
So for me to summarize: * Never say env again 😉 * Do not store the collection in the appsettings * Use consts, so that they cannot be overwritten * Use the IOptions<>
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
Yeah, i go always into details. I got busted on that on my graduation internship as well.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
That is good to know!
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
Thats true (with user secrets). I am still a bit searching in what is the best. Since i am working with Docker as well.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
What i currently do is: Docker-compose: (set some env-variables like the mongo-connection uri) then in the program.cs i do (dont mind the lowercase):
builder.services.configure<mongodbsettings>(options => { options.connectionuri = builder.configuration["mongodb_uri"]})
builder.services.configure<mongodbsettings>(options => { options.connectionuri = builder.configuration["mongodb_uri"]})
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
🤣
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
That was the only way for me to get the docker-compose env variables haha
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
ah the one you suggested earlier
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
Yeah i definitely need to rewrite this connection part
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
its from docker-compose though
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
this is appsettings?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
Dont you want to get the uri direct from docker-compose or is appsettings the middleman in this process?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
okay thank you, i will give it a shot. let me refactor the mongocontext etc. first
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo ago
TeBeCo
Do not store the collection in the appsettings
I don't understand the purpose, personnally. If it suits you, you do you 😄
Use consts, so that they cannot be overwritten
it's not an issue of not being overwritten it's mostly that if you don't need to configure them, then yeah const is better for that to avoid change
Use the IOptions<>
open <#251386218351165441> and type in $options PLEASE SUFFIX YOUR TOptions with Options => MongoDbOptions
React with ❌ to remove this embed.
electronic heartbreak.
well, docker-compose is leading in the process
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
thats not an issue i rather change it all and make the project better than create wonky code 🙂 after some reading I see what is going on here. Yet i still face difficulties in understanding how to pass docker environment variables to that appsettings.json file.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo ago
TeBeCo
conf store are: appsettings appsettings.env secret env var console args keyvault etc ....
React with ❌ to remove this embed.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
as far of now i only get that when you use this:
public class MongoDbOptions
{
public const string SectionName = "MongoDb";

public required string Uri { get; set; }
}

builder.Service
.AddOptions<MongoDbOptions>()
.BindConfiguration(MongoDbOptions.SectionName);
public class MongoDbOptions
{
public const string SectionName = "MongoDb";

public required string Uri { get; set; }
}

builder.Service
.AddOptions<MongoDbOptions>()
.BindConfiguration(MongoDbOptions.SectionName);
it automatically finds those properties in the json when you do like: { Mongo { Uri:. .. } }
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
yeah yeah those are the details but the concept is more important So, if I define in my docker-compose an environment variable with the double underscores (for example SOME__PASSWORD=cheese), and in the appsettings.json i do:
MongoDB {
SOME__PASSWORD
}
MongoDB {
SOME__PASSWORD
}
it should get it?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
electronic heartbreak.
I can't call unfortunately. But to not waste your time, I will read your messages a couple of 100 times in order to get a better understanding of the situation. I definetly appreciate your help and support, so thank you for helping me out. I have it 😂 😭 Let me explain what I did: 1. I first tried using the Option binding with the appsettings.json as you had shown with:
public class MongoDbOptions
{
public const string SectionName = "MongoDb";

public required string Uri { get; set; }
}

builder.Service
.AddOptions<MongoDbOptions>()
.BindConfiguration(MongoDbOptions.SectionName);
public class MongoDbOptions
{
public const string SectionName = "MongoDb";

public required string Uri { get; set; }
}

builder.Service
.AddOptions<MongoDbOptions>()
.BindConfiguration(MongoDbOptions.SectionName);
Because you define in the sectionName the MongoDB text, the bindConfiguration knowns what to 'listen' to in the appsettings.json. With those nested values you can easier point out what is what. 2. The final step was to get it done through docker-compose. I really did read your messages countless times over to make sure my single neuron understood what should be done. I changed the docker-compose env variables to ones with __, so it became: MONGODB__URI. I ofcourse removed the setup from appsettings.json in order to avoid confusion. I then just started the app and it works :sunglas: .
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX5mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server