Creating and implementing the book library database model

In this section, we will begin by building the database model that will be used by our BookLibrary project solution that will be used by our Razor templates when we create these, and then the WebViewController.cs file will communicate and interact with each of the Razor template views and handle the actions within them.

Let's look at how we can achieve this, by performing following steps:

  1. Create an empty class within the Models folder, by choosing Add | NewFile..., as shown in the following screenshot:

    Creating and implementing the book library database model

  2. Next, choose the Empty Class option, located within the General section, and enter in BookItem, as shown in the following screenshot:

    Creating and implementing the book library database model

  3. Next, click on the New button to allow the wizard to proceed and create the new empty class file.

Up until now, all we have done is create our BookItem class file. This class will be used by each of our Razor template views, as well as our WebViewController.cs file that will eventually allow communication to happen between each of the Razor template views, as well as being able to handle actions within them, whenever the user interacts with them.

Let's now start to implement the code required for our BookItem class model, by performing the following steps:

  1. Ensure that the BookItem.cs file is displayed within the code editor, and enter the following code snippet:
            // 
            //  BookItem.cs 
            //  BookLibrary Database Model 
            // 
            //  Created by Steven F. Daniel on 20/10/2016. 
            //  Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. 
            // 
            using SQLite; 
     
            namespace BookLibrary {
                 public class BookItem
                 {
                     public BookItem()
                     {
                     }
                     [PrimaryKey, AutoIncrement]
                     public int Id { get; set; }
                     public string Title { get; set; }
                     public string Author { get; set; }
                     public string Isbn { get; set; }
                     public string Synopsis { get; set; }
                 }
             }  
    

In the preceding code snippet, we have successfully defined our database model that will be used to represent our book items. You will notice something that is different within our model to what you would have seen within our TrackMyWalks app: here we have defined a [PrimaryKey, AutoIncrement] item for our id field, this will tell our BookLibrary database to set the id property to automatically increment whenever we add a new item to our database.

If you have used relational databases in the past, such as Microsoft SQLServer, Oracle, or Microsoft Access, this should be quite familiar to you. In the next section, we will use this model to set up and initialize our database, by creating a database library wrapper to handle connections to our database, as well as Creation, Retrieval, Updating, and Deletion (CRUD), of each of our book entries.

Note

The Android version of the BookItem class model is available in the companion source code for this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.34