The first one is additionally returning the integer Id of newly added row. Two static methods – SavePerson and GetPerson allow to save and retrieve Person entity to/from the database accordingly. Then, db.CreateTable() creates People table if it doesn’t exist, according to what we defined in Person entity model. What do you think ?Īs you can see, PeopleRepository executes its static constructor when any of static methods is called for the first time, in which it instantiates db variable of type SQLiteConnection, giving it the path to db file we defined. If database file doesn’t exist, it will be automatically created. On the other hand, I don’t think such rather simple app requires something more. I’m not sure if the repository is the right choice. interface), as I simply don’t know what approach I will use for the next entities. I haven’t even created any abstract layer for the repository (e.g. As a folder to store db file, I used Environment.GetFolderPath() which returns local path defined in Android system’s environmental variable $HOME: I created Constants.cs class in which I put a read-only constant string returning path to database file. On Android, it can be stored either on internal or external storage. ORM does it for us before any query is made on the database, but we need to specify where we want to keep the db file. Location of database fileīefore database can be used, a database file must be created on the device. I also implemented ToString() method in order to be able to easily see details of our entity if needed. In our case, single entity is called Person, but the table should be named in plural (table = set of entities) so we want it to be called People. This attribute’s constructor takes a single argument name, which is the name of the table which will be created underneath. We also have TableAttribute defined on Person class. AutoIncrementAttribute – marks the column with AUTO_INCREMENT (unique number is generated when a new record is added into a table).PrimaryKeyAttribute – marks the column as PRIMARY KEY. As we are designing a database entities, we also need to have keys for each of them – in this case we add Id property, and decorate it with two attributes: Just a class, in which we define basic properties of a Person – Name, LastName, PhoneNumber and Email. I created “Entities” folder for that and added the following class file: First thing we need to do is to define entity class. In case of MoneyBack, I decided to start with defining Person (plural: People) entity. It means we’re all set □ Entity definition In order to install SQLite.NET package, simply open Android.Xamarin solution in Visual Studio, open Package Manager Console and type the following command: install-package Sqlite-NetĪfter few seconds, two files are added to our project: Files added by SQLite.NET SQLite database can be easily operated using SQLite.NET ORM library available to download and install via Nuget.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |