Building a generic repository for mongodb

hello folks I was thinking build a generic repository structure for mongodb. but someday if i want to change the db i don't want to start all over again. I hope I explained myself well.
51 Replies
canton7
canton74mo ago
I think the critical bit you missed was your actual question
only you know
only you know4mo ago
I don't think so but the question is: I'm creating a api with using mongodb. I want to build a repo for being generic(i mean if i want to change db someday i don't want to code all over again). And that repo will communicate with mongorepo. but how do I build it i don't know really. I'm kinda new sorry.
canton7
canton74mo ago
(Normally questions can be identified by a question mark "?") "How do I build it" is a pretty vague question, which means you're less likely to get a useful answer. Which bit of it are you struggling with exactly?
only you know
only you know4mo ago
if you're not gonna help me pls stop bro. you know what I mean. I'm at the office right now as a intern. and i'm in a hurry. I explained my problem. What should I do? here its your question mark
canton7
canton74mo ago
I'm trying to find out what exactly you need help with 🤷‍♂️ Cool, I'll stop. Good luck!
only you know
only you know4mo ago
Thank you mr so clever
Pobiega
Pobiega4mo ago
What have you tried and where are you stuck? If you "dont know how to build it" then you have not done enough research Also, fix your attititude. Entitlement is not a good look and will get you ignored or banned.
only you know
only you know4mo ago
I've searched for new mongoDB entity framework core provider can't figured out for how to implement it. and also i tried to create a generic repo and implemented to mongodb repo. but couldn't run the server i guess i skipped some parts or did somethings wrong.
Pobiega
Pobiega4mo ago
EF Core with mongo isnt a great fit EF is primarily for relational databases - Mongo is a document database how you model and query your data is entirely different
only you know
only you know4mo ago
I explained my problem and look at his answer bro i didn't do anything to make him tilted
Pobiega
Pobiega4mo ago
I'm fully with Canton here lol, you are the one with the problem, not him 🙂
only you know
only you know4mo ago
what's your suggestion then, my senior didnt wanted to see mongodb library in my service codes. 🤣
Pobiega
Pobiega4mo ago
See, thats where your repository comes in the services talk to the repo, the repo talks to mongo bam, no mongo related stuff in your services. And ideally, your repository API surface doesnt expose any mongo internals
only you know
only you know4mo ago
yes i thought the same. I could do it Java easily but im kinda new at .net
Pobiega
Pobiega4mo ago
When using a document database, I strongly urge you to have repositories over your aggregate entities, not the individual component types as you might with a relational db
only you know
only you know4mo ago
I did something like this. Probably there are so many mistakes but the logic?
No description
No description
only you know
only you know4mo ago
what do you think
Pobiega
Pobiega4mo ago
how to model your data is actually by far the hardest problem to solve with document databases ObjectId is a mongo internal type if your interface exposes it, you are leaking mongo internals to your service layer repo owns the database connection - that will probably lead to some problems. How do you know when to dispose it?
only you know
only you know4mo ago
My senior wanted a basic api communicates with mongo from docker. But he told me that I want you to write a generic code for someday if we'll change our database we can do it by easily
Pobiega
Pobiega4mo ago
I understand that.
only you know
only you know4mo ago
sorry
Pobiega
Pobiega4mo ago
Which is why I point out that you must not leak any mongo DB internals
only you know
only you know4mo ago
wdym by leaking
Pobiega
Pobiega4mo ago
Look at your interface the input parameter type for Delete is ObjectID the full name for ObjectId is... MongoDB.Bson.ObjectId that type is from a mongodb package
only you know
only you know4mo ago
oh got you now i should change them
Pobiega
Pobiega4mo ago
yep. also, generic repositories are not very useful on their own, most of the time They only provide the simplest of querying - get by pkey, get all, etc
only you know
only you know4mo ago
then what are your suggestions
Pobiega
Pobiega4mo ago
Combine it with typed interfaces ie
public class UserRepository : IGenericRepository<User>, IUserRepository
public class UserRepository : IGenericRepository<User>, IUserRepository
the generic one gives you the generic methods, but IUserRepo can give you actually domain-related methods like FindUserByEmail etc its rarely enough to only have the generic methods
only you know
only you know4mo ago
oh I see. I saw few examples like that but I didn't understand the point for defining UserRepo as class and also as Interface in Java I've never used it. Maybe it's wrong too but I'm not used to use it so that's why I didn't understand the point
Pobiega
Pobiega4mo ago
well the entire purpose of having interfaces is to refer to the implementation via the interface if you already know the concrete implementation, why bother haviing interfaces at all?
public class UserRepository : GenericRepository<User>, IUserRepository
public class UserRepository : GenericRepository<User>, IUserRepository
then since you have a baseclass for generic repos, which is fine
only you know
only you know4mo ago
not bothering it's just don't know how to use it so scared to understand nothing
Pobiega
Pobiega4mo ago
Sure you can skip the interfaces and use the raw repo directly Nothing stops you from doing public class UserRepository : GenericRepository<User> and injecting UserRepository
only you know
only you know4mo ago
what do you think which one more make sense. You are more experienced than me. I'll listen to you as suggestion and also mentor:=) instead of make sense I can say useful or popular.
Pobiega
Pobiega4mo ago
I mean, if you dont plan on having other implementations or use mock testing, its fine to not have interfaces thats not a problem. The main point I wanted to get across is that GenericRepository<User> will rarely be useful as anything other than a base class because specialized queries are needed for 99% of apps
only you know
only you know4mo ago
I don't plan right now but I might need it
Pobiega
Pobiega4mo ago
VS and Rider both can "Extract interface" from a class, so that wont be a problem
only you know
only you know4mo ago
so I should use it with other repos to make it useful right
Pobiega
Pobiega4mo ago
yeah, use it as a base class have you done any database modelling before?
only you know
only you know4mo ago
so I'll use base class for implementing methods to other classes?
Pobiega
Pobiega4mo ago
? If you come from java, surely you know what a base class is? 😛
only you know
only you know4mo ago
I didn't say I know Java🥲
Pobiega
Pobiega4mo ago
I'd probably even consider marking the GenericRepository<T> class as abstract
only you know
only you know4mo ago
I'll try it
Pobiega
Pobiega4mo ago
you will need some contraints on it regardless Read up on database modelling with aggregate entities and C# generic contraints those topics will be important
only you know
only you know4mo ago
definitely I'll any other suggestions to look for
Pobiega
Pobiega4mo ago
not at this time
only you know
only you know4mo ago
and last question. Is it really good idea that having generic repo and also mongo repo? I think question is little awkward but I hope you understand what I mean
Pobiega
Pobiega4mo ago
Huh? No I don't understand Mongo repo?
only you know
only you know4mo ago
I mean Mongo's methods for a class name I made up I d didn't know how do I name it
Pobiega
Pobiega4mo ago
I still dont follow The entire idea behind repos is to hide the implementation So you should not have a mongo repository, your repos represent your data - whatever that might be
only you know
only you know4mo ago
my generic codes will communicate with any other db's but right no matter what I use If I can correctly build the structure