Learn how to write the code for the app service hosted ASP.NET backend.
- [Instructor] In this video we will modify one of the projects we just downloaded, Todo together service to configure our database. We will implement our table controller classes, debug the app and make sure the database is configured properly. Let's open the solution todo together, under the todo together underscore runtime folder, and take a look at what is provided for us to help us get started. When you open the project you may see a bunch of errors or red squiggly marks. To fix that problem, go to the build menu and select build solution.
Next we're going to look at the solution explorer, on the right hand side, to see what's provided to us. This project consists of five key components. A startup class, which will execute code on mobile app startup. A todo item data object, which is just a list of two properties for the todo item object. Notice that this implements the entity data base class, which will provide some additional properties when managing the relationship with the database. There's also a todo item table controller that implements the table controller interface.
Which enables C.R.U.D. scenarios for the todo item database table. C.R.U.D. meaning: Create, read, update and delete. There's also a values controller class, which is an API controller which acts as a REST API, that can be accessed by our client application, our UWP app. Finally, under models there's a database context. And this implements the database context interface. What this does is, it will configure the database with the database set objects that are properties of the database context class.
In other words, if I were to deploy this application right now and try to access the database, the first time it would create a todo items table if it did not already exist. We're going to use this to configure our database with the objects that are important to us. In order for us to do that we'll first need to create and implement our objects. Our primary app scenario is for our user to be able to create a project, which can contain a list of tasks, a list of collaborative users and a name. A task just has a name and a state. It's either completed or it's not.
The objects will need our project, task item, user and then an assignment object, which will be the relationship between a user and a project, when a user is just a collaborator. Under data objects, right click add, and new class in the solution explorer, and create the first one, call it project dot cs. We're going to do this three more times for the other objects of importance. For the task we're actually going to call it task item so we don't get confused when we do asynchronous programming later.
Next we're going to add a user class. Add new class. User dot cs. And finally we'll add the assignment class. Now, we needed to find the properties of these objects properly for the database to have the right columns. So remember, the todo item class that was provided to us inherits from the entity data base class. Let's do the same for the objects we just created.
On line eight for assignment dot cs, I will add colon entity data after assignment. Now it's going to be missing a reference, so you'll see this little light bulb or you can hit control dot and then enter, to resolve the missing reference. Do this again for the other three objects. Now we can add all of the properties for the project class. On line 11, let's start with the project name. I'll type public string name, give it a getter and a setter. On line 12 I'll add another string, public string owner id.
In a SQL table each row, column, value, can only be a single value. So I can not store a user object, for example. The owner id property will help me locate the user object when I'm trying to retrieve the owners information for the project. The next property will be the user object itself. Public user owner on line 13, with get and set. I'm going to add a foreign key on the owner object to owner id. The foreign key constraint is used to prevent operations that would destroy a link between the two tables or two columns.
On line 13 I'll type foreign key, with owner id as the property. In order to do this though I first need to add a reference. The reference that I need to get is system dot component model dot data annotations. You can right click on references, add references and find it under framework. Click okay, and then hit control dot on foreign key. Now I have the missing reference. Next I'll add the collection of tasks as a public icollection, task items, on line 16.
Sorry, task item. And I'll call it tasks. Finally, on line 17, I'll add the collection of users that make up the collaborators. Public icollection user, collaborators, get, set. Next, we can implement the assignment class. The assignment class is just going to have two properties. The owner id, which is the local key, which we'll call user id. And the project id.
On line 11 we'll add public string user id. And on line 12 we'll add public string project id. We'll see this in action when we start writing the UWP app. But this is setting it up so that we can add collaborators to projects. Next, we'll implement the task item class. Task item is going to have three properties. On line 11, the first property will be the project id. This will allow us to map a task to a specific project.
The next property will be the task name. Public string name on line 12. Finally, we'll add one more property to determine whether or not a task is complete. Public bool complete. Finally, our user object. The user object is going to have information that we're going to get from Facebook. Including the email and the name, and so we'll add those as properties. On line 11 we'll add public string user name. On line 12 we'll add public string email.
And then on line 13 we're going to add the provider id. This will be the unique id returned to us from Facebook when the user authenticates. And we can store this as a unique identifier for the user. Now that our data transfer objects are defined we can set our database context to properly configure our SQL database. If we open up our database context in the solution explorer, todo together context, we can see public db set, todo item, todo items. We actually don't need that anymore since we were just using todo item as a reference.
So I'm going to delete the todo item class. I will then copy and paste this line three more times to configure it for our four objects. On line 26, 27 and 28. I'll set it first for the assignment object and I'll call it, assignments. Next I'll create it for the project class and I'll call it, projects. On line 27 I'll create it for the task item class and I'll call it, task items.
And on line 28 I'll create it for the user class and call it, users. The only missing piece now is our table controller object for each table to perform the C.R.U.D. operations. To create these we're going to take a look at the existing todo item controller. We're actually going to just copy and paste this code into the new class for each of the objects. So we'll create a new one by right clicking on controllers and going to add, class. The first one we'll name project controller.
And this will control the C.R.U.D. operations for project. The easiest way to do this is to delete all of this. You can select all by hitting control a. And paste in from todo item controller. Now, a neat trick, is you can hit control f to find every occurrence of the word todo item by typing todo item. You can then expand the arrow and select replace to edit what we're going to replace it with. We're going to replace every occurrence of todo item with project. You can then hit alt a, or select replace all, to replace all occurrences in the document.
So now, you shouldn't see any more red squigglies and everything should have project. So get all projects and get project. We can do this again for the other objects we just created. So let's go ahead and create add, class. And we will call this one user controller dot cs. Again select all with control a. Paste the contents of the todo item controller with control v. Control f to find every instance of todo item. And replace with user.
Now let's do it again for assignment. Let's do it one more time for task item. With that done we can actually delete the todo item controller. It served its purpose as an example. With that, our server side code is complete for now. The only thing we need to do now is remove the final remnants from the template. In the startup dot mobile app class, and under the app underscore start folder, at the very bottom you will see this seed method.
And this will seed the database with some fake data. Since we've deleted the todo item class this isn't going to build properly. You can either delete this or comment it out, by highlighting it all and pressing control k, control c. If you want to replace the contents later this can serve as a reference, but you don't need it for now. Our server code is complete for now until we add authentication. Next, I'll show you how to test and deploy the service to make sure everything works.
- Working with .NET for UWP app development
- Establishing application architecture
- Configuring Azure services
- Configuring the mobile app service backend
- Testing and publishing the service backend
- Using Facebook authentication
- Using XAML
- Client server abstraction API
- Using the UWP community toolkit
- Styling app views
- Creating adaptive views
- Testing an app for multiple user accounts
- Publishing to the Windows Store
- Sideloading app packages