Monster CRUD operations

Currently, our Inventory service only creates the same monster, and we need it to create new monsters and perform other operations, such as read, update, and delete monsters. By the way, the standard database operations of Create, Read, Update, and Delete is often referred to as CRUD. So, roll up your sleeves; we are actually going to do a little coding and build the monster CRUD.

Open up the InventoryService script in the editor of your choice again and scroll down to the CreateMonster method. Delete the CreateMonster method and perform the following instructions to replace it and add the other new methods:

  • CREATE: Add the following method in order to replace the CreateMonsters method:
    public Monster CreateMonster(Monster m) 
    { 
        var id = _connection.Insert(m); 
        m.Id = id; 
        return m; 
    } 
    

    Instead of creating a hardcoded monster, we now take a monster object and insert it as a new object/record in the database. This returns the new auto incremented id, which we set on the Monster object, and return it back to the calling code. We will let another part of our code create the details of the monster.

  • READ (single): We will handle two versions of the read method: one to read or find an individual monster and another to read all the monsters. Add the following code to read a single monster:
    public Monster ReadMonster(int id)
    {
     return _connection.Table<Monster>()
     .Where(m => m.Id == id).FirstOrDefault();
     }

    This method takes an id and finds the matching object monster in the table using the Where method, which takes a function delegate as a parameter. The code looks like it is using Linq to SQL, but it is not. The Where and FirstOrDefault are added as part of the SQLite implementation to remain cross-platform.

    Note

    iOS does not currently support Linq, which often causes confusion for developers coming from a traditional C# background even on other platforms, such as Linux or Mac. If you want your application to be cross-platform compatible, avoid using the System.Linq namespace entirely.

  • READ (all): Handling the reading of all monsters is even simpler:
    public IEnumerable<Monster> ReadMonsters() 
    { 
        return _connection.Table<Monster>(); 
    } 
    

    One line of code is all it takes to pull all the monsters from the database. It doesn't get much simpler than that.

  • UPDATE: Update the monster code as follows:
    public int UpdateMonster(Monster m)
    {
     return _connection.Update(m);
     }

    The UpdateMonster method takes a monster object and updates it in the database. An int value containing the number of records/monsters updated will be returned. This should always return 1. Note that the monster object passed to the update method should have an existing ID. If the monster object's Id property is 0, then you should instead use the CreateMonster method instead. The CreateMonster method will create a new monster in the database and set the Id property.

  • DELETE: Finally, when we have no more use for a monster object, we would want to be able to delete it, using the following code:
    public int DeleteMonster(Monster m)
    {
     return _connection.Delete(m);
     }

    The DeleteMonster method is like the UpdateMonster method. It takes the monster you want to delete and then deletes it from the database. Returning the number of objects it deleted, which should only be 1. Again, the monster object must have a valid Id property. If it doesn't have a valid Id, then it doesn't really exist in the database anyway.

Hopefully, you appreciate the ease with which we coded those basic monster CRUD operations. Having the object relational mapping available as part of the SQLite4Unity3d wrapper allowed us to quickly implement the database persistence for our monster inventory. At no point, we even have to utter the words SQL, never mind write any SQL code. Furthermore, in the future, implementing other objects in the Inventory service should be just as easy.

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

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