Luku 5 * Tietojen hakeminen sovelluksiin

advertisement
Software development
Chapter 5 – Data management
Contents
•
•
•
•
•
•
Basics of data binding
Using online data
SQL databases
What is OData?
Assignments
Questions and answers
❷❸
Need for data binding
• The most simple method to display data on
screen is to display static data
• This simple method is suitable for some
applications, such as e-readers or some simple
games
• On a larger scale applications like these are
not very versatile
Basics of data binding
• Data binding is an essential method for
displaying data in Windows 8 applications
• You can always write code that sets some
interface component to display data as
programmed, in larger projects this might get
troublesome
Data binding and XAML
• Data binding can make two things less arduous
–
–
Displaying data (from code to screen)
The situation where the user edits displayed data
through a component such as TextBox (from screen to
code)
• Data binding is processed in the code through cooperation between classes and XAML strings
Startin point: C# class ”Student”
public class Student
{
public int Student ID { get; set; }
public string Name { get; set; }
public string Social security ID { get; set; }
public string Email { get; set; }
}
XAML definition
• You need to know two things to write the
correct XAML definition
1.
2.
Which source file property needs to be set as
the source
Which property of which interface component
should be set as the destination
• For example
–
Student.Name --> TextBlock.Text
XAML syntax
• You can recognize data binding processes in
XAML interfaces from the word Binding inside
curly brackets { and }
• {Binding} definition is connected to that target
component's property, which will be the
destination of the data bond
Example
<TextBlock x:Name="StudentIDTextBlock"
Text="{Binding Student ID}" ...
<TextBlock x:Name="NameTextBlock”
Text="{Binding Name}" ...
<TextBlock x:Name="SocialIDTextBlock"
Text="{Binding Social security ID}" ...
<TextBlock x:Name="EmailTextBlock"
Text="{Binding Email}" ...
Defining data contents
• Once the XAML data bond has been defined,
you must still tell the code which object
instance the data bond will be connected to
• This is done by using the XAML page's
DataContext property
Creating an object instance
• Typically an object's data is fetched from a
data base, the Internet, or they are formed
during processing
• You can also use static objects as an easy
method to try how data binding works
Example
Student o = new Student()
{
Student ID = 12345,
Name = "Olli Opiskelija",
Social security ID = "010290-123A",
Email = "olli@oppilaitos.fi"
};
Connecting the DataContext property
• You can connect any object to a page's
DataContext property
• Data binding definitions in XAML will start
looking for defined properties in the set object
• Example
–
this.DataContext = o;
Using online data
• Your Windows 8 applications will come alive
after you connect them to online data
• The online data feed can take many forms
–
A Twitter feed, the day's news headlines in an RSS
feed, or data produced by some background
process in the XML format, etc.
Many possibilities
• Windows 8 applications are capable of
versatile use of online data
• Usually online data is fetched by using the
HTTP protocol
–
It is also possible to use socket or tcp/ip interfaces
or ftp
HttpClient class
• A class titled HttpClient is available for Windows
8 applications
–
Found in the namespace Windows.Web.Http
• The class works asynchronously
–
The application will not stop and wait for the data to
be downloaded, but allows the user to continue using
the application
• Supports also encrypted https protocol
connections
Example
HttpClient client = new HttpClient();
Uri uri = new Uri("http://www.bing.com/");
string data = await client.GetStringAsync(uri);
Application example: RSS reader
• Let's look at a sample application that reads
the Yleisradio main news feed through an RSS
feed
–
RSS = Really Simple Syndication
• Data can be downloaded with the HTTP
protocol and data in RSS format is based on
XML
Downloading RSS data with HTTP
HttpClient client = new HttpClient();
Uri uri = new Uri(
"http://yle.fi/uutiset/rss/paauutiset.rss");
string data = await client.GetStringAsync(uri);
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
RSS feed's title elements
Fetching titles from RSS feed
• In the previous example the fetched RSS data
was input into an XML component
• With this kind of component certain elements
can be found using the XML file's tree
structure
Using an XML component
• Finding title elements
–
XmlNodeList nodes =
xml.SelectNodes("//channel/item/title");
• Listing the contents of title elements, that is
the news headlines
–
List<string> titles = nodes.Select(n =>
n.InnerText).ToList();
SQL databases
• SQL is the most important query language especially for
database, which can be used to search for and update data
in the database
–
Short for Structured Query Language
• SQL databases can be accessed in many ways
–
LAN databases typical, cloud-based SQL databases also widely
used
• If an application is intended to be used
• If the application is intended to be used outside an
intranet, the database connection is generally established
through public Internet by using web technologies
Ways to access data
Web interfaces
Socket connections
(tcp/ip)
SQL database
Local installation
Proxy server
Options in a nutshell
• The most simple option is to install the
database on the same device as the
application
• The more common solution is to have a
Windows 8 application access a separate
server running the database over a network
Connection methods
• Traditional LAN databases may require the use
of so called socket connections
• More modern cloud-era databases allow
forming connections through HTTP
• HTTP can also be used to access the database
through the Internet
OData
• ODataor Open Data is a standard launched by
Microsoft and other IT operators
• Odata can be used to transfer database data
(”SQL data”) over the Internet
• The Odata standard is intended to work
especially with HTTP and XML and JSON data
formats
OData platform support
• Odata standard's newest version is 3.0
• It is already supported by many different
platforms
–
The device itself is platform and device independent
• Supported platforms include Windows, Mac OS X
and Linux
• Applications supportin OData include Excel, PHP,
Ruby, and naturally Microsoft's .NET ja C# (and
therefore Windows 8 applications)
Assignments
• List at least three different methods to save data
in a Windows 8 application. What advantages and
disadvantages does each method have?
• How can you build an application that are
functional even when an Internet connection is
not available?
• What advantages do open standards such as
OData have for data transfer? What other data
transfer standards can you name?
Questions and answers 1
My application requires only simple data saving. What saving
methods should I use?
• For simple programs it can be enough to have a local XML file as a
part of the application. You can process XML files for example by
using XmlDocument class, which is found in the namespace
Windows.Data.Xml.Dom.
• You can also use some light SQL-based databases, such as SQLite.
This database is easily introduced into your application with the so
called NuGet package. You can find by using running a search for
"SQLite for Windows Runtime" in Visual Studio.
• You might also want to take a look at the new Windows Azure
Mobile Services, which can be used to quickly implement a web
background system to your application
Questions and answers 2
I'm developing a Windows 8 application and I'm writing my code using
Visual Studio. The application should communicate with the background
system by using the HTTP protocol. Do I have to use Visual Studio to also
create the background system.
• The advantage of open technologies (such as HTTP) is that that the used
development tools don't matter as long as standards are followed.
Therefore your can write your Windows 8 application's interface in Visual
Studio and the background system in Python or PHP if you so wish.
• There are, however, several advantages to writing also the background
system in Visual Studio, as you don't need to use two development tools
or more. Additionally, you can make use of efficient data transfer methods
such as OData.
• You might want to familiarize yourself with Microsoft's ASP.NET
technologies. They can be programmed in, for example, C#.
Questions and answers 3
I've heard that many background systems have been attacked and account
names and passwords have been stolen form them. How can I avoid such
mistake?
• This is a multifaceted, but important question. Especially the large
background systems and databases of popular applications are alluring
targets to criminals.
• It is important that users' account names and passwords are appropriately
encrypted. All encryption method are not equally good, and therefore you
should carefully choose the most efficient encryption for your
applications. Many other elements affect the security of background
applications, and many books have been written on the subject.
• You can read more on the subject in, for example, "Building Secure
ASP.NET Applications," which can be found on Microsoft's developer site
at http://msdn.microsoft.com/en-us/library/ff649100.aspx.
Download