Entity Framework CRUD - FULL STACK WPF (.NET CORE) MVVM #2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to full stack WPF last episode we set up our domain model we set up our database now we're going to talk to it before we do that I actually want to clean up this DB context so as you see down here we just have our connection string hard-coded and I really want to pass that in through the constructor so that we can swap it out use our DB context against different databases if we need to so to do that we are going to generate some constructors and we're just going to override one of the base constructors of DB context which takes in DB context options which is exactly what this is kinda this is this is a builder if you put if you put options at the end you get the options so that's what we're gonna do so we have this constructor I'm just gonna move it down here underneath my properties and it's not gonna have anything in it because the base class will take care of all that now to pass this in you might be thinking all right well when we migrate the database how is it going to get the connection string well to do that with anything framework and create a new class call this the design time dbcontext options factory and what happens with this is whenever you run migrations and any framework will look for this class that will inherit or implement I believe it's design time dbcontext factory and it's generic it takes your DB context hopefully I have this right there we go so whenever you run migrations it'll use the DB context that you create here so this is where we're going to store our connection string for now so we're gonna create some dbcontext options I think we're gonna have to make the Builder first so like that I'm gonna have to import some things so dbcontext options builder and then let's go back here we're gonna grab all this it's big old connection string we can get rid of this one configuring method go back here paste that in tell our options to use sequel server and then we can return our dbcontext that is going to use those options and there we go so we have that setup and let's just delete our database and try running this make sure everything migrates well so let's go back here okay so I'm going to delete this database and what else do I need to do I think we're good to go okay I'll still eat our migrations too might as well so we're gonna add migration initial ah guess there's the startup project I forget that no guys switch the default project there forget that all right so we got our migration it's update database there we go the migration is done we can go in here let's give this a refresh and there's our database all the tables everything's good and we no longer have this connection string jammed inside of this D bite out of inside of this DB context we can pass it in we can swap it out whenever we need to real quick I named this design time dbcontext options factory that's kind of a bad name because it's not creating dbcontext options it's actually creating the DB context so let's get rid of this option at the end and you know what I'm gonna I'm gonna actually rename this simple trader dbcontext Factory I'm very particular about my naming I wanted to be explicit all right let's get this party started let's create our data service for entity framework so we're gonna create a new folder here call it services and we'll add a new class and we will call this generic data service and what this class is going to do it's actually going to be generic it's going to implement our I data service but the thing about this class is it's going to handle all of our crota operations for all of our domain objects and we're gonna be able to handle this all in one class so we're not going to be writing a bunch of data services for crud operations for every single domain object that we have we can all just do it in this generic data service all right so let's start filling out this class filling out these crud operations so a lot of people what they'll do is they'll create a field up here we get on the field and it'll just be their dbcontext and use this context so we'll do a bunch of DB operations get your accounts all that stuff but what I want to do is I don't want to actually have this context up here what I want to do is I want to pass in my dbcontext Factory and the reason I want to do that is in all of these in each of these methods I want to create a DB context for each operation I don't want to use the same context for all of these operations because one thing about any frameworks DB context is it's not the read safe so if multiple threads are trying to use this DB context at the same time you're gonna face some funky things you might get exceptions so we're going to use our DB context factory to create a new context for each operation so this really isn't that difficult all we really want to do is just pass in a simple trader DB context Factory instead it will name this context Factory and then we'll also pass this through the constructor alright very nice so now we can fill out this class using our DB context so first thing I want to do with create is actually I really want to make all these async because again I don't want to be blocking the UI when we call these methods so all these could be a sink and then for create I'm gonna make these these operations are very actually very simple this is going to be a pretty concise class but we're going to be doing using simple Trinity V context context equals our context Factory create DV context actually what I did I haven't written this but I gave this a default parameter of null because we're not actually using these args and I don't want every single caller of this function to have to pass in all so we create our simple trader DV context and using that context what we're gonna do is we're gonna wait with the context we're gonna add a sync correction node now that's not what we're gonna do we're gonna use set and what set what we do is we pass in we can pass in this t argument and it'll give us the DB set for this T so if T say was an asset transaction what it would do is it would give us it'll give us this DB set for asset transactions and that's basically what allows us to make this entire class generic and we don't have to have a data service for each entity so we get the set and what we're going to do is we're going to add a sync the entity that gets passed into the method and we actually get an error here so T must be a reference type basically all that means we need to have a constraint on T so we can say where T is a class and a class is of course a reference type and all of our domain objects are also classes so this is fair game right here so we add our enemy to the DB context and then always do is say context that Save Changes sinc I'm not actually doing any air handling here checking like if it's successful I just want to run through the basics here and get something out there and we'll handle the air handling probably I don't know maybe at the end of this episode but it's going to be important so we have our create and create actually returns an entity so we're gonna return the entity that gets created I think that any new framework will give it an ID field when we call this add I think I'm not entirely sure we might have to see what does this actually return okay we can say it returns an entity yeah that's what we want to do is so we want to say new enemy call this new what I don't know I created an entity and then we'll just say create an entity dot entity we're gonna have to test all this out and make sure it works feeling pretty good about it though all right let's move on to delete we're just gonna copy all this get the using statement copied and this one's gonna be a little bit funky okay so what I want to do is first we need to find the entity because we're only passing in the ID we're not passing in the entire entity so let's say T entity equals we're gonna use our concept and we're gonna use our DB set and we're gonna say first or default you sink and in here we're gonna pass in what we're looking for so we're looking for II wear the enemy's ID what's going on here okay I need to import this she using any frame record so we're looking for the first enemy where and this is where we face issues we we really want the first enemy where the ID is going to equal ID but we have no way of knowing if this t even has an ID field so this is simple to fix all we have to do is go back to our main model we're gonna create a new class called domain object and domain object all it's gonna have is a field or a property called ID and then all of our domain objects are going to inherit from domain object and they're going to use the ID field I want the main object instead of their own ID field so basically we can ensure that each of our domain objects have an ID field so we're going to add it to user we can add it to asset transaction but we're actually not going to add it the stock because stock doesn't actually have its own database table it doesn't have an ID field and then with that we can put a constraint on T where instead of it having to be a class we want it to be a domain object and then we ensure that each T that gets passed into this class will have an ID field so back to this first or default we can get the first entity where the ID equals the ID that's passed in so we retrieve the entity and now we want to use our are set to remove this enemy then we Save Changes and we can just return tricks we have no error handling right now but again I just want to get the crud operations out there and we'll handle their handling after that so now our get method is actually gonna be very similar I'm gonna copy all of that and instead of removing what we find we're just gonna return it it's pretty simple there now get all whoops I'm gonna copy all this again always got to be careful with copy/paste but we should be good and for this what I'm gonna do is I'm gonna say instead of first or default async I'm gonna say to list async and that will give us all the entities in this set but we're not going to return to T of entity we're gonna say ienumerable of T and then we'll return all the entities and last but not least we have update I'm gonna copy this one so with update what were we what we're gonna do is we're gonna take the any that's passed in let me get rid of all this junk I'm gonna take the anything that's passed in and we're going to set its ID to this ID parameter because what we're doing is we're updating the current saved entity in the database with the ID of ID and we're setting all of its values to whatever gets passed into this entity so basically to do that we just set the ID of this entity to be this ID so this is the new value and all we have to do is say or use our context get our set and we're going to update entity and that's not actually a sink for some reason I'm not actually sure why but either way wouldn't we save that'll be a sink and then we're just gonna return the new entity so that is all of our crowdy operations I don't actually like using bar but this is an entity entry of T and then I don't even have to feel I created any is the wrong name for this we're gonna call this created result because it's not actually the entity the entity is stored in this but yeah that that cleans it up a little bit I wanted to create like a unit test project run some unit tests integration tests all that but this video is getting a little bit long so we'll save that for another video so all I'm gonna do to make sure this works I just throw in a console app and let's just run through some things we're gonna create a I data service for the user domain object and we're just gonna call it user service equals new generic data service of user and it's gonna take a new simple trader dbcontext Factory and then we got to add a bunch of references add a reference add reference import import import some more all right so we have our user service and what we're gonna say is user service dot create new user give it a user name of test and then we're gonna say wait so that it finishes all right sets this as the startup project and let's give it a go all right let's look at our users table and we have a user so we have a courier so it looks like create is working let's do user service I get all dot let's get the result of that and then we're gonna get a count of how many users are returned not either write that out to the console sexually method there we go so we should get a one because we have one user in database there we go I did see a one maybe I should put a console.readline here so we can see the results and everything so get all is working let's do your like a get by ID pass it I think the ID is one so we've run this we don't actually have a two-string on your user objects but we should see a bunch of stuff that shows that the user gets really no we didn't get anything oh the idea is to okay oh what the heck how did that happen there we go okay that so that get all method that was the count that wasn't the idea I don't know why I thought that was the idea so good is working let's do a little update we'll say an update to for some reason it's - because I had stuff in the database before but anyways we're going to update user - we're gonna give it a username of tests user alright let's refresh that we have test user and now let's just do it delete and it should remove it from the database there we go nice that's going to wrap it up for this episode so we cleaned up our dbcontext we introduced a DV context factory to create our DB context and pull this connection string into its own little self-contained factory method and then we set up our generic data service we test it a little bit still gonna add some error handling you know like what if we don't find the entity when we try to delete it it would if we return null when we get all those kinds of things but we'll handle those in a future episode and thank you guys for watching if you have any suggestions for this series please leave a comment if you have any criticisms for this series if I'm if you don't like what I'm doing leave a comment as well but anyways thank you guys for watching
Info
Channel: SingletonSean
Views: 22,793
Rating: undefined out of 5
Keywords: wpf, mvvm, easy, hard, difficult, switch, views, microsoft, visual, studio, binding, viewmodel, property, inotifypropertychanged, class, interface, user, control, window, learn, how to, teach, architecture, pattern, new, correct, switching, toggle, button, content, main, command, icommand, programming, tutorial, full, stack, entity, framework, model, dto, dao, data, access, orm, object, core, .net, c#, service, layer, onion, walkthrough, project, finance, stock, trade, trading, development, uml, visio, create, read, update, delete, factory, crud
Id: 7pkmqrrjAAQ
Channel Id: undefined
Length: 21min 27sec (1287 seconds)
Published: Fri Nov 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.