C
C#only you know

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.
C
canton741d ago
I think the critical bit you missed was your actual question
OYK
only you know41d 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.
C
canton741d 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?
OYK
only you know41d 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
C
canton741d ago
I'm trying to find out what exactly you need help with 🤷‍♂️ Cool, I'll stop. Good luck!
OYK
only you know41d ago
Thank you mr so clever
P
Pobiega41d 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.
OYK
only you know41d 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.
P
Pobiega41d 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
OYK
only you know41d ago
I explained my problem and look at his answer bro i didn't do anything to make him tilted
P
Pobiega41d ago
I'm fully with Canton here lol, you are the one with the problem, not him 🙂
OYK
only you know41d ago
what's your suggestion then, my senior didnt wanted to see mongodb library in my service codes. 🤣
P
Pobiega41d 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
OYK
only you know41d ago
yes i thought the same. I could do it Java easily but im kinda new at .net
P
Pobiega41d 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
OYK
only you know41d ago
I did something like this. Probably there are so many mistakes but the logic?
No description
No description
OYK
only you know41d ago
what do you think
P
Pobiega41d 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?
OYK
only you know41d 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
P
Pobiega41d ago
I understand that.
OYK
only you know41d ago
sorry
P
Pobiega41d ago
Which is why I point out that you must not leak any mongo DB internals
OYK
only you know41d ago
wdym by leaking
P
Pobiega41d 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
OYK
only you know41d ago
oh got you now i should change them
P
Pobiega41d 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
OYK
only you know41d ago
then what are your suggestions
P
Pobiega41d 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
OYK
only you know41d 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
P
Pobiega41d 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
OYK
only you know41d ago
not bothering it's just don't know how to use it so scared to understand nothing
P
Pobiega41d 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
OYK
only you know41d 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.
P
Pobiega41d 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
OYK
only you know41d ago
I don't plan right now but I might need it
P
Pobiega41d ago
VS and Rider both can "Extract interface" from a class, so that wont be a problem
OYK
only you know41d ago
so I should use it with other repos to make it useful right
P
Pobiega41d ago
yeah, use it as a base class have you done any database modelling before?
OYK
only you know41d ago
so I'll use base class for implementing methods to other classes?
P
Pobiega41d ago
? If you come from java, surely you know what a base class is? 😛
OYK
only you know41d ago
I didn't say I know Java🥲
P
Pobiega41d ago
I'd probably even consider marking the GenericRepository<T> class as abstract
OYK
only you know41d ago
I'll try it
P
Pobiega41d 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
OYK
only you know41d ago
definitely I'll any other suggestions to look for
P
Pobiega41d ago
not at this time
OYK
only you know41d 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
P
Pobiega41d ago
Huh? No I don't understand Mongo repo?
OYK
only you know41d ago
I mean Mongo's methods for a class name I made up I d didn't know how do I name it
P
Pobiega41d 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
OYK
only you know41d 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
Want results from more Discord servers?
Add your server
More Posts
Reading large xml file from archive by using XmlReader in Parallel modeHello 👋. I am looking for how can I read data from archive xml file in Parallel mode. I have archiResizing the Console window - Console ApplicationHi there, I'm trying to resize the console window in a Console Application. I have tried multiple ✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -3D Unity RaycastingHi guys, currently working on a puzzle game involving lasers and mirrors, however, have hit a brick Computer Science - Computer ArchitectureOn a 64 bit system (where the bus length and word size are 64 bits) how would we determine the ammouConnecting SQLite in MAUIHow to connect the database without absolute path? If with it, the program works only on the computeDifferent HttpClient headersWhen I make an HTTP request in my browser (Chrome), I get different response headers than I do in myHttpRequestException:An error occurred while sending the request. WebExceptionHttpRequestException:An error occurred while sending the request. WebException:Unable to connect to ✅ EF does not put datetimes properly to the datebaseI am writing a backend app in asp.net core. This is my service code : ```c# public virtual async TaShadow Prop Error with IdentityDbContextThis is my IdentityDbContext: public class ApplicationUser : IdentityUser { [Required(ErrorMessWpf custom button template probleHi! I am trying to create custom button control, but I have a problem with a content property. ButtoWinfoms problem in Hosts fileI am writing a mini site blocker, the program has 2 functions to block the site and add the site thaDTO validation with DRY principle (without repeating the same validation)Hello, I would like to know what is used in practice, regarding the DTO validation. Let's say for 2 I'm using fedora, and i want create some .NET formsWhat should i do to create projects with ASP.NET forms in Linux system(Fedora)?✅ My API returns 500 internal server error with the SQL exception text, why?Hello every one, I built an ASP.NET API in .NET and I wrote a controller Action that looks like thi