DEV Community

Discussion on: Tutorial: MongoDB With C# The Easy Way!

Collapse
 
anthopark profile image
Anthony Park • Edited

Thank you so much for this beautiful wrapper library. I'm about to use this library in my next project and was wondering how the DI scoping works with the DB connection.

I'm used to the idea of having a DBContext that is scoped lifecycle and use that DBContext in a repository object, which I also register in a DI container as the scoped.

Would you recommend making a repository class that uses DB? If so, how does the lifecycle works?

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

you're most welcome. i believe this bit of info from the documentation answers your question. specifically, have a look at the repository pattern sample project link.

Collapse
 
anthopark profile image
Anthony Park • Edited

This tremendously helped! I'm now thinking of testing a service object that depends on the repository. I'm thinking of try DB.InitAsync with mongo2go in the xunit project and was wondering if you have some suggestions around the right way to mocking the DB connection with your library.. Thanks a ton!

Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

integration testing is the recommended approach. something like this if it's a web api project.

however I think doing db.initasync with mongo2go address in the setup/class fixture should do the trick.

let me know if it doesn't and I'll have a deeper look.

Thread Thread
 
anthopark profile image
Anthony Park

Thanks for the quick response! I was able to connect to Mongo2Go with your library but came across an issue which I've described in your repo issues.

I think I may need to use Xunit's class fixture and persist the connection throughout the test cases in a test class.