The Repository Pattern explained for EVERYONE (with Code Examples) ๐Ÿš€

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now first let's have a look at one or two definitions of the repository pattern we'll break this down a little and then I guess even more important is how would you actually write the code of such a repository pattern so how would you develop this stuff I will show you that and make sure to stick around until the end because then we will also talk about object relational mapping which is often used in the combination of the repository pattern so let's start here are the definitions in the Microsoft documentation for instance the repository is or repositories are classes or components that encapsulate the logic required to access data sources all right they centralize common data access functionality providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer so it is in essence well a class by the example I will show you in a minute a c-sharp class I also like to call this a service and this service then it has some logic in it or it accesses something else to well get the data from a database for instance and that's where object relational mapping comes in with the example of Entity framework for instance because Entity framework itself also uses the repository pattern and to be able to use these functions then you would use a link to for instance Access Data from a specific table now another definition is by Martin Fowler here in the book patterns of Enterprise application architecture so this guy really knows what he's talking about he says a repository performs the tasks of an intermediary this is quite interesting I think between the domain model layer and the data mapping acting in a similar way to to a set of domain objects in memory client objects declaratively built queries and send them the support and build queries and sent them to the repositories for answers so this again we will see this in a minute this means a controller for instance a web API controller we're talking.net here can use a repository well to get some answers right conceptually a repository encapsulates that's the same as here above a set of objects stored in the database and operations that can be performed on them providing a way that is closer to the persistence layer meaning you can use a repository to get the persisted data from a database and you do not have to make the actual work to get the data from the database yourself repositories also support the purpose of separating clearly in One Direction the dependency between the work domain and the data allocation are mapping now dependency is also a great term here because we are actually using dependency injection together with the repositories and now let's do the codes right so in here I just opened the really the default application of a web API project in Visual Studio 2022 version here is now 17.4.0 and we're using.net7 here all right and in this example project you can see already the following we have the weather forecast controller so I hope you're a bit familiar with web apis or web services in general what do we have here we have the weather forecast class this is just a certain model here in essence and this controller thingy here is in essence the endpoint any client can access or can can send some data to this endpoint or receive something from this endpoint here we have this single function here and we will use this example now again this is just a controller and this controller now randomly generates weather forecasts when we run this then Swagger UI will open and we will get these randomly generated weather forecasts now as you can see here there's our function we can try this out hit execute and here's the result all right so we already get the data here now imagine we want this data from a database and imagine and you would need this data again on another place you want to write another controller and again you need this data well then again you would have to write this certain function here that is either accessing the database and returning the data from there or even if you just want to use this thing here you want random weather forecasts then you would have to write another controller and again use the same function use the same code this sucks right so the magic word now is a repository now when I do this usually I just call these things services and I know that lots of other developers do this as well so let's say we've got a Services folder here and now we also create a new folder for whether forecast service for instance and in here now to make this work we need two things we need an interface and we need an implementation class so in here now let's just create a new class for actually I wanted to create an interface let's start with the interface the I weather forecast service interface and then we create the the corresponding implementation class here so this then would simply be the weather forecasts service and this thing here of course uses the weather forecast service interface all right and now in the interface let me have a look here what's what is this function all about so this is the name in this specific case so maybe we can just use this copy this here and now we know we've got a function returning an i enumerable with uh of the type of weather forecast and this function is called get we save this and yes we rebuild and applied the changes although we've got now some build errors of course because we have to implement this function here in the implementation class so in here now we can actually press Ctrl and period implement the interface automatically and now what we want to do is we simply grab this thing and put it here all right and with that of course we also need our summaries so let me just remove them here actually put them here and now we've got our repository in essence all right we have the interface defining the function and then in here we've got the actual method and now what we want to do here is we have to inject this service and then just use the method from the repository so in here now we just say I weather forecast service this thing is called weather forecast service maybe we put this in a new line and in here now we say create and assign the field weather forecast service and we add the underscore here and here and now we've got our repository injected with the help of dependency injection and here now instead of returning this these lines of codes in essence what we now do is we call our weather forecast service with the get function and we're done all right so this is really what they meant in the definition that we just want to get some answers from our repository in this case here our weather forecast service you could also call this weather forecast repository or when you wanted to inject this maybe you already saw that in some repository and some well in some GitHub repositories for instance then it's not called weather forecast service then it would be called weather forecast repo for instance some devs do it like that I usually call this stuff service it is a web service in essence so that's what I do and now the the really nice thing is that we have no so called fat controller anymore the controller has not a lot of logic here it's just forward the request from the client to the actual repository and then Returns the actual data all right one more thing is missing here but still let's just run this one more time and I hope that Swagger is opening up there it is and now when we try this out we get this Arrow here unable to resolve service for type I weather forecast service while attempting to activate the weather forecast controller now what does this mean this is more part of dependency injection here we want to inject the I forecast service right so we want to use the implementation class weather forecast service but the thing is where where is this here where do we see actually that we want to use the weather forecast service right it's it is not here it's not here so the controller or the web API in general has no idea what is actually going on here what do you want to do what do you want from me dear developer or dear user so what we have to do is we have to tell the web API and the controller what implementation class to use and for that we go to our program Cs and just register the service and we do it like that build a services and then add either scoped transient or Singleton several definitions several functions here available for us most of the time you would use add scoped and then here you just say I whether forecasts service that's it actually and then here also weather forecast service that's it already added the reference here another option would be since C sharp 10 that we just add a global using directive here but that's another topic and now here with that we tell our web API whenever something for instance the controller wants to inject the I weather forecast service then please use the weather forecast service implementation class and the beautiful thing in here is if you want to change something play around with that and you're not sure can I actually change the code here not sure about that really don't want to touch it because it works so what you can do instead is you go here you just create another class and do it right here add a new class weather for cost service let's say extended or version 2 whatever it is again it uses the I weather forecast service we implement the interface and now here let's just say we we grab the exact same code but instead of again we need the summaries maybe not the best example here maybe you could move that to another class but stay with me here I think to get the idea here we just generate five examples now what about 50 but examples results so let's say if this and this save everything here and now pay attention we've got our interface right this is a class that implements the interface this is another class that implements the interface but here in the program CS we say we register that way that we say if you want to use the this interface here then please use this implementation class now let's run this one more time and when speaker UI is here I have no idea if this is the correct version we get these Five results right and again you wanted to play around with that stuff so what now if you want to just use the extended version save that try this out hit execute maybe we have to run this one more time and now let's see it executes yep now we get 50 results you see the magic of this now you do not have you we did not have to change the controller here we just said still I want to use the I weather forecast service here and I just wanted to test something else I wanted to use another implementation now imagine you have 50 controllers for instance and every single controller uses the iweather forecast service actually a good example would be the ilogger for instance right and every controller you want to lock something but you want to change the behavior of the logger now imagine you would use fat controllers you would have to change the behavior in 50 classes and 50 controllers this is not happening here you just make one change either in the implementation class you already have or you just use another one you can just inherit from another class and this is the only place where you have to change it here you say instead of the weather forecast service now I want to use the extended one and it's done and now regarding object relational mapping Entity framework uses the repository pattern in combination of the unit of work pattern and if you first want to know what actually object relational mapping in general is then just click on the video here on the screen
Info
Channel: Patrick God
Views: 55,990
Rating: undefined out of 5
Keywords:
Id: Wiy54682d1w
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Tue Nov 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.