Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Cross-platform UI Development with Xamarin.Forms

You're reading from  Cross-platform UI Development with Xamarin.Forms

Product type Book
Published in Aug 2015
Publisher
ISBN-13 9781784391195
Pages 330 pages
Edition 1st Edition
Languages
Author (1):
Paul Johnson Paul Johnson
Profile icon Paul Johnson

Table of Contents (22) Chapters

Cross-platform UI Development with Xamarin.Forms
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. In the Beginning… 2. Let's Get the Party Started 3. Making It Look Pretty and Logging In 4. Making Your Application Portable 5. Data, Generics, and Making Sense of Information 6. A View to a Kill 7. Connect Me to Your Other Services 8. What a Bind! 9. Addressing the Issue 10. This is the World Calling… 11. A Portable Settings Class 12. Xamarin Forms Labs 13. Social Media into the Mix 14. Bringing It All Together Index

Chapter 5. Data, Generics, and Making Sense of Information

.NET provides the developer with a rich library of code which enables us to process and manipulate data with or without a database. We also have generic types and database style manipulators using LINQ. In short, .NET gives the developer all the tools required for complex and speedy data manipulation.

In this chapter, we shall be covering the following topics:

  • Using generics to speed up development

  • Using LINQ to make queries simpler to use and extract the correct information

  • Writing the database helper class

  • Using reflection within the SQL helper class and elsewhere

A history lesson


In the beginning, if a developer needed to store data then the way to do this was array based. There was nothing inherently wrong with arrays, except that you had to initialize them so they could only have a finite size, and trying to add past that size would result in something known as undefined behavior (meaning that just about anything could happen, though usually it would result in a crash). Eventually, it was possible to resize arrays, but this was difficult.

For anything more complex than a straight array, it was not a simple task. A linked list was possible (this can be considered as being a structure linked to another instance of the structure). The useful aspect of a linked list was that it was forever expandable and could be traversed up and down the list to find data. The downside was that the developer had to remember to clear the unused structures or risk running out of memory.

.NET generics


With the advent of .NET 2.0, this problem was solved; Microsoft introduced generics to the mix. The generic classes are instantiated once and then you can add to them as many times as you like. The framework itself does the memory management for the list, so they can be considered as an ever-extendable array.

In which case, why are they called generics as arrays have a definite type (string, int, byte and so on)? The answer to that is the type argument. A .NET generic takes a generic type (typically referred to as T). This T can be anything—even another generic (for example, there is nothing to stop you from having List<Dictionary<T, List<U>>>, the caveat being that T and U will need to be defined somewhere).

This obviously will mean that you can create not only a list of strings, but a list of classes, structs, UI objects, and pretty much anything you want. Unless the generic has a global scope or static type, when the class that the generic is defined in goes...

LINQ me up baby – yeah!


LINQ gives the developer the power of SQL but for collections. It is not uncommon to order data coming from an SQL database for given parameters (such as searching on a property and outputting the data based on surname and the output ordered DateTime, then the first letter of the first name property), but in terms of collections prior to LINQ manipulation, it literally meant iterating through each piece of data.

In terms of our message collection, we can construct our lists like the following:

  1. From the SQLite database, grab all messages with a parent id of -1.

  2. Using the initial list, perform the following steps:

    1. Iterate through the list.

    2. From the SQLite database, grab messages where parentid == id and store it in a List collection.

  3. On the List, use LINQ to order by DateTime.

  4. Store the ids in a Dictionary collection.

Simple! At the end, there will be a Dictionary with the initial ID followed by the List of IDs.

The code would look like the following. In this example, DBManager...

The database helper class


All smart phones come with a version of SQLite installed (including the likes of the Blackberry range), and while SQLite provides a number of inbuilt methods of storing data, they are often cumbersome and in terms of what they do (or how they do it), aren't exactly clear.

The second problem is that the connection has to be made every time a query is to be made.

Thankfully, it is more than possible to write a helper class that deals with the creation, insertion, amendment, and retrieval of data.

There are a number of advantages to having a helper class in this situation:

  • All the data methods are in one place (reducing the number of times code has to be written; therefore, less opportunity for error)

  • Potential for commonality of code to be reduced, making the database footprint smaller

  • Data retrieval can be made simpler

  • Lends well to generic types and optimization of the retrieval of data

Generic types within the helper class

While it is always useful to have, say, a method...

Too much information!


Using * within the SQL query has an unfortunate drawback. It retrieves everything based on the parameters passed into the query. This could mean hundreds of objects returned to List.

As previously discussed, help is at hand. That help is in the form of LINQ.

Getting Linq'd

It should become apparent that LINQ is a powerful addition to the programmer's arsenal. To quote the grandfather of telesales, Billy Mays—but wait, there's more!

Finding data with LINQ

There are six ways of finding data within a collection:

  • Where

  • First and FirstOrDefault

  • Single and SingleOrDefault

  • Select

  • SelectMany

  • Last and LastOrDefault

Where

Where allows searching a collection based on any parameter within the collection. This will result in List<T> or IEnumerable<T>. If .ToList() is omitted, IEnumerable is generated:

var demoList = otherList.Where(t=>t.something == "foo");
var demoList = otherList.Where(t=>t.something == "foo").ToList();

First and FirstOrDefault

These two LINQ methods...

Summary


This chapter has covered a lot of ground. It has taken a relatively simple topic and turned it on its head! Not only have we found the use of a database helper class, but have also shown how simple it is to come up with a helper class that is very flexible and extensible with the use of generic types, classes, collections, and functions.

This chapter has also looked at the power behind LINQ, one of the most useful libraries within .NET for data manipulation outside of an SQL database framework.

For more examples of using LINQ, I recommend the Microsoft 101 LINQ examples at https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Cross-platform UI Development with Xamarin.Forms
Published in: Aug 2015 Publisher: ISBN-13: 9781784391195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}