✅ 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:
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•5mo ago
Message Not Public
Sign In & Join Server To View
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•5mo ago
Message Not Public
Sign In & Join Server To View
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•5mo ago
Message Not Public
Sign In & Join Server To View
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•5mo ago
Message Not Public
Sign In & Join Server To View
Yeah, i go always into details. I got busted on that on my graduation internship as well.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
That is good to know!
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
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•5mo ago
Message Not Public
Sign In & Join Server To View
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):
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
🤣
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
That was the only way for me to get the docker-compose env variables haha
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
ah the one you suggested earlier
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Yeah i definitely need to rewrite this connection part
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
its from docker-compose though
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
this is appsettings?
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
Dont you want to get the uri direct from docker-compose or is
appsettings
the middleman in this process?Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
okay thank you, i will give it a shot. let me refactor the mongocontext etc. first
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
Do not store the collection in the appsettingsI don't understand the purpose, personnally. If it suits you, you do you 😄
Use consts, so that they cannot be overwrittenit'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
Quoted by
<@689473681302224947> from #Expandability of the ASP.NET WEB API - Mongo tutorial code (click here)
React with ❌ to remove this embed.
well, docker-compose is leading in the process
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
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•5mo ago
Message Not Public
Sign In & Join Server To View
TeBeCo
conf store are:
appsettings
appsettings.env
secret
env var
console args
keyvault
etc ....
Quoted by
<@689473681302224947> from #Expandability of the ASP.NET WEB API - Mongo tutorial code (click here)
React with ❌ to remove this embed.
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
as far of now i only get that when you use this:
it automatically finds those properties in the json when you do like:
{
Mongo {
Uri:.
..
}
}Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
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:
it should get it?Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
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:
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•5mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View