[MUSIC] Okay, now we're looking at Protocol in Action and or, we're going to use an example here of Data Sources. We've talked about that earlier using Data Source, data adaptors as an example. So we have, this landscaper that we have hundreds of databases our there, commercial and open source databases. And if you were to use library like ado dot net or JDBC or ODBC. There's different different libraries out there that provide, for different languages provide different Data Source adaptors and you'll notice that whether you're talking to Oracle or MySQL or MongoDB. The interfaces and to those the methods that you will go retrieve records from or you delete records or update records, whatever you're going to do, they're the same. They don't change between the different database implementations. So, we want to actually write some codes that mimics that and see how protocols help the communication and the interface to be concrete and clear. So again, in the data sources example, there's many different databases, all have different implementations, like we said, Oracle vs MongoDB, we have a Sequel versus No Sequel so it's a drastically different implementation. And all have a similar use at the developer library level. And then we'll look at a data reader and what we're going to demonstrate when we look at the data reader is how to use a protocol as a type. There is a purpose for that so we want to look at that and understand that, so now we're going to go to the playground, and here we're in the playground to demonstrate protocols in action and we're using a Data Source example, and so we're going to make this very simple and we're going to define two types of databases a SQL server and an Oracle database. Those are our examples. In the implementation, we're actually going to make an Oracle and a SQL server database, but they're going to be very simple here, one is going to have the, be simply in array, that's our database there, an array, SQL server is going to use an array for its database and Oracle DV is going to use a dictionary. So, they have different implementations. And very simple. So we wanted to find a protocol, and this protocol is going to have the four main database operations. It's going to create, it's going to read, update and delete. And we are going to define a very, just again for the sake of this simple demonstration, we're going to do we're going to create a model and our database is only going to ever use this one model called Record. And Record has a record ID on it and a name and a phone number. And record ID, we're really never going to do anything at least from a writing standpoint to that, we just let it, when the class is created, it's actually going to create a global unique identifier using NSUUID. But I may refer to that as a grid, that's how I'm used to talking about it. So, when it initializes, we can also, or we create an initializer on this that or we can set the name and phone number. So that's our record model and that's all our work on SQL server databases understand and our simple example. So we've created our protocol, we are going to require that anybody who is conforming to our protocol anybody that's going to create these data adapters for us against Oracle or SQL server They need to conform to these four methods. So here, we have our SQL Server Data Source class implementation, and so, we conform to the Data Source protocol. And you'll see, if we look through this, they'll contain all four required methods from our protocol. And SQL Server again uses an array, so it basically leverages all the functionality on array to take in a record and fill that container with the input records. When we want to read, it just returns the array When we want to update a record, we basically pass in a record, loop through, I mean very simple, loop through it, look for a record ID that equals the one we're inputting that's already in our database. And if it's there, we update it. Almost the same functionality in the delete. We find it and then we, we basically remove our record out of database using the array remove it index method. Now Oracle database, I'm sorry Oracle data Oracle datasource, is very similar and again we're using a dictionary to implement our database. So a different implementation, but we are going to conform to the data source protocol, and here you'll see the same for required methods. And a little bit different on the implementation because it is using, it is using a dictionary. So, here you could see we're going to, when we do create a record we actually use the dictionary's built in capability to call the, put in a key subscript. And then internally it will actually add this record, if it doesn't already exist. So we assume that in our real simple implementation that the record id isn't already in there and it will add this to the Oracle db. And we'll jump real quick down to update, because it uses it's if you see it's pretty much the same thing happening here. If the record is already in there for an update, it just updates that record using the dictionary subscript, the IOS library dictionary subscripting mechanism that's already built in. When we read, we basically just a little different than our SQL server where we were able to return since SQL server was a implemented just using an array here where we actually need to go through all of our values in the dictionary and create an array, upend each record to that array, then return that array. So that's our Oracle implementation. And Delete basically uses the remove value for key. It already has a a mechanism to use a key to go in and find whatever it is that's in there for that key and remove that entry out of there. So those are our implementations and here's how we use it. So, we say, let Data Source and I keep that name very generic because you'll see in a minute kind of the, the gist of this demonstration is to show that we can swap out the, in this case the SQL server data source for the Oracle data source and we shouldn't have to change any code here because their interfaces are the same. We're only going to call those interfaces when we use it so when we switch it, there shouldn't be any compilers and it actually should function the same way for Oracle and SQL server regardless of which data source we're using. So here we have a data source, we create a variable called data source. And we instantiate a SqlServerDataSource instance, so we set it to an instance of a SqlServerDataSource, and then we create two records. We creat a MickeyMouse record and a Donald record. And for Mickey, it's Mickey Mouse, and we call the record initializer. And we pass in the name and the phone number, and for Donald Duck we do the same thing, pass in the name and a phone number in the initializer and for both of them we call the data sort create method and we pass in that record instance. So when we, kind of we set up a print statement to let us know what we're about to do and then we actually go and actually read. We get the array of records and we iterate through them and we print out the data that's in them and you can see that down here. So before update we get our Mickey record and then we get our Donald record and that's coming from this code right here. And then we go down to the, we take the Mickey Mouse record, and we update that record. We change its phone number. He was kind of jealous that Donald Duck used a cool word in his phone number so Mickey Mouse want to do the same So he goes and changes that and adds that, I'm sorry updates that, cause the update method to update the Mickey Mouse record and there we go we do the same thing we did up here basically retrieve the array of records and we reiterate through them This print statement right here shows what's happened down here so you can see that printed out there. [MUSIC]